-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
186 lines (160 loc) · 4.92 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
178
179
180
181
182
183
184
185
186
from colorama import Fore, Style
import sys
if len(sys.argv) == 0:
fname = input("LOAD: ")
else:
fname = sys.argv[1]
source = open(fname, 'r')
source_raw = source.read().rstrip()
source.close()
source = source_raw.split('\n')
try:
source.remove('')
except:
source = source
i = []
def reg(var):
if var.startswith('$'):
if var not in regs:
throw("RegisterError", "Register {} does not exist or has not been initialized in the current context! {}".format(var.replace('$','',1),p+1))
else:
return (regs[var])
elif var.startswith("\""):
Str = ''
for item in i:
Str = Str + ' ' + item
Str = Str.split("\"")
Str.remove(Str[0])
String = Str[0].replace('_',' ')
return String
pass
elif var.startswith('.'):
return source.index(str(var))
else:
return int(var)
def throw(error, msg=''):
print("\n\033[1m{}{}{} :: {} @{}! {}{}\n".format(Fore.GREEN,p+1,Fore.RESET,Fore.RED,error,msg,Style.RESET_ALL))
exit()
def warn(warning, msg=''):
print("\n\033[1m{}{} {}:: {}{}! {}{}\n".format(Fore.GREEN,p+1,Fore.RESET,Fore.LIGHTYELLOW_EX,warning,msg,Style.RESET_ALL))
def confirm(foo, bar, ret=False, error='HaltError', msg='Unsure if arguments are the same!'):
if not foo == bar:
if not ret:
throw(error, msg)
else:
return 0
else:
if ret:
return 1
def math(a, b, o):
a = int(a)
b = int(b)
if o == '+':
return a + b
elif o == '-':
return a - b
elif o == '*':
return a * b
elif 0 == '/':
return a / b
regs = {}
p = 0
left_point = None
while p < len(source):
item = source[p]
i = item.split(' ')
l = len(i) - 1
if i[0].startswith('//'):
p += 1
continue
for item in i:
i[i.index(item)] = item.rstrip()
if i[0] == "@include":
f = open(reg(i[1]), 'r')
data = f.read().rstrip()
f.close()
header = data.split('\n')
for item in header:
header[header.index(item)] = item.rstrip()
source = header + source
p+=len(header)+1
continue
if i[0] == 'mov':
if i[1].startswith("\""):
Str = ''
for item in i:
Str = Str + ' ' + item
Str = Str.split("\"")
Str.remove(Str[0])
regs[Str[1].replace(' ','',1)] = Str[0].replace('_',' ')
else:
regs[i[2]] = int(reg(i[1]))
elif i[0] == 'add':
regs[i[3]] = int(reg(i[1])) + int(reg(i[2]))
elif i[0] == 'out':
print(reg(i[1]))
elif i[0] == 'sub':
confirm(l, 3)
regs[i[3]] = int(reg(i[1])) - int(reg(i[2]))
elif i[0] == 'times':
regs[i[3]] = math(reg(i[1]), reg(i[2]), '*')
elif i[0] == 'div':
regs[i[3]] = math(reg[i[1]], reg(i[2]), '/')
elif i[0] == 'goto':
if not p == reg(i[1])-1:
p = reg(i[1]) - 1
else:
throw('LoopWarning','Goto results in an endless loop!')
continue
elif i[0] == 'if':
i1 = i
try:
i1.remove('is')
i1.remove('goto')
except:
None
if reg(i1[1]) == reg(i1[2]):
p = source.index(reg(i1[3]))
continue
elif i[0] == 'join':
regs[i[3]] = reg(i[1]) + reg(i[2])
elif i[0] == 'in':
regs[i[2]] = input(reg(i[1]))
elif i[0] == 'end':
break
elif i[0] == 'int':
try:
regs[i[1]] = int(regs[i[1]])
except ValueError:
throw("ConversionError", 'Invalid conversion to int!')
elif i[0] == 'str':
regs[i[0]] = str(regs[i[1]])
elif i[0] == 'type':
if type(reg(i[1])) is int:
regs[i[2]] = 1
elif type(reg(i[1])) is str:
regs[i[2]] = 2
elif i[0] == 'dump':
print("CORE MEMORY:\n",regs)
warn('Stop_check', 'An application requsted a core dump')
elif i[0] == 'call':
left_point = p
print(left_point)
p=reg(i[1])
continue
elif i[0] == 'ret':
try:
type(left_point)
except:
throw('CallError','You have\'nt gone anywhere, so where can you?')
p= left_point
elif i[0] == 'inc':
confirm(type(reg(i[1])),int, error='MathError', msg="Cannot increment type <String>")
confirm(i[1].startswith('$'), True, error='RegisterError', msg='Attempting to write to a read-only register. Register=({})'.format(i[1]))
regs[i[1]] = reg(i[1])+1
elif i[0] == 'dec':
confirm(type(reg(i[1])),int, error='MathError', msg="Cannot decrement type <String>")
confirm(i[1].startswith('$'), True, error='RegisterError', msg='Attempting to write to a read-only register. Register=({})'.format(i[1]))
regs[i[1]] = reg(i[1])+1
#At this point, all that is left to do is increase the code pointer.
p += 1