Skip to content

Commit

Permalink
Begin Lua stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Jun 25, 2019
1 parent 1f867c0 commit 9b9140a
Show file tree
Hide file tree
Showing 19 changed files with 671 additions and 215 deletions.
14 changes: 7 additions & 7 deletions docs/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GEM
eventmachine (>= 0.12.9)
http_parser.rb (~> 0.6.0)
eventmachine (1.2.7)
ffi (1.10.0)
ffi (1.11.1)
forwardable-extended (2.6.0)
http_parser.rb (0.6.0)
i18n (0.9.5)
Expand All @@ -31,8 +31,8 @@ GEM
jekyll (>= 3.7, < 5.0)
jekyll-sass-converter (1.5.2)
sass (~> 3.4)
jekyll-seo-tag (2.6.0)
jekyll (~> 3.3)
jekyll-seo-tag (2.6.1)
jekyll (>= 3.3, < 5.0)
jekyll-sitemap (1.3.1)
jekyll (>= 3.7, < 5.0)
jekyll-watch (2.2.1)
Expand All @@ -50,14 +50,14 @@ GEM
jekyll-seo-tag (~> 2.1)
pathutil (0.16.2)
forwardable-extended (~> 2.6)
public_suffix (3.0.3)
public_suffix (3.1.0)
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
ffi (~> 1.0)
rouge (3.3.0)
rouge (3.4.1)
ruby_dep (1.5.0)
safe_yaml (1.0.5)
sass (3.7.3)
sass (3.7.4)
sass-listen (~> 4.0.0)
sass-listen (4.0.0)
rb-fsevent (~> 0.9, >= 0.9.4)
Expand All @@ -74,4 +74,4 @@ DEPENDENCIES
tzinfo-data

BUNDLED WITH
2.0.1
2.0.2
160 changes: 0 additions & 160 deletions examples/math.nelua

This file was deleted.

28 changes: 13 additions & 15 deletions examples/mersenne.nelua
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-- TODO: make enum work
-- TODO: var&
-- TODO: self sugar methods
-- TODO: static globals

-- Mersenne Twister random number generator, implementation taken from
-- http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c
Expand All @@ -14,7 +11,7 @@ local mt19937 = @record {
}

-- initialize random state with a seed
local function mt19937_seed(self: pointer<mt19937>, seed: uint32)
function mt19937:seed(seed: uint32)
self.i = MT19937_N
self.v[0]= seed & 0xffffffff
for i=1_u32,<MT19937_N do
Expand All @@ -24,8 +21,8 @@ local function mt19937_seed(self: pointer<mt19937>, seed: uint32)
end

-- generates a random number on [0, 0xffffffff]
local function mt19937_random_uint32(self: pointer<mt19937>)
local MAG01: array<uint32, 2> = {0x0, 0x9908b0df}
function mt19937:random_uint32()
local MAG01: uint32[2] = {0x0, 0x9908b0df}
local LOWER_MASK: compconst = 0x7fffffff_u32
local UPPER_MASK: compconst = 0x80000000_u32
local N: compconst, M: compconst = MT19937_N, 397_u32
Expand Down Expand Up @@ -53,18 +50,19 @@ local function mt19937_random_uint32(self: pointer<mt19937>)
end

-- generates a random number on [0,1) interval
local function mt19937_random_float32(self: pointer<mt19937>)
return mt19937_random_uint32(self) * (1.0 / 4294967295.0)
function mt19937:random_float32()
return self:random_uint32() * (1.0 / 4294967295.0)
end

-- generates a random number on [0,1) with 53-bit resolution
local function mt19937_random_float64(self: pointer<mt19937>)
local a = mt19937_random_uint32(self) >> 5
local b = mt19937_random_uint32(self) >> 6
function mt19937:random_float64()
local a = self:random_uint32() >> 5
local b = self:random_uint32() >> 6
return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0)
end

