Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Builtin functions #232

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
primitive -> builtin
  • Loading branch information
mascarenhas committed May 18, 2018
commit 72f8f0470125ab460706e86aa2e19f8767653717
2 changes: 1 addition & 1 deletion spec/coder_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2328,7 +2328,7 @@ describe("Titan code generator", function()

end)

describe("#primitives", function()
describe("#builtins", function()
it("call print", function ()
run_coder_app([[
]], [[
Expand Down
4 changes: 2 additions & 2 deletions titan-compiler/ast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ return typedecl("Ast", {
Field = {"loc", "name", "exp"},
},

Primitive = {
PrimitiveFunction = { "name", "_type" }
Builtin = {
BuiltinFunc = { "name", "_type" }
},
})
22 changes: 11 additions & 11 deletions titan-compiler/checker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ checkexp = util.make_visitor({
local ftype = node.exp._type
local fname = expname(node.exp)
local var = node.exp.var
if not (var and var._decl and (var._decl._tag == "Ast.PrimitiveFunction" or
if not (var and var._decl and (var._decl._tag == "Ast.BuiltinFunc" or
var._decl._tag == "Ast.TopLevelFunc" or
var._decl._tag == "Type.ModuleMember" or
var._decl._tag == "Type.StaticMethod")) then
Expand Down Expand Up @@ -1533,29 +1533,29 @@ local function makemoduletype(modname, modast)
return types.Module(modname, members)
end

local function add_primitives(st)
local function add_builtins(st)
st:add_symbol("print",
ast.PrimitiveFunction("print", types.Function({}, { types.Nil() }, types.Value())))
ast.BuiltinFunc("print", types.Function({}, { types.Nil() }, types.Value())))
st:add_symbol("assert",
ast.PrimitiveFunction("assert",
ast.BuiltinFunc("assert",
types.Function({ types.Value(), types.String() }, { types.Value() }, false)))
st:add_symbol("dofile",
ast.PrimitiveFunction("dofile",
ast.BuiltinFunc("dofile",
types.Function({ types.String() }, { types.Array(types.Value()) }, types.Value())))
st:add_symbol("error",
ast.PrimitiveFunction("error",
ast.BuiltinFunc("error",
types.Function({ types.String() }, { types.Nil() }, false)))
st:add_symbol("dostring",
ast.PrimitiveFunction("dostring",
ast.BuiltinFunc("dostring",
types.Function({ types.String() }, { types.Array(types.Value()) }, types.Value())))
st:add_symbol("tostring",
ast.PrimitiveFunction("tostring",
ast.BuiltinFunc("tostring",
types.Function({ types.Value() }, { types.String() }, false)))
st:add_symbol("tofloat",
ast.PrimitiveFunction("tofloat",
ast.BuiltinFunc("tofloat",
types.Function({ types.String() }, { types.Float() }, false)))
st:add_symbol("tointeger",
ast.PrimitiveFunction("tointeger",
ast.BuiltinFunc("tointeger",
types.Function({ types.String() }, { types.Integer() }, false)))
end

Expand All @@ -1575,7 +1575,7 @@ function checker.check(modname, ast, subject, filename, loader)
return nil, "you must pass a loader to import modules"
end
local st = symtab.new(modname)
add_primitives(st)
add_builtins(st)
local errors = {subject = subject, filename = filename}
checktoplevel(ast, st, errors, loader)
checkbodies(ast, st, errors)
Expand Down
6 changes: 3 additions & 3 deletions titan-compiler/coder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ local function func_name(modname, name)
return modprefix(modname) .. name .. "_titan"
end

local function primitive_name(name)
local function builtin_name(name)
return "titan_" .. name
end

Expand Down Expand Up @@ -1067,8 +1067,8 @@ local function codecall(ctx, node)
local ftype = fnode._type
if node.args._tag == "Ast.ArgsFunc" then
if fnode._tag == "Ast.VarName" then
if fnode._decl._tag == "Ast.PrimitiveFunction" then
fname = primitive_name(fnode._decl.name)
if fnode._decl._tag == "Ast.BuiltinFunc" then
fname = builtin_name(fnode._decl.name)
else
assert(fnode._decl._tag == "Ast.TopLevelFunc")
fname = func_name(ctx.module, fnode.name)
Expand Down
2 changes: 1 addition & 1 deletion titan-runtime/titan.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ const TValue *getgeneric (Table *t, const TValue *key) {
}
}

/* Primitives */
/* Builtins */
LUAI_FUNC int titan_print(lua_State *L, int nargs, ...) {
if(nargs > 0) {
lua_checkstack(L, 3);
Expand Down
2 changes: 1 addition & 1 deletion titan-runtime/titan.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ LUAI_FUNC void *loadlib (lua_State *L, const char *file);
LUAI_FUNC void *loadsym (lua_State *L, void *lib, const char *sym);
LUAI_FUNC const TValue *getgeneric (Table *t, const TValue *key);

/* Primitives */
/* Builtins */
LUAI_FUNC int titan_print(lua_State *L, int nargs, ...);
LUAI_FUNC TValue titan_assert(lua_State *L, TValue cond, TString *msg);
LUAI_FUNC Table *titan_dofile(lua_State *L, TString *fname, int nargs, ...);
Expand Down