当前位置: 首页>>代码示例>>Python>>正文


Python interpreter.Interpreter类代码示例

本文整理汇总了Python中freeode.interpreter.Interpreter的典型用法代码示例。如果您正苦于以下问题:Python Interpreter类的具体用法?Python Interpreter怎么用?Python Interpreter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Interpreter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_function_definition_and_call_1

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
开发者ID:BackupTheBerlios,项目名称:freeode-svn,代码行数:30,代码来源:test_2_interpreter.py

示例2: test_data_statement_simple_1

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)
开发者ID:BackupTheBerlios,项目名称:freeode-svn,代码行数:25,代码来源:test_2_interpreter.py

示例3: test_print_function_1

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')
开发者ID:BackupTheBerlios,项目名称:freeode-svn,代码行数:25,代码来源:test_2_interpreter.py

示例4: test_ProgramGenerator__create_program

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()
开发者ID:BackupTheBerlios,项目名称:freeode-svn,代码行数:32,代码来源:test_pygenerator.py

示例5: test_graph_function_1

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
开发者ID:BackupTheBerlios,项目名称:freeode-svn,代码行数:32,代码来源:test_2_interpreter.py

示例6: test_Interpreter_create_path_2

def test_Interpreter_create_path_2(): #IGNORE:C01111
    msg = 'Interpreter: create_path method: return existing path, extend path'
    #skip_test(msg)
    print msg
    
    from freeode.interpreter import InterpreterObject, Interpreter
    from freeode.util import DotName
    
    #create an interpreter 
    intp = Interpreter()
    
    #the root object where the long name will be created
    root = InterpreterObject()
    #create all attributes so that this dotname can be looked up
    #o_name_1 should be the object representing the rightmost element (name)
    o_name_1 = intp.create_path(root, DotName('this.is_.a.long.dotted.name'))
    
    #access existing path, don't try to create it twice
    o_name_2 = intp.create_path(root, DotName('this.is_.a.long.dotted.name'))
    assert o_name_1 is o_name_2
    
    #extend existing path
    o_indeed = intp.create_path(root, DotName('this.is_.a.long.dotted.name.indeed'))
    o_indeed_2 = o_name_1.indeed                     #pylint:disable-msg=E1103
    assert o_indeed is o_indeed_2
开发者ID:eike-welk,项目名称:freeode,代码行数:25,代码来源:test_interpreter_1.py

示例7: test_interpreter_method_call

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)
开发者ID:BackupTheBerlios,项目名称:freeode-svn,代码行数:35,代码来源:test_2_interpreter.py

示例8: test_data_statement_roles_1

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
开发者ID:BackupTheBerlios,项目名称:freeode-svn,代码行数:29,代码来源:test_2_interpreter.py

示例9: test_StatementVisitor__visit_NodeCompileStmt__code_generation_1

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
开发者ID:BackupTheBerlios,项目名称:freeode-svn,代码行数:33,代码来源:test_2_interpreter.py

示例10: test_operator_dispatch_2

def test_operator_dispatch_2(): #IGNORE:C01111
    msg = 'Test Interpreter: handling of operators with unknown Float values.'
    #skip_test(msg)
    print msg
    from freeode.interpreter import Interpreter, IFloat, istype
    from freeode.ast import (NodeOpInfix2, NodeOpPrefix1, NodeFuncCall, 
                             RoleVariable)
    from freeode.util import func #, aa_make_tree

    intp = Interpreter()
    
    val_2 = IFloat()
    val_2.__siml_role__ = RoleVariable
    val_3 = IFloat()
    val_3.__siml_role__ = RoleVariable
    
    op_sub = NodeOpInfix2('-', [val_2, val_3])
    res = intp.eval(op_sub)
    print res
    assert isinstance(res, NodeFuncCall)
    assert res.function is func(IFloat.__sub__)
    assert istype(res, IFloat)
    
    op_neg = NodeOpPrefix1('-', [val_2])
    res = intp.eval(op_neg)
    print res
    assert isinstance(res, NodeFuncCall)
    assert res.function is func(IFloat.__neg__)
    assert istype(res, IFloat)
开发者ID:eike-welk,项目名称:freeode,代码行数:29,代码来源:test_interpreter_1.py

示例11: test_ProgramGenerator__create_program_2

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')
开发者ID:eike-welk,项目名称:freeode,代码行数:60,代码来源:test_pygenerator.py

示例12: test_argument_list_2

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.'
开发者ID:BackupTheBerlios,项目名称:freeode-svn,代码行数:25,代码来源:test_3_interpreter_errors.py

示例13: test_ProgramGenerator__create_program_1

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')
开发者ID:eike-welk,项目名称:freeode,代码行数:59,代码来源:test_pygenerator.py

示例14: test_interpreter_user_defined_operators_2

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
开发者ID:BackupTheBerlios,项目名称:freeode-svn,代码行数:58,代码来源:test_2_interpreter.py

示例15: test_unknown_const_1

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'
开发者ID:BackupTheBerlios,项目名称:freeode-svn,代码行数:55,代码来源:test_optimizer.py


注:本文中的freeode.interpreter.Interpreter类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。