Skip to content

Commit 76f3696

Browse files
committed
interp and type check for Lwhile
1 parent 4b7a6c3 commit 76f3696

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

interp_Pwhile.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from ast import *
2+
from interp_Pif import InterpPif
3+
from utils import *
4+
5+
class InterpPwhile(InterpPif):
6+
7+
def interp_stmts(self, ss, env):
8+
if len(ss) == 0:
9+
return
10+
match ss[0]:
11+
case While(test, body, []):
12+
while self.interp_exp(test, env):
13+
self.interp_stmts(body, env)
14+
return self.interp_stmts(ss[1:], env)
15+
case _:
16+
return super().interp_stmts(ss, env)
17+

type_check_Pwhile.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from ast import *
2+
from type_check_Pvar import check_type_equal
3+
from type_check_Pif import TypeCheckPif
4+
from utils import *
5+
6+
class TypeCheckPwhile(TypeCheckPif):
7+
8+
def type_check_stmts(self, ss, env):
9+
if len(ss) == 0:
10+
return
11+
match ss[0]:
12+
case While(test, body, []):
13+
test_t = self.type_check_exp(test, env)
14+
check_type_equal(bool, test_t, test)
15+
body_t = self.type_check_stmts(body, env)
16+
return self.type_check_stmts(ss[1:], env)
17+
case _:
18+
return super().type_check_stmts(ss, env)
19+

0 commit comments

Comments
 (0)