forked from ceu-lang/ceu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsval.lua
More file actions
122 lines (111 loc) · 3.11 KB
/
Copy pathsval.lua
File metadata and controls
122 lines (111 loc) · 3.11 KB
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
115
116
117
118
119
120
121
122
local _ceu2c = { ['or']='||', ['and']='&&', ['not']='!' }
local function ceu2c (op)
return _ceu2c[op] or op
end
--[[
-- Fills nodes with "sval" and "cval".
-- sval: static value
-- cval: C value
--]]
F =
{
Dcl_var = function (me)
if me.var.cls then
if me.var.arr then
ASR(me.var.arr.sval, me, 'invalid static expression')
end
end
end,
Op2_call = function (me)
local _, f, ins = unpack(me)
if not f.cval then
return
end
local ps = {}
for i, exp in ipairs(ins) do
if not exp.cval then
return
end
ps[i] = exp.cval
end
me.cval = f.cval..'('..table.concat(ps,',')..')'
end,
Op2_any = function (me)
local op, e1, e2 = unpack(me)
if e1.cval and e2.cval then
me.cval = '('..e1.cval..ceu2c(op)..e2.cval..')'
end
if e1.sval and e2.sval then
local v = loadstring('return '..e1.sval..op..e2.sval)
me.sval = v and tonumber(v())
end
end,
['Op2_-'] = 'Op2_any',
['Op2_+'] = 'Op2_any',
['Op2_%'] = 'Op2_any',
['Op2_*'] = 'Op2_any',
['Op2_/'] = 'Op2_any',
['Op2_|'] = 'Op2_any',
['Op2_&'] = 'Op2_any',
['Op2_<<'] = 'Op2_any',
['Op2_>>'] = 'Op2_any',
['Op2_^'] = 'Op2_any',
['Op2_=='] = 'Op2_any',
['Op2_!='] = 'Op2_any',
['Op2_>='] = 'Op2_any',
['Op2_<='] = 'Op2_any',
['Op2_>'] = 'Op2_any',
['Op2_<'] = 'Op2_any',
['Op2_or'] = 'Op2_any',
['Op2_and'] = 'Op2_any',
Op1_any = function (me)
local op, e1 = unpack(me)
if e1.cval then
me.cval = '('..ceu2c(op)..e1.cval..')'
end
if e1.sval then
local v = loadstring(op..e1.sval)
me.sval = v and tonumber(v())
end
end,
['Op1_~'] = 'Op1_any',
['Op1_-'] = 'Op1_any',
['Op1_not'] = 'Op1_any',
Op1_cast = function (me)
local tp, exp = unpack(me)
if exp.cval then
me.cval = '(('.._TP.c(tp)..')'..exp.cval..')'
end
end,
RawExp = function (me)
me.cval = unpack(me)
end,
Nat = function (me)
me.cval = string.sub(me[1], 2)
end,
SIZEOF = function (me)
local tp = unpack(me)
if type(tp) == 'string' then
me.cval = 'sizeof('.._TP.c(me[1])..')'
end
if type(tp) == 'string' then -- sizeof(type) vs sizeof(exp)
local t = (_TP.deref(tp) and _ENV.c.pointer) or _ENV.c[tp]
ASR(t and (t.tag=='type' or t.tag=='unk'), me,
'undeclared type '..tp)
t.tag = 'type'
me.sval = t and t.len
end
end,
STRING = function (me)
me.cval = me[1]
end,
NUMBER = function (me)
me.cval = me[1]
me.sval = tonumber(me[1])
end,
NULL = function (me)
me.cval = '((void *)0)'
me.sval = '((void *)0)'
end,
}
_AST.visit(F)