Renamed rule % = % to action % %
This commit is contained in:
parent
6f4b67632a
commit
82cc997ddf
@ -19,16 +19,16 @@ parse [first in %list, first %list] as: 1 st in %list
|
||||
parse [last in %list, last %list] as: 1 st to last in %list
|
||||
|
||||
# Membership testing
|
||||
rule [%item is in %list, %list contains %item, %list has %item] =:
|
||||
action [%item is in %list, %list contains %item, %list has %item]:
|
||||
for %key = %value in %list:
|
||||
if (%key == %item): return (yes)
|
||||
return (no)
|
||||
|
||||
rule [..]
|
||||
action [..]
|
||||
%item isn't in %list, %item is not in %list
|
||||
%list doesn't contain %item, %list does not contain %item
|
||||
%list doesn't have %item, %list does not have %item
|
||||
..=:
|
||||
..:
|
||||
for %key = %value in %list:
|
||||
if (%key == %item): return (no)
|
||||
return (yes)
|
||||
@ -71,31 +71,31 @@ compile [remove index %index from %list] to:
|
||||
"table.remove(\(%list as lua), \(%index as lua))"
|
||||
|
||||
|
||||
rule [flatten %lists] =:
|
||||
action [flatten %lists]:
|
||||
%flat = []
|
||||
for %list in %lists:
|
||||
for %item in %list:
|
||||
add %item to %flat
|
||||
return %flat
|
||||
|
||||
rule [dict %items] =:
|
||||
action [dict %items]:
|
||||
%dict = []
|
||||
for %pair in %items:
|
||||
%dict -> (%pair -> 1) = (%pair -> 2)
|
||||
return %dict
|
||||
|
||||
rule [entries in %dict] =:
|
||||
action [entries in %dict]:
|
||||
%entries = []
|
||||
for %k = %v in %dict:
|
||||
add {key=%k, value=%v} to %entries
|
||||
return %entries
|
||||
|
||||
rule [keys in %dict] =:
|
||||
action [keys in %dict]:
|
||||
%keys = []
|
||||
for %k = %v in %dict: add %k to %keys
|
||||
return %keys
|
||||
|
||||
rule [values in %dict] =:
|
||||
action [values in %dict]:
|
||||
%values = []
|
||||
for %k = %v in %dict: add %v to %values
|
||||
return %values
|
||||
@ -128,15 +128,15 @@ compile [%expression for %key = %value in %iterable] to:
|
||||
return comprehension;
|
||||
end)(nomsu)
|
||||
|
||||
rule [%items sorted] =:
|
||||
action [%items sorted]:
|
||||
%copy = (% for all %items)
|
||||
sort %copy
|
||||
return %copy
|
||||
rule [%items sorted by %key] =:
|
||||
action [%items sorted by %key]:
|
||||
%copy = (% for all %items)
|
||||
sort %copy by %key
|
||||
return %copy
|
||||
rule [unique %items] =:
|
||||
action [unique %items]:
|
||||
keys in (dict ([%,yes] for all %items))
|
||||
|
||||
# Metatable stuff
|
||||
@ -147,7 +147,7 @@ compile [default dict] to: ".."
|
||||
self[key] = t;
|
||||
return t;
|
||||
end})"
|
||||
rule [chain %dict to %fallback] =:
|
||||
action [chain %dict to %fallback]:
|
||||
when (type of %fallback) == ?:
|
||||
* "table":
|
||||
=lua "setmetatable(\%dict, \%fallback)"
|
||||
|
@ -39,7 +39,7 @@ immediately:
|
||||
|
||||
# Helper function
|
||||
immediately:
|
||||
rule [tree %tree has function call %call] =:
|
||||
action [tree %tree has function call %call]:
|
||||
lua> ".."
|
||||
local target = (\%call).stub;
|
||||
for subtree,_ in coroutine.wrap(function() nomsu:walk_tree(\%tree); end) do
|
||||
|
@ -1,8 +1,8 @@
|
||||
#..
|
||||
This File contains rules for making rules and macros and some helper functions to make
|
||||
This File contains actions for making actions and macros and some helper functions to make
|
||||
that easier.
|
||||
|
||||
# Rule to make macros:
|
||||
# Helper function
|
||||
immediately:
|
||||
lua> ".."
|
||||
nomsu.parse_spec = function(nomsu, spec)
|
||||
@ -17,6 +17,7 @@ immediately:
|
||||
return signature, args;
|
||||
end
|
||||
|
||||
# Macro to make macros:
|
||||
immediately:
|
||||
lua> ".."
|
||||
nomsu:defmacro("compile %macro_def to %body", \(__line_no__), function(nomsu, \%macro_def, \%body)
|
||||
@ -60,14 +61,14 @@ immediately:
|
||||
return {statements=lua};
|
||||
end, \(__src__ 1));
|
||||
|
||||
# Rule to make rules:
|
||||
# Macro to make actions:
|
||||
immediately:
|
||||
compile [rule %signature = %body] to code:
|
||||
compile [action %signature %body] to code:
|
||||
lua> ".."
|
||||
nomsu:assert(\%signature.type == "List",
|
||||
"Invalid type for rule definition signature. Expected List, but got: "..tostring(\%signature.type));
|
||||
"Invalid type for action definition signature. Expected List, but got: "..tostring(\%signature.type));
|
||||
nomsu:assert(\%body.type == "Block",
|
||||
"Invalid type for rule definition body. Expected Block, but got: "..tostring(\%body.type));
|
||||
"Invalid type for action definition body. Expected Block, but got: "..tostring(\%body.type));
|
||||
local signature, args = nomsu:parse_spec(\%signature);
|
||||
local body_lua = nomsu:tree_to_lua(\%body);
|
||||
body_lua = body_lua.statements or ("return "..body_lua.expr..";");
|
||||
@ -78,7 +79,7 @@ immediately:
|
||||
end, %s);]]):format(signature, args, body_lua, nomsu:repr(src));
|
||||
return def_lua;
|
||||
|
||||
# Rule to make nomsu macros:
|
||||
# Macro to make nomsu macros:
|
||||
immediately:
|
||||
lua> ".."
|
||||
nomsu:defmacro("parse %shorthand as %longhand", \(__line_no__), (function(nomsu, \%shorthand, \%longhand)
|
||||
@ -106,7 +107,7 @@ immediately:
|
||||
return {statements=lua_code};
|
||||
end), \(__src__ 1));
|
||||
|
||||
rule [remove rule %stub] =:
|
||||
action [remove action %stub]:
|
||||
lua> ".."
|
||||
local def = nomsu.defs[\%stub];
|
||||
for _, alias in ipairs(def.aliases) do
|
||||
@ -114,13 +115,13 @@ rule [remove rule %stub] =:
|
||||
end
|
||||
|
||||
immediately:
|
||||
rule [%tree as lua] =:
|
||||
action [%tree as lua]:
|
||||
=lua "nomsu:tree_to_lua(\%tree).expr"
|
||||
rule [%tree as lua statements] =:
|
||||
action [%tree as lua statements]:
|
||||
lua> ".."
|
||||
local lua = nomsu:tree_to_lua(\%tree);
|
||||
return lua.statements or (lua.expr..";");
|
||||
rule [%tree as value] =:
|
||||
action [%tree as value]:
|
||||
=lua "nomsu:tree_to_value(\%tree)"
|
||||
compile [repr %obj] to:
|
||||
"nomsu:repr(\(%obj as lua))"
|
||||
@ -144,22 +145,22 @@ compile [nomsu %method %args] to: "nomsu[\(%method as lua)](nomsu, unpack(\(%arg
|
||||
compile [tree %tree with %replacements] to: ".."
|
||||
nomsu:replaced_vars(\(%tree as lua), \(%replacements as lua))
|
||||
|
||||
parse [rule %signature] as:
|
||||
parse [action %signature] as:
|
||||
(nomsu's "defs")->(nomsu "get_stub" [\%signature])
|
||||
|
||||
# Get the source code for a function
|
||||
rule [help %rule] =:
|
||||
action [help %action]:
|
||||
lua> ".."
|
||||
local fn_def = nomsu.defs[nomsu:get_stub(\%rule)]
|
||||
local fn_def = nomsu.defs[nomsu:get_stub(\%action)]
|
||||
if not fn_def then
|
||||
nomsu:writeln("Rule not found: "..nomsu:repr(\%rule));
|
||||
nomsu:writeln("Action not found: "..nomsu:repr(\%action));
|
||||
else
|
||||
nomsu:writeln(fn_def.src or "<unknown source code>");
|
||||
end
|
||||
|
||||
# Compiler tools
|
||||
parse [eval %code, run %code] as: nomsu "run" [%code]
|
||||
rule [source code from tree %tree] =:
|
||||
action [source code from tree %tree]:
|
||||
lua> ".."
|
||||
local _,_,leading_space = \%tree.src:find("\\n(%s*)%S");
|
||||
if leading_space then
|
||||
|
@ -1,9 +1,9 @@
|
||||
require "lib/metaprogramming.nom"
|
||||
|
||||
# Error functions
|
||||
rule [error!, panic!, fail!, abort!] =:
|
||||
action [error!, panic!, fail!, abort!]:
|
||||
nomsu "error" []
|
||||
rule [error %msg] =:
|
||||
action [error %msg]:
|
||||
nomsu "error"[%msg]
|
||||
compile [assert %condition %msg] to code: ".."
|
||||
if not (\(%condition as lua)) then
|
||||
@ -13,7 +13,7 @@ compile [assert %condition %msg] to code: ".."
|
||||
parse [assert %condition] as: assert %condition (nil)
|
||||
|
||||
# Text functions
|
||||
rule [join %strs with glue %glue] =:
|
||||
action [join %strs with glue %glue]:
|
||||
lua do> ".."
|
||||
local str_bits = {}
|
||||
for i,bit in ipairs(\%strs) do str_bits[i] = nomsu:stringify(bit) end
|
||||
@ -42,7 +42,7 @@ compile [random number, random, rand] to: "math.random()"
|
||||
compile [random int %n, random integer %n, randint %n] to: "math.random(\(%n as lua))"
|
||||
compile [random from %low to %high, random number from %low to %high, rand %low %high] to:
|
||||
"math.random(\(%low as lua), \(%high as lua))"
|
||||
rule [random choice from %elements, random choice %elements, random %elements] =:
|
||||
action [random choice from %elements, random choice %elements, random %elements]:
|
||||
=lua "\%elements[math.random(#\%elements)]"
|
||||
|
||||
# Math functions
|
||||
@ -65,7 +65,7 @@ compile [log %, ln %, natural log %] to: "math.log(\(% as lua))"
|
||||
compile [log % base %base] to: "math.log(\(% as lua), \(%base as lua))"
|
||||
compile [floor %] to: "math.floor(\(% as lua))"
|
||||
compile [round %, % rounded] to: "math.floor(\(% as lua) + .5)"
|
||||
rule [%n rounded to the nearest %rounder] =:
|
||||
action [%n rounded to the nearest %rounder]:
|
||||
=lua "(\%rounder)*math.floor((\%n / \%rounder) + .5)"
|
||||
compile [tau] to: "(2*math.pi)"
|
||||
compile [pi] to: "math.pi"
|
||||
@ -73,7 +73,7 @@ compile [e] to: "math.e"
|
||||
compile [golden ratio, phi] to: "((1 + math.sqrt(5)) / 2)"
|
||||
|
||||
# Common utility functions
|
||||
rule [avg of %items, average of %items] =:
|
||||
action [avg of %items, average of %items]:
|
||||
=lua "(nomsu.utils.sum(\%items)/#\%items)"
|
||||
compile [min of %items, smallest of %items, lowest of %items] to:
|
||||
"nomsu.utils.min(\(%items as lua))"
|
||||
|
@ -233,7 +233,7 @@ do
|
||||
local arg_positions = { }
|
||||
self:assert(stub, "NO STUB FOUND: " .. tostring(repr(signature)))
|
||||
if self.debug then
|
||||
self:writeln(tostring(colored.bright("DEFINING RULE:")) .. " " .. tostring(colored.underscore(colored.magenta(repr(stub)))) .. " " .. tostring(colored.bright("WITH ARGS")) .. " " .. tostring(colored.dim(repr(arg_names))))
|
||||
self:writeln(tostring(colored.bright("DEFINING ACTION:")) .. " " .. tostring(colored.underscore(colored.magenta(repr(stub)))) .. " " .. tostring(colored.bright("WITH ARGS")) .. " " .. tostring(colored.dim(repr(arg_names))))
|
||||
end
|
||||
for i = 1, #arg_names - 1 do
|
||||
for j = i + 1, #arg_names do
|
||||
|
@ -36,7 +36,7 @@ if _VERSION == "Lua 5.1"
|
||||
-- type checking?
|
||||
-- Fix compiler bug that breaks when file ends with a block comment
|
||||
-- Add compiler options for optimization level (compile-fast vs. run-fast, etc.)
|
||||
-- Do a pass on all rules to enforce parameters-are-nouns heuristic
|
||||
-- Do a pass on all actions to enforce parameters-are-nouns heuristic
|
||||
|
||||
lpeg.setmaxstack 10000 -- whoa
|
||||
{:P,:R,:V,:S,:Cg,:C,:Cp,:B,:Cmt} = lpeg
|
||||
@ -185,7 +185,7 @@ class NomsuCompiler
|
||||
stub, arg_names, escaped_args = unpack(signature[sig_i])
|
||||
arg_positions = {}
|
||||
@assert stub, "NO STUB FOUND: #{repr signature}"
|
||||
if @debug then @writeln "#{colored.bright "DEFINING RULE:"} #{colored.underscore colored.magenta repr(stub)} #{colored.bright "WITH ARGS"} #{colored.dim repr(arg_names)}"
|
||||
if @debug then @writeln "#{colored.bright "DEFINING ACTION:"} #{colored.underscore colored.magenta repr(stub)} #{colored.bright "WITH ARGS"} #{colored.dim repr(arg_names)}"
|
||||
for i=1,#arg_names-1 do for j=i+1,#arg_names
|
||||
if arg_names[i] == arg_names[j] then @error "Duplicate argument in function #{stub}: '#{arg_names[i]}'"
|
||||
|
||||
@ -555,7 +555,7 @@ end]]\format(lua_code))
|
||||
elseif not def and @@math_patt\match(tree.stub)
|
||||
-- This is a bit of a hack, but this code handles arbitrarily complex
|
||||
-- math expressions like 2*x + 3^2 without having to define a single
|
||||
-- rule for every possibility.
|
||||
-- action for every possibility.
|
||||
bits = {}
|
||||
for tok in *tree.value
|
||||
if tok.type == "Word"
|
||||
@ -735,7 +735,7 @@ end]]\format(lua_code))
|
||||
if not x
|
||||
@error "Nothing to get stub from"
|
||||
-- Returns a single stub ("say %"), list of arg names ({"msg"}), and set of arg
|
||||
-- names that should not be evaluated from a single rule def
|
||||
-- names that should not be evaluated from a single action def
|
||||
-- (e.g. "say %msg") or function call (e.g. FunctionCall({Word("say"), Var("msg")))
|
||||
if type(x) == 'string'
|
||||
-- Standardize format to stuff separated by spaces
|
||||
|
Loading…
Reference in New Issue
Block a user