-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcompile.py
155 lines (123 loc) · 4.79 KB
/
compile.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
#! /usr/bin/python3
import sys
sys.dont_write_bytecode = True
from Syntax import Syntax
from erronoues import *
from tokens import *
from Logger import *
from functions import *
from pathlib import Path
import subprocess
sys.dont_write_bytecode = True
__testcase__ = False
arguments = sys.argv
if len(arguments) > 2 and ('java' not in arguments and 'cpp' not in arguments):
# print('testcase is on')
test_payload = eval(arguments[2])
payload=iter(test_payload)
__testcase__ = True
class Declare:
declared_variables = {}
def __str__(self) -> str:
return f'''
variable_name : type
{Declare.declared_variables}'''
def register(self):
if self._type == NUMBER:
self.integer()
elif self._type == STRING:
self.string()
else: raise Exception('invalid type')
def __init__(self,line:str):
self.command = line
self.line = line.split(" ")
self._type = self.line[0]
self._identifier = self.line[1]
self._value = self.line[-1]
Declare.declared_variables[self._identifier] = self._type
@staticmethod
def inputVar(identifiers:str):
if ',' in identifiers:
identifiers = identifiers.split(',')
else : identifiers = [identifiers]
for identifier in identifiers:
_type = Declare.declared_variables[identifier]
if _type == NUMBER:
if not __testcase__:globals()[identifier] = float(input())
else : globals()[identifier] = next(payload)
else:
if not __testcase__:globals()[identifier] = input()
else : globals()[identifier] = next(payload)
def integer(self):
self._value = float(self._value)
globals()[self._identifier] = self._value
# print(self._identifier)
def string(self):
if '\"' in self._value or "\'" in self._value:
globals()[self._identifier] = self._value
else : raise Exception('invalid declare')
class Compile:
def __init__(self,filepath,mode='r') -> None:
self.filepath = filepath
self.mode = mode
with open(self.filepath,self.mode) as algorithm:
self.contents = algorithm.readlines()
self.contents = list(map(lambda line: line.strip(), self.contents))
self.contents = list(filter(lambda line: len(line)>0 and COMMENT not in line, self.contents))
def bof_to_eof(self):
if START in self.contents and STOP in self.contents:
# self.contents = self.contents[self.contents.index(START)+1:self.contents.index(STOP)]
self.contents.remove(START)
self.contents.remove(STOP)
elif not len(self.contents) > 0 : exit()
else : raise BasicError(self.contents)
def main(self):
self.initterm()
self.bof_to_eof()
self.observe()
def initterm(self):
if len(self.contents) > 0 and not (START == self.contents[0]):
raise BasicError('incorrect way of starting the program')
def observe(self):
if START in self.contents: raise Exception(START)
if STOP in self.contents: raise Exception(STOP)
def while_loop(condition:str,commands:list):
boolean_ =eval(condition)
while boolean_:
for command in commands:
exec(command)
boolean_ = eval(condition)
if __name__ == "__main__":
# print(arguments[1])
# sys.exit()
compilation = Compile(filepath = arguments[1])
compilation.main()
syntax = Syntax(compilation.contents)
# print(syntax.contents)
syntax.translate()
# print(Syntax.modulirise)
# print(syntax.contents)
# exit()
# syntax.while_loop("i != 5 ",9)
for line in syntax.modulirise:
exec(eval(line))
if "Declarations" in syntax.contents:
index_declaration = syntax.contents.index("Declarations")
syntax.contents.remove("Declarations")
line = syntax.contents[index_declaration]
while line.startswith('num') or line.startswith('string'):
del syntax.contents[index_declaration]
if "=" in line:Declare(line).register()
else: Declare(line)
line = syntax.contents[index_declaration]
if sys.argv[-1] == "java" or sys.argv[-1] == "cpp":
# print(syntax.contents,Declare.declared_variables)
arg1 = "**".join(syntax.contents).encode('utf-8').hex()
arg2 = str(Declare.declared_variables)
arg2 = arg2.encode('utf-8').hex()
output=str(subprocess.getoutput(f'%python3% {sys.path[3]}\\\\Transcriber.py \"{arg1}\" {arg2}'))
print(output)
sys.exit()
for line in syntax.contents:
# print(line)
exec(line)