Skip to content

Commit

Permalink
Field punning shorthand syntax for records, closes #16
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Sep 25, 2020
1 parent 29bc9b2 commit 4539446
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
11 changes: 9 additions & 2 deletions nelua/syntaxdefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ local function get_parser()

local parser = PEGParser()
parser:set_astbuilder(astbuilder)
local to_astnode = parser.defs.to_astnode

-- spaces including new lines
parser:set_peg("SPACE", "%s")
Expand Down Expand Up @@ -419,7 +420,9 @@ local function get_parser()
{| (table_row (%SEPARATOR table_row)* %SEPARATOR?)? |}
eRCURLY) -> to_astnode
table_row <- table_pair / expr
table_pair <- ({} '' -> 'Pair' (%LBRACKET eexpr eRBRACKET / name) %ASSIGN eexpr) -> to_astnode
table_pair <-
({} '' -> 'Pair' (%LBRACKET eexpr eRBRACKET / name) %ASSIGN eexpr) -> to_astnode /
({} %ASSIGN -> 'Pair' name) -> to_punned_pair_astnode
doexpr <-
({} %LPAREN %DO -> 'DoExpr' block eEND eRPAREN) -> to_astnode
Expand Down Expand Up @@ -555,7 +558,11 @@ local function get_parser()
name <- %cNAME / ppname
id <- ({} '' -> 'Id' name) -> to_astnode
]])
]], {
to_punned_pair_astnode = function(pos, tag, name)
return to_astnode(pos, tag, name, to_astnode(pos+1, 'Id', name))
end
})

-- operators
grammar:set_pegs([[
Expand Down
3 changes: 2 additions & 1 deletion spec/02-syntaxdefs_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,15 @@ describe("expression", function()
}}}})
end)
it("table", function()
assert.parse_ast(nelua_parser, "return {}, {a}, {a,b}, {a=b}, {[a] = b}",
assert.parse_ast(nelua_parser, "return {}, {a}, {a,b}, {a=b}, {[a] = b}, {=a}",
n.Block{{
n.Return{{
n.Table{{}},
n.Table{{ n.Id{'a'} }},
n.Table{{ n.Id{'a'}, n.Id{'b'} }},
n.Table{{ n.Pair{'a', n.Id{'b'}} }},
n.Table{{ n.Pair{n.Id{'a'}, n.Id{'b'}} }},
n.Table{{ n.Pair{'a', n.Id{'a'}} }},
}}}})
end)
it("surrounded expression", function()
Expand Down
2 changes: 2 additions & 0 deletions spec/05-cgenerator_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,8 @@ it("records", function()
assert(Point({1,2}).y == 2)
assert(Point({x=1,2}).y == 2)
assert(Point({1,y=2}).x == 1)
local x, y = 1, 2
assert(Point({=x,=y}).y == 2)
]])
assert.run_c([[
local Point = @record {x: integer, y: integer}
Expand Down

0 comments on commit 4539446

Please sign in to comment.