def visitDecl(self, ctx): my_ast = AST.Decl() decl_info = self.extract_decl(ctx.pureDecl()) var = AST.Variable(decl_info[0]) var.set_source_loc(source_from_ctx(ctx)) my_ast.add_child(var) self._current_sym_table[decl_info[0]] = VarEntry(decl_info[1], None) my_ast.set_source_loc(source_from_ctx(ctx)) if ctx.ASSIGN_OP() is not None: assign = AST.AssignOp() assign.add_child(my_ast) assign.add_child(self.visit(ctx.expr())) assign.set_source_loc(source_from_ctx(ctx)) return assign else: return my_ast
def visitExpr(self, ctx): # skip if ctx.functionCall(): a = self.visit(ctx.getChild(0)) return a if ctx.LEFT_PAREN() and ctx.RIGHT_PAREN(): return self.visit(ctx.getChild(1)) if ctx.getChildCount() == 1: return self.visit(ctx.getChild(0)) my_ast = None # unary operator if ctx.getChildCount() == 2: if ctx.NOT_OP(): my_ast = AST.Not() elif ctx.PLUS(): my_ast = AST.Pos() elif ctx.MINUS(): my_ast = AST.Neg() elif ctx.AMP(): my_ast = AST.Adress() elif ctx.STAR(): my_ast = AST.Indir() elif isinstance( ctx.getChild(0), GrammarParser.ExprContext): # postfix operators go here if ctx.DECR(): my_ast = AST.DecrPost() elif ctx.INCR(): my_ast = AST.IncrPost() elif ctx.arrayIndex(): my_ast = AST.Index() my_ast.set_index(self.visit(ctx.arrayIndex())) my_ast.set_positional_child(0, self.visit(ctx.getChild(0))) my_ast.set_source_loc(source_from_ctx(ctx)) return my_ast elif ctx.INCR(): my_ast = AST.IncrPre() elif ctx.DECR(): my_ast = AST.DecrPre() my_ast.add_child(self.visit(ctx.getChild(1))) # binary operators if ctx.getChildCount() == 3: # ASSIGNMENT if ctx.ASSIGN_OP(): my_ast = AST.AssignOp() # BOOLEAN OPERATORS elif ctx.AND_OP(): my_ast = AST.And() elif ctx.OR_OP(): my_ast = AST.Or() # COMPARISON OPERATORS if ctx.SMALLER_OP(): my_ast = AST.Less() elif ctx.GREATER_OP(): my_ast = AST.More() elif ctx.EQUAL_OP(): my_ast = AST.Equal() elif ctx.SMALLER_E_OP(): my_ast = AST.LessE() elif ctx.GREATER_E_OP(): my_ast = AST.MoreE() elif ctx.NOT_EQUAL_OP(): my_ast = AST.NotEqual() # MATH OPERATORS if ctx.STAR(): my_ast = AST.Prod() elif ctx.SLASH(): my_ast = AST.Div() elif ctx.PERCENT(): my_ast = AST.Mod() elif ctx.PLUS(): my_ast = AST.Sum() elif ctx.MINUS(): my_ast = AST.Sub() my_ast.add_child(self.visit(ctx.getChild(0))) my_ast.add_child(self.visit(ctx.getChild(2))) my_ast.set_source_loc(source_from_ctx(ctx)) return my_ast