def s(self): """ id = expression | IF cond THEN s | IF cond THEN s else s | WHILE cond DO s """ token = self.current_token if token.type == IDN: varNode = Var(token) self.eat(IDN) op = self.current_token self.eat(EQL) right = self.expr() return Assign(varNode, op, right) elif token.type == IF: self.eat(IF) condition = self.cond() self.eat(THEN) statement1 = self.s() if self.current_token.type == ELSE: statement2 = self.s() return IfThenElse(condition, statement1, statement2) return IfThen(condition, statement1) elif token.type == WHILE: self.eat(WHILE) condition = self.cond() self.eat(DO) statement = self.s() return WhileDo(condition, statement)
def p_id(self, p): '''id : ID''' curr_symt = self.tableptr.top() entry = curr_symt.look_up(p[1]) if entry is None: print('undefined identifier %s.' % (p[1])) sys.exit(0) p[0] = Var(p[1], entry)
def split_expr(self, expr_ast): if isinstance(expr_ast, BinOp): tl = self.split_expr(expr_ast.left_child) tr = self.split_expr(expr_ast.right_child) if expr_ast.token.type == 'ASGN': self.temp_body.append(BinOp(tl, tr, expr_ast.token)) return None temp_var = Var('t' + str(self.temp_count + self.temp_start)) self.temp_count += 1 self.temp_body.append( BinOp(temp_var, BinOp(tl, tr, expr_ast.token), Token('ASGN', '='))) return temp_var elif isinstance(expr_ast, UnaryOp): if expr_ast.token.type in ('NOT', 'UMINUS'): t = self.split_expr(expr_ast.child) temp_var = Var('t' + str(self.temp_count + self.temp_start)) self.temp_count += 1 self.temp_body.append( BinOp(temp_var, UnaryOp(t, expr_ast.token), Token('ASGN', '='))) return temp_var else: t = self.split_expr(expr_ast.child) return UnaryOp(t, expr_ast.token) elif isinstance(expr_ast, FunctionCall): t_params = [self.split_expr(x) for x in expr_ast.actual_params] # temp_var = Var('t' + str(self.temp_count + self.temp_start)) # self.temp_count += 1 # self.temp_body.append(BinOp(temp_var, FunctionCall(expr_ast.id, t_params), Token('ASGN', '='))) # return temp_var return FunctionCall(expr_ast.id, t_params) else: return expr_ast
def factor(self): """factor : IDN | INT8 | INT10 | INT16 | LPAREN expr RPAREN""" token = self.current_token if token.type in (INT8, INT10, INT16): self.eat(token.type) return Num(token) elif token.type == LPAREN: self.eat(LPAREN) node = self.expr() self.eat(RPAREN) return node elif token.type == IDN: self.eat(IDN) return Var(token)
def factor(self): """factor : INTEGER | LPAREN expr RPAREN | { expr } """ token = self.current_token if token.type == INTEGER: self.eat(INTEGER) return Num(token) if token.type == LPAREN: self.eat(LPAREN) node = self.aexp() self.eat(RPAREN) return node if token.type == VAR: self.eat(VAR) return Var(token.value)
def command(self): """ c ::= skip | x := e | if b then c1 else c2 | while b do c """ if self.current_token.type == '{': self.eat('{') c = self.comma_command() self.eat('}') return c if self.current_token.type == 'skip': self.eat('skip') return Skip() if self.current_token.type == 'if': self.eat('if') b = self.b_or() self.eat('then') c1 = self.comma_command() self.eat('else') c2 = self.comma_command() return If(b, c1, c2) if self.current_token.type == 'while': # print("current token:", self.current_token.type, self.current_token.value) self.eat('while') # print("current token:", self.current_token.type, self.current_token.value) b = self.b_or() # print(b, b.token, b.value) # print("current token:", self.current_token.type, self.current_token.value) self.eat('do') # print("current token:", self.current_token.type, self.current_token.value) c = self.command() # print(c) # print("current token:", self.current_token.type, self.current_token.value) return While(b, c) if self.current_token.type == VAR: x = Var(self.current_token.value) self.eat(VAR) self.eat(':=') e = self.aexp() return Assign(x, e)
def p_id_nonuse(self, p): '''id_d : ID''' p[0] = Var(p[1]) self.last_id = p[1]
def p_id(self, p): '''id : ID''' t = Token('VAR', p[1]) p[0] = Var(t)
def addvar(self, var: Var): if var.getsort() in self.sorts: self.vars.add(var) else: raise MaudeException("Unknown sort: %s" % var.getsort())