-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmsinterpreter.py
182 lines (122 loc) · 5.4 KB
/
msinterpreter.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
import sys
from words import *
from commands import *
from ignored_characters import *
class Command:
"""A command.
A command represents the most basic units of the language. Each one can be executed
separately.
Attributes:
name: A string indicating the reserved words for the command.
content: A string indicating extra words that go with the command. Depending on
whether it needs them or not, it can be used or simply ignored. Most of the time,
it is ignored.
"""
def __init__(self, name: str, content: str) -> None:
self.name = name
self.content = content
def __repr__(self) -> str:
return f"({ self.name },{ self.content })"
def file_to_commands(file_code) -> list:
"""Takes the source code of a given program and returns a list of the commands."""
commands = []
while(len(file_code) > 0):
command_name_matched = False
i = 0
while i < len(COMMAND_NAMES) and not command_name_matched:
if file_code[0].strip().startswith(COMMAND_NAMES[i]):
command_name_matched = True
if file_code[0].strip().startswith(LOOP_START):
file_code.pop(0)
commands.append(file_to_commands(file_code))
elif file_code[0].strip().startswith(LOOP_END):
file_code.pop(0)
return commands
else:
command_content = file_code[0].replace(COMMAND_NAMES[i], "")
command_content = remove_ignored_characters(command_content)
command = Command(COMMAND_NAMES[i], command_content)
commands.append(command)
file_code.pop(0)
i += 1
if not command_name_matched:
file_code.pop(0)
return commands
def remove_ignored_characters(command_content: str) -> str:
"""Takes a string and returns the same string, but without including any of the
characters in IGNORED_CHARACTERS."""
modified_content = command_content
for letter in command_content:
if letter in IGNORED_CHARACTERS:
modified_content = modified_content.replace(letter, "")
return modified_content
def execute_commands(commands: list, position: list, clipboard: list, array: list) -> None:
while(len(commands) > 0):
if type(commands[0]) == list:
while array[position[0]] != 0:
loop = []
for command in commands[0]:
loop.append(command)
execute_commands(loop, position, clipboard, array)
elif commands[0].name == ASSIGN_VALUE:
words = commands[0].content.split(" ")
for word in words:
if word in NOUNS:
array[position[0]] += 1
elif word in ADJECTIVES:
array[position[0]] *= 2
elif word == CHANGE_SIGN:
array[position[0]] *= -1
elif commands[0].name == PRINT_POSITION_CHAR:
print(chr(array[position[0]]), end="")
elif commands[0].name == PRINT_POSITION_INT:
print(array[position[0]], end="")
elif commands[0].name == INPUT_TO_POSITION_CHAR:
input_value = input()
if len(input_value) == 1:
array[position[0]] = ord(input_value)
elif commands[0].name == INPUT_TO_POSITION_INT:
input_value = input()
try:
input_value = int(input_value)
except:
print("El valor ingresado no es un int.")
if type(input_value) == int:
array[position[0]] = input_value
elif commands[0].name == COPY:
clipboard[0] = array[position[0]]
elif commands[0].name == PASTE:
array[position[0]] = clipboard[0]
elif commands[0].name == ASSIGN_ZERO or commands[0].name == ASSIGN_ZERO_ALT:
array[position[0]] = 0
elif commands[0].name == MOVE_POINTER_RIGHT:
if len(array) == position[0]+1:
array.append(0)
position[0] += 1
elif commands[0].name == MOVE_POINTER_LEFT:
if not position[0] == 0:
position[0] -= 1
commands.pop(0)
def interpretate(path: str) -> None:
"""Takes a string with the path to the script and interpretates it."""
try:
script = open(path, "r", encoding='utf-8')
file_code = script.read().lower().split(".")
position = [0]
array = [0]
clipboard = [0]
commands = file_to_commands(file_code)
if commands[0].name == PROGRAM_START:
if commands[len(commands) - 1].name == PROGRAM_END:
execute_commands(commands, position, clipboard, array)
else:
print("Error: No se encuentra final del código. ¿Termina acaso con "
f"\"{ PROGRAM_END }\"?")
else:
print("Error: No se encuentra inicio del código. ¿Comienza acaso con "
f"\"{ PROGRAM_START }\"?")
script.close()
except OSError as e:
print(f"No se pudo abrir {path}:\n{e}\n", file=sys.stderr)
if __name__ == "__main__":
interpretate(sys.argv[1])