-
Notifications
You must be signed in to change notification settings - Fork 203
/
sumlanguage2.t
32 lines (32 loc) · 1.02 KB
/
sumlanguage2.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
local sumlanguage = {
name = "sumlanguage"; --name for debugging
entrypoints = {"sum"}; -- list of keywords that will start our expressions
keywords = {"done"}; --list of keywords specific to this language
expression = function(self,lex)
local sum = 0
local variables = terralib.newlist()
lex:expect("sum")
if not lex:matches("done") then
repeat
if lex:matches(lex.name) then --if it is a variable
local name = lex:next().value
lex:ref(name) --tell the Terra parser we will access a Lua variable, 'name'
variables:insert(name) --add its name to the list of variables
else
sum = sum + lex:expect(lex.number).value
end
until not lex:nextif(",")
end
lex:expect("done")
return function(environment_function)
local env = environment_function() --capture the local environment
--a table from variable name => value
local mysum = sum
for i,v in ipairs(variables) do
mysum = mysum + env[v]
end
return mysum
end
end;
}
return sumlanguage