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