-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtmacro.lua
54 lines (42 loc) · 1.09 KB
/
tmacro.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
local mloadstring = require "luafish.macro" . loadstring
local function mdostring(code)
local f = assert(mloadstring(code, 'tmacro.lua', true))
return f()
end
mdostring [[
ONCOMPILE(function()
function MACRO.SETTYPE(obj_ast, type_ast)
obj_ast.stype = type_ast[1]
end
function MACRO.TYPE(obj_ast)
return 'value', obj_ast.stype
end
function MACRO.TOAST(ast)
return 'value', ast
end
function MACRO.ISNUMBER(ast)
return 'value', M.TNumber.isa(ast.stype)
end
end)
assert(1 + 1 == 2)
local x = 2
SETTYPE(x, 'integer')
local function test(y)
SETTYPE(y, 'integer')
for y=1,10 do
assert(TYPE(y) == nil)
-- NOTE: the below will cause the above assert to fail.
-- that might not be what we want. Should types be mutable?
-- SETTYPE(y, 'number')
end
assert(TYPE(y) == 'integer')
end
assert(TYPE(x) == 'integer')
test(x)
local x = 1
assert(ISNUMBER(1+2+x*x))
local ast = TOAST(1 + 2*x)
assert(ast.tag == 'Op' and ast[1] == '+')
assert(ast[3].tag == 'Op' and ast[3][1] == '*')
]]
print 'done'