Skip to content

Commit

Permalink
Type builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Jun 26, 2019
1 parent ed5d036 commit fc1b350
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 11 deletions.
18 changes: 18 additions & 0 deletions nelua/cbuiltins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@ function functions.print(context, node)
return funcname
end

function functions.type(context, node, emitter)
local argnode = node[1][1]
local type = argnode.attr.type
context:add_runtime_builtin('type_strings')
local typename
if type:is_numeric() then
typename = 'number'
elseif type:is_nilptr() then
typename = 'pointer'
elseif type:is_any() then
assert(false, 'type for any values not implemented yet')
else
typename = type.name
end
emitter:add('&nelua_typestr_', typename)
return nil
end

function functions.error()
return 'nelua_panic_string'
end
Expand Down
2 changes: 1 addition & 1 deletion nelua/cgenerator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ function visitors.Call(context, node, emitter)
-- same type, no need to cast
emitter:add(argnode)
end
else
elseif callee then
visitor_Call(context, node, emitter, argnodes, callee, isblockcall)
end
end
Expand Down
3 changes: 3 additions & 0 deletions nelua/scope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ function Scope:get_symbol(name, node)
if symtype then
symbol = Symbol(name, node, symtype)
symbol.attr.const = true
if symbol.attr.type:is_function() then
symbol.attr.type.sideeffect = false
end
end
end
if not symbol and node and config.strict and not self.context.preprocessing then
Expand Down
5 changes: 3 additions & 2 deletions nelua/symdefs.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
local typedefs = require 'nelua.typedefs'
--local types = require 'nelua.types'
local types = require 'nelua.types'
local primtypes = typedefs.primtypes

local symdefs = {}

symdefs.nilptr = primtypes.Nilptr
symdefs.assert = primtypes.any
symdefs.error = primtypes.any
symdefs.error = types.FunctionType(nil, {primtypes.string})
symdefs.print = primtypes.any
symdefs.type = types.FunctionType(nil, {primtypes.any}, {primtypes.string})

--symdefs.assert = types.FunctionType(nil, {primtypes.boolean, primtypes.auto})
--symdefs.print = types.FunctionType(nil, {primtypes.varargs})
Expand Down
2 changes: 1 addition & 1 deletion runtime/c/nelua_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void nelua_panic_string(const nelua_string s) {
}
{% if context.builtins['cstring2string'] then %}
nelua_string nelua_cstring2string(const char *s) {
nelua_assert_cstring(s != NULL, "NULL is cstring while converting to string");
nelua_assert_cstring(s != NULL, "NULL cstring while converting to string");
size_t slen = strlen(s);
nelua_string str = (nelua_string)malloc(sizeof(nelua_string_object) + slen+1);
str->len = slen; str->res = slen + 1;
Expand Down
11 changes: 11 additions & 0 deletions runtime/c/nelua_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,14 @@ static inline bool nelua_any_to_boolean(const nelua_any a) {
return true;
}
{% end %}
{% if context.builtins['type_strings'] then %}
static const struct { uintptr_t len, res; char data[4]; } nelua_typestr_nil = {3,3,"nil"};
static const struct { uintptr_t len, res; char data[5]; } nelua_typestr_type = {4,4,"type"};
static const struct { uintptr_t len, res; char data[7]; } nelua_typestr_string = {6,6,"string"};
static const struct { uintptr_t len, res; char data[7]; } nelua_typestr_number = {6,6,"number"};
static const struct { uintptr_t len, res; char data[7]; } nelua_typestr_record = {6,6,"record"};
static const struct { uintptr_t len, res; char data[8]; } nelua_typestr_boolean = {7,7,"boolean"};
static const struct { uintptr_t len, res; char data[8]; } nelua_typestr_integer = {7,7,"integer"};
static const struct { uintptr_t len, res; char data[8]; } nelua_typestr_pointer = {7,7,"pointer"};
static const struct { uintptr_t len, res; char data[9]; } nelua_typestr_function = {8,8,"function"};
{% end %}
22 changes: 20 additions & 2 deletions spec/05-cgenerator_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -858,12 +858,12 @@ it("entrypoint", function()
]], "hello\nwonderful\nworld")
end)

it("print", function()
it("print builtin", function()
assert.run({'-g', 'c', '-e', "print(1,0.2,1e2,0xf,0b01)"},
'1\t0.200000\t100\t15\t1')
end)

it("assert", function()
it("assert builtin", function()
assert.generate_c(
"assert(true)",
"nelua_assert(true)")
Expand All @@ -885,4 +885,22 @@ it("assert", function()
]], "assertion failed!")
end)

it("type builtin", function()
assert.run_c([[
local function f() end
local R = @record{x:integer}
local r: R
assert(r.x == 0)
assert(type('a') == 'string')
assert(type(1) == 'number')
assert(type(false) == 'boolean')
assert(type(f) == 'function')
assert(type(R) == 'type')
assert(type(r) == 'record')
assert(type(&r) == 'pointer')
assert(type(nilptr) == 'pointer')
assert(type(nil) == 'nil')
]])
end)

end)
16 changes: 11 additions & 5 deletions stdlib/os.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ function os.date()
return 'NIY (not implementet yet)'
end

function os.difftime(t1: integer, t2: integer)
return t2 - t1
end

function os.execute(command: string): boolean, string, integer
--TODO: optional string param
--TODO: on error return nil instead of false
Expand All @@ -69,8 +73,8 @@ function os.exit(code: integer)
exit(@cint(code))
end

function os.getenv(varname: string): cstring
return getenv(varname)
function os.getenv(varname: string): string
return @string(getenv(varname))
end

function os.remove(filename: string)
Expand Down Expand Up @@ -122,14 +126,16 @@ end
--------------------------------------------------------------------------------
-- tests

print(os.clock())
assert(os.clock() >= 0)
assert(os.difftime(0,0) == 0 and os.difftime(0,1) == 1)
print(os.date())
print(os.getenv('HOME'))
assert(type(os.getenv('PATH')) == 'string')
print(os.tmpname())
print(os.execute('my_invalid_command'))
print(os.rename('my_invalid_file', 'a'))
print(os.remove('my_invalid_command'))
print(os.setlocale('C','all'))
assert(os.setlocale('C','all') == 'C')
assert(os.setlocale('C','numeric') == 'C')
print(os.time())
os.exit(0)
assert(false)

0 comments on commit fc1b350

Please sign in to comment.