-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.lua
114 lines (100 loc) · 2.35 KB
/
env.lua
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
_ENV = {
n_vars = '0',
}
function alloc (var)
local v = _ENV.n_vars
_ENV.n_vars = _ENV.n_vars..'+'..var.size
return '('..v..')'
end
function _ENV.reg (var)
if var.arr then
return '(('..var.tp..')(VARS+'..var.reg..'))'
else
return '(*(('..var.tp..'*)(VARS+'..var.reg..')))'
end
end
function newvar (var)
local blk = _ITER'Block'()
ASR(not blk.vars[var.id], var,
'variable "'..var.id..'" already declared')
blk.vars[var.id] = var
var.blk = blk
if var.arr then
ASR(var.dim>0, var,'invalid array dimension')
var.tp = var.tp..'*'
end
if var.tp == 'void' then
var.size = '0'
else
var.size = '(sizeof('..var.tp..')*'..var.dim..')'
end
var.reg = alloc(var)
var.val = _ENV.reg(var) -- TODO: arrays?
return var
end
function getvar (id)
for stmt in _ITER'Block' do
local var = stmt.vars[id]
if var then
return var
end
end
end
function newevt (evt)
local blk = evt.dir=='internal' and _ITER'Block'()
or _ITER('Block',true)()
ASR(not blk.evts[evt.id], evt,
'event "'..evt.id..'" already declared')
blk.evts[evt.id] = evt
evt.blk = blk
return evt
end
function getevt (id)
for stmt in _ITER'Block' do
local evt = stmt.evts[id]
if evt then
return evt
end
end
end
F = {
Block_pre = function (me)
me.vars = {}
me.evts = {}
end,
Dcl_int = function (me)
local tp, dim, id, exp = unpack(me)
local var = newvar {
ln = me.ln,
id = id,
tp = tp,
arr = dim and tp,
dim = dim or 1,
}
me.var = var
if not var.arr then
var.dir = 'internal'
me.evt = newevt(var)
me.evt.var = var
end
end,
Dcl_ext = function (me)
local tp, id = unpack(me)
me.evt = newevt {
ln = me.ln,
id = id,
tp = tp,
dir = 'input',
}
end,
Var = function (me)
me.var = ASR(getvar(me[1]),
me, 'variable "'..me[1]..'" is not declared')
end,
Evt = function (me)
local id = unpack(me)
me.evt = ASR(getevt(id),
me, 'event "'..id..'" is not declared')
end,
}
_VISIT(F)