Skip to content

Commit

Permalink
More code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Jun 26, 2019
1 parent fc1b350 commit 0f6afa9
Show file tree
Hide file tree
Showing 20 changed files with 350 additions and 175 deletions.
1 change: 1 addition & 0 deletions nelua/astbuilder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ end

function ASTBuilder:register(tag, shape)
shape.attr = shapetypes.table:is_optional()
shape.uid = shapetypes.number:is_optional()
shape = shapetypes.shape(shape)
local klass = class(ASTNode)
klass.tag = tag
Expand Down
9 changes: 9 additions & 0 deletions nelua/astnode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ ASTNode.tag = 'Node'
ASTNode.nargs = 0
ASTNode._astnode = true

local uid = 0
local function genuid()
uid = uid + 1
return uid
end

function ASTNode:_init(...)
local nargs = select('#', ...)
for i=1,nargs do
self[i] = select(i, ...)
end
self.attr = {}
self.uid = genuid()
end

function ASTNode:args()
Expand Down Expand Up @@ -63,6 +70,7 @@ function ASTNode:clone()
node.srcname = self.srcname
node.modname = self.modname
node.cloned = true
node.uid = genuid()
return node
end

Expand Down Expand Up @@ -127,6 +135,7 @@ end
-------------------
local ignored_stringfy_keys = {
pos = true, src = true, srcname=true, modname=true,
uid = true,
processed = true,
untyped = true,
cloned = true,
Expand Down
6 changes: 3 additions & 3 deletions nelua/cbuiltins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ function functions.type(context, node, emitter)
typename = 'number'
elseif type:is_nilptr() then
typename = 'pointer'
elseif type:is_any() then
assert(false, 'type for any values not implemented yet')
else
elseif type:is_any() then --luacov:disable
node:raisef('type() for any values not implemented yet')
else --luacov:enable
typename = type.name
end
emitter:add('&nelua_typestr_', typename)
Expand Down
6 changes: 3 additions & 3 deletions nelua/ccontext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ function CContext:typename(type)
end
elseif type:is_string() then
self.has_string = true
elseif type:is_function() then
assert(false, 'ctype for functions not implemented yet')
elseif type:is_any() then
elseif type:is_function() then --luacov:disable
error('ctype for functions not implemented yet')
elseif type:is_any() then --luacov:enable
self.has_any = true
self.has_string = true
self.has_type = true
Expand Down
7 changes: 5 additions & 2 deletions nelua/cgenerator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,10 @@ local function visitor_Call(context, node, emitter, argnodes, callee, isblockcal
emitter:add_val2type(calleetype.argtypes[1], callee)
else
if attr.pointercall then
emitter:add('*')
emitter:add('(*', callee, ')(')
else
emitter:add(callee, '(')
end
emitter:add(callee, '(')
end

for i,funcargtype,argnode,argtype,lastcallindex in izipargnodes(callargtypes, argnodes) do
Expand Down Expand Up @@ -661,6 +662,7 @@ function visitors.ForNum(context, node, emitter)
context:pop_scope()
end

--[[
function visitors.ForIn(_, node, emitter)
local itvarnodes, inexpnodes, blocknode = node:args()
emitter:add_indent_ln("{")
Expand All @@ -672,6 +674,7 @@ function visitors.ForIn(_, node, emitter)
emitter:dec_indent()
emitter:add_indent_ln("}")
end
]]

function visitors.Break(_, _, emitter)
emitter:add_indent_ln('break;')
Expand Down
26 changes: 11 additions & 15 deletions nelua/luagenerator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -300,23 +300,19 @@ end
-- operators
function visitors.UnaryOp(context, node, emitter)
local opname, argnode = node:args()
if opname == 'tostring' then
emitter:add('tostring(', argnode, ')')
else
local op = node:assertraisef(luadefs.unary_ops[opname], 'unary operator "%s" not found', opname)
if config.lua_version ~= '5.3' then
local fallop = luadefs.lua51_unary_ops[opname]
if fallop then
context:add_runtime_builtin(fallop.builtin)
emitter:add(fallop.func, '(', argnode, ')')
return
end
local op = node:assertraisef(luadefs.unary_ops[opname], 'unary operator "%s" not found', opname)
if config.lua_version ~= '5.3' then
local fallop = luadefs.lua51_unary_ops[opname]
if fallop then
context:add_runtime_builtin(fallop.builtin)
emitter:add(fallop.func, '(', argnode, ')')
return
end
local surround = node.attr.inoperator
if surround then emitter:add('(') end
emitter:add(op, argnode)
if surround then emitter:add(')') end
end
local surround = node.attr.inoperator
if surround then emitter:add('(') end
emitter:add(op, argnode)
if surround then emitter:add(')') end
end

function visitors.BinaryOp(context, node, emitter)
Expand Down
4 changes: 2 additions & 2 deletions nelua/pegbuilder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ function PEGBuilder:add_group_peg(groupname, name, patt, defs, overwrite)
self.group_pegs[groupname] = group
end
local fullname = string.format('%s_%s', groupname, name)
if tabler.ifind(group, fullname) then
if tabler.ifind(group, fullname) then --luacov:disable
errorer.assertf(overwrite, 'group peg "%s" already exists', fullname)
else
else --luacov:enable
table.insert(group, fullname)
end
merge_defs(self, defs)
Expand Down
8 changes: 8 additions & 0 deletions nelua/preprocessor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,20 @@ function PPContext:tovalue(val, orignode)
node = self.aster.String{val}
elseif traits.is_number(val) or traits.is_bignumber(val) then
local num = bn.new(val)
local neg = false
if num:isneg() then
num = num:abs()
neg = true
end
if num:isintegral() then
node = self.aster.Number{'dec', num:todec()}
else
local int, frac = num:todec():match('^(-?%d+).(%d+)$')
node = self.aster.Number{'dec', int, frac}
end
if neg then
node = self.aster.UnaryOp{'neg', node}
end
elseif traits.is_boolean(val) then
node = self.aster.Boolean{val}
--TODO: table, nil
Expand Down
4 changes: 1 addition & 3 deletions nelua/scope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ local function symbol_resolve_type(symbol)
end

function Scope:add_symbol(symbol)
if not symbol.name then
return
end
local name = symbol.name
assert(name)
local oldsymbol = self.symbols[name]
if oldsymbol then
symbol.node:assertraisef(not config.strict,
Expand Down
16 changes: 8 additions & 8 deletions nelua/syntaxdefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,14 @@ local function get_parser(std)
({} '' -> 'Preprocess' ppstring ) -> to_astnode
]])

