Extending the Charme Interpreter

社会主义新天地 提交于 2019-12-11 06:44:32

问题


I need to extend the Charme interpreter (described here) by adding a primitive procedure <= to the global environment. I know that to do this I also need to define a procedure that implements the primitive, and modify initializeGlobalEnvironment to install the primitive.

This is what I have for initializeGlobalEnvironment --

def initializeGlobalEnvironment():
    global globalEnvironment
    globalEnvironment = Environment(None)
    globalEnvironment.addVariable('true', True)
    globalEnvironment.addVariable('false', False)
    globalEnvironment.addVariable('+', primitivePlus)
    globalEnvironment.addVariable('-', primitiveMinus)
    globalEnvironment.addVariable('*', primitiveTimes)
    globalEnvironment.addVariable('=', primitiveEquals)
    globalEnvironment.addVariable('zero?', primitiveZero)
    globalEnvironment.addVariable('>', primitiveGreater)
    globalEnvironment.addVariable('<', primitiveLessThan)

回答1:


I am not sure about the exact syntax for charme, but the approximate scheme-y code would be

primitiveLessThanEqualTo = '(lambda (x y) (if (= x y) (true) (if (< x y) (true) (false))))'
globalEnvironment.addVariable('<=', primitiveLessThanEqualTo)


来源:https://stackoverflow.com/questions/5494256/extending-the-charme-interpreter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!