-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvar.py
executable file
·447 lines (313 loc) · 11.2 KB
/
var.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/usr/bin/env python3
# VAR programming language
# Copyright 2018 Marcus Hansson
#
# Usage: [Python 3 path] var.py [file name]
# Example: c:\Python34\python.exe var.py test.var
import sys
import string
import array
VARIABLE = "VAR"
OUTPUT = "OUT"
INPUT = "INP"
INCREMENT = "INC"
DECREMENT = "DEC"
WHILE = "WHL"
END = "END"
INTEGER = "INT"
STRING = "STR"
CONDITION = "CON"
LETTERS = string.ascii_lowercase + string.ascii_uppercase
NUMBERS = "0123456789"
SPACE = " "
NEW_LINE = "\n"
QUOTATION_MARK = '"'
BRACKET_LEFT = "["
BRACKET_RIGHT = "]"
COMMENT = "//"
def convert_from_ascii(value_list):
value = ""
while value_list[len(value_list) - 1] == 0:
del value_list[-1]
if len(value_list) == 0:
break
for number in value_list:
value += chr(number)
return value
def convert_to_ascii(string):
value_list = []
if string == "":
return [0]
for char in string:
value_list.append(ord(char))
return value_list
def print_error(number, arg1="", arg2="", arg3=""):
error_message = "\nERROR: "
if number == 1:
error_message += "No variable named '" + arg1 + "'."
elif number == 2:
error_message += "Cannot " + arg1 + " variable '" + arg2 + "' with '" + arg3 + "'."
elif number == 3:
error_message += "Cannot " + arg1 + " variable '" + arg2 + "' by a string value (unless length is 1)."
elif number == 4:
error_message += "Missing '" + str(arg1) + "'."
elif number == 5:
error_message += "Cannot assign '" + arg1 + "' to '" + arg2 + "' by using indexing. Only single value or character allowed."
elif number == 6:
error_message += "Cannot make variable '" + arg1 + "' to type integer."
elif number == 7:
error_message += "Cannot make variable '" + arg1 + "' to type string."
elif number == 8:
error_message += "Invalid syntax."
print(error_message)
sys.exit()
class Operation:
def __init__(self, code):
self.code = code
self.variables = {}
self.code_pointer = -1
self.char = ""
self.loops = {}
def prepare_loops(self, code):
temp_loop_stack, self.loops = [], {}
command = ""
command_position = True # True if the current character is part of any first word
for position, char in enumerate(code):
if command_position:
if char in ["\n", " "]:
command = command.upper()
command_position = False
if command == WHILE:
temp_loop_stack.append((position, 1))
elif command == CONDITION:
temp_loop_stack.append((position, 0))
elif command == END:
start = temp_loop_stack.pop()
self.loops[start[0]] = position
self.loops[position] = start
command_position = True
command = ""
else:
command += char
else:
if char == "\n":
command_position = True
def update_char(self):
try:
self.char = self.code[self.code_pointer]
except IndexError as e:
return False
return True
def next_char(self):
self.code_pointer += 1
return self.update_char()
def set_char(self, new_index):
self.code_pointer = new_index
return self.update_char()
def create_variable(self):
var_name = self.get_chars_until([SPACE, BRACKET_LEFT])
var_index = None
if self.char == BRACKET_LEFT:
self.next_char()
var_index = self.get_index()
self.next_char()
ascii_list = self.get_value()
self.assign_variable(var_name, var_index, ascii_list)
def assign_variable(self, var_name, var_index, new_ascii_list):
self.next_char()
if var_name not in self.variables:
self.variables[var_name] = [0]
if var_index:
current_ascii_list = self.variables[var_name].copy()
while len(current_ascii_list) < var_index:
current_ascii_list.append(0)
before_index = current_ascii_list[:var_index]
after_index = current_ascii_list[var_index + 1:]
new_ascii_list = before_index + new_ascii_list + after_index
self.variables[var_name] = new_ascii_list
def get_variable_as_ascii_list(self):
var_name = self.get_chars_until([NEW_LINE, BRACKET_LEFT])
if self.char == BRACKET_LEFT:
self.next_char()
var_index = self.get_index()
current_ascii_list = self.get_variable_value(var_name)
while len(current_ascii_list) < var_index + 1:
current_ascii_list.append(0)
ascii_list = [current_ascii_list[var_index]]
else:
ascii_list = self.get_variable_value(var_name)
return ascii_list
def get_string_as_ascii_list(self):
self.next_char()
string = self.get_chars_until([QUOTATION_MARK])
self.next_char()
ascii_list = convert_to_ascii(string)
return ascii_list
def get_number_as_ascii_list(self):
var_value = self.get_chars_until([NEW_LINE, SPACE])
ascii_list = [int(var_value)]
return ascii_list
def create_input_variable(self):
var_name = self.get_chars_until([NEW_LINE])
var_value = input()
ascii_list = convert_to_ascii(var_value)
self.assign_variable(var_name, None, ascii_list)
def get_var_change(self):
if self.char == " ":
self.next_char()
var_value = self.get_value()
else:
var_value = [1]
return var_value
def decrement_value(self):
self.adjust_value("decrement")
def increment_value(self):
self.adjust_value("increment")
def adjust_value(self, adjust_string):
var_name = self.get_chars_until([SPACE, NEW_LINE])
adj_value = self.get_var_change()
if len(adj_value) > 1:
print_error(3, adjust_string, var_name)
try:
adj_value = int(adj_value[0])
except ValueError as e:
print_error(2, adjust_string, var_name, adj_value)
self.next_char()
if var_name in self.variables:
var_value = self.variables[var_name]
else:
print_error(1, var_name)
if len(var_value) > 1:
print_error(3, adjust_string, convert_from_ascii(var_value))
if adjust_string == "increment":
self.variables[var_name][0] = var_value[0] + adj_value
else:
self.variables[var_name][0] = var_value[0] - adj_value
def get_chars_until(self, end_char_list):
chars = ""
while self.char not in end_char_list:
chars += self.char
if not self.next_char():
print_error(4, end_char_list)
return chars
def get_variable_value(self, var_name):
value_list = []
if var_name in self.variables.keys():
value_list = self.variables[var_name].copy()
else:
print_error(1, var_name)
return value_list
def get_index(self):
if self.char in NUMBERS:
inside_brackets = self.get_chars_until([BRACKET_RIGHT])
var_index = int(inside_brackets)
elif self.char.isalpha():
inside_brackets = self.get_chars_until([BRACKET_RIGHT])
inside_brackets_ascii_list = self.get_variable_value(inside_brackets)
var_index = int(inside_brackets_ascii_list[0])
else:
print_error(8)
self.next_char()
return var_index
def get_value(self):
if self.char == QUOTATION_MARK:
ascii_list = self.get_string_as_ascii_list()
elif self.char in NUMBERS:
ascii_list = self.get_number_as_ascii_list()
elif self.char.isalpha():
ascii_list = self.get_variable_as_ascii_list()
else:
print_error(8)
return ascii_list
def print_output(self):
ascii_list = self.get_value()
output = convert_from_ascii(ascii_list)
print(output, end="")
ending_newline_flag = 1
if self.char == " ":
self.next_char()
ending_newline_flag = int(self.get_value()[0])
if ending_newline_flag:
print()
def check_if_true(self):
var_name = self.get_chars_until([NEW_LINE])
new_index = self.code_pointer - len(var_name)
self.set_char(new_index)
ascii_list = self.get_variable_as_ascii_list()
var_value = ascii_list[0]
if var_value == 0:
self.code_pointer = self.loops[self.code_pointer - len(var_name) - 1]
def end_while(self):
block_start_pos, repeat = self.loops[self.code_pointer - 1]
if repeat:
self.set_char(block_start_pos + 1)
self.check_if_true()
def make_integer(self):
var_name = self.get_chars_until([NEW_LINE])
ascii_list = self.variables[var_name].copy()
var_value = convert_from_ascii(ascii_list)
try:
self.variables[var_name] = [int(var_value)]
except ValueError as e:
print(e)
print_error(6, var_name)
def make_string(self):
var_name = self.get_chars_until([NEW_LINE])
ascii_list = self.variables[var_name].copy()
if len(ascii_list) == 1:
self.variables[var_name] = convert_to_ascii(str(ascii_list[0]))
else:
print_error(7, var_name)
def execute(file_name):
with open(file_name) as f:
code = clean_up_code(f.readlines())
compile_code(code)
f.close()
def clean_up_code(code):
clean_code = ""
for line in code:
if COMMENT in line:
line = line[:line.index(COMMENT)]
line = line.strip()
if line != "":
clean_code += line + "\n"
return clean_code
def compile_code(code):
op = Operation(code)
op.prepare_loops(code)
command = ""
while op.code_pointer < len(op.code):
if op.char in ["\n", " "]:
op.next_char()
command = command.upper()
if command == VARIABLE:
op.create_variable()
elif command == OUTPUT:
op.print_output()
elif command == INPUT:
op.create_input_variable()
elif command == INCREMENT:
op.increment_value()
elif command == DECREMENT:
op.decrement_value()
elif command == WHILE:
op.check_if_true()
elif command == END:
op.end_while()
elif command == INTEGER:
op.make_integer()
elif command == STRING:
op.make_string()
elif command == CONDITION:
op.check_if_true()
command = ""
else:
command += op.char
op.next_char()
def main():
if len(sys.argv) == 2:
execute(sys.argv[1])
else:
print("Please pass code file as argument: python3", sys.argv[0], "file_name")
if __name__ == "__main__":
main()