-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathST.py
72 lines (55 loc) · 1.5 KB
/
ST.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
Pascal0 Symbol Table, Emil Sekerinski, March 2016.
The symbol table is a list of scopes, each scope is a list of entries.
Symbol table entries are Var, Ref, Const, Type, Proc, StdProc;
all have a field tp for the type. Types are Int, Bool, Record, Array.
"""
from SC import mark
class Var:
def __init__(self, tp):
self.tp = tp
class Ref:
def __init__(self, tp):
self.tp = tp
class Const:
def __init__(self, tp, val):
self.tp, self.val = tp, val
class Type:
def __init__(self, tp):
self.tp = tp
class Proc:
def __init__(self, par):
self.tp, self.par = None, par
class StdProc:
def __init__(self, par):
self.tp, self.par = None, par
class Int: pass
class Bool: pass
class Record:
def __init__(self, fields):
self.fields = fields
class Array:
def __init__(self, base, lower, length):
self.base, self.lower, self.length = base, lower, length
def init():
global symTab
symTab = [[]]
def newObj(name, entry):
top, entry.lev, entry.name = symTab[0], len(symTab) - 1, name
for e in top:
if e.name == name:
mark("multiple definition", 400); return
top.append(entry)
def find(name):
for l in symTab:
for e in l:
if name == e.name: return e
mark('undefined identifier ' + name, 401)
undef = Var(Int); undef.lev, undef.adr = 0, 0
return undef
def openScope():
symTab.insert(0, [])
def topScope():
return symTab[0]
def closeScope():
symTab.pop(0)