-
Notifications
You must be signed in to change notification settings - Fork 0
/
SIMPL.py
77 lines (66 loc) · 2.75 KB
/
SIMPL.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
# INCLUDE <NAME>
# LOGIC <OUTPUT> <INPUT1> <INPUT2?> <LENGTH> <STEP>
# AND, OR, XOR, NOT
# DEF <TO> <FROM>
# GOTO <NAME>
# LBL <NAME>
outputData = ""
def convert(line):
global outputData
output = " "
elements = line.split(' ')
match elements[0]:
# GATES
case "AND":
if len(elements) == 5:
output += f"for (size_t i = 0; i < { elements[4] }; i++) "
output += f"mem[{ int(elements[1]) } + i] = mem[{ int(elements[2]) } + i] & mem[{ int(elements[3]) } + i];"
else:
output += f"mem[{ int(elements[1]) }] = mem[{ int(elements[2]) }] & mem[{ int(elements[3]) }];"
case "OR":
if len(elements) == 5:
output += f"for (size_t i = 0; i < { elements[4] }; i++) "
output += f"mem[{ int(elements[1]) } + i] = mem[{ int(elements[2]) } + i] | mem[{ int(elements[3]) } + i];"
else:
output += f"mem[{ int(elements[1]) }] = mem[{ int(elements[2]) }] | mem[{ int(elements[3]) }];"
case "XOR":
if len(elements) == 5:
output += f"for (size_t i = 0; i < { elements[4] }; i++) "
output += f"mem[{ int(elements[1]) } + i] = mem[{ int(elements[2]) } + i] ^ mem[{ int(elements[3]) } + i];"
else:
output += f"mem[{ int(elements[1]) }] = mem[{ int(elements[2]) }] ^ mem[{ int(elements[3]) }];"
case "NOT":
if len(elements) == 4:
output += f"for (size_t i = 0; i < { elements[3] }; i++) "
output += f"mem[{ int(elements[1]) } + i] = !mem[{ int(elements[2]) } + i];"
else:
output += f"mem[{ int(elements[1]) }] = !mem[{ int(elements[2]) }];"
# DEFINE
case "DEF":
output += f"#define { elements[1] } { elements[2] }"
# GOTO & LABEL
case "GOTO":
output += f"goto { elements[1] };"
case "LBL":
output += f"{ elements[1] }:"
# INCLUDE
case "INCLUDE":
includeFile = open(elements[1], "r", encoding="utf-8")
include = includeFile.readlines()
includeFile.close()
for line in include:
convert(line)
outputData += output + '\n'
codeFile = open(input("Enter the SIMPL file location: "), "r", encoding="utf-8")
code = codeFile.readlines()
codeFile.close()
outputData += "#include <bitset>\n"
memLen = int(code[0][0 : len(code)])
outputData += f"std::bitset<{memLen}> mem = std::bitset<{memLen}>();\n"
outputData += "int main() {\n"
for i in range(1, len(code)):
convert(code[i])
outputData += "}\n"
writeFile = open(input("Enter the name of the cpp file you'd wish to write to: "), 'w')
writeFile.write(outputData)
writeFile.close()