forked from ceu-lang/ceu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcval.lua
99 lines (90 loc) · 2.42 KB
/
cval.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
local _ceu2c = { ['or']='||', ['and']='&&', ['not']='!' }
local function ceu2c (op)
return _ceu2c[op] or op
end
--[[
-- cval: C value
--]]
F =
{
Dcl_var = function (me)
if me.var.pre=='var' then
local is_arr = TP.check(me.var.tp,'[]','-&')
if is_arr and TP.is_ext(me.var.tp,'_') then
local arr = me.var.tp.arr
ASR(type(arr)=='table' and arr.cval,
me, 'invalid array dimension')
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
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
end,
['Op1_~'] = 'Op1_any',
['Op1_-'] = 'Op1_any',
['Op1_+'] = 'Op1_any',
['Op1_not'] = 'Op1_any',
Op1_cast = function (me)
local tp, exp = unpack(me)
if tp.tag=='Type' and exp.cval then
me.cval = '(('..TP.toc(tp)..')'..exp.cval..')'
end
end,
RawExp = function (me)
me.cval = unpack(me)
end,
Nat = function (me)
me.cval = string.sub(me[1], 2)
end,
STRING = function (me)
me.cval = me[1]
end,
NUMBER = function (me)
me.cval = me[1]
end,
NULL = function (me)
me.cval = '((void *)0)'
end,
}
AST.visit(F)