Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
prob-teaching-notebooks
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
general
stups
prob-teaching-notebooks
Commits
edc22717
Commit
edc22717
authored
Nov 17, 2020
by
Chris
Browse files
Options
Downloads
Patches
Plain Diff
Bugfixes und erste Tests WHILE-Interpreter
parent
98e6eed4
No related branches found
No related tags found
1 merge request
!1
Master
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
info4/kapitel-8/Interpreter/test_while_interpreter.py
+22
-0
22 additions, 0 deletions
info4/kapitel-8/Interpreter/test_while_interpreter.py
info4/kapitel-8/Interpreter/whileinterpreter.py
+11
-5
11 additions, 5 deletions
info4/kapitel-8/Interpreter/whileinterpreter.py
with
33 additions
and
5 deletions
info4/kapitel-8/Interpreter/test_while_interpreter.py
0 → 100644
+
22
−
0
View file @
edc22717
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
"
)
This diff is collapsed.
Click to expand it.
info4/kapitel-8/Interpreter/whileinterpreter.py
+
11
−
5
View file @
edc22717
...
...
@@ -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
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment