Пример #1
0
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)
Пример #2
0
def test_operator_dispatch_1(): #IGNORE:C01111
    msg = 'Test Interpreter: handling of operators with known Float values.'
    #skip_test(msg)
    print msg
    
    from freeode.interpreter import (IFloat, Interpreter)
    from freeode.ast import NodeOpInfix2, NodeOpPrefix1
    
    intp = Interpreter()
    
    val_2 = IFloat(2)
    val_3 = IFloat(3)
    
    op_sub = NodeOpInfix2('-', [val_2, val_3])
    res = intp.eval(op_sub)
    print res
    assert res.value == -1 
    
    op_neg = NodeOpPrefix1('-', [val_2])
    res = intp.eval(op_neg)
    print res
    assert res.value == -2 
Пример #3
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)
Пример #4
0
def test_expression_evaluation_5(): #IGNORE:C01111
    msg = \
    '''
    Test expression evaluation (returning of partially evaluated expression 
    when accessing variables)
    '''
    #skip_test(msg)
    print msg

    from freeode.ast import RoleVariable, NodeFuncCall
    from freeode.interpreter import (IModule, IFloat, ExecutionEnvironment, 
                                     Interpreter, istype)
    import freeode.simlparser as simlparser
    from freeode.util import func #, aa_make_tree
    
    #parse the expression
    ps = simlparser.Parser()
    ex = ps.parseExpressionStr('a + 2*2')
#    print
#    print 'AST (parser output): -----------------------------------------------------------'
#    print aa_make_tree(ex)
    
    #create module where name lives
    mod = IModule()
    #create attribute 'a' with no value
    val_2 = IFloat()
    val_2.__siml_role__ = RoleVariable
    mod.a = val_2
#    print
#    print 'Module where variable is located: --------------------------------------------'
#    print aa_make_tree(mod)
    
    #create environment for lookup of variables (stack frame)
    env = ExecutionEnvironment()
    env.global_scope = mod
    
    #interpret the expression
    intp = Interpreter()
    intp.environment = env
    res = intp.eval(ex)
#    print
#    print 'Result object - should be an unevaluated expression: --------------------------------------------------------------'
#    print aa_make_tree(res)
    assert isinstance(res, NodeFuncCall)
    assert res.function is func(IFloat.__add__)
    assert istype(res, IFloat)
Пример #5
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)
Пример #6
0
def test_function_call_unknown_arguments_2(): #IGNORE:C01111
    msg = 'Test Interpreter: call real library function with unknown argument'
    #skip_test(msg)
    print msg

    from freeode.interpreter import Interpreter, IFloat, istype
    from freeode.ast import NodeFuncCall, NodeIdentifier, RoleVariable

    #create the interpreter
    intp = Interpreter()    #IGNORE:W0612
    intp.create_test_module_with_builtins()
    #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], {})
    #interpret call
    ret_val = intp.eval(call)
    #evaluating a function call with unknown arguments must return a function call
    assert isinstance(ret_val, NodeFuncCall)
    assert istype(ret_val, IFloat)
Пример #7
0
def test_expression_evaluation_2(): #IGNORE:C01111
    msg = 'Test expression evaluation, all operators and brackets (only immediate values)'
    #skip_test(msg)
    print msg

    from freeode.interpreter import Interpreter
    import freeode.simlparser as simlparser
    
    #parse the expression
    ps = simlparser.Parser()
    ex = ps.parseExpressionStr('(((0-1+2)*3--1)/4)**5*(-1+2*--1**4)%6')
    print
    print 'AST (parser output): -----------------------------------------------------------'
    #print ex
    
    #interpret the expression
    intp = Interpreter()
    res = intp.eval(ex)
    print
    print 'Result object: --------------------------------------------------------------'
    #print res 
    assert res.value == 1
Пример #8
0
def test_expression_evaluation_1(): #IGNORE:C01111
    msg = 'Test expression evaluation (only immediate values)'
    #skip_test(msg)
    print msg

    from freeode.interpreter import Interpreter
    import freeode.simlparser as simlparser
    
    #parse the expression
    ps = simlparser.Parser()
    ex = ps.parseExpressionStr('0+1*2')
#    print
#    print 'AST (parser output): -----------------------------------------------------------'
#    print ex
    
    #interpret the expression
    intp = Interpreter()
    res = intp.eval(ex)
#    print
#    print 'Result object: --------------------------------------------------------------'
#    print res 
    assert res.value == 2.0
Пример #9
0
def test_expression_evaluation_3(): #IGNORE:C01111
    msg = 'Test expression evaluation (access to variables)'
    #skip_test(msg)
    print msg

    from freeode.interpreter import (IModule, IFloat,  
                                     Interpreter, ExecutionEnvironment)
    import freeode.simlparser as simlparser
    
    #parse the expression
    ps = simlparser.Parser()
    ex = ps.parseExpressionStr('1 + a * 2')
    print
    print 'AST (parser output): -----------------------------------------------------------'
    print ex
    
    #create module where name lives
    mod = IModule()
    val_2 = IFloat(2.0)
    mod.a = val_2
    print
    print 'Module where variable is located: --------------------------------------------'
    print mod
    
    #create environment for lookup of variables (stack frame)
    env = ExecutionEnvironment()
    env.global_scope = mod
    
    #interpret the expression
    intp = Interpreter()
    intp.push_environment(env)
    res = intp.eval(ex)
    print
    print 'Result object: --------------------------------------------------------------'
    print res 
    assert res.value == 5.0