Skip to content

Commit 7813944

Browse files
committed
builtin -> foreign
1 parent 70340c9 commit 7813944

File tree

12 files changed

+75
-76
lines changed

12 files changed

+75
-76
lines changed

doc/manual.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ returns the corresponding integer value, otherwise returns `0`.
462462

463463
Here is the complete syntax of Titan in extended BNF. As usual in extended BNF, {A} means 0 or more As, and \[A\] means an optional A.
464464

465-
program ::= {tlfunc | tlvar | tlrecord | tlimport | tlforeignimport | tlbuiltin}
465+
program ::= {tlfunc | tlvar | tlrecord | tlimport | tlforeignimport | tlforeignfunc}
466466

467467
tlfunc ::= [local] function Name '(' [parlist] ')' ':' type block end
468468

@@ -474,7 +474,7 @@ Here is the complete syntax of Titan in extended BNF. As usual in extended BNF,
474474

475475
tlforeignimport ::= [local Name '='] foreign import LiteralString
476476

477-
tlbuiltin ::= builtin function Name '(' [parlist] ')' ':' type
477+
tlforeignfunc ::= foreign function Name '(' [parlist] ')' ':' type
478478

479479
param ::= Name ':' type | '...' ':' type
480480

spec/checker_spec.lua

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,7 +1635,7 @@ describe("Titan type checker", function()
16351635
end
16361636
]])
16371637
assert_type_error("access a function", [[
1638-
builtin function bar()
1638+
foreign function bar()
16391639
function foo(): integer
16401640
return bar
16411641
end
@@ -1649,7 +1649,7 @@ describe("Titan type checker", function()
16491649
end
16501650
]])
16511651
assert_type_error("assign to a function",[[
1652-
builtin function bar()
1652+
foreign function bar()
16531653
function foo(): integer
16541654
bar = 2
16551655
end
@@ -1675,10 +1675,10 @@ describe("Titan type checker", function()
16751675
assert.match("access a function", err)
16761676
end)
16771677

1678-
it("catches use of external builtin function as first-class value", function ()
1678+
it("catches use of external foreign function as first-class value", function ()
16791679
local modules = {
16801680
foo = [[
1681-
builtin function foo()
1681+
foreign function foo()
16821682
]],
16831683
bar = [[
16841684
local foo = import "foo"
@@ -1711,10 +1711,10 @@ describe("Titan type checker", function()
17111711
assert.match("assign to a function", err)
17121712
end)
17131713

1714-
it("catches assignment to external builtin function", function ()
1714+
it("catches assignment to external foreign function", function ()
17151715
local modules = {
17161716
foo = [[
1717-
builtin function foo()
1717+
foreign function foo()
17181718
]],
17191719
bar = [[
17201720
local foo = import "foo"
@@ -1951,23 +1951,23 @@ describe("Titan type checker", function()
19511951
function g(): (integer, integer)
19521952
return 20, 30
19531953
end
1954-
builtin function f(x: integer, y: integer, z: integer)
1954+
foreign function f(x: integer, y: integer, z: integer)
19551955
function h()
19561956
f(g())
19571957
end
19581958
]], args = 2, params = 3 }, { code = [[
19591959
function g(): (integer, integer)
19601960
return 20, 30
19611961
end
1962-
builtin function f(x: integer, y: integer, z: integer)
1962+
foreign function f(x: integer, y: integer, z: integer)
19631963
function h()
19641964
f(20, 30, g())
19651965
end
19661966
]], args = 4, params = 3 }, { code = [[
19671967
function g(): (integer, integer)
19681968
return 20, 30
19691969
end
1970-
builtin function f(x: integer, y: integer, z: integer)
1970+
foreign function f(x: integer, y: integer, z: integer)
19711971
function h()
19721972
f(20, (g()))
19731973
end
@@ -1979,15 +1979,15 @@ describe("Titan type checker", function()
19791979
end
19801980
end)
19811981

1982-
it("vararg builtins and functions", function ()
1982+
it("vararg foreigns and functions", function ()
19831983
assert_type_check([[
1984-
builtin function f(a: string, ...: float)
1984+
foreign function f(a: string, ...: float)
19851985
function g()
19861986
f('foo', 1, 2.5)
19871987
end
19881988
]])
19891989
assert_type_error("only the last parameter", [[
1990-
builtin function f(...: float, a: string)
1990+
foreign function f(...: float, a: string)
19911991
]])
19921992
assert_type_error("only the last parameter", [[
19931993
function f(...: float, a: string)
@@ -1999,7 +1999,7 @@ describe("Titan type checker", function()
19991999
end
20002000
]])
20012001
assert_type_error("expected float but found string", [[
2002-
builtin function f(a: string, ...: float)
2002+
foreign function f(a: string, ...: float)
20032003
function g()
20042004
f('foo', 1, 'bar')
20052005
end

spec/coder_spec.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,7 +2304,7 @@ describe("Titan code generator", function()
23042304

23052305
end)
23062306

2307-
describe("#builtins", function()
2307+
describe("#foreigns", function()
23082308
it("call print", function ()
23092309
run_coder_app([[
23102310
]], [[
@@ -2412,10 +2412,10 @@ describe("Titan code generator", function()
24122412
]])
24132413
end)
24142414

2415-
it("calls builtin from another module", function ()
2415+
it("calls foreign from another module", function ()
24162416
local modules = {
24172417
string = [[
2418-
builtin function byte(s: string, i: integer): integer
2418+
foreign function byte(s: string, i: integer): integer
24192419
]],
24202420
bar = [[
24212421
import "string"

spec/parser_spec.lua

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,28 +1185,28 @@ describe("Titan parser", function()
11851185
assert_expression_syntax_error([[ foo as ]], "CastMissingType")
11861186

11871187
assert_program_syntax_error([[
1188-
builtin funtion foo (a:int, ) : int
1189-
]], "BuiltinFunc")
1188+
foreign funtion foo (a:int, ) : int
1189+
]], "ForeignFunc")
11901190

11911191
assert_program_ast([[
1192-
builtin function print(...: value)
1193-
builtin function assert(val: value, msg: string): value
1194-
builtin function dofile(fname: string, ...: value): {value}
1195-
builtin function dostring(fname: string, ...: value): {value}
1196-
builtin function error(msg: string)
1197-
builtin function tostring(val: value): string
1198-
builtin function tofloat(val: string): float
1199-
builtin function tointeger(val: string): integer
1192+
foreign function print(...: value)
1193+
foreign function assert(val: value, msg: string): value
1194+
foreign function dofile(fname: string, ...: value): {value}
1195+
foreign function dostring(fname: string, ...: value): {value}
1196+
foreign function error(msg: string)
1197+
foreign function tostring(val: value): string
1198+
foreign function tofloat(val: string): float
1199+
foreign function tointeger(val: string): integer
12001200
]], {
1201-
{ _tag = "Ast.TopLevelBuiltin", name = "print", params = { {_tag = "Ast.Vararg"} }},
1202-
{ _tag = "Ast.TopLevelBuiltin", name = "assert" },
1203-
{ _tag = "Ast.TopLevelBuiltin", name = "dofile", params = { {_tag = "Ast.Decl"}, {_tag = "Ast.Vararg"} },
1201+
{ _tag = "Ast.TopLevelForeignFunc", name = "print", params = { {_tag = "Ast.Vararg"} }},
1202+
{ _tag = "Ast.TopLevelForeignFunc", name = "assert" },
1203+
{ _tag = "Ast.TopLevelForeignFunc", name = "dofile", params = { {_tag = "Ast.Decl"}, {_tag = "Ast.Vararg"} },
12041204
rettypes = { {_tag = "Ast.TypeArray"} } },
1205-
{ _tag = "Ast.TopLevelBuiltin", name = "dostring" },
1206-
{ _tag = "Ast.TopLevelBuiltin", name = "error" },
1207-
{ _tag = "Ast.TopLevelBuiltin", name = "tostring" },
1208-
{ _tag = "Ast.TopLevelBuiltin", name = "tofloat" },
1209-
{ _tag = "Ast.TopLevelBuiltin", name = "tointeger" },
1205+
{ _tag = "Ast.TopLevelForeignFunc", name = "dostring" },
1206+
{ _tag = "Ast.TopLevelForeignFunc", name = "error" },
1207+
{ _tag = "Ast.TopLevelForeignFunc", name = "tostring" },
1208+
{ _tag = "Ast.TopLevelForeignFunc", name = "tofloat" },
1209+
{ _tag = "Ast.TopLevelForeignFunc", name = "tointeger" },
12101210
})
12111211
end)
12121212
end)

titan-compiler/ast.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ return typedecl("Ast", {
2323
TopLevelRecord = {"loc", "name", "fields"},
2424
TopLevelImport = {"loc", "localname", "modname"},
2525
TopLevelForeignImport = {"loc", "localname", "headername"},
26-
TopLevelBuiltin = {"loc", "name", "params", "rettypes"},
26+
TopLevelForeignFunc = {"loc", "name", "params", "rettypes"},
2727
},
2828

2929
Decl = {

titan-compiler/checker.lua

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ checkstat = util.make_visitor({
331331
local texp = var._type
332332
if texp._tag == "Type.Module" then
333333
checker.typeerror(errors, var.loc, "trying to assign to a module")
334-
elseif texp._tag == "Type.Function" or texp._tag == "Type.BuiltinFunction" then
334+
elseif texp._tag == "Type.Function" or texp._tag == "Type.ForeignFunction" then
335335
checker.typeerror(errors, var.loc, "trying to assign to a function")
336336
else
337337
-- mark this declared variable as assigned to
@@ -768,7 +768,7 @@ checkexp = util.make_visitor({
768768
"trying to access module '%s' as a first-class value",
769769
node.var.name)
770770
node._type = types.Invalid()
771-
elseif texp._tag == "Type.Function" or texp._tag == "Type.BuiltinFunction" then
771+
elseif texp._tag == "Type.Function" or texp._tag == "Type.ForeignFunction" then
772772
checker.typeerror(errors, node.loc,
773773
"trying to access a function as a first-class value")
774774
node._type = types.Invalid()
@@ -1032,14 +1032,14 @@ checkexp = util.make_visitor({
10321032
local fname = expname(node.exp)
10331033
local var = node.exp.var
10341034
if not (var and var._decl and
1035-
(var._decl._tag == "Ast.TopLevelBuiltin" or
1035+
(var._decl._tag == "Ast.TopLevelForeignFunc" or
10361036
var._decl._tag == "Ast.TopLevelFunc" or
10371037
var._decl._tag == "Type.ModuleMember" or
10381038
var._decl._tag == "Type.StaticMethod")) then
10391039
checker.typeerror(errors, node.loc,
10401040
"first-class functions are not supported in this version of Titan")
10411041
end
1042-
if ftype._tag ~= "Type.Function" and ftype._tag ~= "Type.BuiltinFunction" then
1042+
if ftype._tag ~= "Type.Function" and ftype._tag ~= "Type.ForeignFunction" then
10431043
checker.typeerror(errors, node.loc,
10441044
"'%s' is not a function but %s",
10451045
fname, types.tostring(ftype))
@@ -1257,7 +1257,7 @@ local function toplevel_name(node)
12571257
return node.decl.name
12581258
elseif tag == "Ast.TopLevelFunc" or
12591259
tag == "Ast.TopLevelRecord" or
1260-
tag == "Ast.TopLevelBuiltin" then
1260+
tag == "Ast.TopLevelForeignFunc" then
12611261
return node.name
12621262
elseif tag == "Ast.TopLevelMethod" or tag == "Ast.TopLevelStatic" then
12631263
return nil
@@ -1490,7 +1490,7 @@ local toplevel_visitor = util.make_visitor({
14901490
rectype.methods[node.name] = node._type
14911491
end,
14921492

1493-
["Ast.TopLevelBuiltin"] = function(node, st, errors)
1493+
["Ast.TopLevelForeignFunc"] = function(node, st, errors)
14941494
local ptypes = {}
14951495
local vararg = false
14961496
for i, pdecl in ipairs(node.params) do
@@ -1511,7 +1511,7 @@ local toplevel_visitor = util.make_visitor({
15111511
table.insert(rettypes, typefromnode(rt, st, errors))
15121512
end
15131513
local fname = st and st.modname .. "." .. node.name or node.name
1514-
node._type = types.BuiltinFunction(fname,
1514+
node._type = types.ForeignFunction(fname,
15151515
ptypes, rettypes, vararg)
15161516
return node
15171517
end,
@@ -1579,7 +1579,7 @@ local function makemoduletype(modname, modast)
15791579
local tag = tlnode._tag
15801580
if tag == "Ast.TopLevelFunc" or
15811581
tag == "Ast.TopLevelRecord" or
1582-
tag == "Ast.TopLevelBuiltin" then
1582+
tag == "Ast.TopLevelForeignFunc" then
15831583
members[tlnode.name] = types.ModuleMember(modname, tlnode.name, tlnode._type)
15841584
elseif tag == "Ast.TopLevelVar" then
15851585
members[tlnode.decl.name] = types.ModuleMember(modname, tlnode.decl.name, tlnode._type)
@@ -1589,16 +1589,16 @@ local function makemoduletype(modname, modast)
15891589
return types.Module(modname, members)
15901590
end
15911591

1592-
local function add_builtins(st)
1593-
local nodes = parser.parse("builtin.titan", [[
1594-
builtin function print(...: value)
1595-
builtin function assert(val: value, msg: string): value
1596-
builtin function dofile(fname: string, ...: value): {value}
1597-
builtin function dostring(fname: string, ...: value): {value}
1598-
builtin function error(msg: string)
1599-
builtin function tostring(val: value): string
1600-
builtin function tofloat(val: string): float
1601-
builtin function tointeger(val: string): integer
1592+
local function add_foreigns(st)
1593+
local nodes = parser.parse("foreign.titan", [[
1594+
foreign function print(...: value)
1595+
foreign function assert(val: value, msg: string): value
1596+
foreign function dofile(fname: string, ...: value): {value}
1597+
foreign function dostring(fname: string, ...: value): {value}
1598+
foreign function error(msg: string)
1599+
foreign function tostring(val: value): string
1600+
foreign function tofloat(val: string): float
1601+
foreign function tointeger(val: string): integer
16021602
]])
16031603
for _, node in ipairs(nodes) do
16041604
st:add_symbol(node.name, toplevel_visitor(node))
@@ -1621,7 +1621,7 @@ function checker.check(modname, ast, subject, filename, loader)
16211621
return nil, "you must pass a loader to import modules"
16221622
end
16231623
local st = symtab.new(modname)
1624-
add_builtins(st)
1624+
add_foreigns(st)
16251625
local errors = {subject = subject, filename = filename}
16261626
checktoplevel(ast, st, errors, loader)
16271627
checkbodies(ast, st, errors)

titan-compiler/coder.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ local function func_name(modname, name)
108108
return modprefix(modname) .. name .. "_titan"
109109
end
110110

111-
local function builtin_name(name)
111+
local function foreign_name(name)
112112
return "titan_" .. mangle_qn(name)
113113
end
114114

@@ -1039,10 +1039,10 @@ local function codecall(ctx, node)
10391039
local ftype = fnode._type
10401040
if node.args._tag == "Ast.ArgsFunc" then
10411041
if fnode._tag == "Ast.VarName" then
1042-
if ftype._tag == "Type.BuiltinFunction" then
1043-
fname = builtin_name(ftype.name)
1042+
if ftype._tag == "Type.ForeignFunction" then
1043+
fname = foreign_name(ftype.name)
10441044
ctx.functions[ftype.name] = {
1045-
mangled = builtin_name(ftype.name),
1045+
mangled = foreign_name(ftype.name),
10461046
type = ftype
10471047
}
10481048
else
@@ -1064,10 +1064,10 @@ local function codecall(ctx, node)
10641064
mangled = fname
10651065
}
10661066
end
1067-
elseif ftype._tag == "Type.BuiltinFunction" then
1068-
fname = builtin_name(ftype.name)
1067+
elseif ftype._tag == "Type.ForeignFunction" then
1068+
fname = foreign_name(ftype.name)
10691069
ctx.functions[ftype.name] = {
1070-
mangled = builtin_name(ftype.name),
1070+
mangled = foreign_name(ftype.name),
10711071
type = ftype
10721072
}
10731073
else
@@ -2446,7 +2446,7 @@ local function init_data_from_other_modules(tlcontext, includes, initmods, mpref
24462446

24472447
for name, func in pairs(tlcontext.functions) do
24482448
local fname = func.mangled
2449-
if func.type._tag == "Type.BuiltinFunction" then
2449+
if func.type._tag == "Type.ForeignFunction" then
24502450
table.insert(includes, "extern " .. function_sig(fname, func.type) .. ";")
24512451
else
24522452
table.insert(includes, storage_class .. " " .. function_sig(fname, func.type, is_dynamic) .. ";")

titan-compiler/lexer.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ local keywords = {
165165
"and", "break", "do", "else", "elseif", "end", "for", "false",
166166
"function", "goto", "if", "in", "local", "nil", "not", "or",
167167
"repeat", "return", "then", "true", "until", "while", "import",
168-
"record", "as", "foreign", "builtin"
168+
"record", "as", "foreign"
169169
}
170170

171171
local type_keywords = {

titan-compiler/parser.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ local grammar = re.compile([[
235235
/ toplevelfunc
236236
/ toplevelvar
237237
/ toplevelrecord
238-
/ toplevelbuiltin
238+
/ toplevelforeign
239239
/ import
240240
/ shortimport
241241
/ foreign
@@ -249,9 +249,9 @@ local grammar = re.compile([[
249249
LPAREN^LParPList paramlist RPAREN^RParPList
250250
rettypeopt block END^EndFunc) -> TopLevelFunc
251251
252-
toplevelbuiltin <- (P BUILTIN FUNCTION^BuiltinFunc NAME^NameFunc
252+
toplevelforeign <- (P FOREIGN FUNCTION^ForeignFunc NAME^NameFunc
253253
LPAREN^LParPList paramlist RPAREN^RParPList
254-
rettypeopt) -> TopLevelBuiltin
254+
rettypeopt) -> TopLevelForeignFunc
255255
256256
toplevelvar <- (P localopt decl ASSIGN^AssignVar
257257
!(IMPORT / FOREIGN)
@@ -483,7 +483,6 @@ local grammar = re.compile([[
483483
IMPORT <- %IMPORT SKIP*
484484
AS <- %AS SKIP*
485485
FOREIGN <- %FOREIGN SKIP*
486-
BUILTIN <- %BUILTIN SKIP*
487486
488487
BOOLEAN <- %BOOLEAN SKIP*
489488
INTEGER <- %INTEGER SKIP*

titan-compiler/syntax_errors.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ local errors = {
169169

170170
NameMethod = "Expected a method name after the colon.",
171171

172-
BuiltinFunc = "Expected a builtin function declaration.",
172+
ForeignFunc = "Expected a foreign function declaration.",
173173
}
174174

175175
syntax_errors.errors = errors

0 commit comments

Comments
 (0)