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)
def test_ExpressionGenerator_2(): #IGNORE:C01111 msg = 'Test creation of expression strings. Check function call.' #skip_test(msg) print msg import math from freeode.pygenerator import ExpressionGenerator from freeode.interpreter import IFloat, BUILTIN_LIB from freeode.ast import NodeFuncCall e_gen = ExpressionGenerator() #create a variable a = IFloat() a.target_name = 'a' #get the "sin" function sin = BUILTIN_LIB.sin #create expression "sin(a)" and create Python code for it expr = NodeFuncCall(sin, (a,)) expr_str = e_gen.create_expression(expr) print expr_str #Execute the generated Python code assert eval(expr_str, {'a':0, 'sin':math.sin}) == 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)
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)
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)
def test_ExpressionGenerator_1(): #IGNORE:C01111 msg = 'Test creation of expression strings. Check all operators' #skip_test(msg) print msg from numpy import float64 from freeode.pygenerator import ExpressionGenerator, func from freeode.interpreter import IFloat from freeode.ast import RoleConstant, NodeFuncCall, NodeParentheses e_gen = ExpressionGenerator() #create some variables and numbers a = IFloat() a.target_name = 'a' b = IFloat() b.target_name = 'b' c = IFloat(2) c.role = RoleConstant #Get some functions that are converted into Python operators add = func(IFloat.__add__) sub = func(IFloat.__sub__) mul = func(IFloat.__mul__) div = func(IFloat.__div__) pow = func(IFloat.__pow__) mod = func(IFloat.__mod__) neg = func(IFloat.__neg__) #expression a + b * 2 expr = NodeFuncCall(add, (a, NodeFuncCall(mul, (b, c)))) expr_str = e_gen.create_expression(expr) print expr_str assert eval(expr_str, {'a':1, 'b':2, 'float64':float64}) == 1 + 2 * 2 #5 #expression a - b / 2 expr = NodeFuncCall(sub, (a, NodeFuncCall(div, (b, c)))) expr_str = e_gen.create_expression(expr) print expr_str assert eval(expr_str, {'a':1, 'b':2, 'float64':float64}) == 1 - 2 / 2 #0 #expression a ** b % 2 expr = NodeFuncCall(pow, (a, NodeFuncCall(mod, (b, c)))) expr_str = e_gen.create_expression(expr) print expr_str assert eval(expr_str, {'a':2, 'b':3, 'float64':float64}) == 2 ** 3 % 2 #0 #expression - 2 expr = NodeFuncCall(neg, (c,)) expr_str = e_gen.create_expression(expr) print expr_str assert eval(expr_str, {'float64':float64}) == -2 #expression (a + b) * 2 expr = NodeFuncCall(mul, (NodeParentheses((NodeFuncCall(add, (a, b)),) ), c) ) expr_str = e_gen.create_expression(expr) print expr_str assert eval(expr_str, {'a':1, 'b':2, 'float64':float64}) == (1 + 2) * 2 #6
def test_determine_result_role_2(): #IGNORE:C01111 msg = 'Test determine_result_role: errors' #skip_test(msg) print msg from freeode.interpreter import IFloat, determine_result_role from freeode.ast import (RoleConstant, RoleParameter, RoleVariable, RoleUnkown) #unknown role u = IFloat() u.__siml_role__ = RoleUnkown #some constants c_1 = IFloat() c_1.__siml_role__ = RoleConstant c_2 = IFloat() c_2.__siml_role__ = RoleConstant #some parameters p_1 = IFloat() p_1.__siml_role__ = RoleParameter p_2 = IFloat() p_2.__siml_role__ = RoleParameter #some variables v_1 = IFloat() v_1.__siml_role__ = RoleVariable v_2 = IFloat() v_2.__siml_role__ = RoleVariable #one argument with RoleUnknown - should raise exception def raise_1(): determine_result_role((c_1, p_1, v_1, u), {'a':c_2, 'b':p_2, 'c':v_2, }) assert_raises(ValueError, None, raise_1) #one argument with RoleUnknown - should raise exception def raise_2(): determine_result_role((c_1, p_1), {'a':c_2, 'b':p_2, 'u':u}) assert_raises(ValueError, None, raise_2) #one argument with RoleUnknown - should raise exception def raise_3(): determine_result_role((c_1, u), {'a':c_2}) assert_raises(ValueError, None, raise_3)
def test_determine_result_role_1(): #IGNORE:C01111 msg = 'Test determine_result_role: ' #skip_test(msg) print msg from freeode.interpreter import IFloat, determine_result_role from freeode.ast import RoleConstant, RoleParameter, RoleVariable #some constants c_1 = IFloat() c_1.__siml_role__ = RoleConstant c_2 = IFloat() c_2.__siml_role__ = RoleConstant #some parameters p_1 = IFloat() p_1.__siml_role__ = RoleParameter p_2 = IFloat() p_2.__siml_role__ = RoleParameter #some variables v_1 = IFloat() v_1.__siml_role__ = RoleVariable v_2 = IFloat() v_2.__siml_role__ = RoleVariable #determine the most variable role among the arguments assert issubclass(determine_result_role((c_1, p_1, v_1), {'a':c_2, 'b':p_2, 'c':v_2, }), RoleVariable) assert determine_result_role((c_1, p_1), {'a':c_2, 'b':p_2}) == RoleParameter assert determine_result_role((c_1,), {'a':c_2}) == RoleConstant