-
Notifications
You must be signed in to change notification settings - Fork 1
/
cps2.pp.lua
164 lines (149 loc) · 3.93 KB
/
cps2.pp.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
--before section
@section 'define' --after section
@macro {
head= [[define ?1fn ( ??,params ) ?,code end]],
body = [[?fn = function(?params) ?code end]],
sections= {
define=[[local ?fn ]],
module=[[?fn = ?fn ,]]
}
}
@macro {
head='callcc ?,v = ?cfun (?,values) ??,rest endcc',
body ='?cfun(function(...) ?v=...; ?rest end,?values)'
}
@macro {
head='callcc ?,v = ?cfun () ??,rest endcc',
body ='?cfun(function(...) ?v=...; ?rest end)'
}
@macro {
head='callcc ?cfun (?,values) ??,rest endcc',
body ='?cfun(function() ?rest end,?values)'
}
@macro {
head='callcc ?cfun () ??,rest endcc',
body ='?cfun(function() ?rest end)'
}
@macro {
head= [[funcc ( ?,params ) ??,statements end]],
body =
[=[function(C,?params)
@apply({{head='retc(??,values)',body='return C(?values)'},
{head='while',body='WHILE'},
{head='break',body='BREAK'},
{head='continue',body='continue'},
{head='call ?,v = ?cfun (?,values) ??,rest @end',
body ='?cfun(function(...) ?v=...; ?rest end,?values)' },
{head='call ?,v = ?cfun () ??,rest @end',
body ='?cfun(function(...) ?v=...; ?rest end)' },
{head='call ?cfun (?,values) ??,rest @end',
body ='?cfun(function() ?rest end,?values)' },
{head='call ?cfun () ??,rest @end',
body ='?cfun(function() ?rest end)' },
{head ='store_and_ret_cc(?dest) ??,rest @end',
body=[[
local %temp = funcc() ?rest end
?dest = %temp
retc ()]]
},
{head ='store_and_ret_cc(?dest, ??,values) ??,rest @end',
body=[[
local %temp = funcc() ?rest end
?dest = %temp
retc (?values)]]
},
},
?statements)
end]=],
}
@macro {
head= [[funcc () ??,statements end]],
body =
[=[function(C)
@apply({{head='retc(??,values)',body='return C(?values)'},
{head='while',body='WHILE'},
{head='break',body='BREAK'},
{head='continue',body='continue'},
{head='call ?,v = ?cfun (?,values) ??,rest @end',
body ='?cfun(function(...) ?v=...; ?rest end,?values)' },
{head='call ?,v = ?cfun () ??,rest @end',
body ='?cfun(function(...) ?v=...; ?rest end)' },
{head='call ?cfun (?,values) ??,rest @end',
body ='?cfun(function() ?rest end,?values)' },
{head='call ?cfun () ??,rest @end',
body ='?cfun(function() ?rest end)' },
{head ='store_and_ret_cc(?dest) ??,rest @end',
body=[[
local %temp = funcc() ?rest end
?dest = %temp
retc ()]]
},
{head ='store_and_ret_cc(?dest, ?,values) ??,rest @end',
body=[[
local %temp = funcc() ?rest end
?dest = %temp
retc (?values)]]
},
},
?statements)
end]=],
}
@macro {
head= [[defc ?1fn ( ??,params ) ??,statements end]],
body = [[?fn = funcc(?params) ?statements end]],
sections= {
define=[[local ?fn ]],
module=[[?fn = ?fn ,]]
}
}
count3 = funcc(store)
local n=1
store_and_ret_cc(store[1],n)
n=2
store_and_ret_cc(store[1],n)
n=3
retc(n)
end
local cont={}
local m
callcc m=count3(cont)
print(m)
callcc m=cont[1](cont)
print(m)
callcc m=cont[1](cont)
print(m)
endcc
endcc
endcc
funcc_count3 = funcc(cont)
local m
call m=count3(cont)
print(m)
call m=cont[1](cont)
print(m)
call m=cont[1](cont)
print(m)
retc()
end
callcc funcc_count3(cont)
endcc
--oops, no continuation allowed in the test!
@macro {
head='WHILE ?exp DO ??,statements END',
body=[[
local function %loop()
if ?exp then
@apply({{head='BREAK',body= "__*done*__"},
{head='CONTINUE',body='@ %loop()'},}, ?statements)
return %loop()
end
return '__*done*__'
end
local %save=table.pack(%loop())
if %save[1]~='__*done*__' then retc(table.unpack(%save,%save.n)) end
]]
}
local n = {
@section 'module'
}
return n