예제 #1
0
def test_SimlFunction_1(): #IGNORE:C01111
    msg =  \
'''
Test SimlFunction: call user defined function 
User defined functions are created without parser.
'''    
    #skip_test(msg)
    print msg
    
    from freeode.interpreter import (Interpreter, SimlFunction, 
                                     Signature, IFloat, IString)
    from freeode.ast import NodeFuncArg, NodeReturnStmt, NodeIdentifier
    from freeode.util import UserException

    #create the interpreter
    intp = Interpreter()    #IGNORE:W0612
    lib = intp.built_in_lib
    #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=lib)
    #call with existing value
    intp.apply(f1, (val_1,))
    
    #create a function with return statement - uses interpreter for executing the statement
    # func test(a:Float) -> Float:
    #     return a
    f2 = SimlFunction('test', Signature([NodeFuncArg('a', IFloat)], IFloat), 
                      statements=[NodeReturnStmt([NodeIdentifier('a')])], 
                      global_scope=lib)
    #call function and see if value is returned
    ret_val = intp.apply(f2, (val_1,))
    assert ret_val.value == 1. #IGNORE:E1103

    #create a function with wrong return type
    # func test(a:Float) -> String:
    #     return a
    f3= SimlFunction('test', Signature([NodeFuncArg('a', IFloat)], IString), 
                      statements=[NodeReturnStmt([NodeIdentifier('a')])], 
                      global_scope=lib)
    def raise_1():
        intp.apply(f3, (val_1,))
    assert_raises(UserException, 3200320, raise_1)
예제 #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