- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtransform.lua
More file actions
Latest commit
244 lines (217 loc) · 8.16 KB
/
Copy pathtransform.lua
File metadata and controls
244 lines (217 loc) · 8.16 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
locallua_ast=require('sci-lang.lua-ast')
localfunctionadd_body(body, ...)
localarg= { ... }
fori=1,#argdo
body[#body+1] =arg[i]
end
end
localfunctionaexpr_index(ast, var, line)
localp_idx=ast:expr_property(var, '_p', line)
returnast:expr_index(p_idx, ast:identifier('__i'), line)
end
localfunctionaexpr_loop1(ast, lhs, rhs)
returnast:for_stmt(
ast:identifier('__i', 1),
ast:literal(0, 1),
ast:expr_binop('-', ast:expr_property(lhs, '_n', 1), ast:literal(1, 1), 1),
nil,
{ ast:assignment_expr({ aexpr_index(ast, lhs, 1) }, { rhs }, 1) },
1, 1)
end
localfunctionaexpr_dim(ast, what, arrays)
returnast:expr_function_call(ast:identifier('__dim_'..what, 1), arrays, 1)
end
localfunctionaexpr_terminal(ast, node, fargs, fvals)
assert(#fargs==#fvals)
localkind, ivar=node.kind, #fargs+1
fargs[ivar] =ast:identifier('__x'..ivar, 1)
ifkind=='IndexAlgebraExpression' then
fvals[ivar] =node.object
elseifkind=='Identifier' orkind=='Literal' then
fvals[ivar] =node
end
returnfargs[ivar]
end
localaexpr_set
localfunctionaexpr_linear_access(ast, node, fbody, fargs, fvals, temps, arrays)
assert(type(temps) =='table')
localkind, operator=node.kind, node.operator
ifkind=='IndexAlgebraExpression' then
localvar=aexpr_terminal(ast, node, fargs, fvals)
arrays[#arrays+1] =var
returnaexpr_index(ast, var, 1)
elseifkind=='Identifier' orkind=='Literal' then
returnaexpr_terminal(ast, node, fargs, fvals)
elseifkind=='UnaryAlgebraExpression' then
returnast:expr_unop(node.operator, aexpr_linear_access(ast, node.argument, fbody, fargs, fvals, temps, arrays), node.line)
elseifkind=='BinaryAlgebraExpression' then
ifoperator=='**' oroperator=='^^' then
localivar=#temps+1
temps[ivar] =ast:identifier('__t'..ivar, 1)
arrays[#arrays+1] =temps[ivar]
aexpr_set(ast, node, temps[ivar], ast:identifier('__stack_array', 1), fbody, fargs, fvals, temps)
returnaexpr_index(ast, temps[ivar], 1)
else
localleft=aexpr_linear_access(ast, node.left, fbody, fargs, fvals, temps, arrays)
localright=aexpr_linear_access(ast, node.right, fbody, fargs, fvals, temps, arrays)
returnast:expr_binop(node.operator, left, right, node.line)
end
end
error('internal: unreachable')
end
localfunctionaexpr_elw_set(ast, node, out, out_kind, fbody, fargs, fvals, temps)
localarrays= { }
localaccess=aexpr_linear_access(ast, node, fbody, fargs, fvals, temps, arrays)
localpre
ifout_kindthen
local__dim=aexpr_dim(ast, 'elw_'..(#arrays), arrays)
pre=ast:local_decl({ out.name }, { ast:expr_function_call(out_kind, { arrays[1], __dim }, 1) }, 1)
else
local__dim=aexpr_dim(ast, 'elw_'..(#arrays+1), { out, unpack(arrays) })
pre=ast:new_statement_expr(__dim, 1)
end
localelw=aexpr_loop1(ast, out, access)
returnpre, elw
end
localfunctionaexpr_singlify(ast, node, fbody, fargs, fvals, temps)
localkind, transpose=node.kind, false
ifkind=='UnaryAlgebraExpression' andnode.operator=='`' then
transpose=true
node=node.argument
kind=node.kind
end
transpose=ast:literal(transpose, 1)
ifkind=='IndexAlgebraExpression' orkind=='Identifier' orkind=='Literal' then
returnaexpr_terminal(ast, node, fargs, fvals), transpose
else
localivar=#temps+1
temps[ivar] =ast:identifier('__t'..ivar, 1)
aexpr_set(ast, node, temps[ivar], ast:identifier('__stack_array', 1), fbody, fargs, fvals, temps)
returntemps[ivar], transpose
end
end
localfunctionaexpr_mul_set(ast, out, out_kind, left, right, left_tr, right_tr)
local__mul=ast:identifier('__mul', 1)
localpre
ifout_kindthen
local__dim=aexpr_dim(ast, 'mul_2', { left, right, left_tr, right_tr })
pre=ast:local_decl({ out.name }, { ast:expr_function_call(out_kind, { left, __dim }, 1) }, 1)
else
local__dim=aexpr_dim(ast, 'mul_3', { out, left, right, left_tr, right_tr })
pre=ast:new_statement_expr(__dim, 1)
end
localmul=ast:new_statement_expr(ast:expr_function_call(__mul, { out, left, right, left_tr, right_tr }, 1), 1)
returnpre, mul
end
localfunctionaexpr_pow_set(ast, out, out_kind, left, right)
local__pow=ast:identifier('__pow', 1)
localpre
ifout_kindthen
local__dim=aexpr_dim(ast, 'pow_1', { left })
pre=ast:local_decl({ out.name }, { ast:expr_function_call(out_kind, { left, __dim }, 1) }, 1)
else
local__dim=aexpr_dim(ast, 'pow_2', { out, left })
pre=ast:new_statement_expr(__dim, 1)
end
localpow=ast:new_statement_expr(ast:expr_function_call(__pow, { out, left, right }, 1), 1)
returnpre, pow
end
aexpr_set=function(ast, node, out, out_kind, fbody, fargs, fvals, temps)
localkind, operator=node.kind, node.operator
ifkind=='BinaryAlgebraExpression' and (operator=='**' oroperator=='^^') then
localleft, left_tr=aexpr_singlify(ast, node.left, fbody, fargs, fvals, temps)
localright, right_tr=aexpr_singlify(ast, node.right, fbody, fargs, fvals, temps)
ifoperator=='**' then
add_body(fbody, aexpr_mul_set(ast, out, out_kind, left, right, left_tr, right_tr))
else
add_body(fbody, aexpr_pow_set(ast, out, out_kind, left, right))
end
else
add_body(fbody, aexpr_elw_set(ast, node, out, out_kind, fbody, fargs, fvals, temps))
end
end
localexpr_count=0
localproto= { firstline=1, lastline=1 }
localfunctionaexpr_clear(ast, temps, fbody)
if#temps>0then
add_body(fbody, ast:new_statement_expr(ast:expr_function_call(ast:identifier('__stack_clear', 1), { }, 1), 1))
end
end
localfunctionaexpr_root(ast, fargs, fvals, set_node, out_kind, return_stmt)
expr_count=expr_count+1
local__r1=ast:identifier('__r1', 1)
localfbody, temps= { }, { }
aexpr_set(ast, set_node, __r1, out_kind, fbody, fargs, fvals, temps)
aexpr_clear(ast, temps, fbody)
add_body(fbody, return_stmt)
fbody.lastline=1
ast.pre[#ast.pre+1] =ast:local_function_decl('__aexpr_'..expr_count, fargs, fbody, proto)
returnast:expr_function_call(ast:identifier('__aexpr_'..expr_count, 1), fvals, 1)
end
localfunctionaexpr_new(ast, node)
local__r1=ast:identifier('__r1', 1)
returnaexpr_root(ast, { }, { }, node, ast:identifier('__array_alloc', 1), ast:return_stmt({ __r1 }, 1))
end
localfunctionaexpr_assign(ast, node)
local__r1=ast:identifier('__r1', 1)
returnaexpr_root(ast, { __r1 }, { node.left[1].object }, node.right[1], nil, nil)
end
localtransform_map= {
IndexAlgebraExpression=aexpr_new,
UnaryAlgebraExpression=aexpr_new,
BinaryAlgebraExpression=aexpr_new,
AssignmentAlgebraExpression=aexpr_assign,
}
localfunctiontransform(ast, node)
iftype(node) =='table' then
localtransform_kind=transform_map[node.kind] -- Fails if not node.
iftransform_kindthen-- To be transformed nodes.
returntransform_kind(ast, node)
else-- Not to be transformed nodes.
localo= { }
fork,vinpairs(node) do
o[k] =transform(ast, v)
end
returno
end
end
returnnode-- Not nodes.
end
localfunctionlocalize(ast, what, from, line)
locallhs, rhs= { }, { }
fori,kinipairs(what) do
lhs[i] ='__'..k
rhs[i] =ast:expr_property(from, k, line)
end
returnast:local_decl(lhs, rhs, line)
end
localfunctionpre_init(ast)
localdim_elw_x= { }
fori=1,10dodim_elw_x[i] ='dim_elw_'..iend
local__alg=ast:identifier('__alg', 1)
return {
ast:local_decl(
{ __alg.name },
{ ast:expr_property(
ast:expr_function_call(ast:identifier('require', 1), { ast:literal('sci.alg', 1) }, 1),
'__',
1), },
1),
localize(ast, { 'mul', 'pow', 'dim_mul_2', 'dim_mul_3', 'dim_pow_1', 'dim_pow_2', 'stack_array', 'stack_clear', 'array_alloc' }, __alg, 1),
localize(ast, dim_elw_x, __alg, 1)
}
end
localfunctionroot(tree)
localtast=lua_ast.New()
tast:fscope_begin()
tast.pre=pre_init(tast)
localvalid_tree=transform(tast, tree)
fori=1,#tast.predo
table.insert(valid_tree.body, i, tast.pre[i])
end
tast:fscope_end()
returnvalid_tree
end
return {
root=root,
}