示例#1
0
def compile_line(program, expr):
    iterator = Peekable(new_split_iter(expr))
    if peek(iterator) == "deffn":
        """
        name, parms, body = define_func(iterator)
        local_variables = VarTree()
        code_line = len(program.code)
        program.functions.assign(name, (parms, body, code_line, local_variables))
        body.comp( local_variables, program )
        """
        pass
    else:
        if program.first_stmt == -1:  # if 'main' not yet assigned
            program.first_stmt = len(program.code)  #    start executing here
        to_expr_tree(expr).comp(program.variables, program)
        program.code.append(Print(program.code[-1].get_temp()))
示例#2
0
def evaluate(expr):
    itera = Peekable(new_split_iter(expr))
    if peek(itera) == "deffn":
        name, p, body = define_func(itera)
        functions.assign(name, (p, body))
    else:
        print(expr,':',to_expr_tree(expr).evaluate(variables, functions))
def evaluate(expr):
    """Define a new function, or evaluate an expression

    If the first word in the line "deffn", it should appear as
          deffn <function name> ( <parameters> ) = <function body>
          a VarTree will associate the function name with
            a list of parameters (at least one, maybe more)
            and a tree representing the function body
    otherwise the input line is evaluated in an expression tree
    """
    iterator = Peekable(new_split_iter(expr))
    if peek(iterator) == "deffn":
        name, parms, body = define_func(iterator)
        functions.assign(name, (parms, body))
    else:
        print(expr, ':', to_expr_tree(expr).evaluate(variables, functions))
示例#4
0
def compile_line(program, expr):
    """Define a new function, or evaluate an expression

    If the first word in the line "deffn", it should appear as
          deffn <function name> ( <parameters> ) = <function body>
    otherwise the input line is evaluated in an expression tree
    """
    iterator = Peekable(new_split_iter(expr))
    if peek(iterator) == "deffn":
        name, body = define_func(iterator)
        local_variables = BinaryTree()
        program.functions.assign(name, (body, local_variables))
        compile_tree(body, local_variables, program.functions,
                     program.code)  # program code - output
    else:
        if program.first_stmt == -1:  # if 'main' not yet assigned
            program.first_stmt = len(program.code)  #    start executing here
        compile_tree(to_expr_tree(expr), program.variables, program.functions,
                     program.code)
        program.code.append(Print(program.code[-1].get_temp()))
示例#5
0
def test(expr):
    tree = to_expr_tree(expr)
    print(expr, ':', tree.evaluate(V))
    if '?' not in expr:
        print("from postfix:", eval_postfix(tree.postfix()))