diff options
Diffstat (limited to 'lib/utils.nom')
| -rw-r--r-- | lib/utils.nom | 85 |
1 files changed, 39 insertions, 46 deletions
diff --git a/lib/utils.nom b/lib/utils.nom index 1afc33a..11596f1 100644 --- a/lib/utils.nom +++ b/lib/utils.nom @@ -1,73 +1,66 @@ require "lib/metaprogramming.nom" # Error functions -rule [error!, panic!, fail!, abort!] =: - nomsu "error"[] -rule [error %msg] =: +rule (error!; panic!; fail!; abort!) =: + nomsu "error" [] +rule (error %msg) =: nomsu "error"[%msg] -macro statement [assert %condition] =: ".." - |if not (\%condition as lua\) then +parse (assert %condition) as: lua code ".." + |if not (\(%condition)) then | nomsu:error() |end -macro statement [assert %condition %msg] =: ".." - |if not (\%condition as lua\) then - | nomsu:error(\%msg as lua\) +parse (assert %condition %msg) as: lua code ".." + |if not (\(%condition)) then + | nomsu:error(\(%msg)) |end -macro statement [show generated lua %block] =: ".." - |nomsu:writeln(\lua expr "nomsu:repr(nomsu:tree_to_lua(vars.block.value))"\) +parse (show generated lua %block) as: lua code ".." + |nomsu:writeln(\(repr (nomsu "tree_to_lua" [%block]))) # String functions -rule [join %strs] =: +rule (join %strs) =: lua block ".." |local str_bits = {} |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] =: +rule (join %strs with glue %glue) =: lua block ".." |local str_bits = {} |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) +parse (capitalize %str; %str capitalized) as: lua expr "(\(%str)):gsub('%l', string.upper, 1)" -macro [repr %obj] =: - ".."|nomsu:repr(\%obj as lua\) - -macro [%obj as string] =: - ".."|nomsu.utils.repr_if_not_string(\%obj as lua\) - -macro [say %str] =: - ".."|nomsu:writeln(nomsu.utils.repr_if_not_string(\%str as lua\)) +parse (say %str) as: lua block ".." + |nomsu:writeln(nomsu.utils.repr_if_not_string(\(%str))) # Number ranges -macro [%start to %stop] =: ".." - |nomsu.utils.range(\%start as lua\, \%stop as lua\) -macro [%start to %stop by %step, %start to %stop via %step] =: ".." - |nomsu.utils.range(\%start as lua\, \%stop as lua\, \%step as lua\) +parse (%start to %stop) as: lua expr ".." + |nomsu.utils.range(\(%start), \(%stop)) +parse (%start to %stop by %step; %start to %stop via %step) as: lua expr ".." + |nomsu.utils.range(\(%start), \(%stop), \(%step)) # Common utility functions -macro [random number, random, rand] =: "math.random()" -macro [random int %n, random integer %n, randint %n] =: ".."|math.random(\%n as lua\) -macro [random from %low to %high, random number from %low to %high, rand %low %high] =: ".." - |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] =: ".."|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\) +parse (random number; random; rand) as: lua expr "math.random()" +parse (random int %n; random integer %n; randint %n) as: lua expr "math.random(\(%n))" +parse (random from %low to %high; random number from %low to %high; rand %low %high) as: + lua expr "math.random(\(%low), \(%high))" +rule (random choice from %elements; random choice %elements; random %elements) =: + lua expr "vars.elements[math.random(#vars.elements)]" +parse (sum of %items; sum %items) as: lua expr "nomsu.utils.sum(\(%items))" +parse (product of %items; product %items) as: lua expr "nomsu.utils.product(\(%items))" +parse (all of %items) as: lua expr "nomsu.utils.all(\(%items))" +parse (any of %items) as: lua expr "nomsu.utils.any(\(%items))" # 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 ".."|(nomsu.utils.sum(vars.items)/#vars.items) -macro [min of %items, smallest of %items, lowest of %items] =: - ".."|nomsu.utils.min(\%items as lua\) -macro [max of %items, biggest of %items, largest of %items, highest of %items] =: - ".."|nomsu.utils.min(\%items as lua\) -macro [min of %items with respect to %keys] =: - ".."|nomsu.utils.min(\%items as lua\, \%keys as lua\) -macro [max of %items with respect to %keys] =: - ".."|nomsu.utils.max(\%items as lua\, \%keys as lua\) +rule (avg of %items; average of %items) =: + lua expr "(nomsu.utils.sum(vars.items)/#vars.items)" +parse (min of %items; smallest of %items; lowest of %items) as: + lua expr "nomsu.utils.min(\(%items))" +parse (max of %items; biggest of %items; largest of %items; highest of %items) as: + lua expr "nomsu.utils.max(\(%items))" +parse (min of %items with respect to %keys) as: + lua expr "nomsu.utils.min(\(%items), \(%keys))" +parse (max of %items with respect to %keys) as: + lua expr "nomsu.utils.max(\(%items), \(%keys))" |
