Skip to content

Commit

Permalink
Smart unit name detection, deprecate modname pragma
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Feb 2, 2020
1 parent d23f24b commit 7259403
Show file tree
Hide file tree
Showing 14 changed files with 23 additions and 36 deletions.
2 changes: 0 additions & 2 deletions examples/linkedlist.nelua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
-- generate pretty name on C source files
## modname = 'list'
## strict = true

require 'memory'
Expand Down
2 changes: 1 addition & 1 deletion lib/math.nelua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## strict = true
## modname = 'nelua'
## unitname = 'nelua'

global math = @record{}

Expand Down
2 changes: 1 addition & 1 deletion lib/memory.nelua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## strict = true
## modname = 'nelua'
## unitname = 'nelua'

local function memcpy(dest: pointer, src: pointer, n: csize): pointer <cimport'memcpy',cinclude'<string.h>',nodecl> end
local function memmove(dest: pointer, src: pointer, n: csize): pointer <cimport'memmove',cinclude'<string.h>',nodecl> end
Expand Down
1 change: 0 additions & 1 deletion lib/mystring.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
--TODO: optional parameters/returns

## strict = true
## modname = 'mystring'

--------------------------------------------------------------------------------
-- C imports
Expand Down
1 change: 1 addition & 0 deletions lib/os.nelua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## strict = true
## unitname = 'nelua'

--TODO: optional params/returns

Expand Down
8 changes: 3 additions & 5 deletions nelua/analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1744,9 +1744,9 @@ function analyzer.analyze(ast, parser, context)
context = AnalyzerContext(visitors, parser)
end

context:push_pragmas()
if ast.srcname then
local state = context:push_state()
state.modname = pegger.filename_to_modulename(ast.srcname)
context.pragmas.unitname = pegger.filename_to_unitname(ast.srcname)
end

-- phase 1 traverse: preprocess
Expand All @@ -1767,9 +1767,7 @@ function analyzer.analyze(ast, parser, context)
until resolutions_count == 0
context:pop_state()

if ast.srcname then
context:pop_state()
end
context:pop_pragmas()

return context
end
Expand Down
10 changes: 3 additions & 7 deletions nelua/analyzercontext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ function AnalyzerContext:_init(visitors, parser)
self.usedcodenames = {}
end

function AnalyzerContext:reset_pragmas()
tabler.clear(self.pragmas)
end

function AnalyzerContext:push_pragmas()
table.insert(self.pragmastack, self.pragmas)
local newpragmas = tabler.copy(self.pragmas)
Expand Down Expand Up @@ -104,9 +100,9 @@ function AnalyzerContext:ensure_runtime_builtin(name, p1, p2)
end

function AnalyzerContext:choose_codename(name)
local modname = self.pragmas.modname or self.state.modname
if modname then
name = modname .. '_' .. name
local unitname = self.pragmas.unitname or self.state.unitname
if unitname and unitname ~= '' then
name = unitname .. '_' .. name
end
local count = self.usedcodenames[name]
if count then
Expand Down
11 changes: 4 additions & 7 deletions nelua/builtins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ function builtins.require(context, node)
return
end

local modulename = argnode.attr.value
attr.modulename = modulename
local unitname = argnode.attr.value
attr.unitname = pegger.filename_to_unitname(unitname)

-- load it and parse
local filepath = fs.findmodulefile(modulename, config.path)
local filepath = fs.findmodulefile(unitname, config.path)
if not filepath then
-- maybe it would succeed at runtime
attr.runtime_require = true
Expand All @@ -52,12 +52,9 @@ function builtins.require(context, node)
-- analyze it
local ast = attr.loadedast
local state = context:push_state()
if ast.srcname then
state.modname = pegger.filename_to_modulename(ast.srcname)
end
context:push_scope(context.rootscope)
context:push_pragmas()
context:reset_pragmas()
context.pragmas.unitname = attr.unitname
state.inrequire = true
if justloaded then
preprocessor.preprocess(context, ast)
Expand Down
4 changes: 2 additions & 2 deletions nelua/cbuiltins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,8 @@ function inlines.require(context, node, emitter)
end
local ast = node.attr.loadedast
if node.attr.runtime_require then
if node.attr.modulename then
node:raisef("compile time module '%s' not found", node.attr.modulename)
if node.attr.unitname then
node:raisef("compile time module '%s' not found", node.attr.unitname)
else
node:raisef('runtime require is not supported in C backend yet')
end
Expand Down
6 changes: 2 additions & 4 deletions nelua/symdefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ local function define_function(name, args, rets, props)
type = type,
const = true,
builtin = true,
staticstorage = true,
modname = 'nelua',
staticstorage = true
}
if props then
tabler.update(symbol, props)
Expand All @@ -33,8 +32,7 @@ local function define_const(name, type, value)
const = value == nil,
value = value,
builtin = true,
staticstorage = true,
modname = 'nelua',
staticstorage = true
}
symdefs[name] = symbol
end
Expand Down
2 changes: 1 addition & 1 deletion nelua/typedefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ typedefs.field_pragmas = {
noinit = shaper.boolean,
nostatic = shaper.boolean,
nofloatsuffix = shaper.boolean,
modname = shaper.string:is_optional(),
unitname = shaper.string:is_optional(),
}

local common_annots = {
Expand Down
6 changes: 3 additions & 3 deletions nelua/utils/pegger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,15 @@ function pegger.split_execargs(s)
return split_execargs_patt:match(s)
end

local filename_to_modulename_patt = re.compile[[
local filename_to_unitname_patt = re.compile[[
p <- {~ filebeg? c* ~}
c <- extend -> '' / [_%w] / (%s+ / [_/\.-]) -> '_' / . -> 'X'
filebeg <- [./\]+ -> ''
extend <- '.' [_%w]+ !.
]]

function pegger.filename_to_modulename(s)
return filename_to_modulename_patt:match(s)
function pegger.filename_to_unitname(s)
return filename_to_unitname_patt:match(s)
end

local template_peg = re.compile([[
Expand Down
2 changes: 1 addition & 1 deletion spec/05-cgenerator_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,7 @@ it("context states", function()
})

assert.generate_c([[
## modname = 'mylib'
## unitname = 'mylib'
local function foo() <cexport>
end
]], "extern void mylib_foo();")
Expand Down
2 changes: 1 addition & 1 deletion tests/memory_test.nelua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## strict = true
## modname = 'test'

--## cflags '-fsanitize=address -fsanitize=undefined -fsanitize=leak'

require 'memory'
Expand Down

0 comments on commit 7259403

Please sign in to comment.