-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
177 lines (169 loc) · 6.24 KB
/
main.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
import argparse
import subprocess
import os
import uuid
class interactive_compiler:
def add_extended_funcs(self):
extended_functions = 'void print(auto prompt){std::cout<<prompt<<endl;}'
self.functions += extended_functions
self.functions_dict.append('print')
def __init__(self):
parser = argparse.ArgumentParser(description='')
parser.add_argument('-d', '--debug',
action='store_true',
help='enable debugging',
dest='debug',
default=False)
parser.add_argument('-s', '--stripped',
action='store_true',
help='disable additional helper functions and default namespace',
dest='stripped',
default=False)
self.args = parser.parse_args()
self.includes = '#include <iostream>\nusing namespace std;\n'
self.func_level = 0
self.functions = ''
self.functions_dict = []
self.vars = ''
self.vars_dict = []
self.main = ''
self.types = ['int', 'string', 'void', 'float', 'char']
if self.args.stripped:
self.includes = ''
else:
self.add_extended_funcs()
def compile(self):
assembled_file = self.includes+'\n'+self.functions+'\nint main(){\n'+\
self.vars+'\n'+self.main+'\nreturn 0;\n}'
if self.args.debug:
print(f'VARS DICT: {self.vars_dict}')
print(f'FULL FILE:\n{assembled_file}')
with open('tmp.cpp', 'w') as file:
file.write(assembled_file)
filename = str(uuid.uuid4())
if self.args.debug:
print('COMPILATION STAGE START:')
compile_command = ['g++', 'tmp.cpp', '-o', filename]
if not self.args.debug:
compile_command.append('-w')
subprocess.run(compile_command)
if os.name == "nt":
name = ".\\" + filename + ".exe"
else:
name = "./" + filename
if os.path.isfile(name):
if self.args.debug:
print('COMPILATION STAGE END')
subprocess.run([name])
subprocess.run(['rm', name])
else:
print('COMPILATION FAILED')
subprocess.run(['rm', 'tmp.cpp'])
def include(self, name):
self.includes += f'#include {name}\n'
def validate(self, line):
if line[-1] != ';':
if line.split(' ')[0] in self.types and '(' in line:
return line
else:
line += ';'
if self.args.debug:
print('VALIDATOR: line completed with ;')
return line
def exec_line(self, line):
first_word = line.split(' ')[0]
cmd_list = {'reset': 'reset the scope',
'scope': 'print the current scope',
'add_type': 'add a new var type',
'help': 'print help',
'debug': 'enable/disable debugging'}
if not line:
print('execution complete, exiting')
exit()
elif line[0] == '!' and not first_word[1:] in cmd_list:
print(first_word[1:])
print(f'{line[1:]} is not a command')
return
elif first_word == '!reset':
self.__init__()
print('scope reset')
return
elif first_word == '!scope':
print(f'INCLUDES:\n{self.includes}')
print(f'VARIABLE_TYPES: {self.types}')
print(f'VARIABLES:\n{self.vars_dict}')
print(f'FUNCTIONS:\n{self.functions}')
return
elif first_word == '!help':
print('cmd list:')
for cmd in cmd_list:
print(f'{cmd}: {cmd_list[cmd]}')
return
elif first_word == '!add_type':
for type in line.split(' ')[1:]:
if type not in self.types:
self.types.append(type)
else:
print('already present')
return
elif first_word == '!debug':
if self.args.debug:
self.args.debug = False
print('debugging is now off')
else:
self.args.debug = True
print('debugging is now on')
return
elif line in self.vars_dict:
if self.args.debug:
print('AUTOPRINTING')
line = f'cout<<{line}<<endl;'
self.main += line+'\n'
elif self.func_level:
if '}' in line:
self.func_level -= 1
self.functions += '}\n'
else:
self.functions += self.validate(line)
return
elif first_word in self.types:
if '(' in line:
self.func_level += 1
self.functions += line
self.functions_dict.append(line.split(' ')[1].split('(')[0])
return
else:
var_type, var_name = line.split(' ', 1)
var_name = var_name.split('=')[0]
if var_name in self.vars_dict:
tmp = self.vars
self.vars = ''
for x in tmp.split('\n'):
if f' {var_name}' not in x:
self.vars=self.vars+x+'\n'
self.vars += f'{self.validate(line)}\n'
if not var_name in self.vars_dict:
self.vars_dict.append(var_name)
return
elif first_word == '#include':
self.includes += line
return
else:
if self.args.debug:
print('UNRECOGNIZED LINE')
self.main += self.validate(line)+'\n'
self.compile()
self.main = ''
icpp = interactive_compiler()
if __name__ == '__main__':
default_prompt = '>>> '
function_prompt = '... '
print('for help: !help')
try:
while True:
if icpp.func_level > 0:
icpp.exec_line(input(function_prompt))
else:
icpp.exec_line(input(default_prompt))
except (EOFError, KeyboardInterrupt):
print('\ninterrupted, stopping')