예제 #1
0
def test_function_call_unknown_arguments_1(): #IGNORE:C01111
    msg = 'Test expression evaluation (calling built in functions), unknown arguments'
    #skip_test(msg)
    print msg
    
    from freeode.interpreter import (IModule, ExecutionEnvironment, 
                                     Interpreter, signature, 
                                     IFloat, RoleVariable, istype, test_allknown)
    from freeode.ast import NodeFuncCall, NodeIdentifier
    from freeode.util import aa_make_tree
    import math
    
    #create module where the function lives
    mod = IModule()
    #create sqrt function that can be called from Siml
    @signature([IFloat], IFloat)
    def sqrt(x): 
        test_allknown(x)
        return IFloat(math.sqrt(x.value))
    #put function into module
    mod.sqrt = sqrt
    print
    print 'Module where function is located: --------------------------------------------'
    #print aa_make_tree(mod)
    
    #create environment for lookup of variables (stack frame)
    env = ExecutionEnvironment()
    env.global_scope = mod
    #create visitor for evaluating the expression
    intp = Interpreter()
    intp.push_environment(env) 
    
    #create a Siml value as function argument
    val_1 = IFloat()    
    val_1.__siml_role__ = RoleVariable
    #create function call with unkown argument
    call = NodeFuncCall(NodeIdentifier('sqrt'), [val_1], {})
    
    #evaluate the function call
    ret_val = intp.eval(call)
    print
    print 'Result object: --------------------------------------------------------------'
    print aa_make_tree(ret_val)
    #evaluating a function call with unknown arguments must return a function call
    assert isinstance(ret_val, NodeFuncCall)
    assert istype(ret_val, IFloat)
예제 #2
0
def test_expression_evaluation_4(): #IGNORE:C01111
    msg = 'Test expression evaluation (calling built in functions)'
    #skip_test(msg)
    print msg

    from freeode.interpreter import (IModule, ExecutionEnvironment,
                                     Interpreter, signature, IFloat)
    from freeode.util import aa_make_tree
    import freeode.simlparser as simlparser
    
    import math
    
    #parse the expression
    ps = simlparser.Parser()
    ex = ps.parseExpressionStr('sqrt(2)')
    print
    print 'AST (parser output): -----------------------------------------------'
    #print aa_make_tree(ex) 
    
    #create module where names live
    mod = IModule()
    #create sqrt function that can be called from Siml
    @signature([IFloat], IFloat)
    def sqrt(x): 
        return IFloat(math.sqrt(x.value))
    #put function into module
    mod.sqrt = sqrt
    print
    print 'Module where function is located: ----------------------------------'
    #print aa_make_tree(mod) 
    
    #create environment for lookup of variables (stack frame)
    env = ExecutionEnvironment()
    env.global_scope = mod
    #create visitor for evaluating the expression
    intp = Interpreter()
    intp.push_environment(env)
    #evaluate the expression
    res = intp.eval(ex)
    print
    print 'Result object: -----------------------------------------------------'
    print aa_make_tree(res)  
    assert res.value == math.sqrt(2)