-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMathVisitor.py
59 lines (43 loc) · 1.78 KB
/
MathVisitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Generated from Math.g4 by ANTLR 4.7.2
from antlr4 import *
from MathAst import *
if __name__ is not None and "." in __name__:
from .MathParser import MathParser
else:
from MathParser import MathParser
# This class defines a complete generic visitor for a parse tree produced by MathParser.
class MathVisitor(ParseTreeVisitor):
# Visit a parse tree produced by MathParser#compileUnit.
def visitCompileUnit(self, ctx:MathParser.CompileUnitContext):
return self.visit(ctx.expr())
# Visit a parse tree produced by MathParser#infixExpr.
def visitInfixExpr(self, ctx:MathParser.InfixExprContext):
node = InfixExpressionNode()
if ctx.OP_ADD():
node.value = '+'
elif ctx.OP_DIV():
node.value = '/'
elif ctx.OP_MUL():
node.value = '*'
elif ctx.OP_SUB():
node.value = '-'
node.left = self.visit(ctx.left)
node.right = self.visit(ctx.right)
return node
# Visit a parse tree produced by MathParser#unaryExpr.
def visitUnaryExpr(self, ctx:MathParser.UnaryExprContext):
if ctx.OP_ADD():
return self.visit(ctx.expr())
elif ctx.OP_SUB():
return NegateNode(self.visit(ctx.expr()))
return self.visitChildren(ctx)
# Visit a parse tree produced by MathParser#funcExpr.
def visitFuncExpr(self, ctx:MathParser.FuncExprContext):
return self.visitChildren(ctx)
# Visit a parse tree produced by MathParser#numberExpr.
def visitNumberExpr(self, ctx:MathParser.NumberExprContext):
return NumberNode(value=int(str(ctx.NUM())))
# Visit a parse tree produced by MathParser#parensExpr.
def visitParensExpr(self, ctx:MathParser.ParensExprContext):
return self.visit(ctx.expr())
del MathParser