-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_ast.py
55 lines (38 loc) · 1.25 KB
/
base_ast.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
from abc import ABC, abstractmethod
class Node:
def evaluate(self):
raise NotImplementedError()
def accept(self, visitor_):
pass
class AtomicNode(Node):
def __init__(self, lex):
self.lex = lex
def accept(self, visitor_):
return visitor_.visit(self)
class UnaryNode(Node):
def __init__(self, node: Node):
self.node = node
def evaluate(self):
value = self.node.evaluate()
return self.operate(value)
def accept(self, visitor_):
return visitor_.visit(self)
@abstractmethod
def operate(self, value):
raise NotImplementedError()
class BinaryNode(Node):
def __init__(self, left: Node, right: Node):
self.left = left
self.right = right
def evaluate(self):
lvalue = self.left.evaluate()
rvalue = self.right.evaluate()
return self.operate(lvalue, rvalue)
def accept(self, visitor_):
return visitor_.visit(self)
def operate(self, lvalue, rvalue):
raise NotImplementedError()
# def get_printer(AtomicNode=AtomicNode, UnaryNode=UnaryNode, BinaryNode=BinaryNode, ):
# class PrintVisitor(object):
# printer = PrintVisitor()
# return (lambda ast: printer.visit(ast))