Skip to content

Commit

Permalink
Use type codename for function naming
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Jun 28, 2019
1 parent 4334fdd commit ccbc4b5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion nelua/cgenerator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ local function visit_assignments(context, emitter, varnodes, valnodes, decl)
decemitter:add('static ')
end
decemitter:add(varnode)
if valnode and valnode.attr.compconst then
if valnode and (valnode.attr.compconst or varattr.const or varattr.compconst) then
-- initialize to const values
decemitter:add(' = ')
assert(not lastcallindex)
Expand Down
32 changes: 24 additions & 8 deletions nelua/typechecker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ function visitors.Table(context, node, desiredtype)
end

function visitors.Pragma(context, node, symbol)
if symbol and node.attr.symbol == symbol then
-- quick return
return
end
local name, argnodes = node:args()
context:traverse(argnodes)

Expand Down Expand Up @@ -283,6 +287,7 @@ function visitors.Pragma(context, node, symbol)
if name == 'strict' then
config.strict = true
end
node.attr.symbol = symbol
end

function visitors.Id(context, node)
Expand Down Expand Up @@ -1074,16 +1079,31 @@ function visitors.VarDecl(context, node)
end
if valtype then
varnode:assertraisef(not valtype:is_void(), 'cannot assign to expressions of type void')
local foundtype = false
if varnode.attr.compconst then
-- for consts the type must be known ahead
vartype = valtype
symbol.attr.type = valtype
foundtype = true
symbol.attr.value = valnode.attr.value
elseif valtype:is_type() then
-- for 'type' types the type must also be known ahead
vartype = valtype
symbol.attr.type = valtype
assert(valnode and valnode.attr.holdedtype)
foundtype = true
symbol.attr.compconst = true
symbol.attr.holdedtype = valnode.attr.holdedtype
end

if foundtype then
if vartype ~= valtype then
assert(not vartype)
vartype = valtype
symbol.attr.type = valtype

local pragmanode = varnode[4]
if pragmanode then
-- must retravese pragma node early once type is found ahead
context:traverse(pragmanode, symbol)
end
end
else
-- lazy type evaluation
symbol:add_possible_type(valtype)
Expand All @@ -1093,10 +1113,6 @@ function visitors.VarDecl(context, node)
"variable '%s' of type '%s' is not coercible with expression of type '%s'",
symbol.name, vartype, valtype)
end
if valtype:is_type() then
assert(valnode and valnode.attr.holdedtype)
symbol.attr.holdedtype = valnode.attr.holdedtype
end
else
-- delay type evaluation
symbol:add_possible_type(nil)
Expand Down
12 changes: 12 additions & 0 deletions spec/05-cgenerator_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,18 @@ it("pragmas", function()
]])
end)

it("type codenames", function()
assert.generate_c([[
local myrecord !codename 'myrecord' = @record{x: integer}
function myrecord:foo() return self.x end
local r = myrecord{}
return r:foo()
]], {
"typedef struct myrecord {\n int64_t x;\n} myrecord;",
"static int64_t myrecord_foo(myrecord_pointer self);"
})
end)

it("entrypoint", function()
assert.run_c([[
print 'hello'
Expand Down

0 comments on commit ccbc4b5

Please sign in to comment.