本文整理汇总了Python中interpreter.Interpreter.execute方法的典型用法代码示例。如果您正苦于以下问题:Python Interpreter.execute方法的具体用法?Python Interpreter.execute怎么用?Python Interpreter.execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类interpreter.Interpreter
的用法示例。
在下文中一共展示了Interpreter.execute方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from interpreter import Interpreter [as 别名]
# 或者: from interpreter.Interpreter import execute [as 别名]
def main():
# Inputs for interpreter and generator
grammar = {'[':_push_state, ']':_pop_state,
'F':_forwards, 'L':_left, 'R':_right,
}
rules = {'T':'F[LT][RT]'}
data = 'T'
generations = 5
# Create a generator
g = Generator(data, rules)
data = g.nth_generation(generations)
# Create and set up interpreter
i = Interpreter(grammar)
i.use_memory = True # Use the interpreter's memory,
# i.e. pass the interpreter as the
# first argument to every callback in the grammar
# Initialise a turtle
t = turtle.Turtle()
t.hideturtle()
t.left(90)
# Load required items into interpreter memory
i.load('turtle', t) # Load in a turtle
i.load('state', []) # Load in an empty list for turtle's state stack
i.execute(data)
turtle.exitonclick()
示例2: main
# 需要导入模块: from interpreter import Interpreter [as 别名]
# 或者: from interpreter.Interpreter import execute [as 别名]
def main():
grammar = { '>':_inc_pointer,
'<':_dec_pointer,
'+':_inc_data,
'-':_dec_data,
'.':_output,
',':_input,
'[':_if_zero_jump_forward,
']':_if_nonzero_jump_back
}
data = '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.' # 'Hello world'
i = Interpreter(grammar)
i.use_memory = True
i.load('tape',[0])
i.load('ptr',0)
i.load('loop',0)
i.execute(data)
示例3: __init__
# 需要导入模块: from interpreter import Interpreter [as 别名]
# 或者: from interpreter.Interpreter import execute [as 别名]
#.........这里部分代码省略.........
else:
return False
return True
def setTransferStage(self, transferHeader):
"""Set the transfer stage to process by the vm."""
if "transfer" in transferHeader:
self.transferStage = TRANSFER_STAGE.CHUNKER
self.tokenizer = TransferWordTokenizer()
# Set chunker mode, by default 'lu'.
if "chunk" in transferHeader:
self.chunkerMode = CHUNKER_MODE.CHUNK
else:
self.chunkerMode = CHUNKER_MODE.LU
elif "interchunk" in transferHeader:
self.transferStage = TRANSFER_STAGE.INTERCHUNK
self.tokenizer = ChunkWordTokenizer()
elif "postchunk" in transferHeader:
self.transferStage = TRANSFER_STAGE.POSTCHUNK
self.tokenizer = ChunkWordTokenizer(solveRefs=True, parseContent=True)
def tokenizeInput(self):
"""Call to the tokenizer to divide the input in tokens."""
self.words, self.superblanks = self.tokenizer.tokenize(self.input)
def initializeVM(self):
"""Execute code to initialize the VM, e.g. default values for vars."""
self.PC = 0
self.status = VM_STATUS.RUNNING
while self.status == VM_STATUS.RUNNING and self.PC < len(self.code):
self.interpreter.execute(self.code[self.PC])
def getSourceWord(self, pos):
"""Get the part of a source word needed for pattern matching, depending
on the transfer stage."""
if self.transferStage == TRANSFER_STAGE.CHUNKER:
return self.words[pos].source.lu
elif self.transferStage == TRANSFER_STAGE.INTERCHUNK:
word = self.words[pos].chunk
return word.attrs["lem"] + word.attrs["tags"]
else:
return self.words[pos].chunk.attrs["lem"]
def getNextInputPattern(self):
"""Get the next input pattern to analyze, lowering the lemma first."""
try:
pattern = self.getSourceWord(self.nextPattern)
tag = pattern.find("<")
pattern = pattern[:tag].lower() + pattern[tag:]
self.nextPattern += 1
except IndexError:
return None
return pattern
def getUniqueSuperblank(self, pos):
"""Get the superblank at pos avoiding duplicates."""
try:
if pos != self.lastSuperblank:
self.lastSuperblank = pos
示例4: BytecodeGenerator
# 需要导入模块: from interpreter import Interpreter [as 别名]
# 或者: from interpreter.Interpreter import execute [as 别名]
# print 'x', node
if not self.clean:
self.bytecode.append(("POP_STACK", None))
if self.main:
self.bytecode.append(("END", None))
return self.bytecode
to_bytecode = lambda xs: BytecodeGenerator()(xs)
if __name__ == "__main__":
from pprint import pprint
with open("example.js") as fobj:
instructions = parse(fobj.read())
bcg = BytecodeGenerator(True)
bytecode = bcg(instructions)
# pprint(bytecode)
from interpreter import Interpreter
ip = Interpreter()
ip.execute(bytecode)
# print 'locals', ip.namespace.locals