local default_mt19937: mt19937 = {
-- pre initialized mersenne (faster startup)
local default_mt19937 = @mt19937{
i = 0,
v = {
0x5a682c69,0x1d6a2d67,0x3f086e20,0xeba44ff7,0xadad9a48,0xe11f0603,0x60f8b793,0x30a445b6,
Expand Down Expand Up @@ -148,6 +146,6 @@ local default_mt19937: mt19937 = {
}
}

print(mt19937_random_float64(&default_mt19937))
print(mt19937_random_float64(&default_mt19937))
print(mt19937_random_float64(&default_mt19937))
print(default_mt19937:random_float64())
print(default_mt19937:random_float64())
print(default_mt19937:random_float64())
6 changes: 5 additions & 1 deletion nelua/cbuiltins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function functions.assert(context, node)
local args = node:args()
if #args == 2 then
context:add_runtime_builtin('assert_message')
return 'nelua_assert_message'
return 'nelua_assert_string'
elseif #args == 1 then
context:add_runtime_builtin('assert')
return 'nelua_assert'
Expand Down Expand Up @@ -149,4 +149,8 @@ function functions.print(context, node)
return funcname
end

function functions.error()
return 'nelua_panic_string'
end

return builtins
19 changes: 19 additions & 0 deletions nelua/cemitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,23 @@ function CEmitter:add_string2cstring(val)
self:add('(', val, ')->data')
end

function CEmitter:add_cstring2string(val)
--TODO: free allocated strings using reference counting
self.context:add_runtime_builtin('assert_cmessage')
self:add_ln('({')
self:inc_indent()
self:add_indent_ln('const char* __ctmp = ', val, ';')
self:add_indent_ln('nelua_assert_cstring(__ctmp != NULL, "NULL is cstring while converting to string");')
self:add_indent_ln('size_t __ctmplen = strlen(__ctmp);')
self:add_indent_ln('nelua_string __tmp = (nelua_string)malloc(sizeof(nelua_string_object) + __ctmplen+1);')
self:add_indent_ln('__tmp->len = __ctmplen; __tmp->res = __ctmplen + 1;')
self:add_indent_ln('memcpy(__tmp->data, __ctmp, __ctmplen);')
self:add_indent_ln('__tmp->data[__ctmplen] = 0;')
self:add_indent_ln('__tmp;')
self:dec_indent()
self:add_indent_ln('})')
end

function CEmitter:add_val2type(type, val, valtype)
if not valtype and traits.is_astnode(val) then
valtype = val.attr.type
Expand All @@ -136,6 +153,8 @@ function CEmitter:add_val2type(type, val, valtype)
self:add_any2type(type, val)
elseif type:is_cstring() and valtype:is_string() then
self:add_string2cstring(val)
elseif type:is_string() and valtype:is_cstring() then
self:add_cstring2string(val)
elseif type:is_pointer() and type.subtype == valtype then
-- automatice reference
assert(val and val.attr.autoref)
Expand Down
5 changes: 3 additions & 2 deletions nelua/cgenerator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,9 @@ function visitors.Call(context, node, emitter)
if argnode.attr.type ~= type then
-- type really differs, cast it
emitter:add_ctypecast(type)
emitter:add('(', argnode, ')')
emitter:add('(')
emitter:add_val2type(type, argnode)
emitter:add(')')
else
-- same type, no need to cast
emitter:add(argnode)
Expand All @@ -441,7 +443,6 @@ function visitors.CallMethod(context, node, emitter)

visitor_Call(context, node, emitter, argnodes, callee, isblockcall)


--[[
local name, args, callee, block_call = node:args()
if block_call then
Expand Down
9 changes: 7 additions & 2 deletions nelua/scope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,22 @@ function Scope:add_return_type(index, type)
if not returntypes then
returntypes = {}
self.possible_returntypes[index] = returntypes
elseif tabler.ifind(returntypes, type) then
elseif type and tabler.ifind(returntypes, type) then
return
end
table.insert(returntypes, type)
if type then
table.insert(returntypes, type)
else
self.has_unknown_return = true
end
end

function Scope:resolve_returntypes()
local resolved_returntypes = {}
for i,returntypes in pairs(self.possible_returntypes) do
resolved_returntypes[i] = typedefs.find_common_type(returntypes) or typedefs.primtypes.any
end
resolved_returntypes.has_unknown = self.has_unknown_return
self.resolved_returntypes = resolved_returntypes
return resolved_returntypes
end
Expand Down
1 change: 1 addition & 0 deletions nelua/symdefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local symdefs = {}

symdefs.nilptr = primtypes.Nilptr
symdefs.assert = primtypes.any
symdefs.error = primtypes.any
symdefs.print = primtypes.any

--symdefs.assert = types.FunctionType(nil, {primtypes.boolean, primtypes.auto})
Expand Down

0 comments on commit 9b9140a

Please sign in to comment.