-
Notifications
You must be signed in to change notification settings - Fork 39
/
interp_Cif.py
35 lines (31 loc) · 992 Bytes
/
interp_Cif.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
from ast import *
from interp_Lif import InterpLif
from utils import *
class InterpCif(InterpLif):
def interp(self, p):
match p:
case CProgram(blocks):
env = {}
self.blocks = blocks
self.interp_stmts(blocks['start'], env)
def interp_stmts(self, ss, env):
match ss:
case [t]:
return self.interp_tail(t, env)
case [s, *ss]:
self.interp_stmt(s, env, [])
return self.interp_stmts(ss, env)
def interp_tail(self, s, env):
match s:
case Return(value):
return self.interp_exp(value, env)
case Goto(label):
return self.interp_stmts(self.blocks[label], env)
case If(test, [Goto(thn)], [Goto(els)]):
match self.interp_exp(test, env):
case True:
return self.interp_stmts(self.blocks[thn], env)
case False:
return self.interp_stmts(self.blocks[els], env)
case _:
raise Exception('interp_tail: unexpected ' + repr(s))