Pythonからの単純なインタプリタを構築します.06 : whileループ
4275 ワード
この投稿では' while 'ループを実装します.
リンク242479152
class Interpreter:
# ....(previous code)....
def While(self,xs):
_ , cond , block = xs
while self.eval(cond):
if isinstance(block[0],list):
for x in block:
self.eval(x)
else:
self.eval(block)
code=[
["Set","sum",0],
["Set","i",0],
["While", ["Lt", ["Get","i"], 100], [
["Set","i", ["Add",["Get","i"], 1] ],
["Set","sum", ["Add",["Get", "sum"],["Get","i"]]]
]],
["Print","sum(1..100) = ",["Get","sum"] ]
]
interpreter=Interpreter()
interpreter.run(code)
出力:sum(1..100) = 5050
完全なコードへのリンク:part6リンク242479152
Reference
この問題について(Pythonからの単純なインタプリタを構築します.06 : whileループ), 我々は、より多くの情報をここで見つけました https://dev.to/smadev/lets-build-a-simple-interpreter-from-scratch-in-python-pt-06-while-loop-51nfテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol