当前位置: 首页>>代码示例>>Python>>正文


Python Interpreter.push_environment方法代码示例

本文整理汇总了Python中freeode.interpreter.Interpreter.push_environment方法的典型用法代码示例。如果您正苦于以下问题:Python Interpreter.push_environment方法的具体用法?Python Interpreter.push_environment怎么用?Python Interpreter.push_environment使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在freeode.interpreter.Interpreter的用法示例。


在下文中一共展示了Interpreter.push_environment方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_function_call_unknown_arguments_1

# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import push_environment [as 别名]
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)
开发者ID:eike-welk,项目名称:freeode,代码行数:48,代码来源:test_interpreter_1.py

示例2: test_expression_evaluation_4

# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import push_environment [as 别名]
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)
开发者ID:eike-welk,项目名称:freeode,代码行数:45,代码来源:test_interpreter_1.py

示例3: test_expression_evaluation_3

# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import push_environment [as 别名]
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
开发者ID:eike-welk,项目名称:freeode,代码行数:38,代码来源:test_interpreter_1.py


注:本文中的freeode.interpreter.Interpreter.push_environment方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。