diff --git a/docs/pages/overview.md b/docs/pages/overview.md index 1beb2db1..ab72afa0 100644 --- a/docs/pages/overview.md +++ b/docs/pages/overview.md @@ -791,7 +791,7 @@ Memory can be allocated using C malloc and free. ```nelua require 'memory' -require 'allocators.generic_allocator' +require 'allocators.generic' local Person = @record{name: string, age: integer} local p: Person* = generic_allocator.new(@Person) diff --git a/examples/linkedlist.nelua b/examples/linkedlist.nelua index a2fb10f6..919aa32b 100644 --- a/examples/linkedlist.nelua +++ b/examples/linkedlist.nelua @@ -1,5 +1,5 @@ require 'memory' -require 'allocators.generic_allocator' +require 'allocators.generic' local allocator = @generic_allocator diff --git a/examples/overview.nelua b/examples/overview.nelua index 4fa60cbf..daddc106 100644 --- a/examples/overview.nelua +++ b/examples/overview.nelua @@ -393,7 +393,7 @@ do -- Dereferencing and referencing end require 'memory' -require 'allocators.generic_allocator' +require 'allocators.generic' do -- Allocating memory local Person = @record{name: string, age: integer} diff --git a/lib/gc.nelua b/lib/allocators/gc.nelua similarity index 91% rename from lib/gc.nelua rename to lib/allocators/gc.nelua index e2a01714..f6808a1c 100644 --- a/lib/gc.nelua +++ b/lib/allocators/gc.nelua @@ -1,11 +1,10 @@ -- Tiny and simple mark and sweep garbage collector -- Implementation based on https://github.com/orangeduck/tgc -## unitname = 'nelua' ## cinclude '' require 'memory' -require 'allocators.generic_allocator' +require 'allocators.generic' local jmp_buf = @record{dummy: usize} local function setjmp(env: jmp_buf) end @@ -335,14 +334,13 @@ function GC:_sweep() self.nitems = self.nitems - 1 end - self:_unmark_all() self:_resize_less() self.mitems = self.nitems + (@usize)(self.nitems * self.sweepfactor) + 1 for j:usize=0, gc:stop() return ret end + +require 'allocators.interface' + +global gc_allocator = @record{} + +function gc_allocator.alloc(size: usize): pointer + check(size > 0_u, 'gc_allocator.alloc: size cannot be zero') + local p: pointer = gc:alloc(size) + check(p, 'gc_allocator.alloc: allocation fail') + return p +end + +function gc_allocator.alloc0(size: usize): pointer + check(size > 0_u, 'gc_allocator.alloc0: size must be greater than 0') + local p: pointer = gc:alloc0(size) + check(p, 'gc_allocator.alloc0: allocation fail') + return p +end + +function gc_allocator.realloc(p: pointer, size: usize): pointer + check(size > 0_u, 'gc_allocator.realloc: size must be greater than 0') + p = gc:realloc(p, size) + check(size == 0 or p, 'gc_allocator.realloc: allocation fail') + return p +end + +function gc_allocator.realloc0(p: pointer, newsize: usize, oldsize: usize): pointer + check(newsize > 0_u, 'gc_allocator.realloc0: size must be greater than 0') + p = gc:realloc0(p, newsize, oldsize) + check(newsize == 0_u or p, 'gc_allocator.realloc0: allocation fail') + return p +end + +function gc_allocator.dealloc(p: pointer) + gc:dealloc(p) +end + +## implement_allocator_interface(gc_allocator) diff --git a/lib/allocators/gc_allocator.nelua b/lib/allocators/gc_allocator.nelua deleted file mode 100644 index 29259a39..00000000 --- a/lib/allocators/gc_allocator.nelua +++ /dev/null @@ -1,40 +0,0 @@ -## unitname = 'nelua' - -require 'gc' -require 'allocators.allocator_interface' - -global gc_allocator = @record{} - -function gc_allocator.alloc(size: usize): pointer - check(size > 0_u, 'gc_allocator.alloc: size cannot be zero') - local p: pointer = gc:alloc(size) - check(p, 'gc_allocator.alloc: allocation fail') - return p -end - -function gc_allocator.alloc0(size: usize): pointer - check(size > 0_u, 'gc_allocator.alloc0: size must be greater than 0') - local p: pointer = gc:alloc0(size) - check(p, 'gc_allocator.alloc0: allocation fail') - return p -end - -function gc_allocator.realloc(p: pointer, size: usize): pointer - check(size > 0_u, 'gc_allocator.realloc: size must be greater than 0') - p = gc:realloc(p, size) - check(size == 0 or p, 'gc_allocator.realloc: allocation fail') - return p -end - -function gc_allocator.realloc0(p: pointer, newsize: usize, oldsize: usize): pointer - check(newsize > 0_u, 'gc_allocator.realloc0: size must be greater than 0') - p = gc:realloc0(p, newsize, oldsize) - check(newsize == 0_u or p, 'gc_allocator.realloc0: allocation fail') - return p -end - -function gc_allocator.dealloc(p: pointer) - gc:dealloc(p) -end - -## implement_allocator_interface(gc_allocator) diff --git a/lib/allocators/generic_allocator.nelua b/lib/allocators/generic.nelua similarity index 96% rename from lib/allocators/generic_allocator.nelua rename to lib/allocators/generic.nelua index 57cfc85a..8849f932 100644 --- a/lib/allocators/generic_allocator.nelua +++ b/lib/allocators/generic.nelua @@ -1,6 +1,4 @@ -## unitname = 'nelua' - -require 'allocators.allocator_interface' +require 'allocators.interface' local function memset(s: pointer, c: cint, n: csize): pointer ',nodecl> end local function malloc(size: csize): pointer ',nodecl> end diff --git a/lib/allocators/allocator_interface.nelua b/lib/allocators/interface.nelua similarity index 99% rename from lib/allocators/allocator_interface.nelua rename to lib/allocators/interface.nelua index 1424162a..a7dc862c 100644 --- a/lib/allocators/allocator_interface.nelua +++ b/lib/allocators/interface.nelua @@ -1,6 +1,4 @@ ##[[ -unitname = 'nelua' - -------------------------------------------------------------------------------- -- compile time checks utilities local function check_span_subtype(v) diff --git a/lib/io.nelua b/lib/io.nelua index c8bfae4b..14bdb1b9 100644 --- a/lib/io.nelua +++ b/lib/io.nelua @@ -1,5 +1,3 @@ -## unitname = 'nelua' - --TODO: optional arguments/returns -------------------------------------------------------------------------------- diff --git a/lib/math.nelua b/lib/math.nelua index d9cc2450..0d392917 100644 --- a/lib/math.nelua +++ b/lib/math.nelua @@ -1,5 +1,3 @@ -## unitname = 'nelua' - global math = @record{} -- constants diff --git a/lib/memory.nelua b/lib/memory.nelua index 9f9ae7c8..5852610b 100644 --- a/lib/memory.nelua +++ b/lib/memory.nelua @@ -1,6 +1,4 @@ ##[[ -unitname = 'nelua' - -------------------------------------------------------------------------------- -- compile time checks utilities local function check_span_subtype(v) diff --git a/lib/os.nelua b/lib/os.nelua index d2dfdcc3..95c956f0 100644 --- a/lib/os.nelua +++ b/lib/os.nelua @@ -1,5 +1,3 @@ -## unitname = 'nelua' - --TODO: optional params/returns -------------------------------------------------------------------------------- @@ -55,7 +53,7 @@ end function os.date(): string --TODO: implement os date - return 'NIY (not implementet yet)' + return 'NIY (not implemented yet)' end function os.difftime(t1: integer, t2: integer): integer diff --git a/lib/sequence.nelua b/lib/sequence.nelua index be96802e..919d1117 100644 --- a/lib/sequence.nelua +++ b/lib/sequence.nelua @@ -8,19 +8,16 @@ -- By default its use the garbage collector unless explicitly told not to do so, -- thus by default there is no need to manually reset the sequence. - -## unitname = 'nelua' - require 'memory' ## local make_sequence = generalize(function(T, allocator) ## staticassert(traits.is_type(T), "invalid type '%s'", T) - ## local codenameprefix = 'nelua_sequence_'..T.name + ## local codenameprefix = 'sequence_'..T.name ## if allocator then local allocator: type = #[allocator]# ## codenameprefix = codenameprefix..'_'..allocator.nick ## else - require 'allocators.gc_allocator' + require 'allocators.gc' local allocator: type = @gc_allocator ## end diff --git a/nelua/builtins.lua b/nelua/builtins.lua index 8d1d112f..572bf341 100644 --- a/nelua/builtins.lua +++ b/nelua/builtins.lua @@ -27,21 +27,27 @@ function builtins.require(context, node) end end - local unitname = argnode.attr.value - attr.unitname = pegger.filename_to_unitname(unitname) - -- load it and parse - local filepath = fs.findmodulefile(unitname, config.path) + local unitpath = argnode.attr.value + local filepath = fs.findmodulefile(unitpath, config.path) if not filepath then if canloadatruntime then -- maybe it would succeed at runtime attr.runtime_require = true return else - node:raisef("in require: module '%s' not found", unitname) + node:raisef("in require: module '%s' not found", unitpath) end end + -- nelua internal libs have unit name of just 'nelua' + local unitname = pegger.filename_to_unitname(unitpath) + local nelualibpath = fs.join(config.data_path, 'lib') + if filepath:find(nelualibpath, 1, true) then + unitname = 'nelua' + end + attr.unitname = unitname + local reqnode = context.requires[filepath] if reqnode and reqnode ~= node then -- already required diff --git a/nelua/cgenerator.lua b/nelua/cgenerator.lua index 44178253..e11e5649 100644 --- a/nelua/cgenerator.lua +++ b/nelua/cgenerator.lua @@ -4,8 +4,6 @@ local traits = require 'nelua.utils.traits' local errorer = require 'nelua.utils.errorer' local stringer = require 'nelua.utils.stringer' local tabler = require 'nelua.utils.tabler' -local fs = require 'nelua.utils.fs' -local config = require 'nelua.configer'.get() local cdefs = require 'nelua.cdefs' local cbuiltins = require 'nelua.cbuiltins' local typedefs = require 'nelua.typedefs' @@ -1151,7 +1149,6 @@ end function generator.generate(ast, context) CContext.promote_context(context, visitors, typevisitors) - context.runtime_path = fs.join(config.runtime_path, 'c') emit_main(ast, context) diff --git a/nelua/configer.lua b/nelua/configer.lua index 65924099..b8e7df4e 100644 --- a/nelua/configer.lua +++ b/nelua/configer.lua @@ -55,12 +55,8 @@ local function create_parser(argv) return argparser end -local function get_runtime_path(arg0) - return fs.join(fs.getdatapath(arg0), 'runtime') -end - -local function get_path(arg0) - local libdir = fs.join(fs.getdatapath(arg0), 'lib') +local function get_path(data_path) + local libdir = fs.join(data_path, 'lib') return fs.join(libdir,'?.nelua')..';'.. fs.join(libdir,'?','init.nelua')..';'.. @@ -84,8 +80,8 @@ function configer.parse(args) local argparser = create_parser(tabler.copy(args)) local ok, options = argparser:pparse(args) except.assertraise(ok, options) - config.runtime_path = get_runtime_path(args[0]) - config.path = get_path(args[0]) + config.data_path = fs.getdatapath(args[0]) + config.path = get_path(config.data_path) metamagic.setmetaindex(options, defconfig) metamagic.setmetaindex(config, options, true) return config @@ -96,8 +92,8 @@ function configer.get() end local function init_default_configs() - defconfig.path = get_path() - defconfig.runtime_path = get_runtime_path() + defconfig.data_path = fs.getdatapath() + defconfig.path = get_path(defconfig.data_path) defconfig.cc = get_cc() defconfig.cflags = os.getenv('CFLAGS') or '' metamagic.setmetaindex(config, defconfig) diff --git a/nelua/preprocessor.lua b/nelua/preprocessor.lua index 95676ae3..2e31b4d3 100644 --- a/nelua/preprocessor.lua +++ b/nelua/preprocessor.lua @@ -223,8 +223,6 @@ function preprocessor.preprocess(context, ast) local symbol = ppcontext.context.scope.symbols[key] if symbol then return symbol - elseif typedefs.field_pragmas[key] then - return context.pragmas[key] elseif typedefs.call_pragmas[key] then return function(...) local args = tabler.pack(...) @@ -242,15 +240,7 @@ function preprocessor.preprocess(context, ast) return nil end end, __newindex = function(_, key, value) - if typedefs.field_pragmas[key] then - local ok, err = typedefs.field_pragmas[key](value) - if not ok then - raise_preprocess_error("invalid type for preprocess variable '%s': %s", key, err) - end - context.pragmas[key] = value - else - rawset(ppcontext.context.env, key, value) - end + rawset(ppcontext.context.env, key, value) end}) -- try to run the preprocess otherwise capture and show the error diff --git a/nelua/typedefs.lua b/nelua/typedefs.lua index 594b7346..755f6afe 100644 --- a/nelua/typedefs.lua +++ b/nelua/typedefs.lua @@ -151,13 +151,6 @@ typedefs.call_pragmas = { afterinfer = shaper.shape{n=shaper.number, shaper.func}, } -typedefs.field_pragmas = { - noinit = shaper.boolean, - nostatic = shaper.boolean, - nofloatsuffix = shaper.boolean, - unitname = shaper.string:is_optional(), -} - local common_annots = { cimport = shaper.shape{shaper.string:is_optional()}, onestring = shaper.shape{shaper.string}, diff --git a/nelua/utils/fs.lua b/nelua/utils/fs.lua index 560ed5fc..de501492 100644 --- a/nelua/utils/fs.lua +++ b/nelua/utils/fs.lua @@ -50,7 +50,8 @@ function fs.getdatapath(arg0) path = fs.join(fs.getpathdir(path), 'conf') end else --luacov:enable - path = fs.getpathdir(fs.getpathdir(fs.getpathdir(fs.abspath(debug.getinfo(1).short_src)))) + local thispath = debug.getinfo(1).short_src + path = fs.getpathdir(fs.getpathdir(fs.getpathdir(fs.abspath(thispath)))) end return path end diff --git a/rockspecs/nelua-dev-1.rockspec b/rockspecs/nelua-dev-1.rockspec index 2d703552..aa610d35 100644 --- a/rockspecs/nelua-dev-1.rockspec +++ b/rockspecs/nelua-dev-1.rockspec @@ -93,12 +93,11 @@ build = { ['nelua'] = 'nelua.lua' }, conf = { - ['lib/allocators/allocator_interface.nelua'] = 'lib/allocators/allocator_interface.nelua', - ['lib/allocators/generic_allocator.nelua'] = 'lib/allocators/generic_allocator.nelua', - ['lib/allocators/gc_allocator.nelua'] = 'lib/allocators/gc_allocator.nelua', + ['lib/allocators/interface.nelua'] = 'lib/allocators/interface.nelua', + ['lib/allocators/generic.nelua'] = 'lib/allocators/generic.nelua', + ['lib/allocators/gc.nelua'] = 'lib/allocators/gc.nelua', ['lib/io.nelua'] = 'lib/io.nelua', ['lib/math.nelua'] = 'lib/math.nelua', - ['lib/gc.nelua'] = 'lib/gc.nelua', ['lib/memory.nelua'] = 'lib/memory.nelua', ['lib/sequence.nelua'] = 'lib/sequence.nelua', ['lib/mystring.nelua'] = 'lib/mystring.nelua', diff --git a/spec/05-cgenerator_spec.lua b/spec/05-cgenerator_spec.lua index eb8a5dff..fcedf49c 100644 --- a/spec/05-cgenerator_spec.lua +++ b/spec/05-cgenerator_spec.lua @@ -1702,11 +1702,11 @@ it("type builtin", function() ]]) end) -it("context states", function() +it("context pragmas", function() assert.generate_c([[ - ## noinit = true + ## context.pragmas.noinit = true local a: integer - ## noinit = false + ## context.pragmas.noinit = false local b: integer ]], { "\nstatic int64_t a;\n", @@ -1714,10 +1714,10 @@ it("context states", function() }) assert.generate_c([[ - ## nostatic = true + ## context.pragmas.nostatic = true local a: integer local function f() end - ## nostatic = false + ## context.pragmas.nostatic = false local b: integer local function g() end ]], { @@ -1728,16 +1728,16 @@ it("context states", function() }) assert.generate_c([[ - ## nofloatsuffix = true + ## context.pragmas.nofloatsuffix = true local a: float32 = 0 ]], { "a = 0.0;", }) assert.generate_c([[ - ## nostatic = true + ## context.pragmas.nostatic = true local a: integer - ## nostatic = false + ## context.pragmas.nostatic = false local b: integer ]], { "\nint64_t a = 0;\n", @@ -1745,7 +1745,7 @@ it("context states", function() }) assert.generate_c([[ - ## unitname = 'mylib' + ## context.pragmas.unitname = 'mylib' local function foo() end ]], "extern void mylib_foo();") diff --git a/spec/06-preprocessor_spec.lua b/spec/06-preprocessor_spec.lua index 102fdd7b..e6b2c73c 100644 --- a/spec/06-preprocessor_spec.lua +++ b/spec/06-preprocessor_spec.lua @@ -315,8 +315,6 @@ assert.ast_type_equals([=[ ## strict = true print 'unit' ]]) - - assert.analyze_error("## unitname = 1", "invalid type for preprocess") end) it("function pragmas", function() diff --git a/tests/memory_test.nelua b/tests/memory_test.nelua index 9ad90ca2..b1819cdc 100644 --- a/tests/memory_test.nelua +++ b/tests/memory_test.nelua @@ -1,5 +1,5 @@ require 'memory' -require 'allocators.generic_allocator' +require 'allocators.generic' local allocator = @generic_allocator diff --git a/tests/sequence_test.nelua b/tests/sequence_test.nelua index 2506d0a7..0c4973b0 100644 --- a/tests/sequence_test.nelua +++ b/tests/sequence_test.nelua @@ -53,7 +53,7 @@ do -- passing by reference assert(#seqb == 0) end -require 'allocators.generic_allocator' +require 'allocators.generic' do -- custom allocator local seq: sequence(integer, generic_allocator) = {1,2,3} assert(#seq == 3 and seq[0] == 0 and seq[1] == 1 and seq[2] == 2 and seq[3] == 3)