Commit f484753c authored by Christopher Happe's avatar Christopher Happe
Browse files

Bugfix: Lokale Verwendung globaler Variable verbessert

parent 42ac3e89
......@@ -38,40 +38,36 @@ class ErrorHandler:
def handle_break(self):
print("BREAK in Zeile " + str(self.line_number))
print("Aktueller Zustand:")
for k,v in values.items():
for k, v in values.items():
print("Variable " + k + ": " + str(v))
user_input = input("Drücke ENTER zum Fotfahren oder schreibe EXIT zum Beenden:")
if user_input.lower() == 'exit':
raise KeyboardInterrupt
def process_assignment(value_list, forbidden_identifiers, identifier_token_1):
def process_assignment(forbidden_identifiers, identifier_token_1):
global values
identifier_1 = identifier_token_1.v
if identifier_1 in forbidden_identifiers:
error_handler.handle_error("Identifier " + identifier_1 +
" ist bereits in Loop vorhanden und darf nicht verwendet werden.")
if next_nonempty_token("Zuweisung", ":=") == 'EQUALS':
error_handler.handle_error(":= in Zuweisung erwartet.")
identifier_token_2 = next_nonempty_token("Zuweisung", "IDENTIFIER (x0, x1, ...) oder NUMBER")
if identifier_token_2.k == 'NUMBER':
value_1 = int(identifier_token_2.v)
value_list.update({identifier_token_1.v: value_1})
return next_token(), value_list
values.update({identifier_token_1.v: value_1})
return next_token()
if not identifier_token_2.k == 'IDENTIFIER':
error_handler.handle_error("IDENTIFIER in Zuweisung erwartet.")
identifier_2 = identifier_token_2.v
if identifier_2 in forbidden_identifiers:
error_handler.handle_error("Identifier " + identifier_2 +
" ist bereits in Loop vorhanden und darf nicht verwendet werden.")
if identifier_2 in value_list:
value_2 = value_list.get(identifier_2)
if identifier_2 in values:
value_2 = values.get(identifier_2)
else:
value_2 = 0
operator_token = next_nonempty_token("Zuweisung", "+ oder -")
op = None
if operator_token.k == 'PLUS':
......@@ -80,15 +76,12 @@ def process_assignment(value_list, forbidden_identifiers, identifier_token_1):
op = operator.__sub__
else:
error_handler.handle_error("+ oder - in Zuweisung erwartet.")
number_token = next_nonempty_token("Zuweisung", "NUMBER")
if not number_token.k == 'NUMBER':
error_handler.handle_error("NUMBER in Zuweisung erwartet.")
value_1 = max(0, op(value_2, int(number_token.v)))
value_list.update({identifier_1: value_1})
return next_token(), value_list
values.update({identifier_1: value_1})
return next_token()
def verify_assignment(forbidden_identifiers, identifier_token_1):
......@@ -118,7 +111,8 @@ def verify_assignment(forbidden_identifiers, identifier_token_1):
return next_token()
def process_loop(value_list, forbidden_identifiers, loop_token):
def process_loop(forbidden_identifiers, loop_token):
global values
identifier_token = next_nonempty_token('LOOP', 'IDENTIFIER (x0, x1, ...)')
if not identifier_token.k == 'IDENTIFIER':
error_handler.handle_error('IDENTIFIER in LOOP erwartet.')
......@@ -128,8 +122,8 @@ def process_loop(value_list, forbidden_identifiers, loop_token):
if not next_nonempty_token("LOOP", "DO").k == 'DO':
error_handler.handle_error('DO in LOOP erwartet.')
if identifier_token.v in value_list:
number_of_loops = int(value_list.get(identifier_token.v))
if identifier_token.v in values:
number_of_loops = int(values.get(identifier_token.v))
else:
number_of_loops = 0
......@@ -139,7 +133,6 @@ def process_loop(value_list, forbidden_identifiers, loop_token):
if number_of_loops == 0:
end_found = False
while not end_found:
token = verify_program(forbidden_identifiers, next_token())
if token is None or token.k not in ['SEMICOLON', 'END']:
......@@ -154,7 +147,7 @@ def process_loop(value_list, forbidden_identifiers, loop_token):
error_handler.line_number = saved_line
end_found = False
while not end_found:
token, value_list = process_program(value_list, forbidden_identifiers, next_token())
token = process_program(forbidden_identifiers, next_token())
if token is None or token.k not in ['SEMICOLON', 'END']:
error_handler.handle_error("SEMICOLON oder END in LOOP erwartet.")
elif token.k == 'SEMICOLON':
......@@ -163,7 +156,7 @@ def process_loop(value_list, forbidden_identifiers, loop_token):
end_found = True
forbidden_identifiers.remove(identifier_token.v)
return next_token(), value_list
return next_token()
def verify_loop(forbidden_identifiers, loop_token):
......@@ -192,16 +185,15 @@ def verify_loop(forbidden_identifiers, loop_token):
return next_token()
def process_program(value_list, forbidden_identifiers, current_token):
values = value_list
def process_program(forbidden_identifiers, current_token):
if current_token is None or current_token.k not in ['IDENTIFIER', 'LOOP']:
error_handler.handle_error("Keine passende Anweisung gefunden\n" +
"Erwartet: IDENTIFIER (x0, x1, ...) oder LOOP")
elif current_token.k == 'IDENTIFIER':
current_token, values = process_assignment(value_list, forbidden_identifiers, current_token)
current_token = process_assignment(forbidden_identifiers, current_token)
elif current_token.k == 'LOOP':
current_token, values = process_loop(value_list, forbidden_identifiers, current_token)
return current_token, values
current_token = process_loop(forbidden_identifiers, current_token)
return current_token
def verify_program(forbidden_identifiers, current_token):
......@@ -247,7 +239,7 @@ def interpret(program):
forbidden_identifiers = []
current_token = next_token()
while current_token is not None:
current_token, values = process_program(values, forbidden_identifiers, current_token)
current_token = process_program(forbidden_identifiers, current_token)
if current_token is not None:
if not current_token.k == 'SEMICOLON':
error_handler.handle_error("Semicolon erwartet")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment