开发者

Extending the Charme Interpreter

开发者 https://www.devze.com 2023-02-20 17:36 出处:网络
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 p

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 in开发者_Go百科itializeGlobalEnvironment 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)


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号