Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for-in construct #42

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions spec/parser_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ describe("Titan parser", function()
inc = { _tag = "Exp_Integer", value = 3 },
start = { _tag = "Exp_Integer", value = 1 } },

{ _tag = "Stat_ForIn",
block = {
stats = {
{ _tag = "Stat_Assign",
exp = { var = { _tag = "Var_Name", name = "i" } },
var = { _tag = "Var_Name", name = "i" } } } },
decl = { _tag = "Decl_Decl", name = "i", type = false },
exp = { _tag = "Exp_Call" } },

{ _tag = "Stat_Return", exp = { _tag = "Exp_Var" } },
})
end)
Expand Down
4 changes: 4 additions & 0 deletions testfiles/statements.titan
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ local function statements(): nil
i = i
end

for i in pairs(t) do
i = i
end

return x
end
1 change: 1 addition & 0 deletions titan-compiler/ast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ types.Stat = {
Repeat = {"block", "condition"},
If = {"thens", "elsestat"},
For = {"decl", "start", "finish", "inc", "block"},
ForIn = {"decl", "exp", "block"},
Assign = {"var", "exp"},
Decl = {"decl", "exp"},
Call = {"callexp"},
Expand Down
20 changes: 20 additions & 0 deletions titan-compiler/checker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,24 @@ local function checkfor(node, st, errors)
return false
end

-- Typechecks a for-in statement
-- node: Stat_For AST node
-- st: symbol table
-- errors: list of compile-time errors
-- returns whether statement always returns from its function (always false for 'for' loop)
local function checkforin(node, st, errors)
checkstat(node.decl, st, errors)
checkexp(node.exp, st, errors)
if node.decl.type then
checkstat(node.decl, st, errors)
local dtype = node.decl._type
node.exp = trycoerce(node.exp, dtype)
checkmatch("'for' expression", dtype, node.exp._type, errors, node.exp._pos)
end
checkstat(node.block, st, errors)
return false
end

-- Typechecks a block statement
-- node: Stat_Block AST node
-- st: symbol table
Expand Down Expand Up @@ -276,6 +294,8 @@ function checkstat(node, st, errors)
st:with_block(checkrepeat, node, st, errors)
elseif tag == "Stat_For" then
st:with_block(checkfor, node, st, errors)
elseif tag == "Stat_ForIn" then
st:with_block(checkforin, node, st, errors)
elseif tag == "Stat_Assign" then
checkexp(node.var, st, errors)
-- mark this variable as assigned to
Expand Down
3 changes: 3 additions & 0 deletions titan-compiler/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ local grammar = re.compile([[
ASSIGN exp COMMA exp
(COMMA exp)? -> opt
DO block END) -> Stat_For
/ ({} FOR decl
IN exp
DO block END) -> Stat_ForIn
/ ({} LOCAL decl ASSIGN exp) -> defstat
/ ({} var ASSIGN exp) -> Stat_Assign
/ ({} (suffixedexp => exp_is_call)) -> Stat_Call
Expand Down