def notices_stmt(node): try: # try the if-then pattern (NOTICES, cond, then_stmt, (NIL, ), STAWP) = node assert_match(NOTICES, 'notices') assert_match(STAWP, 'stawp') assert_match(NIL, 'nil') except ValueError: # if-then pattern didn't match (NOTICES, cond, then_stmt, else_stmt, STAWP) = node assert_match(NOTICES, 'notices') value = walk(cond) if value != 0: walk(then_stmt) else: walk(else_stmt) assert_match(STAWP, 'stawp') else: # if-then pattern matched value = walk(cond) if value != 0: walk(then_stmt)
def put_stmt(node): (PUT, exp) = node assert_match(PUT, 'put') (t, v) = walk(exp) print("{}".format(v))
def nuzzels_stmt(node): (NUZZELS, exp) = node assert_match(NUZZELS, 'nuzzels') value = walk(exp) print("> {}".format(value))
def id_exp(node): (ID, name) = node assert_match(ID, 'id') code = [('push', name)] return code
def get_stmt(node): (GET, name) = node assert_match(GET, 'get') (sym_type, data_type, *_) = state.symbol_table.lookup_sym(name) if sym_type != 'scalar-val': raise ValueError(\ "Cannot assign a value to the name {} in a get"\ .format(name)) s = input("Value for " + name + '? ') try: if data_type == 'integer': value = ('scalar-val', 'integer', int(s)) elif data_type == 'float': value = ('scalar-val', 'float', float(s)) elif data_type == 'string': value = ('scalar-val', 'string', s) except ValueError: raise ValueError(\ "unexpected value for variable {} of type {}"\ .format(name, data_type)) else: raise ValueError(\ "variable {} of type {} and not supported in get statement"\ .format(name, data_type)) state.symbol_table.update_sym(name, sym_type, value)
def integer_exp(node): (INTEGER, value) = node assert_match(INTEGER, 'integer') code = [('push', str(value))] return code
def seq(node, level): (SEQ, stmt, stmt_list) = node assert_match(SEQ, 'seq') walk(stmt, level+1) walk(stmt_list, level+1)
def while_stmt(node, level): (WHILE, cond, body) = node assert_match(WHILE, 'while') walk(cond, level+1) walk(body, level+1)
def while_stmt(node): (WHILE, cond, body) = node assert_match(WHILE, 'while') walk(cond) walk(body)
def assign_stmt(node): (ASSIGN, name, exp) = node assert_match(ASSIGN, 'assign') (type, value) = walk(exp) state.symbol_table.update_sym(name, (type, value))
def paren_exp(node): (PAREN, exp) = node assert_match(PAREN, 'paren') # get rid of parenthesis - not necessary in AST return walk(exp)
def seq(node): (SEQ, stmt, stmt_list) = node assert_match(SEQ, 'seq') walk(stmt) walk(stmt_list)
def print_stmt(node): (PRINT, exp) = node assert_match(PRINT, 'print') (_, v) = walk(exp) print(str(v))
def paren_exp(node): (PAREN, exp) = node assert_match(PAREN, 'paren') # return the value of the parenthesized expression return walk(exp)
def assign_stmt(node): (ASSIGN, name, exp) = node assert_match(ASSIGN, 'assign') value = walk(exp) state.symbol_table[name] = value
def uminus_exp(node): (UMINUS, exp) = node assert_match(UMINUS, 'uminus') val = walk(exp) return -val
def not_exp(node): (NOT, exp) = node assert_match(NOT, 'not') val = walk(exp) return 0 if val != 0 else 1
def nil(node): (NIL, ) = node assert_match(NIL, 'nil') # empty node, so we do nothing pass
def fundef_stmt(node): global frame_size (FUNDEF, name, formal_arglist, body, f_frame_size) = node assert_match(FUNDEF, 'fundef') frame_size = f_frame_size ignore_label = label() code = [('jump', ignore_label)] code += [('#', ' ')] code += [('#', 'Start of function ' + name)] code += [('#', ' ')] code += [(name + ':', )] code += [('pushf', str(frame_size))] code += init_formal_args(formal_arglist, 0, frame_size) code += walk(body) code += [('popf', str(frame_size))] code += [('return', )] code += [('#', ' ')] code += [('#', 'End of function ' + name)] code += [('#', ' ')] code += [(ignore_label + ':', )] code += [('noop', )] frame_size = None return code
def print_stmt(node): (PRINT, exp) = node assert_match(PRINT, 'print') value = walk(exp) print("{}".format(value))
def put_stmt(node): (PUT, exp) = node assert_match(PUT, 'put') value = walk(exp) print("> {}".format(value))
def put_stmt(node): (PUT, exp) = node assert_match(PUT, 'put') (_, v) = walk(exp) print(str(v))
def nil(node): (NIL, ) = node assert_match(NIL, 'nil') # do nothing! pass
def put_stmt(node): (PUT, exp) = node assert_match(PUT, 'put') t = walk(exp) return ('put', t)
def get_stmt(node): (GET, name) = node assert_match(GET, 'get') target_name = state.symbol_table.get_target_name(name) return ('get', target_name)
def get_stmt(node): (GET, name) = node assert_match(GET, 'get') code = [('input', name)] return code
def block_stmt(node): (BLOCK, stmt_list) = node assert_match(BLOCK, 'block') state.symbol_table.push_scope() walk(stmt_list) state.symbol_table.pop_scope()
def id_exp(node): (ID, name) = node assert_match(ID, 'id') target_name = state.symbol_table.get_target_name(name) return ('id', target_name)
def paren_exp(node): (PAREN, exp) = node assert_match(PAREN, 'paren') exp_code = walk(exp) return exp_code
def block_stmt(node): (BLOCK, s) = node assert_match(BLOCK, 'block') code = walk(s) return code