-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
143 lines (118 loc) · 4.38 KB
/
node.py
File metadata and controls
143 lines (118 loc) · 4.38 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Project : Compilateur (Python) #
# #
# File : node.py #
# #
# Description : Contains all node class declarations and functions. #
# #
# Contributors : Corentin TROADEC & Anthony Vuillemin #
# #
# Date : September 2018 #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# - - - - - - - - - - - - - - - - - #
# IMPORT #
# - - - - - - - - - - - - - - - - - #
# PROJECT MODULES
from conf import *
from utils import *
# - - - - - - - - - - - - - - - - - #
# CLASS : Node #
# - - - - - - - - - - - - - - - - - #
class Node() :
def __init__(self,type,val = None) :
self.type = type
self.val = val
self.childs = []
self.nbChild = 0
self.slot = None
#Add a child
def add_child(self,child) :
self.childs.append(child)
self.nbChild = self.nbChild + 1
# Display method
def __str__(self) :
return "["+self.type+"] ~ "+str(self.val)+" ~ "+str(self.nbChild)+" child(s)"
# For node to token
class NodeToken(Node) :
# Constructor
def __init__(self,token) :
self.type = getTypeForLeave(token.token)
self.val = token.val
self.line = token.line
self.col = token.col
self.childs = []
self.nbChild = 0
self.slot = None
# Display method
def __str__(self) :
return "["+self.type+"] ~ "+str(self.val)+" ~ ("+str(self.line)+";"+str(self.col)+") ~ "+str(self.nbChild)+" child(s)"
# - - - - - - - - - - - - - - - - - #
# CLASS : NodeUnaire #
# - - - - - - - - - - - - - - - - - #
# For unaire operator : contain one child.
class NodeUnaire(NodeToken) :
def __init__(self,token,child):
# inherit def
NodeToken.__init__(self,token)
# others declarations
self.type = getUnaireId(token.token)
self.childs.append(child)
self.nbChild = 1
# - - - - - - - - - - - - - - - - - #
# CLASS : BasicNode #
# - - - - - - - - - - - - - - - - - #
# For all the others node : contain at least two childs.
class BasicNode(NodeToken) :
def __init__(self,token,child1,child2) :
#inherit def
NodeToken.__init__(self,token)
# others declarations
self.type = getType(token.token)
self.childs.append(child1)
self.childs.append(child2)
self.nbChild = 2
class NodeVarRef(NodeToken) :
def __init__(self,token,idx = 0) :
#inherit def
NodeToken.__init__(self,token)
self.type = "node_varRef"
self.idx = idx
self.nbChild = 0
# Display method
def __str__(self) :
return "["+self.type+"] ~ "+str(self.val)+" ~ ("+str(self.line)+";"+str(self.col)+") ~ slot #"+str(self.slot)
# - - - - - - - - - - - - - - - - - #
# FUNCTIONS #
# - - - - - - - - - - - - - - - - - #
#Get node type in relation with the token type
def getUnaireId(token_type) :
if token_type in unaire_operator:
return unaire_operator[token_type]
else :
return False
#Get type of a node with no child (leave)
def getTypeForLeave(token_type) :
if token_type == "toke_const" :
return "node_const"
else :
return "node_id"
#Get type
def getType(token_type) :
if token_type in binaire_operator :
return binaire_operator[token_type]["type_node"]
else :
error_compilation(token,"Incompatible token")
#Display the tree since a node given
def display_tree(node,level) :
#DISPLAY NODE
string_space = ""
for i in range(0,level) :
string_space += "\t"
# Tree string - print
return_str = string_space + str(node) + "\n"
#DISPLAY CHILDS
if node != None :
for child in node.childs :
# Add string child in string racine
return_str += display_tree(child,level + 1)
return return_str