grammar:add_group_peg('stat', 'vardecl', [[
({} '' -> 'VarDecl'
%LOCAL -> 'local' cnil
{| etyped_idlist |}
(%ASSIGN {| eexpr_list |})?
) -> to_astnode
]])

if not is_luacompat then
grammar:add_group_peg('stat', 'vardecl', [[
({} '' -> 'VarDecl'
Expand Down Expand Up @@ -350,14 +358,6 @@ local function get_parser(std)
grammar:add_group_peg('stat', 'continue', [[
({} %CONTINUE -> 'Continue') -> to_astnode
]])
else
grammar:add_group_peg('stat', 'vardecl', [[
({} '' -> 'VarDecl'
%LOCAL -> 'local' cnil
{| etyped_idlist |}
(%ASSIGN {| eexpr_list |})?
) -> to_astnode
]])
end

-- expressions
Expand Down
22 changes: 11 additions & 11 deletions nelua/typechecker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -944,16 +944,18 @@ function visitors.ForIn(context, node)
end
context:traverse(inexpnodes)
context:repeat_scope_until_resolution('loop', function()
if itvarnodes then
for i,itvarnode in ipairs(itvarnodes) do
local itsymbol = context:traverse(itvarnode)
if infunctype and infunctype:is_function() then
local fittype = infunctype:get_return_type(i)
itsymbol:add_possible_type(fittype)
end
--[[
if itvarnodes then
for i,itvarnode in ipairs(itvarnodes) do
local itsymbol = context:traverse(itvarnode)
if infunctype and infunctype:is_function() then
local fittype = infunctype:get_return_type(i)
itsymbol:add_possible_type(fittype)
end
end
context:traverse(blocknode)
end
]]
context:traverse(blocknode)
end)
end

Expand Down Expand Up @@ -1196,9 +1198,7 @@ function visitors.FuncDef(context, node)
-- function declaration, must create a new symbol
local name = varnode[1]
local vartype = varnode.attr.type
if not vartype and context.phase == phases.any_inference then
vartype = primtypes.any
end
assert(vartype or context.phase ~= phases.any_inference)
symbol = context.scope:add_symbol(Symbol(name, varnode, vartype))
elseif varnode.tag == 'Id' then
symbol = context:traverse(varnode)
Expand Down
17 changes: 6 additions & 11 deletions nelua/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ local uidcounter = 0
local function genkey(name, node)
local uid
local srcname
if node and node.pos and not node.cloned then
uid = node.pos
srcname = node.srcname
if node then
uid = node.uid
srcname = node.srcname or ''
else
uidcounter = uidcounter + 1
uid = uidcounter
srcname = ''
srcname = '__nonode__'
end
return string.format('%s%s%d', name, srcname, uid)
end
Expand Down Expand Up @@ -459,17 +459,12 @@ function MetaType:set_field(name, symbol)
self.fields[name] = symbol
end

function MetaType:is_equal(type)
return type.name == self.name and
type.key == self.key
end

function MetaType:__tostring()
local ss = sstream('metatype{')
local first = true
for name,sym in iters.opairs(self.fields) do
if first then ss:add(', ') first = false end
ss:add(name, ':', sym.attr.type)
if not first then ss:add(', ') first = false end
ss:add(name, ': ', sym.attr.type)
end
ss:add('}')
return ss:tostring()
Expand Down
8 changes: 0 additions & 8 deletions nelua/utils/stringer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ function stringer.pconcat(...)
return table.concat(t, '\t')
end

function stringer.pformat(format, ...)
if select('#', ...) > 0 then
return string.format(format, ...)
else
return format
end
end

function stringer.pformat(format, ...)
if select('#', ...) == 0 then
return format
Expand Down

0 comments on commit 0f6afa9

Please sign in to comment.