Skip to content

Commit

Permalink
Add <using> annotation to export enum fields
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Nov 12, 2020
1 parent 4a85f6d commit 9fa47f6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
17 changes: 17 additions & 0 deletions nelua/analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,23 @@ function visitors.Annotation(context, node, symbol)
end
elseif name == 'codename' then
objattr.fixedcodename = params
elseif name == 'using' then
assert(objattr._type)
if not objattr.is_enum then
node:raisef("annotation 'using' can only be used with enums")
end
-- inject all enum fields as comptime values
for _,field in ipairs(objattr.fields) do
local fieldsymbol = Symbol{
name = field.name,
codename = objattr.codename..'_'..field.name,
comptime = true,
type = objattr,
value = field.value,
scope = symbol.scope,
}
symbol.scope:add_symbol(fieldsymbol)
end
end

node.done = true
Expand Down
2 changes: 2 additions & 0 deletions nelua/typedefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ typedefs.type_annots = {
-- Mark a record type for forward declaration.
-- This allows to use pointers to a record before defining it.
forwarddecl = true,
-- Whether to use enum fields in the declared scope.
using = true,
}

return typedefs
13 changes: 13 additions & 0 deletions spec/03-typechecker_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1754,4 +1754,17 @@ it("forward type declaration", function()
"cannot be of forward declared type")
end)

it("using annotation", function()
assert.analyze_ast([[
local MyEnum <using> = @enum{
MYENUM_NONE = 0,
MYENUM_ONE = 1,
}
local a: MyEnum = MYENUM_ONE
]])
assert.analyze_error([[
local MyEnum <using> = @record{}
]], "annotation 'using' can only")
end)

end)

0 comments on commit 9fa47f6

Please sign in to comment.