old-trainOS/trainscript/type-operators.cpp.tt

89 lines
1.4 KiB
Text

#include <kernel.h>
#include <console.h>
#include "type.hpp"
#include "variable.hpp"<?
ops = {
{ "add" },
{ "subtract" },
{ "multiply" },
{ "divide" },
{ "modulo" },
{ "lt", "Bool" },
{ "le", "Bool" },
{ "gt", "Bool" },
{ "ge", "Bool" },
{ "eq", "Bool" },
{ "ieq", "Bool" },
}
types = {
Invalid = { }, -- no operators at all, all nil
Int = {
add = "+",
subtract = "-",
multiply = "*",
divide = "/",
modulo = "%",
lt = "<",
le = "<=",
gt = ">",
ge = ">=",
eq = "==",
ieq = "!="
},
Real = {
add = "+",
subtract = "-",
multiply = "*",
divide = "/",
lt = "<",
le = "<=",
gt = ">",
ge = ">=",
eq = "==",
ieq = "!="
},
Bool = {
add = "||",
multiply = "&&",
eq = "==",
ieq = "!="
},
Text = {
add = "+",
eq = "==",
ieq = "!="
}
}
?>
namespace trainscript
{
<? for type,desc in pairs(types) do
for _, _op in ipairs(ops) do
local op = _op[1]
local rt = _op[2] or type
if desc[op] then
local name = type .. "_" .. op;
local sign = desc[op];
?>static Variable <[name]>(const Variable &lhs, const Variable &rhs)
{
return Variable::from<[rt]>(lhs.value<<[type]>>() <[sign]> rhs.value<<[type]>>());
}
<? end
end
end
for type,desc in pairs(types) do ?>
TypeOps <[type]>Operators = {<?
for _, _op in ipairs(ops) do
local op = _op[1] ?>
<?
if desc[op] == nil then
?>nullptr,<?
else
?><[type]>_<[op]>,<?
end
end ?>
};<? end ?>
}