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 The decision is based on the first token in the input line """ iterator = Peekable(new_split_iter(expr)) if peek(iterator) == "deffn": name, parms, body = define_func(iterator) functions.assign(name, (parms, body)) else: print(expr,':',tree_assign(iterator).evaluate(variables, functions))
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()))
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))
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, 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 ) 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() ) )
def to_expr_tree(expr): return tree_assign(Peekable(new_split_iter(expr)))
def eval_infix(expr): """accept a character string, split it into tokens, then evaluate""" return eval_infix_iter(new_split_iter(expr))
def to_postfix (expr): return postfix_sum(Peekable(new_split_iter(expr)))
def eval_infix(expr): return eval_infix_iter(new_split_iter(expr))