本文整理汇总了Python中freeode.interpreter.Interpreter.interpret_module_string方法的典型用法代码示例。如果您正苦于以下问题:Python Interpreter.interpret_module_string方法的具体用法?Python Interpreter.interpret_module_string怎么用?Python Interpreter.interpret_module_string使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类freeode.interpreter.Interpreter
的用法示例。
在下文中一共展示了Interpreter.interpret_module_string方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_ProgramGenerator__create_program
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import interpret_module_string [as 别名]
def test_ProgramGenerator__create_program():
msg = ''' Test ProgramGenerator.create_program:
Just see if function does not crash.
'''
#py.test.skip(msg)
print msg
from freeode.pygenerator import ProgramGenerator
from freeode.interpreter import Interpreter
prog_text = \
'''
class A:
data a: Float
data b: Float param
func initialize(this):
b = 1
func dynamic(this):
$a = b
compile A
'''
#interpret the compile time code
intp = Interpreter()
intp.interpret_module_string(prog_text, 'foo.siml', '__main__')
#create the output text
pg = ProgramGenerator()
pg.create_program('foo.siml', intp.get_compiled_objects())
print pg.get_buffer()
示例2: test_data_statement_simple_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import interpret_module_string [as 别名]
def test_data_statement_simple_1(): #IGNORE:C01111
#py.test.skip('Test data statement: create attributes')
print 'Test data statement: create attributes'
from freeode.interpreter import Interpreter, IFloat, IString
from freeode.ast import DotName
prog_text = \
'''
data a: Float const
data b: String const
'''
#create the interpreter
intp = Interpreter()
#run mini program
intp.interpret_module_string(prog_text, None, 'test')
mod = intp.modules['test']
# print
# print 'module after interpreter run: ---------------------------------'
# print mod
a = mod.get_attribute(DotName('a'))
assert isinstance(a, IFloat)
b = mod.get_attribute(DotName('b'))
assert isinstance(b, IString)
示例3: test_argument_list_2
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import interpret_module_string [as 别名]
def test_argument_list_2(): #IGNORE:C01111
msg = 'Test error in argument list - unknown argument.'
#py.test.skip(msg)
print msg
from freeode.interpreter import Interpreter
from freeode.ast import UserException
prog_text = \
'''
func foo(a):
return a
data a: Float
foo(b=a)
'''
#create the interpreter
intp = Interpreter()
try:
#run mini program
intp.interpret_module_string(prog_text, None, 'test')
except UserException, e:
print e
assert e.errno == 3200260
print 'Correct exception was raised.'
示例4: test_print_function_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import interpret_module_string [as 别名]
def test_print_function_1(): #IGNORE:C01111
#py.test.skip('Test the print function. - actual printing, built in objects.')
print 'Test the print function. - actual printing: Float, String, expression.'
from freeode.interpreter import Interpreter
prog_text = \
'''
#print known constants
print(23)
print('hello ',2, ' the world!')
#print unknown value
data foo: Float
data bar: String
print(foo)
print(bar)
#print unevaluated expression
data a,b: Float
print(a+b)
'''
#create the interpreter
intp = Interpreter()
#run mini program
intp.interpret_module_string(prog_text, None, 'test')
示例5: test_StatementVisitor__visit_NodeCompileStmt__code_generation_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import interpret_module_string [as 别名]
def test_StatementVisitor__visit_NodeCompileStmt__code_generation_1(): #IGNORE:C01111
#py.test.skip('Test StatementVisitor.visit_NodeCompileStmt')
print 'Test StatementVisitor.visit_NodeCompileStmt:'
from freeode.interpreter import (Interpreter, IFloat, SimlFunction)
from freeode.ast import (DotName)
prog_text = \
'''
class A:
data b: Float
func dynamic(this):
b = 2
compile A
'''
#create the interpreter
intp = Interpreter()
#run program
intp.interpret_module_string(prog_text, None, 'test')
#print intp.modules['test']
#print intp.get_compiled_objects()[0]
#there must be one compiled object present
assert len(intp.get_compiled_objects()) == 1
comp_obj = intp.get_compiled_objects()[0]
#the attributes b and dynamic must exist
assert isinstance(comp_obj.get_attribute(DotName('b')), IFloat)
assert isinstance(comp_obj.get_attribute(DotName('dynamic')), SimlFunction)
assert len(comp_obj.get_attribute(DotName('dynamic')).statements) == 1
示例6: test_graph_function_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import interpret_module_string [as 别名]
def test_graph_function_1(): #IGNORE:C01111
#py.test.skip('Test the print function. - code generation for: user defined class.')
print 'Test the print function. - code generation for: user defined class.'
from freeode.interpreter import Interpreter
from freeode.ast import DotName
prog_text = \
'''
class A:
data c: Float
func final(this):
graph(c)
compile A
'''
#create the interpreter
intp = Interpreter()
#run mini program
intp.interpret_module_string(prog_text, None, 'test')
# print
# print 'module after interpreter run: ---------------------------------'
# print intp.modules['test']
#get flattened object
sim = intp.get_compiled_objects()[0]
#print sim
#get the final function with the generated code
final = sim.get_attribute(DotName('final'))
assert len(final.statements) == 1
示例7: test_interpreter_method_call
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import interpret_module_string [as 别名]
def test_interpreter_method_call(): #IGNORE:C01111
#py.test.skip('Method calls do not work! Implement method wrappers!')
print 'Test interpreter: method call ...............................................................'
from freeode.interpreter import Interpreter, DotName
prog_text = \
'''
print('start')
class A:
data a1: Float const
data a2: Float const
func compute(this, x):
print('in compute_a2 x=', x)
return x + 2
data a: A const
a.a1 = a.compute(3)
#print('a.a1 = ', a.a1)
print('end')
'''
#create the interpreter
intp = Interpreter()
#interpret the program
intp.interpret_module_string(prog_text, None, 'test')
print
#print intp.modules['test']
assert (intp.modules['test'].get_attribute(DotName('a'))
.get_attribute(DotName('a1')).value == 5)
示例8: test_function_definition_and_call_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import interpret_module_string [as 别名]
def test_function_definition_and_call_1(): #IGNORE:C01111
#py.test.skip('Test disabled')
print 'Test interpreter object: function definition and function call ...............................................................'
from freeode.interpreter import Interpreter, DotName
prog_text = \
'''
print('start')
func foo(b):
print('in foo. b = ', b)
return b*b
print('after return')
data a: Float const
a = 2*2 + foo(3*4) + foo(2)
print('a = ', a)
print('end')
'''
#create the interpreter
intp = Interpreter()
#run mini program
intp.interpret_module_string(prog_text, None, 'test')
print
print 'module after interpreter run: ---------------------------------'
print intp.modules['test']
assert intp.modules['test'].get_attribute(DotName('a')).value == 2*2 + (3*4)**2 + 2**2
示例9: test_data_statement_roles_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import interpret_module_string [as 别名]
def test_data_statement_roles_1(): #IGNORE:C01111
#py.test.skip('Test data statement: create attributes with different roles')
print 'Test data statement: create attributes with different roles'
from freeode.interpreter import Interpreter
from freeode.ast import (DotName, RoleConstant, RoleParameter,
RoleVariable)
prog_text = \
'''
data a: Float const
data b: Float param
data c: Float variable
'''
#create the interpreter
intp = Interpreter()
#run mini program
intp.interpret_module_string(prog_text, None, 'test')
mod = intp.modules['test']
# print
# print 'module after interpreter run: ---------------------------------'
# print mod
a = mod.get_attribute(DotName('a'))
assert a.role is RoleConstant
b = mod.get_attribute(DotName('b'))
assert b.role is RoleParameter
c = mod.get_attribute(DotName('c'))
assert c.role is RoleVariable
示例10: test_ProgramGenerator__create_program_2
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import interpret_module_string [as 别名]
def test_ProgramGenerator__create_program_2():
msg = \
'''
Test ProgramGenerator.create_program:
Test program with additional initialization function.
Load program as module and test init_*** function.
'''
#skip_test(msg)
print msg
import os
from freeode.pygenerator import ProgramGenerator
from freeode.interpreter import Interpreter
prog_text = \
'''
class A:
data x: Float
data b: Float param
#This is the additional initialization function.
func init_b(this, in_b):
b = in_b #set parameter
x = 0 #set initial value
#10
func initialize(this):
b = 0.1 #set parameter
x = 0 #set initial value
func dynamic(this):
$x = b
compile A
'''
#interpret the compile time code
intp = Interpreter()
intp.interpret_module_string(prog_text, 'foo.siml', '__main__')
#create the output text
pg = ProgramGenerator()
pg.create_program('foo.siml', intp.get_compiled_objects())
#print pg.get_buffer()
#write the buffer into a file, import the file as a module
progname = 'testprog_ProgramGenerator__create_program_2'
prog_text_file = open(progname + '.py','w')
prog_text_file.write(pg.get_buffer())
prog_text_file.close()
module = __import__(progname)
#test the generated module
A = module.A
a = A()
#call generated init_b(...) function
a.init_b(42)
assert a.param.b == 42
#clean up
os.remove(progname + '.py')
os.remove(progname + '.pyc')
示例11: test_ProgramGenerator__create_program_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import interpret_module_string [as 别名]
def test_ProgramGenerator__create_program_1():
msg = \
'''
Test ProgramGenerator.create_program:
Test basic functions of the compiler. Loads generated program as module.
'''
#skip_test(msg)
print msg
import os
from freeode.pygenerator import ProgramGenerator
from freeode.interpreter import Interpreter
prog_text = \
'''
class A:
data x: Float
data b: Float param
func initialize(this):
x = 0
b = 1
solution_parameters(duration = 30, reporting_interval = 0.1)
func dynamic(this):
$x = b
compile A
'''
#interpret the compile time code
intp = Interpreter()
intp.interpret_module_string(prog_text, 'foo.siml', '__main__')
#create the output text
pg = ProgramGenerator()
pg.create_program('foo.siml', intp.get_compiled_objects())
#print pg.get_buffer()
#write the buffer into a file, import the file as a module
progname = 'testprog_ProgramGenerator__create_program_1'
prog_text_file = open(progname + '.py','w')
prog_text_file.write(pg.get_buffer())
prog_text_file.close()
module = __import__(progname)
#test the generated module
A = module.A
a = A()
#call generated initialize(...) function
a.initialize()
assert a.param.b == 1
#solve (trivial) ODE and test solution
a.simulateDynamic()
x_vals = a.getResults()['x']
assert abs(x_vals[-1] - 30) < 1e-6
#clean up
os.remove(progname + '.py')
os.remove(progname + '.pyc')
示例12: test_interpreter_user_defined_operators_2
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import interpret_module_string [as 别名]
def test_interpreter_user_defined_operators_2(): #IGNORE:C01111
'''
User defined operators must also work with constant data.
Same class as in previous test, but all variables are constant.
The used Siml class simulates a geometric vector class.
'''
#py.test.skip('Test user defined operators - code generation.')
print 'Test user defined operators - code generation.'
from freeode.interpreter import Interpreter
from freeode.ast import DotName, RoleConstant
prog_text = \
'''
class Vec1D:
data x: Float role_unknown
func __add__(this, other):
data res: Vec1D
res.x = x + other.x
return res
func __assign__(this, other):
x = other.x
data a,b,c: Vec1D const
a.x = 2
b.x = 3
#--- invoke the operators ----
c=a+b
'''
#create the interpreter and interpret the mini-program
intp = Interpreter()
intp.interpret_module_string(prog_text, None, 'test')
mod = intp.modules['test']
#print
#print 'Interpreted module: -----------------------------------------------------'
#print mod
#get the attributes that we have defined
a = mod.get_attribute(DotName('a'))
a_x = a.get_attribute(DotName('x'))
assert a_x.role == RoleConstant
assert a_x.value == 2
b = mod.get_attribute(DotName('b'))
b_x = b.get_attribute(DotName('x'))
assert b_x.role == RoleConstant
assert b_x.value == 3
c = mod.get_attribute(DotName('c'))
c_x = c.get_attribute(DotName('x'))
assert c_x.role == RoleConstant
assert c_x.value == 5
示例13: test_unknown_const_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import interpret_module_string [as 别名]
def test_unknown_const_1(): #IGNORE:C01111
msg = '''Test correct treatment of unknown constants.'''
py.test.skip(msg)
print msg
from freeode.optimizer import MakeDataFlowDecorations, DataFlowChecker
from freeode.interpreter import (Interpreter, IFloat)
from freeode.ast import DotName, NodeAssignment
prog_text = \
'''
data c1: Float const
class A:
data a, b: Float
data c2: Float const
func dynamic(this):
a = c1
b = c2
compile A
'''
#interpret the program
intp = Interpreter()
intp.interpret_module_string(prog_text, None, 'test')
#the module
#mod = intp.modules['test']
#print mod
#get the flattened version of the A class
sim = intp.get_compiled_objects()[0]
#print sim
#get attributes
a = sim.get_attribute(DotName('a'))
b = sim.get_attribute(DotName('b'))
# c = sim.get_attribute(DotName('c1'))
# c = sim.get_attribute(DotName('c2'))
#get generated main function
dyn = sim.get_attribute(DotName('dynamic'))
hexid = lambda x: hex(id(x))
print 'a:', hexid(a), ' b:', hexid(b)#, ' c2:', hexid(c)
#create the input and output decorations on each statement of the
#function
dd = MakeDataFlowDecorations()
dd.decorate_simulation_object(sim)
#check data flow of all functions
fc = DataFlowChecker()
fc.set_sim_object(sim)
assert False, 'This program should raise an exceptions because unknown const attributes were used'
示例14: test_function_return_value_roles_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import interpret_module_string [as 别名]
def test_function_return_value_roles_1(): #IGNORE:C01111
'''
User defined functions can be called from constant and from variable
environments. Test the roles of their return values.
This test only involves fundamental types.
'''
#py.test.skip('Test roles of return values of user defined functions.')
print 'Test roles of return values of user defined functions.'
from freeode.interpreter import Interpreter
from freeode.ast import (DotName, RoleConstant, RoleAlgebraicVariable)
prog_text = \
'''
func plus2(x):
data r: Float
r = x + 2
return r
data ac,bc: Float const
ac = 3
bc = plus2(ac)
class B:
data av,bv: Float
func dynamic(this):
bv = plus2(av)
compile B
'''
#create the interpreter
intp = Interpreter()
#run mini program
intp.interpret_module_string(prog_text, None, 'test')
# print
mod = intp.modules['test']
# print 'module after interpreter run: ---------------------------------'
# print mod
ac = mod.get_attribute(DotName('ac'))
bc = mod.get_attribute(DotName('bc'))
assert ac.role == RoleConstant
assert bc.role == RoleConstant
assert ac.value == 3
assert bc.value == 5
# #get flattened object
sim = intp.get_compiled_objects()[0]
# print 'Flattened object: ---------------------------------'
# print sim
av = sim.get_attribute(DotName('av'))
bv = sim.get_attribute(DotName('bv'))
assert av.role == RoleAlgebraicVariable
assert bv.role == RoleAlgebraicVariable
示例15: test_interpreter_dollar_operator_2
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import interpret_module_string [as 别名]
def test_interpreter_dollar_operator_2(): #IGNORE:C01111
msg = '''
Test "$" operator.
Bug: $ operator did not work with attributes of user defined classes.
Background: Class instantiation did not get parent refferences right.
'''
#py.test.skip(msg)
print msg
from freeode.interpreter import Interpreter, CallableObject, IFloat
from freeode.ast import DotName, RoleStateVariable, RoleTimeDifferential
prog_text = \
'''
class A:
data z: Float
func dynamic(this):
$z = z
class B:
data a: A
func dynamic(this):
a.dynamic()
compile B
'''
#create the interpreter
intp = Interpreter()
intp.interpret_module_string(prog_text, None, 'test')
print
#print intp.modules['test']
#print intp.get_compiled_objects()[0]
#TODO: Assertions
#get flattened object
sim = intp.get_compiled_objects()[0]
#get the attributes that we have defined
az = sim.get_attribute(DotName('a.z'))
az_dt = sim.get_attribute(DotName('a.z$time')) #implicitly defined by $ operator
dynamic = sim.get_attribute(DotName('dynamic'))
#test some facts about the attributes
assert isinstance(az, IFloat) #a1 is state variable, because it
assert az.role == RoleStateVariable #has derivative
assert isinstance(az_dt, IFloat)
assert az_dt.role == RoleTimeDifferential # $a1 is time differential
assert isinstance(dynamic, CallableObject)
#test if assignment really is 'a1$time' = 'a1'
assign = dynamic.statements[0]
assert assign.target is az_dt
assert assign.expression is az