本文整理汇总了Python中freeode.interpreter.Interpreter.start_collect_code方法的典型用法代码示例。如果您正苦于以下问题:Python Interpreter.start_collect_code方法的具体用法?Python Interpreter.start_collect_code怎么用?Python Interpreter.start_collect_code使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类freeode.interpreter.Interpreter
的用法示例。
在下文中一共展示了Interpreter.start_collect_code方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_StatementVisitor_assign_emit_code_2
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import start_collect_code [as 别名]
def test_StatementVisitor_assign_emit_code_2(): #IGNORE:C01111
#py.test.skip('Test interpreter object: emit code without the usual infrastructure.')
print 'Test StatementVisitor.assign: emit code without the usual infrastructure.'
from freeode.interpreter import (Interpreter, IFloat)
from freeode.ast import (Node, NodeAssignment, NodeOpInfix2)
prog_text = \
'''
data a: Float const
data b: Float variable
data c: Float variable
a = 2*2 #constant no statement emitted
b = 2*a #emit b = 8; compute 2*a at compile time
c = 2*b #emit everything
#print('a = ', a)
#print('b = ', b)
#print('c = ', c)
'''
#create the interpreter
intp = Interpreter()
#enable collection of statements for compilation
intp.start_collect_code()
#interpret the program
intp.interpret_module_string(prog_text, None, 'test')
#get the results of the collection process
stmts, func_locals = intp.stop_collect_code()
print
print '--------------- main module ----------------------------------'
#print intp.modules['test']
#put collected statements into Node for pretty printing
n = Node(statements=stmts)
print
print '--------------- collected statements ----------------------------------'
#print n
assert len(stmts) == 2
# b = 4
assert isinstance(stmts[0], NodeAssignment)
assert isinstance(stmts[0].expression, IFloat) # 8
assert stmts[0].expression.value == 8
# c = 2*b
assert isinstance(stmts[1], NodeAssignment)
assert isinstance(stmts[1].expression, NodeOpInfix2) # 2 * b
assert isinstance(stmts[1].expression.arguments[0], IFloat) # 2
assert stmts[1].expression.arguments[0].value == 2
assert isinstance(stmts[1].expression.arguments[1], IFloat) # b
assert stmts[1].expression.arguments[1].value is None
示例2: test_SimlFunction_3
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import start_collect_code [as 别名]
def test_SimlFunction_3(): #IGNORE:C01111
msg = \
'''
Test SimlFunction: storage of local variables during code collection.
User defined functions are created without parser.
'''
#skip_test(msg)
print msg
from freeode.interpreter import (Interpreter, SimlFunction, IModule,
Signature, IFloat)
from freeode.ast import NodeFuncArg
from freeode.util import DotName #, aa_make_tree
#create the interpreter
intp = Interpreter()
#create a Siml value as function argument
val_1 = IFloat(1)
#create a function without statements (impossible in Siml)
# func test(a:Float):
# ** nothing **
f1 = SimlFunction('test', Signature([NodeFuncArg('a', IFloat)]),
statements=[], global_scope=intp.built_in_lib, loc=None,
dot_name=DotName('test_module.test'))
#create module where the function lives
mod1 = IModule()
mod1.test = f1
#call with existing value
# and set interpreter up to collect code. In this mode local variables of all
# functions must become algebraic variables of the simulation object.
intp.start_collect_code()
intp.apply(f1, (val_1,))
_stmts, fn_locals = intp.stop_collect_code()
#print aa_make_tree(fn_locals)
#The function argument 'a' must appear in the storage for local variables
assert fn_locals.test.i1.a is val_1
示例3: test_StatementVisitor_assign_emit_code_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import start_collect_code [as 别名]
def test_StatementVisitor_assign_emit_code_1(): #IGNORE:C01111
#py.test.skip('Test disabled')
print 'Test StatementVisitor.assign: emit code without the usual infrastructure.'
from freeode.interpreter import (Interpreter, IFloat)
from freeode.ast import (Node, NodeAssignment, NodeOpInfix2)
prog_text = \
'''
data a: Float variable
data b: Float variable
b = 2*a #emit this statement
'''
#create the interpreter
intp = Interpreter()
#enable collection of statements for compilation
intp.start_collect_code()
#interpret the program
intp.interpret_module_string(prog_text, None, 'test')
#get the results of the collection process
stmts, func_locals = intp.stop_collect_code()
print
print '--------------- main module ----------------------------------'
#print intp.modules['test']
#put collected statements into Node for pretty printing
n = Node(statements=stmts)
print
print '--------------- collected statements ----------------------------------'
#print n
#one statement: b = 2*a
assert len(stmts) == 1
assert isinstance(stmts[0], NodeAssignment) # b = 2 * a
assert isinstance(stmts[0].target, IFloat) # b
assert stmts[0].target.value is None
assert isinstance(stmts[0].expression, NodeOpInfix2) # 2*a
assert isinstance(stmts[0].expression.arguments[0], IFloat)# 2
assert stmts[0].expression.arguments[0].value == 2
assert isinstance(stmts[0].expression.arguments[1], IFloat)# a
assert stmts[0].expression.arguments[1].value is None