-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
59 lines (51 loc) · 1.28 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
def print_pretty_ast(ast_text):
i = 0
newline = False
for ch in str(ast_text):
if ch == '(':
if not newline:
print(end='')
#newline or print(end='')
print(ch)
i += 2
newline = True
elif ch == ')':
if not newline:
print()
#newline or print()
i -= 2
newline = True
print(' '*i + ch)
else:
if newline:
print(' '*i, end='')
#not newline or print(' '*i, end='')
print(ch, end='')
newline = False
def stringify(val):
if isinstance(val, bool) and val == True:
return "true"
if isinstance(val, bool) and val == False:
return "false"
if isinstance(val, float) and val.is_integer():
return str(int(val))
return str(val)
def lexing_error(message, lineno):
print(f'{Colors.RED}[Line {lineno}]: {message} {Colors.WHITE}')
import sys
sys.exit(1)
def parse_error(message, lineno):
print(f'{Colors.RED}[Line {lineno}]: {message} {Colors.WHITE}')
import sys
sys.exit(1)
def runtime_error(message, lineno):
print(f'{Colors.RED}[Line {lineno}]: {message} {Colors.WHITE}')
import sys
sys.exit(1)
class Colors:
WHITE = '\033[0m'
BLUE = '\033[94m'
CYAN = '\033[96m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'