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
0
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
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