-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_node.py
41 lines (30 loc) · 1.04 KB
/
ast_node.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
from tokens import *
"""
AST节点, 每个节点对应一种操作
"""
class NumberNode(object):
def __init__(self, token: Token):
self.tok = token
self.pos_start = self.tok.pos_start
self.pos_end = self.tok.pos_end
def __repr__(self):
return f'{self.tok}'
class BinOpNode(object):
# 二元操作 + - * /
def __init__(self, left_node: Token, op_tok: Token, right_node: Token):
self.left_node = left_node
self.op_tok = op_tok
self.right_node = right_node
self.pos_start = self.left_node.pos_start
self.pos_end = self.right_node.pos_end
def __repr__(self):
return f'({self.left_node, self.op_tok, self.right_node})'
class UnaryOpNode(object):
# 一元操作 -1 op_tok = -, node = 1
def __init__(self, op_tok: Token, node: Token):
self.op_tok = op_tok
self.node = node
self.pos_start = self.op_tok.pos_start
self.pos_end = self.node.pos_end
def __repr__(self):
return f'({self.op_tok, self.node})'