-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterraform.t
497 lines (466 loc) · 14.8 KB
/
terraform.t
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
-- SPDX-FileCopyrightText: 2024 René Hiemstra <[email protected]>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <[email protected]>
--
-- SPDX-License-Identifier: MIT
local base = require("base")
local template = require("template")
local concepts = {}
concepts.impl = require("concept-impl")
concepts.para = require("concept-parametrized")
local generate_terrafun, parse_terraform_statement, process_free_function_statement, process_class_method_statement
local namespace, process_method_name, process_where_clause, process_template_parameters, get_template_parameter_list, process_namespace_indexing
local isclasstemplate, isnamespacedfunctiontemplate, isfreefunctiontemplate, isstaticmethod, isvarargstemplate
local conceptlang = {
name = "conceptlang";
entrypoints = {"terraform", "concept"};
keywords = {"where"};
statement =
function(self,lex)
if lex:matches("terraform") then
local templ = parse_terraform_statement(self,lex)
if isclasstemplate(templ) then
return process_class_method_statement(templ)
else
if isnamespacedfunctiontemplate(templ) then
return process_namespaced_function_statement(templ)
elseif isfreefunctiontemplate(templ) then
return process_free_function_statement(templ), { templ.path } -- create the statement: path = ctor(envfun)
end
end
elseif lex:matches("concept") then
local templ = parse_concept_statement(self,lex)
return process_concept_statement(templ), { templ.path }
end
end;
localstatement =
function(self,lex)
if lex:matches("terraform") then
local templ = parse_terraform_statement(self,lex)
if isfreefunctiontemplate(templ) then
return process_free_function_statement(templ), { templ.path } -- create the statement: path = ctor(envfun)
end
elseif lex:matches("concept") then
local templ = parse_concept_statement(self,lex)
return process_concept_statement(templ), { templ.path }
end
end;
}
function table.shallow_copy(t)
local t2 = {}
for k,v in pairs(t) do
t2[k] = v
end
return t2
end
function parse_terraform_statement(self,lex)
local templ = {}
lex:expect("terraform")
--process method path / class path
templ.path = process_namespace_indexing(lex)
templ.classname, templ.methodname = process_method_name(lex, templ.path)
--process templatefunction parameters
templ.params = process_template_parameters(lex, templ.classname)
--process template parameter constraints
templ.constraints = process_where_clause(lex)
--process terra-block
templ.terrastmts = lex:terrastats()
--end of terra-block
lex:expect("end")
return templ
end
function parse_concept_statement(self,lex)
local templ = {}
lex:expect("concept")
--process method path / class path
templ.path = process_namespace_indexing(lex)
templ.classname, templ.methodname = process_method_name(lex, templ.path)
--process template function parameters
templ.params = process_concept_template_parameters(lex)
--if a parametric concept then process constraints
if templ.params then
--process template parameter constraints
templ.constraints = process_where_clause(lex)
end
--process terra-block
templ.luastmts = lex:luastats()
--end of terra-block
lex:expect("end")
return templ
end
--dereference n times
local function dref(t, n)
for i = 1, n do
t = t.type
end
return t
end
function generate_terrafun(templ, localenv)
return function(...)
local types = terralib.newlist{...}
local argumentlist = terralib.newlist{}
for counter,param in ipairs(templ.params) do
local argtype
if param.typename=="__varargs__" then
local varargs = terralib.newlist{}
for k=counter,#types do
varargs:insert(types[k])
end
argtype = tuple(unpack(varargs))
else
argtype = types[counter]
end
local sym = symbol(argtype)
argumentlist:insert(sym)
--add variable to the local environment
localenv[param.name] = sym
--add parametric type parameter to the local environment
if templ.constraints[param.typename] then
--we are dealing with a parametric type
localenv[param.typename] = dref(argtype, param.nref)
end
end
return terra([argumentlist])
[templ.terrastmts(localenv)]
end
end
end
function generate_luafun(templ, localenv)
return function(Self, ...)
localenv["Self"] = Self
local args = terralib.newlist{...}
if templ.params then
for counter,param in ipairs(templ.params) do
--add variable to the local environment
localenv[param.typename] = args[counter]
end
end
templ.luastmts(localenv)
end
end
function process_free_function_statement(templ)
return function(envfun)
--initialize environment and allow easy searching in 'env'
--of variables that are nested inside tables
local localenv = namespace.new(envfun())
--get parameter-types list
local paramconceptlist = get_template_parameter_list(localenv, templ.params, templ.constraints)
--get/register new template function
local templfun = localenv[templ.path] or template.functiontemplate(templ.methodname)
--add current template method implementation
templfun:adddefinition({[paramconceptlist] = generate_terrafun(templ,localenv)})
return templfun
end
end
function process_namespaced_function_statement(templ)
return function(envfun)
--initialize environment and allow easy searching in 'env'
--of variables that are nested inside tables
local localenv = namespace.new(envfun())
--get parameter-types list
local paramconceptlist = get_template_parameter_list(localenv, templ.params, templ.constraints)
--get/register new template function
local templfun
if isstaticmethod(templ,localenv) then
--case of a static method
local class = localenv(templ.path,1)
if not class["staticmethods"] then base.AbstractBase(class) end --add base functionality
templfun = class["staticmethods"][templ.methodname] or template.functiontemplate(templ.methodname)
else
templfun = localenv[templ.path] or template.functiontemplate(templ.methodname)
end
--add current template method implementation
templfun:adddefinition({[paramconceptlist] = generate_terrafun(templ,localenv)})
--register method
if isstaticmethod(templ,localenv) then
local class = localenv(templ.path,1)
class["staticmethods"][templ.methodname] = templfun
else
localenv[templ.path] = templfun
end
end
end
function process_class_method_statement(templ)
return function(envfun)
--initialize environment and allow easy searching in 'env'
--of variables that are nested inside tables
local localenv = namespace.new(envfun())
--get parameter-types list
local paramconceptlist = get_template_parameter_list(localenv, templ.params, templ.constraints)
--get/register new template function
local class = localenv[templ.path]
if not class["templates"] then base.AbstractBase(class) end --add base functionality
local templfun = class["templates"][templ.methodname] or template.Template:new(templ.methodname)
--add current template method implementation
templfun:adddefinition({[paramconceptlist] = generate_terrafun(templ,localenv)})
--register class method
class["templates"][templ.methodname] = templfun
end
end
function process_concept_statement(templ)
return function(envfun)
--initialize environment and allow easy searching in 'env'
--of variables that are nested inside tables
local localenv = namespace.new(envfun())
--if a parametric concept then
if templ.params then
--get parameter-types list
local paramconceptlist = get_template_parameter_list(localenv, templ.params, templ.constraints)
--get/register new template function
local templfun = localenv[templ.path] or concepts.para.parametrizedconcept(templ.methodname)
--add current template method implementation
templfun:adddefinition({[paramconceptlist] = generate_luafun(templ, localenv)})
--return parameterized concept
return templfun
--if a true concept then
else
local newconcept = concepts.impl.newconcept(templ.methodname)
local imp = generate_luafun(templ, localenv)
--add implementation to `newconcept`
imp(newconcept)
return newconcept
end
end
end
--easy set/get access of namespaces of 'n' levels depth
namespace = {
__index = function(t, path)
if type(path)=="table" then
local n = #path
local v = t.env
for k=1,n do
v = v[path[k]]
end
return v
else
return t.env[path]
end
end,
__newindex = function(t, path, value)
if type(path)=="table" then
local n = #path
local v = t.env
for k=1,n-1 do
v = v[path[k]]
end
v[path[n]] = value
else
t.env[path] = value
end
end,
__call = function(t, path, i)
local i = i or 0
if type(path)=="table" then
local n = #path-i
local v = t.env
for k=1,n do
v = v[path[k]]
end
return v
else
return t.env[path]
end
end,
}
namespace.new = function(env)
env["Any"] = concepts.impl.Any
env["Value"] = concepts.impl.ParametrizedValue
env["Vararg"] = concepts.impl.Vararg
local t = {env=env}
return setmetatable(t, namespace)
end
namespace.isa = function(t)
if type(t) == "table" and getmetatable(t) == namespace then
return true
else
return false
end
end
local function dereference(v)
if v:ispointer() then
return dereference(v.type)
end
return v
end
function get_template_parameter_list(localenv, params, constraints)
local uniqueparams, pos, ref = terralib.newlist(), terralib.newlist(), terralib.newlist()
local counter = 1
local ctrs = table.shallow_copy(constraints)
for i,param in ipairs(params) do
local c = ctrs[param.typename]
local tp
if c then --c is either a (parameterized) concepts or has been mutated to an integer that points to
--a concepts already treated in uniqueparams
if type(c)=="number" then
--already treated in uniqueparams, so insert number 'c' and do not
--change uniqueparams
pos:insert(c)
ref:insert(param.nref)
else
--get concepts type
tp = localenv[c.path] or error("Concept " .. tostring(c.name) .. " not found in current scope.")
--evaluate in case of a parametric concepts
if concepts.para.isparametrizedconcept(tp) or type(tp) == "function" then
local args = terralib.newlist()
for i,v in ipairs(c.fargs) do
if type(v) == "table" then
args:insert(localenv[v])
else
args:insert(v)
end
end
tp = tp(unpack(args))
end
ctrs[param.typename] = counter --update to a number in uniqueparams
--update tables defining template parameter list
uniqueparams:insert(tp)
pos:insert(counter)
ref:insert(param.nref)
counter = counter + 1
end
else
--get concrete type from 'env' or primitives
tp = localenv[param.typename] or terralib.types[param.typename] or error("Could not find " .. param.typename)
--update tables defining template parameter list
uniqueparams:insert(tp)
pos:insert(counter)
ref:insert(param.nref)
counter = counter + 1
end
end
return template.paramlist.new(uniqueparams, pos, ref)
end
function process_method_name(lex, path)
local classname, methodname
if lex:nextif(":") then
classname = path[#path]
methodname = lex:expect(lex.name).value
else
methodname = path[#path]
end
return classname, methodname
end
function process_namespace_indexing(lex)
local path = terralib.newlist{}
repeat
path:insert(lex:expect(lex.name).value)
until not lex:nextif(".")
lex:ref(path[1]) --add root entry to local environment
return path, path[#path]
end
function process_template_parameters(lex,classname)
local params = terralib.newlist()
if classname then
--add first parameter 'self'
params:insert({name="self", typename=classname, nref=1})
end
if lex:nextif("(") then
repeat
if lex:matches(lex.name) then
local paramname = lex:expect(lex.name).value
if lex:nextif(":") then
--is this a reference to a type?
local nref = 0
while lex:nextif("&") do
nref = nref + 1
end
local paramtype = lex:expect(lex.name).value
lex:ref(paramtype) --if paramtype is a concrete type (not a concepts),
--then make it available in 'env'
params:insert({name=paramname, typename=paramtype, nref=nref})
elseif lex:nextif("...") then --expecting ...
params:insert({name=paramname, typename="__varargs__", nref=0})
break --... is the last argument in the loop
else
params:insert({name=paramname, typename="__ducktype__", nref=0})
end
end
until not lex:nextif(",")
lex:expect(")")
end
return params
end
function process_concept_template_parameters(lex)
if lex:nextif("(") then
local params = terralib.newlist()
repeat
if lex:matches(lex.name) then
local param = lex:expect(lex.name).value
lex:ref(param) --make param available in 'env'
params:insert({name="", typename=param, nref=0})
end
until not lex:nextif(",")
lex:expect(")")
return params
end
end
local function processfunargs(lex)
if lex:nextif("(") then
local args = terralib.newlist()
repeat
if lex:matches(lex.name) then
local path, name = process_namespace_indexing(lex)
args:insert(path)
elseif lex:matches(lex.number) then
args:insert(lex:expect(lex.number).value)
elseif lex:matches(lex.boolean) then
args:insert(lex:expect(lex.boolean).value)
elseif lex:matches(lex.string) then
args:insert(lex:expect(lex.string).value)
end
until not lex:nextif(",")
lex:expect(")")
return args
end
end
local function process_single_constraint(lex)
if lex:nextif(":") then
if lex:matches(lex.name) then
local path, name = process_namespace_indexing(lex)
lex:ref(path[1])
local fargs = processfunargs(lex)
return {path=path, name=name, fargs=fargs}
elseif lex:matches(lex.string) then
return {path={"Value"}, name="Value", fargs={lex:expect(lex.string).value} }
elseif lex:matches(lex.number) then
return {path={"Value"}, name="Value", fargs={lex:expect(lex.number).value} }
else
error("ParseError: expected a concepts `name`, a string or a number.")
end
else
return {path = {"Any"}, name = "Any"}
end
end
function process_where_clause(lex)
local params = terralib.newlist{
__ducktype__ = {path = {"Any"}, name = "Any"},
__varargs__ = {path = {"Vararg"}, name = "Vararg"}
}
if lex:nextif("where") then
lex:expect("{")
repeat
if lex:matches(lex.name) then
local param = lex:expect(lex.name).value
params[param] = process_single_constraint(lex)
end
until not lex:nextif(",")
lex:expect("}")
end
return params
end
function isclasstemplate(templ)
return templ.classname~=nil
end
function isnamespacedfunctiontemplate(templ)
return templ.classname==nil and #templ.path > 1
end
function isfreefunctiontemplate(templ)
return templ.classname==nil and #templ.path == 1
end
function isstaticmethod(templ,localenv)
return #templ.path>1 and terralib.types.istype(localenv(templ.path,1))
end
function isvarargstemplate(templ)
local n = #templ.params
return templ.params[n].typename == "__varargs__"
end
return conceptlang