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

Bugfixes und erste Tests WHILE-Interpreter

parent 98e6eed4
from whileinterpreter import interpret
from test_loop_interpreter import LOOPInterpreterTest
import unittest
class WHILEInterpreterTest(LOOPInterpreterTest):
def test_while_forbidden_identifier(self):
with self.assertRaises(SyntaxError):
interpret("LOOP x1 DO WHILE x1 != 0 DO x0:=x0+1 END END ")
def test_while(self):
self.assertEqual(interpret('x1:=1; WHILE x1 != 0 DO x0:=2; x1:=x1-1 END'), 2)
self.assertEqual(interpret('WHILE x1 /= 0 DO x0:=1 END'), 0)
self.assertEqual(interpret('WHILE x1 /= 0 DO WHILE x1 /= 0 DO x0:=2 END END'), 0)
def test_while_non_zero(self):
with self.assertRaises(SyntaxError):
interpret("x1:= 1; WHILE x1 /= 1 DO x0:=2 END")
with self.assertRaises(SyntaxError):
interpret("x1:= 2; WHILE x1 /= 1 DO x0:=2 END")
with self.assertRaises(SyntaxError):
interpret("x1:= 1; WHILE x1 /= 0 DO WHILE x1 /= 1 DO x0:=2 END END")
......@@ -9,8 +9,8 @@ class WHILEInterpreter(LOOPInterpreter):
(re.compile(r'x\d+'), 'IDENTIFIER'),
(re.compile(r'\+'), 'PLUS'),
(re.compile(r'-'), 'MINUS'),
(re.compile(r':=|≔|!='), 'EQUALS'),
(re.compile(r'/=|≠'), 'NOTEQUALS'),
(re.compile(r':=|≔'), 'EQUALS'),
(re.compile(r'/=|≠|!='), 'NOTEQUALS'),
(re.compile(r'LOOP'), 'LOOP'),
(re.compile(r'WHILE'), 'WHILE'),
(re.compile(r'DO'), 'DO'),
......@@ -22,7 +22,7 @@ class WHILEInterpreter(LOOPInterpreter):
(re.compile(r'[^\n]*'), 'UNKNOWN')]
def process_program(self, forbidden_identifiers, current_token):
if current_token is None or current_token.k not in ['IDENTIFIER', 'LOOP']:
if current_token is None or current_token.k not in ['IDENTIFIER', 'LOOP', 'WHILE']:
self.error_handler.handle_error("Keine passende Anweisung gefunden\n" +
"Erwartet: IDENTIFIER (x0, x1, ...) oder LOOP")
elif current_token.k == 'IDENTIFIER':
......@@ -34,7 +34,7 @@ class WHILEInterpreter(LOOPInterpreter):
return current_token
def verify_program(self, forbidden_identifiers, current_token):
if current_token is None or current_token.k not in ['IDENTIFIER', 'LOOP']:
if current_token is None or current_token.k not in ['IDENTIFIER', 'LOOP', 'WHILE']:
self.error_handler.handle_error("Keine passende Anweisung gefunden\n" +
"Erwartet: IDENTIFIER (x0, x1, ...) oder LOOP")
elif current_token.k == 'IDENTIFIER':
......@@ -61,6 +61,9 @@ class WHILEInterpreter(LOOPInterpreter):
if not int(zero_token.v) == 0:
self.error_handler.handle_error('0 in WHILE erwartet.')
if not self.next_nonempty_token("WHILE", "DO").k == 'DO':
self.error_handler.handle_error('DO in WHILE erwartet.')
if identifier_token.v in self.values:
while_value = int(self.values.get(identifier_token.v))
else:
......@@ -115,6 +118,9 @@ class WHILEInterpreter(LOOPInterpreter):
if not int(zero_token.v) == 0:
self.error_handler.handle_error('0 in WHILE erwartet.')
if not self.next_nonempty_token("WHILE", "DO").k == 'DO':
self.error_handler.handle_error('DO in WHILE erwartet.')
end_found = False
while not end_found:
token = self.verify_program(forbidden_identifiers, self.next_token())
......@@ -129,4 +135,4 @@ class WHILEInterpreter(LOOPInterpreter):
def interpret(program):
interpreter = WHILEInterpreter()
interpreter.interpret(program)
\ No newline at end of file
return interpreter.interpret(program)
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