-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr.py
59 lines (40 loc) · 1.33 KB
/
expr.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
from token_ import Token
from abc import ABCMeta, abstractmethod
class Expr:
pass
class IVisitor(metaclass=ABCMeta):
@abstractmethod
def visit_binary_expr(self, expr):
raise NotImplementedError
@abstractmethod
def visit_grouping_expr(self, expr):
raise NotImplementedError
@abstractmethod
def visit_literal_expr(self, expr):
raise NotImplementedError
@abstractmethod
def visit_unary_expr(self, expr):
raise NotImplementedError
class Binary(Expr):
def __init__(self, left: Expr, operator: Token, right: Expr):
self.left = left
self.operator = operator
self.right = right
def accept(self, visitor: IVisitor):
return visitor.visit_binary_expr(self)
class Grouping(Expr):
def __init__(self, expression: Expr):
self.expression = expression
def accept(self, visitor: IVisitor):
return visitor.visit_grouping_expr(self)
class Literal(Expr):
def __init__(self, value: object):
self.value = value
def accept(self, visitor: IVisitor):
return visitor.visit_literal_expr(self)
class Unary(Expr):
def __init__(self, operator: Token, right: Expr):
self.operator = operator
self.right = right
def accept(self, visitor: IVisitor):
return visitor.visit_unary_expr(self)