aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/control_flow.nom229
-rw-r--r--lib/metaprogramming.nom29
-rw-r--r--lib/operators.nom40
-rw-r--r--lib/utils.nom85
4 files changed, 176 insertions, 207 deletions
diff --git a/lib/control_flow.nom b/lib/control_flow.nom
index b1df621..460d4dc 100644
--- a/lib/control_flow.nom
+++ b/lib/control_flow.nom
@@ -3,137 +3,99 @@ require "lib/operators.nom"
require "lib/utils.nom"
# Conditionals
-macro statement [if %condition %if_body] =:
- ".."|if \%condition as lua\ then
- | \(lua expr "vars.if_body.value") as lua\
- |end
-
-macro statement [if %condition %if_body else %else_body] =:
- ".."|if \%condition as lua\ then
- | \(lua expr "vars.if_body.value") as lua\
- |else
- | \(lua expr "vars.else_body.value") as lua\
- |end
+parse (if %condition %if_body) as lua code ".."
+ |if \(%condition) then
+ | \(lua expr "vars.if_body.value")
+ |end
+
+parse (if %condition %if_body else %else_body) as lua code ".."
+ |if \(%condition) then
+ | \(lua expr "vars.if_body.value")
+ |else
+ | \(lua expr "vars.else_body.value")
+ |end
# Return
-macro statement [return] =: "do return end"
-macro block [return %return-value] =: ".."
- |return \%return-value as lua\
-
-macro [do %action] =: ".."
- |(\%action as lua\)(nomsu, setmetatable({}, {__index=vars}))
+parse (return) as lua code "do return end"
+parse (return %return-value) as lua code "do return \(%return-value)"
+parse (do %action) as lua expr ".."
+ |(\(%action))(nomsu, setmetatable({}, {__index=vars}))
# GOTOs
-macro statement [-> %label] =: ".."
- |::label_\nomsu "var_to_lua_identifier" [%label]\::
-macro statement [go to %label] =: ".."
- |goto label_\nomsu "var_to_lua_identifier" [%label]\
+parse (-> %label) as lua code ".."
+ |::label_\(nomsu "var_to_lua_identifier" [%label])::
+parse (go to %label) as lua code ".."
+ |goto label_\(nomsu "var_to_lua_identifier" [%label])
# Loop control flow
-macro statement [stop, stop loop, break] =: "break"
-macro statement [stop for, stop for-loop, break for] =: "goto break_for"
-macro statement [stop repeat, stop repeat-loop, break repeat] =: "goto break_repeat"
-macro statement [stop %var, break %var] =: ".."
- |goto break_\nomsu "var_to_lua_identifier" [%var]\
-
-macro statement [continue, continue loop] =: "continue"
-macro statement [continue for, continue for-loop] =: "goto continue_for"
-macro statement [continue repeat, continue repeat-loop] =: "goto continue_repeat"
-macro statement [continue %var, go to next %var, on to the next %var] =: ".."
- |goto continue_\nomsu "var_to_lua_identifier" [%var]\
+parse (stop; stop loop; break) as lua code "break"
+parse (stop for; stop for-loop; break for) as lua code "goto break_for"
+parse (stop repeat; stop repeat-loop; break repeat) as lua code "goto break_repeat"
+parse (stop %var; break %var) as lua code ".."
+ |goto break_\(nomsu "var_to_lua_identifier" [%var])
+
+parse (continue; continue loop) as lua code "continue"
+parse (continue for; continue for-loop) as lua code "goto continue_for"
+parse (continue repeat; continue repeat-loop) as lua code "goto continue_repeat"
+parse (continue %var; go to next %var; on to the next %var) as lua code ".."
+ |goto continue_\(nomsu "var_to_lua_identifier" [%var])
# While loops
-macro block [repeat %body] =:
- ".."|while true do
- | \(lua expr "vars.body.value") as lua\
- | ::continue_repeat::
- |end
- |::break_repeat::
-macro block [repeat while %condition %body] =:
- ".."|while \%condition as lua\ do
- | \(lua expr "vars.body.value") as lua\
- | ::continue_repeat::
- |end
- |::break_repeat::
-macro block [repeat until %condition %body] =:
- ".."|while not (\%condition as lua\) do
- | \(lua expr "vars.body.value") as lua\
- | ::continue_repeat::
- |end
- |::break_repeat::
+parse (repeat %body) as lua block ".."
+ |while true do
+ | \(lua expr "vars.body.value")
+ | ::continue_repeat::
+ |end
+ |::break_repeat::
+parse (repeat while %condition %body) as lua block ".."
+ |while \(%condition) do
+ | \(lua expr "vars.body.value")
+ | ::continue_repeat::
+ |end
+ |::break_repeat::
+parse (repeat until %condition %body) as lua block ".."
+ |while not (\(%condition)) do
+ | \(lua expr "vars.body.value")
+ | ::continue_repeat::
+ |end
+ |::break_repeat::
# Numeric range for loops
-macro block [for %var from %start to %stop by %step %body] =:
- %var-type =: lua expr "vars.var.type"
- assert (%var-type == "Var") ".."
- |For loop has the wrong type for the loop variable. Expected Var, but got: \%var-type\
- # This trashes the loop variables, just like in Python.
- ".."
- |for i=\%start as lua\,\%stop as lua\,\%step as lua\ do
- | \%var as lua\ = i
- | \(lua expr "vars.body.value") as lua\
- | ::continue_for::
- | ::continue_\nomsu "var_to_lua_identifier" [%var]\::
- |end
- |::break_for::
- |::break_\nomsu "var_to_lua_identifier" [%var]\::
-macro block [for %var from %start to %stop %body] =:
- %thunk =: :for %var from %start to %stop by 1 %body
- lua block ".."
- |for i,x in ipairs(vars.thunk.value) do
- | if x.type == 'Var' then vars.thunk.type == vars[x.value] end
- |end
- |return compiler:run_macro(vars.thunk, 'Statement')
-
-macro block [for all %start to %stop by %step %body] =:
- # This trashes the loop variables, just like in Python.
- ".."
- |for i=\%start as lua\,\%stop as lua\,\%step as lua\ do
- | vars[''] = i
- | \(lua expr "vars.body.value") as lua\
- | ::continue_for::
- | ::continue_\nomsu "var_to_lua_identifier" [%]\::
- |end
- |::break_for::
- |::break_\nomsu "var_to_lua_identifier" [%]\::
-macro block [for %var from %start to %stop %body] =:
- %thunk =: :for %var from %start to %stop by 1 %body
- lua block ".."
- |for i,x in ipairs(vars.thunk.value) do
- | if x.type == 'Var' then vars.thunk.type == vars[x.value] end
- |end
- |return compiler:run_macro(vars.thunk, 'Statement')
-
-macro block [for %var in %iterable %body] =:
- %var-type =: lua expr "vars.var.type"
- assert (%var-type == "Var") ".."
- |For loop has the wrong type for the loop variable. Expected Var, but got: \%var-type\
+parse:
+ for %var from %start to %stop by %step %body
+ for %var from %start to %stop via %step %body
+..as lua block ".."
+ |for i=\(%start),\(%stop),\(%step) do
# This trashes the loop variables, just like in Python.
- ".."
- |for i,value in ipairs(\%iterable as lua\) do
- | \%var as lua\ = value
- | \(lua expr "vars.body.value") as lua\
- | ::continue_for::
- | ::continue_\nomsu "var_to_lua_identifier" [%var]\::
- |end
- |::break_for::
- |::break_\nomsu "var_to_lua_identifier" [%var]\::
-
-macro block [for all %iterable %body] =:
- pass # TODO: fix compiler bug
+ | \(%var) = i
+ | \(lua expr "vars.body.value")
+ | ::continue_for::
+ | ::continue_\(nomsu "var_to_lua_identifier" [%var])::
+ |end
+ |::break_for::
+ |::break_\(nomsu "var_to_lua_identifier" [%var])::
+parse (for %var from %start to %stop %body) as: for %var from %start to %stop via 1 %body
+parse:
+ for all %start to %stop by %step %body
+ for all %start to %stop via %step %body
+..as: for % from %start to %stop via %step %body
+parse (for all %start to %stop %body) as: for all %start to %stop via 1 %body
+
+parse (for %var in %iterable %body) as lua block ".."
+ |for i,value in ipairs(\(%iterable)) do
# This trashes the loop variables, just like in Python.
- ".."|for i,value in ipairs(\%iterable as lua\) do
- | vars[''] = value
- | \(lua expr "vars.body.value") as lua\
- | ::continue_for::
- | ::continue_\nomsu "var_to_lua_identifier" [%]\::
- |end
- |::break_for::
- |::break_\nomsu "var_to_lua_identifier" [%]\::
+ | \(%var) = value
+ | \(lua expr "vars.body.value")
+ | ::continue_for::
+ | ::continue_\(nomsu "var_to_lua_identifier" [%var])::
+ |end
+ |::break_for::
+ |::break_\(nomsu "var_to_lua_identifier" [%var])::
+parse (for all %iterable %body) as: for % in %iterable %body
# Switch statement/multi-branch if
-macro block [when %body] =:
+parse (when %body) as lua block:
%result =: ""
%fallthroughs =: []
for %statement in (lua expr "vars.body.value.value"):
@@ -156,15 +118,18 @@ macro block [when %body] =:
go to next %statement
if (lua expr "vars.condition.type == 'Word' and vars.condition.value == 'else'"):
- %result join=: ".."|
+ %result join=: ".."
+ |
|do
..else:
%condition =: %condition as lua
- for all %fallthroughs: %condition join=: ".."| or \% as lua\
- %result join=: ".."|
- |if \%condition\ then
- %result join=: ".."|
- | \(lua expr "vars.thunk.value") as lua\
+ for all %fallthroughs: %condition join= " or \(%)"
+ %result join=: ".."
+ |
+ |if \(%condition) then
+ %result join=: ".."
+ |
+ | \(lua expr "vars.thunk.value")
| goto finished_when
|end
@@ -174,8 +139,8 @@ macro block [when %body] =:
%result
# Switch statement
-macro block [when %branch-value == ? %body] =:
- %result =: ".."|local branch_value = \%branch-value as lua\
+parse (when %branch-value == ? %body) as lua block:
+ %result =: "local branch_value = \(%branch-value)"
%fallthroughs =: []
for %statement in (lua expr "vars.body.value.value"):
%func-call =: lua expr "vars.statement.value"
@@ -197,15 +162,18 @@ macro block [when %branch-value == ? %body] =:
go to next %statement
if (lua expr "vars.condition.type == 'Word' and vars.condition.value == 'else'"):
- %result join=: ".."|
+ %result join=: ".."
+ |
|do
..else:
- %condition =: ".."|branch_value == (\%condition as lua\)
- for all %fallthroughs: %condition join=: ".."| or (branch_value == \% as lua\)
- %result join=: ".."|
+ %condition =: "branch_value == (\(%condition))"
+ for all %fallthroughs: %condition join= " or (branch_value == \(%))"
+ %result join=: ".."
+ |
|if \%condition\ then
- %result join=: ".."|
- | \(lua expr "vars.thunk.value") as lua\
+ %result join=: ".."
+ |
+ | \((lua expr "vars.thunk.value"))
| goto finished_when
|end
@@ -213,4 +181,3 @@ macro block [when %branch-value == ? %body] =:
%result join=: "\n::finished_when::"
%result
-
diff --git a/lib/metaprogramming.nom b/lib/metaprogramming.nom
index e60a235..301d74b 100644
--- a/lib/metaprogramming.nom
+++ b/lib/metaprogramming.nom
@@ -27,7 +27,6 @@ lua code ".."
|end
|nomsu:def("parse nomsu %shorthand as nomsu %longhand", parse_as)
parse nomsu \(parse %shorthand as %longhand) as nomsu \(parse nomsu \%shorthand as nomsu \%longhand)
-parse (foo %x) as (baz %x)
lua code ".."
|nomsu:defmacro("lua expr %code", function(nomsu, vars)
@@ -71,17 +70,12 @@ parse (lua block %block) as:
parse (nomsu) as: lua expr "nomsu"
parse (nomsu's %key) as:
- lua expr "nomsu[\(%key as lua)]"
+ lua expr "nomsu['\(%key)']"
parse (nomsu %method %args) as:
- lua block ".."
- |local args = {"nomsu"}
- |for _,arg in ipairs(vars.args.value) do
- | table.insert(args, nomsu:tree_to_lua(arg))
- |end
- |local method_name = nomsu:repr(nomsu:tree_to_value(vars.method, vars))
- ..with value ".."
- |("nomsu[%s](%s)"):format(method_name, table.concat(args, ", "))
+ lua expr "nomsu['\(%method)'](nomsu, unpack(\(%args)))"
parse (repr %) as: nomsu "repr" [%]
+repr 5
+
# Get the source code for a function
rule (help %rule) =:
@@ -111,3 +105,18 @@ parse (source code %body) as: source code from tree \%body
parse (parse tree %code) as: repr (nomsu "tree_to_str" [\%code])
+parse (parse %code as lua code %lua) as:
+ parse nomsu \%code as nomsu:
+ nomsu "replaced_vars" [\(lua code %lua), lua expr "vars"]
+
+parse (parse %code as lua expr %lua) as:
+ parse nomsu \%code as nomsu:
+ nomsu "replaced_vars" [\(lua expr %lua), lua expr "vars"]
+
+parse (parse %code as lua block %lua) as:
+ parse nomsu \%code as nomsu:
+ nomsu "replaced_vars" [\(lua block %lua), lua expr "vars"]
+
+parse (pass) as lua code: ""
+
+pass
diff --git a/lib/operators.nom b/lib/operators.nom
index f2bb7c2..4e7bd59 100644
--- a/lib/operators.nom
+++ b/lib/operators.nom
@@ -1,25 +1,25 @@
require "lib/metaprogramming.nom"
# Literals
-macro [true, yes] =: "true"
-macro [false, no] =: "false"
-macro [nil, null] =: "nil"
-macro [inf, infinity] =: "math.huge"
-macro [nan, NaN, not a number] =: "(0/0)"
-macro statement [nop, pass] =: ""
+parse (true; yes) as lua expr "true"
+parse (false; no) as lua expr "false"
+parse (nil; null) as lua expr "nil"
+parse (inf; infinity) as lua expr "math.huge"
+parse (nan; NaN; not a number) as lua expr "(0/0)"
+parse (nop; pass) as lua code ""
# Ternary operator
-macro [%if_expr if %condition else %else_expr] =:
- pass # TODO: Fix compiler bug that doesn't parse right here
+parse (%if_expr if %condition else %else_expr) as lua expr ".."
+ |(function(nomsu, vars)
+ # TODO: fix compiler bug that breaks this code if comments immediately follow ".."
#.. Note: this uses a function instead of (condition and if_expr or else_expr)
because that breaks if %if_expr is falsey.
- ".."|(function(nomsu, vars)
- | if \%condition as lua\ then
- | return \%if_expr as lua\
- | else
- | return \%else_expr as lua\
- | end
- |end)(nomsu, vars)
+ | if \(%condition) then
+ | return \(%if_expr)
+ | else
+ | return \(%else_expr)
+ | end
+ |end)(nomsu, vars)
# Variable assignment operator, and += type versions
lua block ".."
@@ -64,11 +64,11 @@ lua block ".."
| end
| 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), [["(\\(%a) ]]..op..[[ \\(%b))"]])
|end
# == and != do equivalence checking, rather than identity checking
-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\))
+parse (%a == %b) as lua expr "nomsu.utils.equivalent(\(%a), \(%b))"
+parse (%a != %b) as lua expr "(not nomsu.utils.equivalent(\(%a), \(%b)))"
# Commutative Operators defined for up to 8 operands
# TODO: work out solution for commutative operators using more clever macros
@@ -125,5 +125,5 @@ lua block ".."
|end
# Unary operators
-macro [- %a] =: ".."|-(\%a as lua\)
-macro [not %a] =: ".."|not (\%a as lua\)
+parse (- %a) as lua expr "-(\(%a))"
+parse (not %a) as lua expr "not (\(%a))"
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))"