-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
183 lines (154 loc) · 6.23 KB
/
utils.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Project : Compilateur (Python) #
# #
# File : utils.py #
# #
# Description : Contain all the differents utils use during the compilation. #
# #
# Contributors : Corentin TROADEC & Anthony Vuillemin #
# #
# Date : September 2018 #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# - - - - - - - - - - - - - - - - - #
# IMPORT #
# - - - - - - - - - - - - - - - - - #
# PROJECT MODULES
from conf import *
from node import *
from token import *
# SYSTEM MODULES
import datetime
# - - - - - - - - - - - - - - - - - #
# FUNCTIONS #
# - - - - - - - - - - - - - - - - - #
# Return true if param s is an int
def RepresentsInt(s):
try:
int(s)
return True
except ValueError:
return False
# Exit the program and give information about an error during the compilation
def error_compilation(token_or_node,msg) :
# HEADER ERROR MSG
header_error_msg = '\033[91m'+"\n+ ----------------------------------- +\n"
header_error_msg += "| ....... COMPILATION FAILED ........ | \n"
header_error_msg += "+ ----------------------------------- +\n"+'\033[0m'
# BUILD MSG
error_msg = "[ERROR] ~ " + msg +"\n"
if token_or_node != None and isinstance(token_or_node,Token) :
error_msg += "[ERROR] ~ An error has been detected : Line "+str(token_or_node.line)
error_msg +=" & Column "+str(token_or_node.col)+"."
error_msg += "\n[ERROR] ~ "+str(token_or_node)
#PRINT
print header_error_msg + '\033[93m' + error_msg +'\033[0m'
# SAVE IN LOG & EXIT
log_msg("ERROR","Error during compilation :\n"+error_msg,True)
log_msg("END / ERROR","Compilation end with an error statement.",True)
print '\033[91m'+ "[END / ERROR] ~ Compilation end with an error statement.\n" +'\033[0m'
exit()
# Write logs in an file
def log_msg(type,msg, compil_error = False) :
if log_activation == True :
now = datetime.datetime.now()
str_now = now.strftime("%d/%m/%Y %H:%M:%S-")
str_now += str(now.microsecond)
log_file = open(log_file_name, "a+")
if type != "" :
log_file.write("["+type+"] - "+str_now+" ~ "+msg+"\n")
else :
log_file.write(msg+"\n")
# Change src file
def change_src_file(ext_file) :
global test_code_file
test_code_file = ext_file
# Return src file used
def used_src_file() :
return test_code_file
# Active debug mod
def active_debug_mod() :
global debug_mod
debug_mod = True
# Return debug mod statement
def stat_debug_mode() :
return debug_mod
# Display debug message if debug mod is on
def DEBUG_MSG(msg,type = "",saveInLogFile = True) :
# Save in log file ?
if saveInLogFile == True :
log_msg(type,msg)
# Debug mod on ?
if debug_mod == True :
if type != "" :
print "["+type+"] ~ "+msg
else :
print msg
# Active debug mod
def active_log_save() :
global log_activation
log_activation = True
# Write an instruction (assemblor) in the specific file
def write_assemblor_file(text) :
ass_file = open(assemblor_file_name,"a+")
ass_file.write(text+"\n")
# Give the result of the expression pass
def eval_expr(node) :
#CONST
if node.type == "node_const" :
return node.val
#UNAIRE OPERATOR
elif node.type == "node_add_U" :
return eval_expr(node.childs[0])
elif node.type == "node_min_U" :
return - eval_expr(node.childs[0])
#!
elif node.type == "node_not_U" :
if eval_expr(node.childs[0]) == 0 :
return 1
else :
return 0
#BINAIRE NATURAL OPERATOR
elif node.type == "node_add" :
return eval_expr(node.childs[0]) + eval_expr(node.childs[1])
elif node.type == "node_min" :
return eval_expr(node.childs[0]) - eval_expr(node.childs[1])
elif node.type == "node_mult" :
return eval_expr(node.childs[0]) * eval_expr(node.childs[1])
elif node.type == "node_pow" :
return pow(eval_expr(node.childs[0]),eval_expr(node.childs[1]))
# DIVISION
elif node.type == "node_div" :
if eval_expr(node.childs[1]) != 0 :
return eval_expr(node.childs[0]) / eval_expr(node.childs[1])
else :
error_compilation(node.childs[1],'Division operation by zero.')
# MODULO
elif node.type == "node_mod" :
if eval_expr(node.childs[1]) != 0 :
return eval_expr(node.childs[0]) % eval_expr(node.childs[1])
else :
error_compilation(node.childs[1],'Modulo operation by zero.')
#BINAIRE LOGIC OPERATOR
elif node.type == "node_equal" :
return booleanToInt(eval_expr(node.childs[0]) == eval_expr(node.childs[1]))
elif node.type == "node_le" :
return booleanToInt(eval_expr(node.childs[0]) < eval_expr(node.childs[1]))
elif node.type == "node_ge" :
return booleanToInt(eval_expr(node.childs[0]) > eval_expr(node.childs[1]))
elif node.type == "node_geq" :
return booleanToInt(eval_expr(node.childs[0]) >= eval_expr(node.childs[1]))
elif node.type == "node_leq" :
return booleanToInt(eval_expr(node.childs[0]) <= eval_expr(node.childs[1]))
elif node.type == "node_diff" :
return booleanToInt(eval_expr(node.childs[0]) != eval_expr(node.childs[1]))
elif node.type == "node_and" :
return booleanToInt(eval_expr(node.childs[0]) and eval_expr(node.childs[1]))
elif node.type == "node_or" :
return booleanToInt(eval_expr(node.childs[0]) or eval_expr(node.childs[1]))
# Convert boolean to Int
# OR et AND attention evaluation - en python tout les enfants ne sont pas evalues
def booleanToInt(p_bool) :
if(p_bool) :
return 1
else :
return 0