Renamed compiler -> nomsu for concision and clarity.
This commit is contained in:
parent
6882862d0f
commit
e4660b169c
@ -12,13 +12,13 @@ with secrets:
|
||||
| add=function(self, key) self[key] = self:by_name(key) end}
|
||||
|})
|
||||
rule [find user %name] =:
|
||||
lua expr "secrets.users:by_name(vars.name) or compiler:error('Failed to find user: '..tostring(vars.name))"
|
||||
lua expr "secrets.users:by_name(vars.name) or nomsu:error('Failed to find user: '..tostring(vars.name))"
|
||||
rule [add user %name] =:
|
||||
lua expr "secrets.users:add(vars.name)"
|
||||
macro [@%name_block] =:
|
||||
%name_str =: lua expr "vars.name_block.value.value.src"
|
||||
".."
|
||||
|compiler:call("find user %", \repr %name_str\)
|
||||
|nomsu:call("find user %", \repr %name_str\)
|
||||
rule [everybody, everyone] =:
|
||||
(%entry's "key") for %entry in (entries in (secret %users))
|
||||
|
||||
@ -74,15 +74,15 @@ with secrets:
|
||||
"inventory", "%'s inventory", "%'s stock of %", "%'s stock of % as str"
|
||||
|
||||
rule [you] =:
|
||||
lua expr "(compiler.you or 'Anonymous')"
|
||||
lua expr "(nomsu.you or 'Anonymous')"
|
||||
|
||||
rule [make %person %action, make %person do %action] =:
|
||||
lua block ".."
|
||||
|do
|
||||
| local old_you = compiler.you
|
||||
| compiler.you = vars.person
|
||||
| ret = vars.action(compiler, vars)
|
||||
| compiler.you = old_you
|
||||
| local old_you = nomsu.you
|
||||
| nomsu.you = vars.person
|
||||
| ret = vars.action(nomsu, vars)
|
||||
| nomsu.you = old_you
|
||||
|end
|
||||
|
||||
say "===================================================="
|
||||
@ -106,7 +106,7 @@ with secrets:
|
||||
macro block [propose %action] =:
|
||||
%source =: source code from tree %action
|
||||
".."
|
||||
|compiler:call("propose source %", \repr %source\)
|
||||
|nomsu:call("propose source %", \repr %source\)
|
||||
|
||||
rule [with everyone's approval do %action] =:
|
||||
run %action
|
||||
|
@ -259,7 +259,7 @@ say the time
|
||||
say ".."|Math expression result is: \lua expr "(1 + 2*3 + 3*4)^2"\
|
||||
|
||||
#.. In the lua environment, "vars" can be used to get local variables/function args, and
|
||||
"compiler" can be used to access the compiler, function defs, and other things
|
||||
"nomsu" can be used to access the compiler, function defs, and other things
|
||||
rule [square root of %n] =:
|
||||
lua expr "math.sqrt(vars.n)"
|
||||
say ".."|The square root of 2 is \square root of 2\
|
||||
|
@ -15,12 +15,12 @@ macro [..]
|
||||
%index st to last in %list, %index nd to last in %list, %index rd to last in %list
|
||||
%index th to last in %list
|
||||
..=:
|
||||
".."|compiler.utils.nth_to_last(\%list as lua\, \%index as lua\)
|
||||
".."|nomsu.utils.nth_to_last(\%list as lua\, \%index as lua\)
|
||||
|
||||
macro [first in %list, first %list] =:
|
||||
".."|\%list as lua\[1]
|
||||
macro [last in %list, last %list] =:
|
||||
".."|compiler.utils.nth_to_last(\%list as lua\, 1)
|
||||
".."|nomsu.utils.nth_to_last(\%list as lua\, 1)
|
||||
|
||||
# Dict iteration convenience function. This could also be accomplished with: for all (entries in %dict): ...
|
||||
macro block [for %key -> %value in %dict %body] =:
|
||||
|
@ -21,14 +21,14 @@ macro block [return %return-value] =: ".."
|
||||
|return \%return-value as lua\
|
||||
|
||||
macro [do %action] =: ".."
|
||||
|(\%action as lua\)(compiler, setmetatable({}, {__index=vars}))
|
||||
|(\%action as lua\)(nomsu, setmetatable({}, {__index=vars}))
|
||||
|
||||
|
||||
# GOTOs
|
||||
macro statement [-> %label] =: ".."
|
||||
|::label_\compiler "var_to_lua_identifier" [%label]\::
|
||||
|::label_\nomsu "var_to_lua_identifier" [%label]\::
|
||||
macro statement [go to %label] =: ".."
|
||||
|goto label_\compiler "var_to_lua_identifier" [%label]\
|
||||
|goto label_\nomsu "var_to_lua_identifier" [%label]\
|
||||
|
||||
# Loop control flow
|
||||
macro statement [break] =: "break"
|
||||
@ -38,7 +38,7 @@ macro statement [break repeat] =: "goto break_repeat"
|
||||
macro statement [break repeat-until] =: "goto break_repeat_until"
|
||||
macro statement [break repeat-while] =: "goto break_repeat_while"
|
||||
macro statement [break %var, stop getting %var, no more %var] =: ".."
|
||||
|goto break_\compiler "var_to_lua_identifier" [%var]\
|
||||
|goto break_\nomsu "var_to_lua_identifier" [%var]\
|
||||
|
||||
macro statement [continue] =: "continue"
|
||||
macro statement [continue for] =: "goto continue_for"
|
||||
@ -47,7 +47,7 @@ macro statement [continue repeat] =: "goto continue_repeat"
|
||||
macro statement [continue repeat-until] =: "goto continue_repeat_until"
|
||||
macro statement [continue repeat-while] =: "goto continue_repeat_while"
|
||||
macro statement [continue %var, go to next %var, on to the next %var] =: ".."
|
||||
|goto continue_\compiler "var_to_lua_identifier" [%var]\
|
||||
|goto continue_\nomsu "var_to_lua_identifier" [%var]\
|
||||
|
||||
# While loops
|
||||
macro block [repeat %body] =:
|
||||
@ -80,10 +80,10 @@ macro block [for %var in %iterable %body] =:
|
||||
| \%var as lua\ = value
|
||||
| \(lua expr "vars.body.value") as lua\
|
||||
| ::continue_for::
|
||||
| ::continue_\compiler "var_to_lua_identifier" [%var]\::
|
||||
| ::continue_\nomsu "var_to_lua_identifier" [%var]\::
|
||||
|end
|
||||
|::break_for::
|
||||
|::break_\compiler "var_to_lua_identifier" [%var]\::
|
||||
|::break_\nomsu "var_to_lua_identifier" [%var]\::
|
||||
|
||||
macro block [for all %iterable %body] =:
|
||||
pass # TODO: fix compiler bug
|
||||
@ -171,7 +171,7 @@ macro block [when %branch-value %body] =:
|
||||
["value", %condition_bits]
|
||||
%result join=: ".."
|
||||
|
|
||||
|if compiler.utils.equivalent(branch_condition, \%condition as lua\) then
|
||||
|if nomsu.utils.equivalent(branch_condition, \%condition as lua\) then
|
||||
| \(lua expr "vars.thunk.value") as lua\
|
||||
| goto finished_when
|
||||
|end
|
||||
|
@ -6,53 +6,52 @@
|
||||
|
||||
lua block ".."
|
||||
|local function make_fn(wrap_in_block)
|
||||
| return function(compiler, vars, kind)
|
||||
| return function(nomsu, vars, kind)
|
||||
# Do a minimal amount of pre-processing (get the aliases and the source)
|
||||
| local aliases = compiler:repr(compiler:get_aliases(vars.macro_def))
|
||||
| local src = compiler:repr(vars.user_macro.src)
|
||||
| local user_macro = compiler:tree_to_lua(vars.user_macro)
|
||||
| local aliases = nomsu:repr(nomsu:get_aliases(vars.macro_def))
|
||||
| local src = nomsu:repr(vars.user_macro.src)
|
||||
| local user_macro = nomsu:tree_to_lua(vars.user_macro)
|
||||
# Then produce a block of code that creates the macro at runtime
|
||||
| local lua = [[
|
||||
| compiler:defmacro(%s, (function(compiler, vars, kind)
|
||||
| if kind == "Expression" then
|
||||
| compiler:error("Macro "..%s.." was defined to be a block, but is being used as an expression")
|
||||
| end
|
||||
| local user_macro = %s
|
||||
| local lua = user_macro(compiler, vars)
|
||||
| %s
|
||||
| return lua, true
|
||||
| end), %s)
|
||||
| ]]
|
||||
| lua = lua:format(aliases, compiler:repr(aliases), user_macro,
|
||||
|nomsu:defmacro(%s, (function(nomsu, vars, kind)
|
||||
| if kind == "Expression" then
|
||||
| nomsu:error("Macro "..%s.." was defined to be a block, but is being used as an expression")
|
||||
| end
|
||||
| local user_macro = %s
|
||||
| local lua = user_macro(nomsu, vars)
|
||||
| %s
|
||||
| return lua, true
|
||||
|end), %s)]]
|
||||
| lua = lua:format(aliases, nomsu:repr(aliases), user_macro,
|
||||
| wrap_in_block and [[lua = "do\\n "..lua.."\\nend"]] or "", src)
|
||||
| return lua, true
|
||||
| end
|
||||
|end
|
||||
|compiler:defmacro("macro statement %macro_def = %user_macro", make_fn(false), "see:lib/metaprogramming.nom")
|
||||
|compiler:defmacro("macro block %macro_def = %user_macro", make_fn(true), "see:lib/metaprogramming.nom")
|
||||
|nomsu:defmacro("macro statement %macro_def = %user_macro", make_fn(false), "see:lib/metaprogramming.nom")
|
||||
|nomsu:defmacro("macro block %macro_def = %user_macro", make_fn(true), "see:lib/metaprogramming.nom")
|
||||
|
||||
macro statement [macro %macro_def = %user_macro] =:
|
||||
".."|compiler:defmacro(
|
||||
| \lua expr "compiler:get_aliases(vars.macro_def)"\,
|
||||
| \lua expr "compiler:tree_to_lua(vars.user_macro)"\,
|
||||
| \lua expr "compiler:repr(vars.user_macro.src)"\)
|
||||
".."|nomsu:defmacro(
|
||||
| \lua expr "nomsu:get_aliases(vars.macro_def)"\,
|
||||
| \lua expr "nomsu:tree_to_lua(vars.user_macro)"\,
|
||||
| \lua expr "nomsu:repr(vars.user_macro.src)"\)
|
||||
|
||||
macro [compiler] =: "compiler"
|
||||
macro [compiler's %key] =: ".."|compiler[\%key as lua\]
|
||||
macro [compiler %method %args] =:
|
||||
macro [nomsu] =: "nomsu"
|
||||
macro [nomsu's %key] =: ".."|nomsu[\%key as lua\]
|
||||
macro [nomsu %method %args] =:
|
||||
lua block ".."
|
||||
|local args = {"compiler"}
|
||||
|local args = {"nomsu"}
|
||||
|for _,arg in ipairs(vars.args.value) do
|
||||
| table.insert(args, compiler:tree_to_lua(arg))
|
||||
| table.insert(args, nomsu:tree_to_lua(arg))
|
||||
|end
|
||||
|return "compiler["..compiler:repr(compiler:tree_to_value(vars.method, vars)).."]("..table.concat(args, ", ")..")"
|
||||
macro [compiler utils %method %args] =:
|
||||
|return "nomsu["..nomsu:repr(nomsu:tree_to_value(vars.method, vars)).."]("..table.concat(args, ", ")..")"
|
||||
macro [nomsu utils %method %args] =:
|
||||
lua block ".."
|
||||
|local args = {}
|
||||
|for i,arg in ipairs(vars.args.value) do
|
||||
| args[i] = compiler:tree_to_lua(arg)
|
||||
| args[i] = nomsu:tree_to_lua(arg)
|
||||
|end
|
||||
|return "compiler.utils["..compiler:repr(compiler:tree_to_value(vars.method, vars)).."]("..table.concat(args, ", ")..")"
|
||||
|return "nomsu.utils["..nomsu:repr(nomsu:tree_to_value(vars.method, vars)).."]("..table.concat(args, ", ")..")"
|
||||
|
||||
# Macro that lets you make new rules
|
||||
#..
|
||||
@ -61,51 +60,51 @@ macro [compiler utils %method %args] =:
|
||||
is currently in the middle of being defined). Being a macro also allows us to snatch
|
||||
the source code and store that
|
||||
macro statement [rule %rule_def = %body] =: ".."
|
||||
|compiler:def(
|
||||
| \compiler "repr" [compiler "get_aliases" [%rule_def]]\,
|
||||
| \compiler "tree_to_lua" [%body]\,
|
||||
| \compiler "repr" [lua expr "vars.body.src"]\)
|
||||
|nomsu:def(
|
||||
| \nomsu "repr" [nomsu "get_aliases" [%rule_def]]\,
|
||||
| \nomsu "tree_to_lua" [%body]\,
|
||||
| \nomsu "repr" [lua expr "vars.body.src"]\)
|
||||
|
||||
rule [fart]=: lua block "print('poot')"
|
||||
|
||||
macro block [alias %aliases = %aliased] =:
|
||||
lua block ".."
|
||||
|local aliases = compiler:get_aliases(vars.aliases)
|
||||
|aliases = compiler:repr(aliases)
|
||||
|local aliases = nomsu:get_aliases(vars.aliases)
|
||||
|aliases = nomsu:repr(aliases)
|
||||
|if vars.aliased.type ~= "Thunk" then
|
||||
| compiler:error("Right hand side of alias % = % should be a Thunk, but got "..vars.aliased.type
|
||||
| nomsu:error("Right hand side of alias % = % should be a Thunk, but got "..vars.aliased.type
|
||||
| ..". Maybe you used = instead of =: by mistake?")
|
||||
|end
|
||||
|local aliased = next(compiler:get_aliases(vars.aliased.value))
|
||||
|aliased = compiler:repr(aliased)
|
||||
|local aliased = next(nomsu:get_aliases(vars.aliased.value))
|
||||
|aliased = nomsu:repr(aliased)
|
||||
|local lua = ([[
|
||||
|compiler:add_aliases(%s, compiler.defs[%s])
|
||||
|nomsu:add_aliases(%s, nomsu.defs[%s])
|
||||
|]]):format(aliases, aliased)
|
||||
|return lua
|
||||
|
||||
# Get the source code for a function
|
||||
rule [help %rule] =:
|
||||
lua block ".."
|
||||
|local fn_def = compiler:get_fn_def(vars.rule)
|
||||
|local fn_def = nomsu:get_fn_def(vars.rule)
|
||||
|if not fn_def then
|
||||
| compiler:writeln("Rule not found: "..compiler:repr(vars.rule))
|
||||
| nomsu:writeln("Rule not found: "..nomsu:repr(vars.rule))
|
||||
|else
|
||||
| compiler:writeln("rule "..compiler:repr(compiler.utils.keys(fn_def.aliases))
|
||||
| nomsu:writeln("rule "..nomsu:repr(nomsu.utils.keys(fn_def.aliases))
|
||||
| .." ="..(fn_def.src or ":\\n <unknown source code>"))
|
||||
|end
|
||||
|
||||
|
||||
# Macro helper functions
|
||||
rule [%tree as value] =:
|
||||
lua expr "compiler:tree_to_value(vars.tree, vars)"
|
||||
lua expr "nomsu:tree_to_value(vars.tree, vars)"
|
||||
|
||||
rule [%tree as lua] =:
|
||||
lua expr "compiler:tree_to_lua(vars.tree)"
|
||||
lua expr "nomsu:tree_to_lua(vars.tree)"
|
||||
|
||||
rule [test, foobar] =: lua expr "print('yup')"
|
||||
|
||||
# Compiler tools
|
||||
rule [eval %code, run %code] =: compiler "run" [%code]
|
||||
rule [eval %code, run %code] =: nomsu "run" [%code]
|
||||
|
||||
rule [source code from tree %tree] =:
|
||||
lua block ".."
|
||||
@ -119,8 +118,8 @@ rule [source code from tree %tree] =:
|
||||
|end
|
||||
|
||||
macro [source code %body] =:
|
||||
compiler "repr" [compiler "call" ["source code from tree %", %body]]
|
||||
nomsu "repr" [nomsu "call" ["source code from tree %", %body]]
|
||||
|
||||
macro [parse tree %code] =:
|
||||
compiler "repr" [compiler "stringify_tree" [lua expr "vars.code.value"]]
|
||||
nomsu "repr" [nomsu "stringify_tree" [lua expr "vars.code.value"]]
|
||||
|
||||
|
@ -4,27 +4,27 @@ require "lib/metaprogramming.nom"
|
||||
macro block [moonscript block %moonscript_code] =:
|
||||
lua block ".."
|
||||
|local parse, compile = require('moonscript.parse'), require('moonscript.compile')
|
||||
|local moon_code = compiler:tree_to_value(vars.moonscript_code, vars)
|
||||
|local moon_code = nomsu:tree_to_value(vars.moonscript_code, vars)
|
||||
|local tree, err = parse.string(moon_code)
|
||||
|if not tree then
|
||||
| compiler:error("Failed to parse moonscript: "..err)
|
||||
| nomsu:error("Failed to parse moonscript: "..err)
|
||||
|end
|
||||
|local lua_code, err, pos = compile.tree(tree)
|
||||
|if not lua_code then
|
||||
| compiler:error(compile.format_error(err, pos, moon_code))
|
||||
| nomsu:error(compile.format_error(err, pos, moon_code))
|
||||
|end
|
||||
|return "do\\n"..lua_code.."\\nend"
|
||||
|
||||
macro [moonscript %moonscript_code] =:
|
||||
lua block ".."
|
||||
|local parse, compile = require('moonscript.parse'), require('moonscript.compile')
|
||||
|local moon_code = compiler:tree_to_value(vars.moonscript_code, vars)
|
||||
|local moon_code = nomsu:tree_to_value(vars.moonscript_code, vars)
|
||||
|local tree, err = parse.string(moon_code)
|
||||
|if not tree then
|
||||
| compiler:error("Failed to parse moonscript: "..err)
|
||||
| nomsu:error("Failed to parse moonscript: "..err)
|
||||
|end
|
||||
|local lua_code, err, pos = compile.tree(tree)
|
||||
|if not lua_code then
|
||||
| compiler:error(compile.format_error(err, pos, moon_code))
|
||||
| nomsu:error(compile.format_error(err, pos, moon_code))
|
||||
|end
|
||||
|return "(function(compiler, vars)\\n"..lua_code.."\\nend)(compiler, vars)"
|
||||
|return "(function(nomsu, vars)\\n"..lua_code.."\\nend)(nomsu, vars)"
|
||||
|
@ -11,46 +11,46 @@ macro [%if_expr if %condition else %else_expr] =:
|
||||
pass # TODO: Fix compiler bug that doesn't parse right here
|
||||
#.. Note: this uses a function instead of (condition and if_expr or else_expr)
|
||||
because that breaks if %if_expr is falsey.
|
||||
".."|(function(compiler, vars)
|
||||
".."|(function(nomsu, vars)
|
||||
| if \%condition as lua\ then
|
||||
| return \%if_expr as lua\
|
||||
| else
|
||||
| return \%else_expr as lua\
|
||||
| end
|
||||
|end)(compiler, vars)
|
||||
|end)(nomsu, vars)
|
||||
|
||||
# Variable assignment operator, and += type versions
|
||||
lua block ".."
|
||||
|local function assign(callback)
|
||||
| return function(compiler, vars, kind)
|
||||
| return function(nomsu, vars, kind)
|
||||
| if kind == "Expression" then
|
||||
| compiler:error("Cannot use an assignment operation as an expression value.")
|
||||
| nomsu:error("Cannot use an assignment operation as an expression value.")
|
||||
| end
|
||||
| if vars.var.type ~= "Var" then
|
||||
| compiler:error("Assignment operation has the wrong type for the left hand side. "
|
||||
| nomsu:error("Assignment operation has the wrong type for the left hand side. "
|
||||
| .."Expected Var, but got: "..vars.var.type.."\\nMaybe you forgot a percent sign on the variable name?")
|
||||
| end
|
||||
| if vars.rhs.type ~= "Thunk" then
|
||||
| compiler:error("Assignment operation has the wrong type for the right hand side. "
|
||||
| nomsu:error("Assignment operation has the wrong type for the right hand side. "
|
||||
| .."Expected Thunk, but got: "..vars.rhs.type.."\\nMaybe you used '=' instead of '=:'?")
|
||||
| end
|
||||
| if #vars.rhs.value.value > 1 then
|
||||
| compiler:error("Assignment operation should not have more than one value on the right hand side.")
|
||||
| nomsu:error("Assignment operation should not have more than one value on the right hand side.")
|
||||
| end
|
||||
| return callback(compiler:tree_to_lua(vars.var),
|
||||
| compiler:tree_to_lua(vars.rhs.value.value[1].value)), true
|
||||
| return callback(nomsu:tree_to_lua(vars.var),
|
||||
| nomsu:tree_to_lua(vars.rhs.value.value[1].value)), true
|
||||
| end
|
||||
|end
|
||||
|compiler:defmacro("%var = %rhs", assign(function(var,result) return var.." = "..result end))
|
||||
|compiler:defmacro("%var += %rhs", assign(function(var,result) return var.." = "..var.." + "..result end))
|
||||
|compiler:defmacro("%var -= %rhs", assign(function(var,result) return var.." = "..var.." - "..result end))
|
||||
|compiler:defmacro("%var *= %rhs", assign(function(var,result) return var.." = "..var.." * "..result end))
|
||||
|compiler:defmacro("%var /= %rhs", assign(function(var,result) return var.." = "..var.." / "..result end))
|
||||
|compiler:defmacro("%var ^= %rhs", assign(function(var,result) return var.." = "..var.." ^ "..result end))
|
||||
|compiler:defmacro("%var and= %rhs", assign(function(var,result) return var.." = "..var.." and "..result end))
|
||||
|compiler:defmacro("%var or= %rhs", assign(function(var,result) return var.." = "..var.." or "..result end))
|
||||
|compiler:defmacro("%var join= %rhs", assign(function(var,result) return var.." = "..var.." .. "..result end))
|
||||
|compiler:defmacro("%var mod= %rhs", assign(function(var,result) return var.." = "..var.." % "..result end))
|
||||
|nomsu:defmacro("%var = %rhs", assign(function(var,result) return var.." = "..result end))
|
||||
|nomsu:defmacro("%var += %rhs", assign(function(var,result) return var.." = "..var.." + "..result end))
|
||||
|nomsu:defmacro("%var -= %rhs", assign(function(var,result) return var.." = "..var.." - "..result end))
|
||||
|nomsu:defmacro("%var *= %rhs", assign(function(var,result) return var.." = "..var.." * "..result end))
|
||||
|nomsu:defmacro("%var /= %rhs", assign(function(var,result) return var.." = "..var.." / "..result end))
|
||||
|nomsu:defmacro("%var ^= %rhs", assign(function(var,result) return var.." = "..var.." ^ "..result end))
|
||||
|nomsu:defmacro("%var and= %rhs", assign(function(var,result) return var.." = "..var.." and "..result end))
|
||||
|nomsu:defmacro("%var or= %rhs", assign(function(var,result) return var.." = "..var.." or "..result end))
|
||||
|nomsu:defmacro("%var join= %rhs", assign(function(var,result) return var.." = "..var.." .. "..result end))
|
||||
|nomsu:defmacro("%var mod= %rhs", assign(function(var,result) return var.." = "..var.." % "..result end))
|
||||
|
||||
# Binary Operators
|
||||
lua block ".."
|
||||
@ -60,13 +60,13 @@ lua block ".."
|
||||
| if type(op) == 'table' then
|
||||
| nomsu_alias, op = unpack(op)
|
||||
| end
|
||||
| compiler:defmacro("%a "..nomsu_alias.." %b", (function(compiler, vars, kind)
|
||||
| return "("..compiler:tree_to_lua(vars.a).." "..op.." "..compiler:tree_to_lua(vars.b)..")"
|
||||
| nomsu:defmacro("%a "..nomsu_alias.." %b", (function(nomsu, vars, kind)
|
||||
| return "("..nomsu:tree_to_lua(vars.a).." "..op.." "..nomsu:tree_to_lua(vars.b)..")"
|
||||
| end), [[".."|(\\%a as lua\\ ]]..op..[[ \\%b as lua\\)]])
|
||||
|end
|
||||
# == and != do equivalence checking, rather than identity checking
|
||||
macro [%a == %b] =: ".."|compiler.utils.equivalent(\%a as lua\, \%b as lua\)
|
||||
macro [%a != %b] =: ".."|(not compiler.utils.equivalent(\%a as lua\, \%b as lua\))
|
||||
macro [%a == %b] =: ".."|nomsu.utils.equivalent(\%a as lua\, \%b as lua\)
|
||||
macro [%a != %b] =: ".."|(not nomsu.utils.equivalent(\%a as lua\, \%b as lua\))
|
||||
|
||||
# Commutative Operators defined for up to 8 operands
|
||||
# TODO: work out solution for commutative operators using more clever macros
|
||||
@ -78,10 +78,10 @@ lua block ".."
|
||||
| local spec = "%1 "..op.." %2"
|
||||
| for n=3,max_operands do
|
||||
| spec = spec .." "..op.." %"..tostring(n)
|
||||
| compiler:defmacro(spec, (function(compiler, vars, kind)
|
||||
| nomsu:defmacro(spec, (function(nomsu, vars, kind)
|
||||
| local bits = {}
|
||||
| for i=1,n do
|
||||
| table.insert(bits, (compiler:tree_to_lua(vars[tostring(i)])))
|
||||
| table.insert(bits, (nomsu:tree_to_lua(vars[tostring(i)])))
|
||||
| end
|
||||
| return "("..table.concat(bits, " "..op.." ")..")"
|
||||
| end))
|
||||
@ -100,7 +100,7 @@ lua block ".."
|
||||
| spec = spec .. " "..op.." %"..tostring(i+1)
|
||||
| end
|
||||
# Chained comparisons need to be functions to avoid re-evaluating their arguments :\
|
||||
| compiler:def(spec, function(compiler, vars)
|
||||
| nomsu:def(spec, function(nomsu, vars)
|
||||
| for i,op in ipairs(chain) do
|
||||
| local a, b, result = vars[i], vars[i+1]
|
||||
| if op == "<" then result = a < b
|
||||
|
@ -6,40 +6,40 @@ require "lib/collections.nom"
|
||||
# Permission functions
|
||||
rule [restrict %rules to within %elite-rules] =:
|
||||
say ".."|Restricting \%rules\ to within \%elite-rules\
|
||||
%rules =: keys in (compiler "get_aliases" [%rules])
|
||||
%elite-rules =: keys in (compiler "get_aliases" [%elite-rules])
|
||||
%rules =: keys in (nomsu "get_aliases" [%rules])
|
||||
%elite-rules =: keys in (nomsu "get_aliases" [%elite-rules])
|
||||
for all (flatten [%elite-rules, %rules]):
|
||||
assert ((compiler's "defs") has key %it) ".."|Undefined function: \%it\
|
||||
assert ((nomsu's "defs") has key %it) ".."|Undefined function: \%it\
|
||||
for all %rules:
|
||||
assert (compiler "check_permission" [%it]) ".."
|
||||
assert (nomsu "check_permission" [%it]) ".."
|
||||
|You do not have permission to restrict permissions for function: \%it\
|
||||
%foo =: dict (..)
|
||||
[%it, yes] for %it in %elite-rules
|
||||
((compiler's "defs")'s %it)'s "whiteset" =: %foo
|
||||
((nomsu's "defs")'s %it)'s "whiteset" =: %foo
|
||||
|
||||
rule [allow %elite-rules to use %rules] =:
|
||||
say ".."|Allowing \%elite-rules\ to use \%rules\
|
||||
%rules =: keys in (compiler "get_aliases" [%rules])
|
||||
%elite-rules =: keys in (compiler "get_aliases" [%elite-rules])
|
||||
%rules =: keys in (nomsu "get_aliases" [%rules])
|
||||
%elite-rules =: keys in (nomsu "get_aliases" [%elite-rules])
|
||||
for all (flatten [%elite-rules, %rules]):
|
||||
assert ((compiler's "defs") has key %it) ".."|Undefined function: \%it\
|
||||
assert ((nomsu's "defs") has key %it) ".."|Undefined function: \%it\
|
||||
for %fn in %rules:
|
||||
assert (compiler "check_permission" [%fn]) ".."
|
||||
assert (nomsu "check_permission" [%fn]) ".."
|
||||
|You do not have permission to grant permissions for function: \%fn\
|
||||
%whiteset =: ((compiler's "defs")'s %fn)'s "whiteset"
|
||||
%whiteset =: ((nomsu's "defs")'s %fn)'s "whiteset"
|
||||
if (not %whiteset): on to the next %fn
|
||||
for all %elite-rules: %whiteset's %it =: yes
|
||||
|
||||
rule [forbid %pleb-rules to use %rules] =:
|
||||
say ".."|Forbidding \%pleb-rules\ to use \%rules\
|
||||
%rules =: keys in (compiler "get_aliases" [%rules])
|
||||
%pleb-rules =: keys in (compiler "get_aliases" [%pleb-rules])
|
||||
%rules =: keys in (nomsu "get_aliases" [%rules])
|
||||
%pleb-rules =: keys in (nomsu "get_aliases" [%pleb-rules])
|
||||
for all (flatten [%pleb-rules, %used]):
|
||||
assert ((compiler's "defs") has key %it) ".."|Undefined function: \%it\
|
||||
assert ((nomsu's "defs") has key %it) ".."|Undefined function: \%it\
|
||||
for all %rules:
|
||||
assert (compiler "check_permission" [%it]) ".."
|
||||
assert (nomsu "check_permission" [%it]) ".."
|
||||
|You do not have permission to grant permissions for function: \%it\
|
||||
%whiteset =: ((compiler's "defs")'s %it)'s "whiteset"
|
||||
%whiteset =: ((nomsu's "defs")'s %it)'s "whiteset"
|
||||
assert %whiteset ".."
|
||||
|Cannot individually restrict permissions for \%it\ because it is currently
|
||||
|available to everyone. Perhaps you meant to use "restrict % to within %" instead?
|
||||
|
@ -20,14 +20,14 @@ macro [secret %key, secret value of %key, secret value for %key] =:
|
||||
macro block [secret %key = %new_value] =:
|
||||
lua block ".."
|
||||
|if vars.key.type ~= "Var" then
|
||||
| compiler:error("Assignment operation has the wrong type for the left hand side. "
|
||||
| nomsu:error("Assignment operation has the wrong type for the left hand side. "
|
||||
| .."Expected Var, but got: "..vars.key.type)
|
||||
|end
|
||||
|if vars.new_value.type ~= "Thunk" then
|
||||
| compiler:error("Assignment operation has the wrong type for the right hand side. "
|
||||
| nomsu:error("Assignment operation has the wrong type for the right hand side. "
|
||||
| .."Expected Thunk, but got: "..vars.new_value.type.."\\nMaybe you used '=' instead of '=:'?")
|
||||
|end
|
||||
".."
|
||||
|local ret
|
||||
|\lua expr "compiler:tree_to_lua(vars.new_value.value)"\
|
||||
|\lua expr "nomsu:tree_to_lua(vars.new_value.value)"\
|
||||
|secrets[\repr (%key's "value")\] = ret
|
||||
|
@ -2,7 +2,7 @@ require "lib/metaprogramming.nom"
|
||||
|
||||
# For unit testing
|
||||
macro block [test %code yields %expected] =:
|
||||
%generated =: lua expr "compiler.utils.repr(compiler:stringify_tree(vars.code.value))"
|
||||
%generated =: repr (nomsu "stringify_tree" [%code's "value"])
|
||||
%expected =: %expected as lua
|
||||
if (%generated != %expected):
|
||||
say "Test failed!"
|
||||
|
@ -2,64 +2,64 @@ require "lib/metaprogramming.nom"
|
||||
|
||||
# Error functions
|
||||
rule [error!, panic!, fail!, abort!] =:
|
||||
compiler "error"[]
|
||||
nomsu "error"[]
|
||||
rule [error %msg] =:
|
||||
compiler "error"[%msg]
|
||||
nomsu "error"[%msg]
|
||||
macro statement [assert %condition] =: ".."
|
||||
|if not (\%condition as lua\) then
|
||||
| compiler:error()
|
||||
| nomsu:error()
|
||||
|end
|
||||
macro statement [assert %condition %msg] =: ".."
|
||||
|if not (\%condition as lua\) then
|
||||
| compiler:error(\%msg as lua\)
|
||||
| nomsu:error(\%msg as lua\)
|
||||
|end
|
||||
|
||||
macro statement [show generated lua %block] =: ".."
|
||||
|compiler:writeln(\lua expr "compiler:repr(compiler:tree_to_lua(vars.block.value))"\)
|
||||
|nomsu:writeln(\lua expr "nomsu:repr(nomsu:tree_to_lua(vars.block.value))"\)
|
||||
|
||||
|
||||
# String functions
|
||||
rule [join %strs] =:
|
||||
lua block ".."
|
||||
|local str_bits = {}
|
||||
|for i,bit in ipairs(vars.strs) do str_bits[i] = compiler.utils.repr_if_not_string(bit) end
|
||||
|for i,bit in ipairs(vars.strs) do str_bits[i] = nomsu.utils.repr_if_not_string(bit) end
|
||||
|return table.concat(str_bits)
|
||||
|
||||
rule [join %strs with glue %glue] =:
|
||||
lua block ".."
|
||||
|local str_bits = {}
|
||||
|for i,bit in ipairs(vars.strs) do str_bits[i] = compiler.utils.repr_if_not_string(bit) end
|
||||
|for i,bit in ipairs(vars.strs) do str_bits[i] = nomsu.utils.repr_if_not_string(bit) end
|
||||
|return table.concat(str_bits, vars.glue)
|
||||
|
||||
macro [capitalize %str, %str capitalized] =: ".."
|
||||
|(\%str as lua\):gsub("%l", string.upper, 1)
|
||||
|
||||
macro [repr %obj] =:
|
||||
".."|compiler:repr(\%obj as lua\)
|
||||
".."|nomsu:repr(\%obj as lua\)
|
||||
|
||||
macro [%obj as string] =:
|
||||
".."|compiler.utils.repr_if_not_string(\%obj as lua\)
|
||||
".."|nomsu.utils.repr_if_not_string(\%obj as lua\)
|
||||
|
||||
macro [say %str] =:
|
||||
".."|compiler:writeln(compiler.utils.repr_if_not_string(\%str as lua\))
|
||||
".."|nomsu:writeln(nomsu.utils.repr_if_not_string(\%str as lua\))
|
||||
|
||||
# Number ranges
|
||||
macro [%start up to %stop] =: ".."
|
||||
|compiler.utils.range(\%start as lua\, \%stop as lua\-1)
|
||||
|nomsu.utils.range(\%start as lua\, \%stop as lua\-1)
|
||||
macro [%start thru %stop, %start through %stop] =: ".."
|
||||
|compiler.utils.range(\%start as lua\, \%stop as lua\)
|
||||
|nomsu.utils.range(\%start as lua\, \%stop as lua\)
|
||||
macro [%start down to %stop] =: ".."
|
||||
|compiler.utils.range(\%start as lua\, \%stop as lua\+1,-1)
|
||||
|nomsu.utils.range(\%start as lua\, \%stop as lua\+1,-1)
|
||||
macro [%start down thru %stop, %start down through %stop] =: ".."
|
||||
|compiler.utils.range(\%start as lua\, \%stop as lua\,-1)
|
||||
|nomsu.utils.range(\%start as lua\, \%stop as lua\,-1)
|
||||
macro [%start up to %stop via %step] =: ".."
|
||||
|compiler.utils.range(\%start as lua\,\%stop as lua\-1,vars.step)
|
||||
|nomsu.utils.range(\%start as lua\,\%stop as lua\-1,vars.step)
|
||||
macro [%start thru %stop via %step, %start through %stop via %step] =: ".."
|
||||
|compiler.utils.range(\%start as lua\,\%stop as lua\,vars.step)
|
||||
|nomsu.utils.range(\%start as lua\,\%stop as lua\,vars.step)
|
||||
macro [%start down to %stop via %step] =: ".."
|
||||
|compiler.utils.range(\%start as lua\,\%stop as lua\+1,-vars.step)
|
||||
|nomsu.utils.range(\%start as lua\,\%stop as lua\+1,-vars.step)
|
||||
macro [%start down thru %stop via %step, %start down through %stop via %step] =: ".."
|
||||
|compiler.utils.range(\%start as lua\,\%stop as lua\,-vars.step)
|
||||
|nomsu.utils.range(\%start as lua\,\%stop as lua\,-vars.step)
|
||||
|
||||
# Common utility functions
|
||||
macro [random number, random, rand] =: "math.random()"
|
||||
@ -68,18 +68,18 @@ macro [random from %low to %high, random number from %low to %high, rand %low %h
|
||||
|math.random(\%low as lua\, \%high as lua\)
|
||||
rule [random choice from %elements, random choice %elements, random %elements] =:
|
||||
lua expr ".."|vars.elements[math.random(#vars.elements)]
|
||||
macro [sum of %items, sum %items] =: ".."|compiler.utils.sum(\%items as lua\)
|
||||
macro [product of %items, product %items] =: ".."|compiler.utils.product(\%items as lua\)
|
||||
macro [all of %items] =: ".."|compiler.utils.all(\%items as lua\)
|
||||
macro [any of %items] =: ".."|compiler.utils.any(\%items as lua\)
|
||||
macro [sum of %items, sum %items] =: ".."|nomsu.utils.sum(\%items as lua\)
|
||||
macro [product of %items, product %items] =: ".."|nomsu.utils.product(\%items as lua\)
|
||||
macro [all of %items] =: ".."|nomsu.utils.all(\%items as lua\)
|
||||
macro [any of %items] =: ".."|nomsu.utils.any(\%items as lua\)
|
||||
# This is a rule, not a macro so we can use vars.items twice without running it twice.
|
||||
rule [avg of %items, average of %items] =:
|
||||
lua expr ".."|(compiler.utils.sum(vars.items)/#vars.items)
|
||||
lua expr ".."|(nomsu.utils.sum(vars.items)/#vars.items)
|
||||
macro [min of %items, smallest of %items, lowest of %items] =:
|
||||
".."|compiler.utils.min(\%items as lua\)
|
||||
".."|nomsu.utils.min(\%items as lua\)
|
||||
macro [max of %items, biggest of %items, largest of %items, highest of %items] =:
|
||||
".."|compiler.utils.min(\%items as lua\)
|
||||
".."|nomsu.utils.min(\%items as lua\)
|
||||
macro [min of %items with respect to %keys] =:
|
||||
".."|compiler.utils.min(\%items as lua\, \%keys as lua\)
|
||||
".."|nomsu.utils.min(\%items as lua\, \%keys as lua\)
|
||||
macro [max of %items with respect to %keys] =:
|
||||
".."|compiler.utils.max(\%items as lua\, \%keys as lua\)
|
||||
".."|nomsu.utils.max(\%items as lua\, \%keys as lua\)
|
||||
|
14
nomsu.lua
14
nomsu.lua
@ -372,7 +372,7 @@ do
|
||||
return tree
|
||||
end,
|
||||
tree_to_value = function(self, tree, vars)
|
||||
local code = "\n return (function(compiler, vars)\nreturn " .. tostring(self:tree_to_lua(tree)) .. "\nend)"
|
||||
local code = "\n return (function(nomsu, vars)\nreturn " .. tostring(self:tree_to_lua(tree)) .. "\nend)"
|
||||
local lua_thunk, err = load(code)
|
||||
if not lua_thunk then
|
||||
error("Failed to compile generated code:\n" .. tostring(code) .. "\n\n" .. tostring(err))
|
||||
@ -387,7 +387,7 @@ do
|
||||
local _exp_0 = tree.type
|
||||
if "File" == _exp_0 then
|
||||
local buffer = {
|
||||
[[return (function(compiler, vars)
|
||||
[[return (function(nomsu, vars)
|
||||
local ret]]
|
||||
}
|
||||
local vars = { }
|
||||
@ -399,7 +399,7 @@ do
|
||||
self:writeln("Error occurred in statement:\n" .. tostring(statement.src))
|
||||
error(code)
|
||||
end
|
||||
local lua_code = "\n return (function(compiler, vars)\n" .. tostring(code) .. "\nend)"
|
||||
local lua_code = "\n return (function(nomsu, vars)\n" .. tostring(code) .. "\nend)"
|
||||
local lua_thunk, err = load(lua_code)
|
||||
if not lua_thunk then
|
||||
error("Failed to compile generated code:\n" .. tostring(code) .. "\n\n" .. tostring(err) .. "\n\nProduced by statement:\n" .. tostring(repr(statement)))
|
||||
@ -432,13 +432,13 @@ do
|
||||
do
|
||||
local ret_value = lua:match("^%s*ret = (.*)")
|
||||
if ret_value then
|
||||
return ([[ (function(compiler, vars)
|
||||
return ([[ (function(nomsu, vars)
|
||||
return %s
|
||||
end)]]):format(ret_value)
|
||||
end
|
||||
end
|
||||
end
|
||||
return ([[ (function(compiler, vars)
|
||||
return ([[ (function(nomsu, vars)
|
||||
local ret
|
||||
%s
|
||||
return ret
|
||||
@ -471,7 +471,7 @@ do
|
||||
args = _accum_0
|
||||
end
|
||||
insert(args, 1, repr(alias))
|
||||
return self.__class:comma_separated_items("compiler:call(", args, ")")
|
||||
return self.__class:comma_separated_items("nomsu:call(", args, ")")
|
||||
end
|
||||
elseif "String" == _exp_0 then
|
||||
return repr(self.__class:unescape_string(tree.value))
|
||||
@ -491,7 +491,7 @@ do
|
||||
insert(concat_parts, repr(string_buffer))
|
||||
string_buffer = ""
|
||||
end
|
||||
insert(concat_parts, "compiler.utils.repr_if_not_string(" .. tostring(self:tree_to_lua(bit)) .. ")")
|
||||
insert(concat_parts, "nomsu.utils.repr_if_not_string(" .. tostring(self:tree_to_lua(bit)) .. ")")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
14
nomsu.moon
14
nomsu.moon
@ -272,7 +272,7 @@ class NomsuCompiler
|
||||
|
||||
tree_to_value: (tree, vars)=>
|
||||
code = "
|
||||
return (function(compiler, vars)\nreturn #{@tree_to_lua(tree)}\nend)"
|
||||
return (function(nomsu, vars)\nreturn #{@tree_to_lua(tree)}\nend)"
|
||||
lua_thunk, err = load(code)
|
||||
if not lua_thunk
|
||||
error("Failed to compile generated code:\n#{code}\n\n#{err}")
|
||||
@ -284,7 +284,7 @@ class NomsuCompiler
|
||||
@error "Invalid tree: #{repr(tree)}"
|
||||
switch tree.type
|
||||
when "File"
|
||||
buffer = {[[return (function(compiler, vars)
|
||||
buffer = {[[return (function(nomsu, vars)
|
||||
local ret]]}
|
||||
vars = {}
|
||||
for statement in *tree.value.body.value
|
||||
@ -294,7 +294,7 @@ class NomsuCompiler
|
||||
error(code)
|
||||
-- Run the fuckers as we go
|
||||
lua_code = "
|
||||
return (function(compiler, vars)\n#{code}\nend)"
|
||||
return (function(nomsu, vars)\n#{code}\nend)"
|
||||
lua_thunk, err = load(lua_code)
|
||||
if not lua_thunk
|
||||
error("Failed to compile generated code:\n#{code}\n\n#{err}\n\nProduced by statement:\n#{repr(statement)}")
|
||||
@ -322,11 +322,11 @@ class NomsuCompiler
|
||||
if #tree.value.value == 1
|
||||
if ret_value = lua\match("^%s*ret = (.*)")
|
||||
return ([[
|
||||
(function(compiler, vars)
|
||||
(function(nomsu, vars)
|
||||
return %s
|
||||
end)]])\format(ret_value)
|
||||
return ([[
|
||||
(function(compiler, vars)
|
||||
(function(nomsu, vars)
|
||||
local ret
|
||||
%s
|
||||
return ret
|
||||
@ -347,7 +347,7 @@ class NomsuCompiler
|
||||
else
|
||||
args = [@tree_to_lua(a) for a in *tree.value when a.type != "Word"]
|
||||
insert args, 1, repr(alias)
|
||||
return @@comma_separated_items("compiler:call(", args, ")")
|
||||
return @@comma_separated_items("nomsu:call(", args, ")")
|
||||
|
||||
when "String"
|
||||
return repr(@@unescape_string(tree.value))
|
||||
@ -364,7 +364,7 @@ class NomsuCompiler
|
||||
if string_buffer ~= ""
|
||||
insert concat_parts, repr(string_buffer)
|
||||
string_buffer = ""
|
||||
insert concat_parts, "compiler.utils.repr_if_not_string(#{@tree_to_lua(bit)})"
|
||||
insert concat_parts, "nomsu.utils.repr_if_not_string(#{@tree_to_lua(bit)})"
|
||||
|
||||
if string_buffer ~= ""
|
||||
insert concat_parts, repr(string_buffer)
|
||||
|
Loading…
Reference in New Issue
Block a user