diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/class.nom | 2 | ||||
| -rw-r--r-- | lib/collections.nom | 123 | ||||
| -rw-r--r-- | lib/control_flow.nom | 493 | ||||
| -rw-r--r-- | lib/math.nom | 110 | ||||
| -rw-r--r-- | lib/metaprogramming.nom | 245 | ||||
| -rw-r--r-- | lib/operators.nom | 185 | ||||
| -rw-r--r-- | lib/text.nom | 64 |
7 files changed, 615 insertions, 607 deletions
diff --git a/lib/class.nom b/lib/class.nom index e65fb81..b7506f3 100644 --- a/lib/class.nom +++ b/lib/class.nom @@ -57,7 +57,7 @@ compile [object %classname %class_body] to body_lua, repr(names), repr(\%line:get_line_no()), table.concat(args, ", "), repr(repr(stub)), table.concat(arg_nomsus, ".."), - repr(nomsu:dedent(\%line.src)), + repr(nomsu:dedent(\%line:get_src())), \%class_identifier, repr(stub), repr(stub))); return ".." diff --git a/lib/collections.nom b/lib/collections.nom index 15f27ed..c31bbee 100644 --- a/lib/collections.nom +++ b/lib/collections.nom @@ -12,7 +12,7 @@ use "lib/operators.nom" compile [..] %index st to last in %list, %index nd to last in %list, %index rd to last in %list %index th to last in %list -..to "utils.nth_to_last(\(%list as lua), \(%index as lua))" +..to {expr:"utils.nth_to_last(\(%list as lua expr), \(%index as lua expr))"} parse [first in %list, first %list] as: 1 st in %list parse [last in %list, last %list] as: 1 st to last in %list @@ -32,25 +32,28 @@ action [..] if (%key is %item): return (no) return (yes) -compile [%list has key %index, %list has index %index] to ".." - ((\(%list as lua))[\(%index as lua)] ~= nil) +# Note: it's important to have the space after "[" to prevent confusion if %index is a string +compile [%list has key %index, %list has index %index] to {..} + expr: ".." + ((\(%list as lua expr))[ \(%index as lua expr)] ~= nil) +# Note: it's important to have the space after "[" to prevent confusion if %index is a string compile [..] %list doesn't have key %index, %list does not have key %index %list doesn't have index %index, %list does not have index %index -..to "((\(%list as lua))[\(%index as lua)] ~= nil)" +..to {expr:"((\(%list as lua expr))[ \(%index as lua expr)] ~= nil)"} compile [length of %list, size of %list, size %list, number of %list, len %list] to - "utils.size(\(%list as lua))" + {expr:"utils.size(\(%list as lua expr))"} compile [append %item to %list, add %item to %list] to - "table.insert(\(%list as lua), \(%item as lua))" + {statements:"table.insert(\(%list as lua expr), \(%item as lua expr))"} compile [pop from %list, remove last from %list] to - "table.remove(\(%list as lua))" + {statements:"table.remove(\(%list as lua expr))"} compile [remove index %index from %list] to - "table.remove(\(%list as lua), \(%index as lua))" + {statements:"table.remove(\(%list as lua expr), \(%index as lua expr))"} action [flatten %lists] @@ -74,14 +77,15 @@ immediately compile [%expression for %item in %iterable] to assume ((%item's "type") is "Var") or barf ".." List comprehension has the wrong type for the loop variable. Expected Var, but got: \(%item's "type") - return ".." - (function() - local comprehension = {}; - for i,\(%item as lua) in ipairs(\(%iterable as lua)) do - comprehension[i] = \(%expression as lua); - end - return comprehension; - end)() + return {..} + expr:".." + (function() + local comprehension = {}; + for i,\(%item as lua expr) in ipairs(\(%iterable as lua expr)) do + comprehension[i] = \(%expression as lua expr); + end + return comprehension; + end)() parse [%expression for all %iterable] as: %expression for % in %iterable compile [%expression for %key = %value in %iterable] to @@ -89,28 +93,31 @@ immediately List comprehension has the wrong type for the key loop variable. Expected Var, but got: \(%key's "type") assume ((%value's "type") is "Var") or barf ".." List comprehension has the wrong type for the value loop variable. Expected Var, but got: \(%value's "type") - return ".." - (function() - local comprehension = {}; - for \(%key as lua), \(%value as lua) in pairs(\(%iterable as lua)) do - comprehension[i] = \(%expression as lua) - end - return comprehension; - end)() + return {..} + expr: ".." + (function() + local comprehension = {}; + for \(%key as lua expr), \(%value as lua expr) in pairs(\(%iterable as lua expr)) do + comprehension[i] = \(%expression as lua expr) + end + return comprehension; + end)() # Dict comprehensions immediately compile [%key = %value for %item in %iterable] to assume ((%item's "type") is "Var") or barf ".." Dict comprehension has the wrong type for the loop variable. Expected Var, but got: \(%item's "type") - return ".." - (function() - local comprehension = {}; - for i,\(%item as lua) in ipairs(\(%iterable as lua)) do - comprehension[\(%key as lua)] = \(%value as lua) - end - return comprehension; - end)() + # Note: it's important to have the space after "[" to prevent confusion if %key is a string + return {..} + expr: ".." + (function() + local comprehension = {}; + for i,\(%item as lua expr) in ipairs(\(%iterable as lua expr)) do + comprehension[ \(%key as lua expr)] = \(%value as lua expr) + end + return comprehension; + end)() parse [%key = %value for all %iterable] as: %key = %value for % in %iterable compile [%key = %value for %src_key = %src_value in %iterable] to @@ -118,21 +125,24 @@ immediately Dict comprehension has the wrong type for the key loop variable. Expected Var, but got: \(%src_key's "type") assume ((%src_value's "type") is "Var") or barf ".." Dict comprehension has the wrong type for the value loop variable. Expected Var, but got: \(%src_value's "type") - return ".." - (function() - local comprehension = {}; - for \(%src_key as lua), \(%src_value as lua) in pairs(\(%iterable as lua)) do - comprehension[\(%key as lua)] = \(%value as lua); - end - return comprehension; - end)() + # Note: it's important to have the space after "[" to prevent confusion if %key is a string + return {..} + expr: ".." + (function() + local comprehension = {}; + for \(%src_key as lua expr), \(%src_value as lua expr) in pairs(\(%iterable as lua expr)) do + comprehension[ \(%key as lua expr)] = \(%value as lua expr); + end + return comprehension; + end)() # Sorting: -compile [sort %items] to "table.sort(\(%items as lua))" -compile [sort %items by %key_expr] to ".." - utils.sort(\(%items as lua), function(\(\% as lua)) - return \(%key_expr as lua); - end) +compile [sort %items] to {statements:"table.sort(\(%items as lua expr))"} +compile [sort %items by %key_expr] to {..} + statements: ".." + utils.sort(\(%items as lua expr), function(\(\% as lua expr)) + return \(%key_expr as lua expr); + end) action [%items sorted] %copy <- (% for all %items) @@ -147,16 +157,17 @@ action [unique %items] [%k for %k=%v in (%=(yes) for all %items)] # Metatable stuff -compile [set %dict's metatable to %metatable] to code ".." - setmetatable(\(%dict as lua), \(%metatable as lua)); - -compile [new counter] to "setmetatable({}, {__index=function() return 0; end})" - -compile [new default dict] to ".." - setmetatable({}, {__index=function(self, key) - t = {}; - self[key] = t; - return t; - end})" +compile [set %dict's metatable to %metatable] to {..} + statements: "setmetatable(\(%dict as lua expr), \(%metatable as lua expr));" + +compile [new counter] to {expr:"setmetatable({}, {__index=function() return 0; end})"} + +compile [new default dict] to {..} + expr: ".." + setmetatable({}, {__index=function(self, key) + t = {}; + self[key] = t; + return t; + end}) # TODO: maybe make a generator/coroutine? diff --git a/lib/control_flow.nom b/lib/control_flow.nom index 7347a29..a6f460a 100644 --- a/lib/control_flow.nom +++ b/lib/control_flow.nom @@ -8,27 +8,35 @@ use "lib/operators.nom" # No-Op immediately - compile [do nothing] to code "" - -# Return -immediately - compile [return] to code "do return; end" - compile [return %return_value] to code "do return \(%return_value as lua); end" + compile [do nothing] to {statements:""} # Conditionals immediately - compile [if %condition %if_body] to code ".." - if \(%condition as lua) then - \(%if_body as lua statements) - end --end if + compile [if %condition %if_body] to + %if_body <- (%if_body as lua) + return {..} + locals: %if_body's "locals" + statements:".." + if \(%condition as lua expr) then + \((%if_body's "statements") or "\(%if_body's "expr");") + end parse [unless %condition %unless_body] as: if (not %condition) %unless_body - compile [if %condition %if_body else %else_body, unless %condition %else_body else %if_body] to code ".." - if \(%condition as lua) then - \(%if_body as lua statements) - else - \(%else_body as lua statements) - end --end if + compile [if %condition %if_body else %else_body, unless %condition %else_body else %if_body] to + %if_body <- (%if_body as lua) + %else_body <- (%else_body as lua) + lua> ".." + local \%locals = {unpack(\%if_body.locals or {})}; + for i,loc in ipairs(\%else_body.locals or {}) do table.insert(\%locals, loc); end + utils.deduplicate(\%locals); + return {..} + locals:%locals + statements:".." + if \(%condition as lua expr) then + \((%if_body's "statements") or "\(%if_body's "expr");") + else + \((%else_body's "statements") or "\(%else_body's "expr");") + end # Conditional expression (ternary operator) #.. Note: this uses a function instead of "(condition and if_expr or else_expr)" @@ -43,151 +51,139 @@ immediately #.. If %when_true_expr is guaranteed to be truthy, we can use Lua's idiomatic equivalent of a conditional expression: (cond and if_true or if_false) if: (%when_true_expr's "type") in {Text:yes, List:yes, Dict:yes, Number:yes} - return "(\(%condition as lua) and \(%when_true_expr as lua) or \(%when_false_expr as lua))" + return {..} + expr:"(\(%condition as lua expr) and \(%when_true_expr as lua expr) or \(%when_false_expr as lua expr))" ..else #.. Otherwise, need to do an anonymous inline function (yuck, too bad lua doesn't have a proper ternary operator!) To see why this is necessary consider: (random()<.5 and false or 99) - return ".." - (function() - if \(%condition as lua) then - return \(%when_true_expr as lua); - else - return \(%when_false_expr as lua); - end - end)() - + return {..} + expr: ".." + (function() + if \(%condition as lua expr) then + return \(%when_true_expr as lua expr); + else + return \(%when_false_expr as lua expr); + end + end)() # GOTOs immediately - compile [=== %label ===, --- %label ---] to code ".." - ::label_\(nomsu "var_to_lua_identifier" [%label])::; - compile [go to %label] to code ".." - goto label_\(nomsu "var_to_lua_identifier" [%label]); + compile [=== %label ===, --- %label ---, *** %label ***] to {..} + statements:"::label_\(nomsu "var_to_lua_identifier" [%label])::;" + compile [go to %label] to {..} + statements:"goto label_\(nomsu "var_to_lua_identifier" [%label]);" # Basic loop control immediately - compile [do next] to code "continue;" - compile [stop] to code "break;" + compile [do next] to {statements:"continue;"} + compile [stop] to {statements:"break;"} # Helper function immediately - compile [for subtree %subtree where %condition in %tree %body] to code ".." - for \(%subtree as lua) in coroutine.wrap(function() nomsu:walk_tree(\(%tree as lua)) end) do - if type(\(%subtree as lua)) == 'table' and \(%subtree as lua).type then - if \(%condition as lua) then - \(%body as lua statements) + compile [if %tree has subtree %subtree where %condition %body] to + %body_lua <- (%body as lua) + %body_statements <- ((%body_lua's "statements") or "\(%body_lua's "expr");") + return {..} + locals: %body_lua's "locals" + statements:".." + for \(%subtree as lua expr) in coroutine.wrap(function() nomsu:walk_tree(\(%tree as lua expr)) end) do + if type(\(%subtree as lua expr)) == 'table' and \(%subtree as lua expr).type then + if \(%condition as lua expr) then + \%body_statements + break; + end + end end - end - end # While loops immediately - compile [do next repetition] to code "goto continue_repeat;" - compile [stop repeating] to code "goto stop_repeat;" - compile [repeat while %condition %body] to code - %continue_labels <- "" - for subtree % where - ((%'s "type") = "FunctionCall") and ((%'s "stub") == "do next repetition") - ..in %body - %continue_labels <- "\n::continue_repeat::;" - stop + compile [do next repetition] to {statements:"goto continue_repeat;"} + compile [stop repeating] to {statements:"goto stop_repeat;"} + compile [repeat while %condition %body] to + %body_lua <- (%body as lua) + %body_statements <- ((%body_lua's "statements") or "\(%body_lua's "expr");") + if %body has subtree % where + ((%'s "type") = "FunctionCall") and ((%'s "stub") is "do next repetition") + ..: <- %body_statments + "\n::continue_repeat::;" %code <- ".." - while \(%condition as lua) do - \(%body as lua statements)\ - ..\%continue_labels + while \(%condition as lua expr) do + \%body_statements end --while-loop - for subtree % where - ((%'s "type") = "FunctionCall") and ((%'s "stub") == "stop repeating") - ..in %body + if %body has subtree % where + ((%'s "type") = "FunctionCall") and ((%'s "stub") is "stop repeating") + .. %code <- ".." do -- scope of "stop repeating" label \%code ::stop_repeat::; end -- end of "stop repeating" label scope - %continue_labels <- "\n::continue_repeat::;" - stop - return %code + return {statements:%code, locals:%body_lua's "locals"} parse [repeat %body] as: repeat while (yes) %body parse [repeat until %condition %body] as: repeat while (not %condition) %body compile [..] repeat %n times %body - ..to code - %continue_labels <- "" - for subtree % where - ((%'s "type") = "FunctionCall") and ((%'s "stub") == "do next repetition") - ..in %body - %continue_labels <- "\n::continue_repeat::;" - stop - # This trashes the loop variables, just like in Python. + ..to + %body_lua <- (%body as lua) + %body_statements <- ((%body_lua's "statements") or "\(%body_lua's "expr");") + if %body has subtree % where + ((%'s "type") = "FunctionCall") and ((%'s "stub") is "do next repetition") + ..: <- %body_statements + "\n::continue_repeat::;" %code <- ".." - for i=1,\(%n as lua) do - \(%body as lua statements)\ - ..\%continue_labels + for i=1,\(%n as lua expr) do + \%body_statements end --numeric for-loop - %stop_labels <- "" - for subtree % where - ((%'s "type") = "FunctionCall") and ((%'s "stub") == "stop repeating") - ..in %body + if %body has subtree % where + ((%'s "type") = "FunctionCall") and ((%'s "stub") is "stop repeating") + .. %code <- ".." do -- scope of "stop repeating" label \%code ::stop_repeat::; end -- end of "stop repeating" label scope - %continue_labels <- "\n::continue_repeat::;" - stop - return %code + return {statements:%code, locals:%body_lua's "locals"} # For loop control flow: immediately - compile [stop %var] to code ".." - goto stop_\(nomsu "var_to_lua_identifier" [%var]); - compile [do next %var] to code ".." - goto continue_\(nomsu "var_to_lua_identifier" [%var]); + compile [stop %var] to {..} + statements:"goto stop_\(nomsu "var_to_lua_identifier" [%var]);" + compile [do next %var] to {..} + statements:"goto continue_\(nomsu "var_to_lua_identifier" [%var]);" # Numeric range for loops immediately compile [..] for %var from %start to %stop by %step %body for %var from %start to %stop via %step %body - ..to code - %continue_labels <- "" - for subtree % where + ..to + %body_lua <- (%body as lua) + %body_statements <- ((%body_lua's "statements") or "\(%body_lua's "expr");") + if %body has subtree % where ((%'s "type") = "FunctionCall") and - ((%'s "stub") == "do next %") and - ((3rd in (%'s "value"))'s "src") == (%var's "src") - ..in %body - %continue_labels <- "\n::continue_\(nomsu "var_to_lua_identifier" [%var])::;" - stop - - if: (%var's "type") is "Var" - %loop_var <- (%var as lua) - %loop_var_shim <- "" - ..else - %loop_var <- "i" - %loop_var_shim <- "\n\(%var as lua) = i;" - # This trashes the loop variables, just like in Python. + ((%'s "stub") is "do next %") and + ((3rd in (%'s "value"))'s "value") is (%var's "value") + ..: <- %body_statements + "\n::continue_\(nomsu "var_to_lua_identifier" [%var])::;" + + # This uses Lua's approach of only allowing loop-scoped variables in a loop + assume ((%var's "type") is "Var") or barf "Loop expected variable, not: \(%var's source code)" %code <- ".." - for \(%loop_var)=\(%start as lua),\(%stop as lua),\(%step as lua) do\ - ..\%loop_var_shim - \(%body as lua statements)\ - ..\%continue_labels + for \(%var as lua expr)=\(%start as lua expr),\(%stop as lua expr),\(%step as lua expr) do + \%body_statements end --numeric for-loop - for subtree % where + if %body has subtree % where ((%'s "type") = "FunctionCall") and - ((%'s "stub") == "stop %") and - ((2nd in (%'s "value"))'s "src") == (%var's "src") - ..in %body + ((%'s "stub") is "stop %") and + ((2nd in (%'s "value"))'s "value") is (%var's "value") + .. %code <- ".." do -- scope for stopping for-loop \%code ::stop_\(nomsu "var_to_lua_identifier" [%var])::; end -- end of scope for stopping for-loop - stop - return %code + return {statements:%code, locals:%body_lua's "locals"} immediately parse [for %var from %start to %stop %body] as: for %var from %start to %stop via 1 %body @@ -197,113 +193,89 @@ immediately ..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 -# For-each loop +# For-each loop (lua's "ipairs()") immediately - compile [for %var in %iterable %body] to code - %continue_labels <- "" - for subtree % where + compile [for %var in %iterable %body] to + %body_lua <- (%body as lua) + %body_statements <- ((%body_lua's "statements") or "\(%body_lua's "expr");") + if %body has subtree % where ((%'s "type") = "FunctionCall") and - ((%'s "stub") == "do next %") and - ((3rd in (%'s "value"))'s "src") == (%var's "src") - ..in %body - %continue_labels <- "\n::continue_\(nomsu "var_to_lua_identifier" [%var])::;" - stop - if: (%var's "type") is "Var" - %loop_var <- (%var as lua) - %loop_var_shim <- "" - ..else - %loop_var <- "value" - %loop_var_shim <- "\n\(%var as lua) = value;" - # This trashes the loop variables, just like in Python. + ((%'s "stub") is "do next %") and + ((3rd in (%'s "value"))'s "value") is (%var's "value") + ..: <- %body_statements + "\n::continue_\(nomsu "var_to_lua_identifier" [%var])::;" + # This uses Lua's approach of only allowing loop-scoped variables in a loop + assume ((%var's "type") is "Var") or barf "Loop expected variable, not: \(%var's source code)" %code <- ".." - for i,\%loop_var in ipairs(\(%iterable as lua)) do\ - ..\%loop_var_shim - \(%body as lua statements)\ - ..\%continue_labels + for i,\(%var as lua expr) in ipairs(\(%iterable as lua expr)) do + \%body_statements end --foreach-loop - for subtree % where + if %body has subtree % where ((%'s "type") = "FunctionCall") and - ((%'s "stub") == "stop %") and - ((2nd in (%'s "value"))'s "src") == (%var's "src") - ..in %body + ((%'s "stub") is "stop %") and + ((2nd in (%'s "value"))'s "value") is (%var's "value") + .. %code <- ".." do -- scope for stopping for-loop \%code ::stop_\(nomsu "var_to_lua_identifier" [%var])::; end -- end of scope for stopping for-loop - stop - return %code + return {statements:%code, locals:%body_lua's "locals"} + parse [for all %iterable %body] as: for % in %iterable %body # Dict iteration (lua's "pairs()") immediately - compile [for %key = %value in %iterable %body] to code - %continue_labels <- "" - for subtree % where + compile [for %key = %value in %iterable %body] to + %body_lua <- (%body as lua) + %body_statements <- ((%body_lua's "statements") or "\(%body_lua's "expr");") + if %body has subtree % where ((%'s "type") = "FunctionCall") and - ((%'s "stub") == "do next %") and - ((3rd in (%'s "value"))'s "src") == (%key's "src") - ..in %body - <- %continue_labels + "\n::continue_\(nomsu "var_to_lua_identifier" [%key])::;" - stop + ((%'s "stub") is "do next %") and + ((3rd in (%'s "value"))'s "value") is (%key's "value") + ..: <- %body_statements + "\n::continue_\(nomsu "var_to_lua_identifier" [%key])::;" - for subtree % where + if %body has subtree % where ((%'s "type") = "FunctionCall") and - ((%'s "stub") == "do next %") and - ((3rd in (%'s "value"))'s "src") == (%value's "src") - ..in %body - <- %continue_labels + "\n::continue_\(nomsu "var_to_lua_identifier" [%value])::;" - stop - - if: (%key's "type") is "Var" - %key_loop_var <- (%key as lua) - %key_loop_var_shim <- "" - ..else - %key_loop_var <- "key" - %key_loop_var_shim <- "\n\(%key as lua) = key;" - if: (%value's "type") is "Var" - %value_loop_var <- (%value as lua) - %value_loop_var_shim <- "" - ..else - %value_loop_var <- "value" - %value_loop_var_shim <- "\n\(%value as lua) = value;" - # This trashes the loop variables, just like in Python. + ((%'s "stub") is "do next %") and + ((3rd in (%'s "value"))'s "value") is (%value's "value") + ..: <- %body_statements + "\n::continue_\(nomsu "var_to_lua_identifier" [%value])::;" + + # This uses Lua's approach of only allowing loop-scoped variables in a loop + assume ((%key's "type") is "Var") or barf "Loop expected variable, not: \(%key's source code)" + assume ((%value's "type") is "Var") or barf "Loop expected variable, not: \(%value's source code)" %code <- ".." - for \%key_loop_var,\%value_loop_var in pairs(\(%iterable as lua)) do\ - ..\%key_loop_var_shim\%value_loop_var_shim - \(%body as lua statements)\ - ..\%continue_labels + for \(%key as lua expr),\(%value as lua expr) in pairs(\(%iterable as lua expr)) do + \%body_statements end --foreach-loop + %stop_labels <- "" - for subtree % where + if %body has subtree % where ((%'s "type") = "FunctionCall") and - ((%'s "stub") == "stop %") and - ((2nd in (%'s "value"))'s "src") == (%key's "src") - ..in %body - <- %stop_labels + "\n::stop_\(nomsu "var_to_lua_identifier" [%key])::;" - stop + ((%'s "stub") is "stop %") and + ((2nd in (%'s "value"))'s "value") is (%key's "value") + ..: <- %stop_labels + "\n::stop_\(nomsu "var_to_lua_identifier" [%key])::;" - for subtree % where + if %body has subtree % where ((%'s "type") = "FunctionCall") and - ((%'s "stub") == "stop %") and - ((2nd in (%'s "value"))'s "src") == (%value's "src") - ..in %body - <- %stop_labels + "\n::stop_\(nomsu "var_to_lua_identifier" [%value])::;" - stop + ((%'s "stub") is "stop %") and + ((2nd in (%'s "value"))'s "value") is (%value's "value") + ..: <- %stop_labels + "\n::stop_\(nomsu "var_to_lua_identifier" [%value])::;" if: %stop_labels is not "" %code <- ".." do -- scope for stopping for % = % loop \%code\%stop_labels end - return %code + return {statements:%code, locals:%body_lua's "locals"} # Switch statement/multi-branch if immediately - compile [when %body] to code - %result <- "" + compile [when %body] to + %code <- "" %fallthroughs <- [] + %locals <- [] %is_first <- (yes) + %seen_else <- (no) for %func_call in (%body's "value") assume ((%func_call's "type") is "FunctionCall") or barf ".." Invalid format for 'when' statement. Only '*' blocks are allowed. @@ -318,37 +290,44 @@ immediately assume %condition or barf ".." Invalid format for 'when' statement. Lines must begin with '*' and have a condition or the word "else" if: %action is (nil) - lua do> "table.insert(\%fallthroughs, \%condition)" + lua> "table.insert(\%fallthroughs, \%condition);" do next %func_call + %action <- (%action as lua) + %action_statements <- ((%action's "statements") or "\(%action's "expr");") + for %local in ((%action's "locals") or []) + lua> "table.insert(\%locals, \%local);" if: =lua "\%condition.type == 'Word' and \%condition.value == 'else'" - - <- %result + ".." + <- %code + ".." else - \(%action as lua statements) - stop + \%action_statements + %seen_else <- (yes) ..else - %condition <- (%condition as lua) + assume (not %seen_else) or barf "'else' clause needs to be last in 'when' block" + %condition <- (%condition as lua expr) for all %fallthroughs <- %condition + " or \(% as lua)" - <- %result + ".." + <- %code + ".." \("if" if %is_first else "elseif") \%condition then - \(%action as lua statements) + \%action_statements %fallthroughs <- [] %is_first <- (no) - if: %result is not "" - <- %result + "\nend" - return %result + assume (%fallthroughs = []) or barf "Unfinished fallthrough conditions in 'when' block" + if: %code is not "" + <- %code + "\nend" + lua> "utils.deduplicate(\%locals);" + return {statements:%code, locals:%locals} # Switch statement immediately - compile [when %branch_value = ? %body, when %branch_value is ? %body] to code - %result <- "" + compile [when %branch_value = ? %body, when %branch_value is ? %body] to + %code <- "" %fallthroughs <- [] + %locals <- [] %is_first <- (yes) %seen_else <- (no) for %func_call in (%body's "value") @@ -364,65 +343,82 @@ immediately lua> "table.insert(\%fallthroughs, \%condition)" do next %func_call + %action <- (%action as lua) + %action_statements <- ((%action's "statements") or "\(%action's "expr");") + for %local in ((%action's "locals") or []) + lua> "table.insert(\%locals, \%local);" + if: =lua "\%condition.type == 'Word' and \%condition.value == 'else'" assume (not %is_first) or barf "'else' clause cannot be first in 'when % = ?' block" - <- %result + ".." + <- %code + ".." else - \(%action as lua statements) + \%action_statements end %seen_else <- (yes) ..else assume (not %seen_else) or barf "'else' clause needs to be last in 'when % = ?' block" %clause <- "" if: ((%condition's "type") is "Text") or ((%condition's "type") is "Number") - %clause <- "branch_value == (\(%condition as lua))" + %clause <- "branch_value == (\(%condition as lua expr))" ..else - %clause <- "utils.equivalent(branch_value, \(%condition as lua))" + %clause <- "utils.equivalent(branch_value, \(%condition as lua expr))" for all %fallthroughs if: ((%'s "type") is "Text") or ((%'s "type") is "Number") - <- %clause + " or branch_value == (\(%condition as lua))" + <- %clause + " or branch_value == (\(%condition as lua expr))" ..else - <- %clause + " or utils.equivalent(branch_value, \(%condition as lua))" - <- %result + ".." + <- %clause + " or utils.equivalent(branch_value, \(%condition as lua expr))" + <- %code + ".." \("if" if %is_first else "elseif") \%clause then - \(%action as lua statements) + \%action_statements %fallthroughs <- [] %is_first <- (no) - assume (%result is not "") or barf "No body for 'when % = ?' block!" + assume (%fallthroughs = []) or barf "Unfinished fallthrough conditions in 'when' block" + assume (%code is not "") or barf "No body for 'when % = ?' block!" unless %seen_else - <- %result + "\nend" - %result <- ".." + <- %code + "\nend" + %code <- ".." do --when % = ? - local branch_value = \(%branch_value as lua);\ - ..\%result + local branch_value = \(%branch_value as lua expr);\ + ..\%code end --when % = ? - return %result + lua> "utils.deduplicate(\%locals);" + return {statements:%code, locals:%locals} # Try/except immediately compile [..] try %action and if it succeeds %success or if it fails %fallback try %action and if it fails %fallback or if it succeeds %success - ..to code ".." - do - local fell_through = false; - local ok, ret = pcall(function() - \(%action as lua statements) - fell_through = true; - end); - if ok then - \(%success as lua statements) - end - if not ok then - \(%fallback as lua statements) - elseif not fell_through then - return ret; - end - end + ..to + %locals <- [] + %action <- (%action as lua) + %fallback <- (%fallback as lua) + for %sub_locals in [%action's "locals", %success's "locals", %fallback's "locals"] + for %local in %sub_locals + lua> "table.insert(\%locals, \%local);" + lua> "utils.deduplicate(\%locals);" + return {..} + locals: %locals + statements: ".." + do + local fell_through = false; + local ok, ret = pcall(function() + \((%action's "statements") or "\(%action's "expr");") + fell_through = true; + end); + if ok then + \((%success's "statements") or "\(%success's "expr");") + end + if not ok then + \((%fallback's "statements") or "\(%fallback's "expr");") + elseif not fell_through then + return ret; + end + end parse [try %action] as try %action and if it succeeds: do nothing ..or if it fails: do nothing @@ -434,24 +430,39 @@ immediately # Do/finally: immediately - compile [do %action] to code - "do\n \(%action as lua statements)\nend" if ((%action's "type") is "Block") - ..else "(\(%action as lua))(nomsu);" - - compile [do %action then always %final_action] to code ".." - do - local fell_through = false; - local ok, ret1 = pcall(function() - \(%action as lua statements) - fell_through = true; - end); - local ok2, ret2 = pcall(function() - \(%final_action as lua statements) - end); - if not ok then error(ret1); end - if not ok2 then error(ret2); end - if not fell_through then - return ret1; - end - end + compile [do %action] to + %action <- (%action as lua) + return {..} + locals: %action's "locals" + statements: ".." + do + \((%action's "statements") or "\(%action's "expr");") + end + + compile [do %action then always %final_action] to + %action <- (%action as lua) + %final_action <- (%action as lua) + %locals <- [] + for %sub_locals in [%action's "locals", %final_action's "locals"] + for %local in %sub_locals + lua> "table.insert(\%locals, \%local);" + lua> "utils.deduplicate(\%locals);" + return {..} + locals: %locals + statements: ".." + do + local fell_through = false; + local ok, ret1 = pcall(function() + \((%action's "statements") or "\(%action's "expr");") + fell_through = true; + end); + local ok2, ret2 = pcall(function() + \((%final_action's "statements") or "\(%final_action's "expr");") + end); + if not ok then error(ret1); end + if not ok2 then error(ret2); end + if not fell_through then + return ret1; + end + end diff --git a/lib/math.nom b/lib/math.nom index 0210293..ea1769a 100644 --- a/lib/math.nom +++ b/lib/math.nom @@ -5,65 +5,71 @@ use "lib/metaprogramming.nom" use "lib/control_flow.nom" # Literals: -compile [infinity, inf] to "math.huge" -compile [not a number, NaN, nan] to "(0/0)" -compile [pi, Pi, PI] to "math.pi" -compile [tau, Tau, TAU] to "(2*math.pi)" -compile [golden ratio] to "((1+math.sqrt(5))/2)" -compile [e] to "math.e" +compile [infinity, inf] to {expr:"math.huge"} +compile [not a number, NaN, nan] to {expr:"(0/0)"} +compile [pi, Pi, PI] to {expr:"math.pi"} +compile [tau, Tau, TAU] to {expr:"(2*math.pi)"} +compile [golden ratio] to {expr:"((1+math.sqrt(5))/2)"} +compile [e] to {expr:"math.e"} # Functions: -compile [% as number] to "tonumber(\(% as lua))" -compile [absolute value %, | % |, abs %] to "math.abs(\(% as lua))" -compile [square root %, √%, sqrt %] to "math.sqrt(\(% as lua))" -compile [sine %, sin %] to "math.sin(\(% as lua))" -compile [cosine %, cos %] to "math.cos(\(% as lua))" -compile [tangent %, tan %] to "math.tan(\(% as lua))" -compile [arc sine %, asin %] to "math.asin(\(% as lua))" -compile [arc cosine %, acos %] to "math.acos(\(% as lua))" -compile [arc tangent %, atan %] to "math.atan(\(% as lua))" -compile [arc tangent %y/%x, atan2 %y %x] to "math.atan2(\(%y as lua), \(%x as lua))" -compile [hyperbolic sine %, sinh %] to "math.sinh(\(% as lua))" -compile [hyperbolic cosine %, cosh %] to "math.cosh(\(% as lua))" -compile [hyperbolic tangent %, tanh %] to "math.tanh(\(% as lua))" -compile [e^%, exp %] to "math.exp(\(% as lua))" -compile [natural log %, ln %, log %] to "math.log(\(% as lua))" -compile [log % base %base, log_%base %, log base %base %] to "math.log(\(% as lua), \(%base as lua))" -compile [floor %] to "math.floor(\(% as lua))" -compile [ceiling %, ceil %] to "math.ceil(\(% as lua))" -compile [round %, % rounded] to "math.floor(\(% as lua) + .5)" +compile [% as number] to {expr:"tonumber(\(% as lua expr))"} +compile [absolute value %, | % |, abs %] to {expr:"math.abs(\(% as lua expr))"} +compile [square root %, √%, sqrt %] to {expr:"math.sqrt(\(% as lua expr))"} +compile [sine %, sin %] to {expr:"math.sin(\(% as lua expr))"} +compile [cosine %, cos %] to {expr:"math.cos(\(% as lua expr))"} +compile [tangent %, tan %] to {expr:"math.tan(\(% as lua expr))"} +compile [arc sine %, asin %] to {expr:"math.asin(\(% as lua expr))"} +compile [arc cosine %, acos %] to {expr:"math.acos(\(% as lua expr))"} +compile [arc tangent %, atan %] to {expr:"math.atan(\(% as lua expr))"} +compile [arc tangent %y/%x, atan2 %y %x] to {expr:"math.atan2(\(%y as lua expr), \(%x as lua expr))"} +compile [hyperbolic sine %, sinh %] to {expr:"math.sinh(\(% as lua expr))"} +compile [hyperbolic cosine %, cosh %] to {expr:"math.cosh(\(% as lua expr))"} +compile [hyperbolic tangent %, tanh %] to {expr:"math.tanh(\(% as lua expr))"} +compile [e^%, exp %] to {expr:"math.exp(\(% as lua expr))"} +compile [natural log %, ln %, log %] to {expr:"math.log(\(% as lua expr))"} +compile [log % base %base, log_%base %, log base %base %] to {expr:"math.log(\(% as lua expr), \(%base as lua expr))"} +compile [floor %] to {expr:"math.floor(\(% as lua expr))"} +compile [ceiling %, ceil %] to {expr:"math.ceil(\(% as lua expr))"} +compile [round %, % rounded] to {expr:"math.floor(\(% as lua expr) + .5)"} action [%n to the nearest %rounder] =lua "(\%rounder)*math.floor((\%n / \%rounder) + .5)" # Any/all/none -compile [all of %items, all %items] to - "(\(joined ((% as lua) for all (%items' "value")) with " and "))" - ..if ((%items' "type") is "List") else "utils.all(\(%items as lua))" +compile [all of %items, all %items] to {..} + expr: + "(\(joined ((% as lua expr) for all (%items' "value")) with " and "))" + ..if ((%items' "type") is "List") else "utils.all(\(%items as lua expr))" parse [not all of %items, not all %items] as: not (all of %items) -compile [any of %items, any %items] to - "(\(joined ((% as lua) for all (%items' "value")) with " or "))" - ..if ((%items' "type") is "List") else "utils.any(\(%items as lua))" +compile [any of %items, any %items] to {..} + expr: + "(\(joined ((% as lua expr) for all (%items' "value")) with " or "))" + ..if ((%items' "type") is "List") else "utils.any(\(%items as lua expr))" parse [none of %items, none %items] as: not (any of %items) -compile [sum of %items, sum %items] to - "(\(joined ((% as lua) for all (%items' "value")) with " + "))" - ..if ((%items' "type") is "List") else "utils.sum(\(%items as lua))" -compile [product of %items, product %items] to - "(\(joined ((% as lua) for all (%items' "value")) with " * "))" - ..if ((%items' "type") is "List") else "utils.product(\(%items as lua))" +compile [sum of %items, sum %items] to {..} + expr: + "(\(joined ((% as lua expr) for all (%items' "value")) with " + "))" + ..if ((%items' "type") is "List") else "utils.sum(\(%items as lua expr))" +compile [product of %items, product %items] to {..} + expr: + "(\(joined ((% as lua expr) for all (%items' "value")) with " * "))" + ..if ((%items' "type") is "List") else "utils.product(\(%items as lua expr))" action [avg of %items, average of %items] =lua "(utils.sum(\%items)/#\%items)" -compile [min of %items, smallest of %items, lowest of %items] to - "utils.min(\(%items as lua))" -compile [max of %items, biggest of %items, largest of %items, highest of %items] to - "utils.max(\(%items as lua))" -compile [min of %items by %value_expr] to ".." - utils.min(\(%items as lua), function(\(\% as lua)) - return \(%value_expr as lua) - end) -compile [max of %items by %value_expr] to ".." - utils.max(\(%items as lua), function(\(\% as lua)) - return \(%value_expr as lua) - end) +compile [min of %items, smallest of %items, lowest of %items] to {..} + expr:"utils.min(\(%items as lua expr))" +compile [max of %items, biggest of %items, largest of %items, highest of %items] to {..} + expr:"utils.max(\(%items as lua expr))" +compile [min of %items by %value_expr] to {..} + expr: ".." + utils.min(\(%items as lua expr), function(\(\% as lua expr)) + return \(%value_expr as lua expr) + end) +compile [max of %items by %value_expr] to {..} + expr: ".." + utils.max(\(%items as lua expr), function(\(\% as lua expr)) + return \(%value_expr as lua expr) + end) # Random functions action [seed random with %] @@ -71,9 +77,9 @@ action [seed random with %] math.randomseed(\%); for i=1,20 do math.random(); end parse [seed random] as: seed random with (=lua "os.time()") -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 number, random, rand] to {expr:"math.random()"} +compile [random int %n, random integer %n, randint %n] to {expr:"math.random(\(%n as lua expr))"} compile [random from %low to %high, random number from %low to %high, rand %low %high] to - "math.random(\(%low as lua), \(%high as lua))" + "math.random(\(%low as lua expr), \(%high as lua expr))" action [random choice from %elements, random choice %elements, random %elements] =lua "\%elements[math.random(#\%elements)]" diff --git a/lib/metaprogramming.nom b/lib/metaprogramming.nom index e7de602..49e20ee 100644 --- a/lib/metaprogramming.nom +++ b/lib/metaprogramming.nom @@ -2,134 +2,99 @@ This File contains actions for making actions and compile-time actions and some helper functions to make that easier. -# Helper function -immediately - lua> ".." - nomsu.parse_spec = function(nomsu, spec) - if spec.type == 'List' then - local names = {}; - for i, alias in ipairs(spec.value) do - if alias.type == "FunctionCall" then - names[i] = alias.src; - elseif alias.type == "Text" then - names[i] = nomsu:tree_to_value(alias); - end - end - local junk, arg_names, junk = nomsu:get_stub(names[1]); - local args = {}; - for i, a in ipairs(arg_names) do args[i] = nomsu:var_to_lua_identifier(a); end - return names, args; - else - local alias = nomsu:tree_to_value(spec); - local junk, arg_names, junk = nomsu:get_stub(alias); - local args = {}; - for i, a in ipairs(arg_names) do args[i] = nomsu:var_to_lua_identifier(a); end - return {alias}, args; - end - end - # Compile-time action to make compile-time actions: -# TODO: reduce code duplication here immediately lua> ".." - do - local function compile_to(name_tree, body_tree, kind) - local names, args = nomsu:parse_spec(name_tree); - local declared_locals = {}; - for i, arg in ipairs(args) do declared_locals[arg] = true; end - names, args = repr(names), table.concat(args, ", "); - local body_lua = nomsu:tree_to_lua(body_tree); - if body_lua.expr and not body_lua.locals then - return [[ - nomsu:define_compile_action(]]..names..[[, ]]..repr(name_tree:get_line_no())..[[, function(]]..args..[[) - return {]]..kind..[[=]]..body_lua.expr..[[}; - end, ]]..repr(nomsu:source_code())..[[); - ]]; - end - local body_code = body_lua.statements or ("return "..body_lua.expr..";"); - local undeclared_locals = {}; - for i, body_local in ipairs(body_lua.locals or {}) do - if not declared_locals[body_local] then - table.insert(undeclared_locals, body_local); - end - end - if #undeclared_locals > 0 then - body_code = "local "..table.concat(undeclared_locals, ", ")..";\\n"..body_code; + nomsu:define_compile_action("compile %actions to %lua", \(!! code location !!), function(\%actions, \%lua) + local signature = {}; + for i, action in ipairs(\%actions.value) do signature[i] = action:get_src(); end + local stubs = nomsu:get_stubs_from_signature(signature); + local stub_args = nomsu:get_args_from_signature(signature); + local arg_set = {}; + for i, arg in ipairs(stub_args[1]) do arg_set[arg] = true; end + if \%lua.type == "Text" then + error("Invalid type for 'compile % to %', expected a dict with expr/statements, but got text.", 0); + end + local body_lua = nomsu:tree_to_lua(\%lua); + local body_code = body_lua.statements or ("return "..body_lua.expr..";"); + local undeclared_locals = {}; + for i, body_local in ipairs(body_lua.locals or {}) do + if not arg_set[body_local] then + table.insert(undeclared_locals, body_local); end - return [[ - do - local function compile_action(]]..args..[[) - ]]..body_code.."\\n"..[[ end - nomsu:define_compile_action(]]..names..[[, ]]..repr(name_tree:get_line_no())..[[, function(]]..args..[[) - return {]]..kind..[[=compile_action(]]..args..[[)}; - end, ]]..repr(nomsu:source_code())..[[); - end]]; + if #undeclared_locals > 0 then + body_code = "local "..table.concat(undeclared_locals, ", ")..";\\n"..body_code; end - local src = \(__src__ 1); - nomsu:define_compile_action("compile %names to %body", \(__line_no__), function(\%names, \%body) - return {statements=compile_to(\%names, \%body, "expr")}; - end, src); - nomsu:define_compile_action("compile %names to code %body", \(__line_no__), function(\%names, \%body) - return {statements=compile_to(\%names, \%body, "statements")}; - end, src); - end + local lua_fn_args = table.concat(stub_args[1], ", "); + local def_tree = nomsu.compilestack[#nomsu.compilestack]; + local code_location = ("%s:%s,%s"):format(def_tree.filename, def_tree.start, def_tree.stop); + return {statements=([[ + nomsu:define_compile_action(]]..repr(signature)..[[, ]]..repr(code_location)..[[, function(]]..lua_fn_args..[[) + ]]..body_code.."\\n"..[[ + end); + ]])}; + end); # Compile-time action to make actions immediately - compile [action %names %body] to code + compile [action %actions %body] to lua> ".." - local names, args = nomsu:parse_spec(\%names); - local declared_locals = {}; - for i, arg in ipairs(args) do declared_locals[arg] = true; end - names, args = repr(names), table.concat(args, ", "); + local signature = {}; + for i, action in ipairs(\%actions.value) do signature[i] = action:get_src(); end + local stubs = nomsu:get_stubs_from_signature(signature); + local stub_args = nomsu:get_args_from_signature(signature); + local arg_set = {}; + for i, arg in ipairs(stub_args[1]) do arg_set[arg] = true; end local body_lua = nomsu:tree_to_lua(\%body); local body_code = body_lua.statements or ("return "..body_lua.expr..";"); local undeclared_locals = {}; for i, body_local in ipairs(body_lua.locals or {}) do - if not declared_locals[body_local] then + if not arg_set[body_local] then table.insert(undeclared_locals, body_local); end end if #undeclared_locals > 0 then body_code = "local "..table.concat(undeclared_locals, ", ")..";\\n"..body_code; end - local src = nomsu:dedent(nomsu:source_code(0)); - local def_lua = ([[ - nomsu:define_action(%s, \(__line_no__), function(%s) - %s - end, %s);]]):format(names, args, body_code, repr(src)); - return def_lua; + local lua_fn_args = table.concat(stub_args[1], ", "); + local def_tree = nomsu.compilestack[#nomsu.compilestack]; + local code_location = ("%s:%s,%s"):format(def_tree.filename, def_tree.start, def_tree.stop); + return {statements=[[ + nomsu:define_action(]]..repr(signature)..[[, ]]..repr(code_location)..[[, function(]]..lua_fn_args..[[) + ]]..body_code.."\\n"..[[ + end); + ]]}; # Macro to make nomsu macros: immediately - lua> ".." - nomsu:define_compile_action("parse %shorthand as %longhand", \(__line_no__), (function(\%shorthand, \%longhand) - local names, args = nomsu:parse_spec(\%shorthand); - names, args = repr(names), table.concat(args, ", "); + compile [parse %shorthand as %longhand] to + lua> ".." + local signature = {}; + for i, action in ipairs(\%shorthand.value) do signature[i] = action:get_src(); end + local stubs = nomsu:get_stubs_from_signature(signature); + local stub_args = nomsu:get_args_from_signature(signature); + local lua_fn_args = table.concat(stub_args[1], ", "); local template; if \%longhand.type == "Block" then - template = {}; - for i, line in ipairs(\%longhand.value) do - template[i] = nomsu:dedent(line.src); - end - template = repr(table.concat(template, "\\n")); + local lines = {}; + for i, line in ipairs(\%longhand.value) do lines[i] = nomsu:dedent(line:get_src()); end + template = repr(table.concat(lines, "\\n")); else - template = repr(nomsu:dedent(\%longhand.src)); + template = repr(nomsu:dedent(\%longhand:get_src())); end - local junk, arg_names, junk = nomsu:get_stub(\%shorthand.value[1]); local replacements = {}; - for i, a in ipairs(arg_names) do replacements[i] = "["..repr(a).."]="..nomsu:var_to_lua_identifier(a); end + for i, a in ipairs(stub_args[1]) do replacements[i] = a.."="..a; end replacements = "{"..table.concat(replacements, ", ").."}"; - local lua_code = ([[ - nomsu:define_compile_action(%s, %s, (function(%s) - local template = nomsu:parse(%s, %s); - local replacement = nomsu:tree_with_replaced_vars(template, %s); - return nomsu:tree_to_lua(replacement); - end), %s)]]):format(names, repr(\%shorthand:get_line_no()), args, template, - repr(\%shorthand:get_line_no()), replacements, repr(nomsu:source_code(0))); - return {statements=lua_code}; - end), \(__src__ 1)); + local def_tree = nomsu.compilestack[#nomsu.compilestack]; + local code_location = ("%s:%s,%s"):format(def_tree.filename, def_tree.start, def_tree.stop); + return {statements=[[ + nomsu:define_compile_action(]]..repr(signature)..[[, ]]..repr(code_location)..[[, function(]]..lua_fn_args..[[) + local template = nomsu:parse(]]..template..[[, ]]..repr(def_tree.filename)..[[); + local replacement = nomsu:tree_with_replaced_vars(template, ]]..replacements..[[); + return nomsu:tree_to_lua(replacement); + end); + ]]}; action [remove action %stub] lua> ".." @@ -143,33 +108,41 @@ action [remove action %stub] immediately action [%tree as lua] - =lua "nomsu:tree_to_lua(\%tree).expr" + =lua "nomsu:tree_to_lua(\%tree)" + + action [%tree as lua expr] + lua> ".." + local lua = nomsu:tree_to_lua(\%tree); + if lua.locals or not lua.expr then + error("Invalid thing to convert to lua expr: "..\%tree:get_src()); + end + return lua.expr; + action [%tree as lua statements] lua> ".." local lua = nomsu:tree_to_lua(\%tree); - return lua.statements or (lua.expr..";"); + local code = lua.statements or (lua.expr..";"); + if lua.locals then + code = "local "..table.concat(lua.locals, ", ")..";\\n"..code; + end + return code; + action [%tree as value] =lua "nomsu:tree_to_value(\%tree)" - compile [repr %obj] to - "repr(\(%obj as lua))" - compile [type of %obj] to - "type(\(%obj as lua))" - + immediately - parse [lua do> %block] as - lua> "do" - lua> %block - lua> "end" + compile [%tree's source code, %tree' source code] to {expr:"\(%tree as lua expr):get_src()"} -compile [nomsu] to "nomsu" + compile [repr %obj] to {expr:"repr(\(%obj as lua expr))"} + compile [type of %obj] to {expr:"type(\(%obj as lua expr))"} -compile [nomsu's %key] to "nomsu[\(%key as lua)]" -compile [nomsu %method %args] to "nomsu[\(%method as lua)](nomsu, unpack(\(%args as lua)))" -compile [tree %tree with %replacements] to ".." - nomsu:tree_with_replaced_vars(\(%tree as lua), \(%replacements as lua)) +compile [nomsu] to {expr:"nomsu"} + +compile [nomsu's %key] to {expr:"nomsu[\(%key as lua expr)]"} +compile [nomsu %method %args] to {expr:"nomsu[\(%method as lua expr)](nomsu, unpack(\(%args as lua expr)))"} action [action %names metadata] - =lua "ACTION_METADATA[ACTIONS[\%names]]" + =lua "ACTION_METADATA[ACTIONS[\%names]]" # Get the source code for a function action [help %action] @@ -186,22 +159,34 @@ parse [run %code] as: nomsu "run" [%code] parse [enable debugging] as: lua> "nomsu.debug = true" parse [disable debugging] as: lua> "nomsu.debug = false" -compile [say %message] to - lua> ".." - if \%message.type == "Text" then - return "nomsu:writeln("..\(%message as lua)..")"; - else - return "nomsu:writeln(stringify("..\(%message as lua).."))"; - end +immediately + compile [say %message] to + lua> ".." + if \%message.type == "Text" then + return {statements="nomsu:writeln("..\(%message as lua expr)..");"}; + else + return {statements="nomsu:writeln(stringify("..\(%message as lua expr).."));"}; + end + +# Return +immediately + #.. Return statement is wrapped in a do..end block because Lua is unhappy if you + put code after a return statement, unless you wrap it in a block. + compile [return] to {statements:"do return; end"} + compile [return %return_value] to {statements:"do return \(%return_value as lua expr); end"} # Error functions -compile [barf!] to "error(nil, 0)" -compile [barf %msg] to "error(\(%msg as lua), 0)" -compile [assume %condition] to "assert(\(%condition as lua))" -compile [assume %condition or barf %msg] to "assert(\(%condition as lua), \(%msg as lua))" +immediately + compile [barf!] to {statements:"error(nil, 0);"} + compile [barf %msg] to {statements:"error(\(%msg as lua expr), 0);"} + compile [assume %condition] to {..} + statements:"if not \(%condition as lua expr) then error('Assumption failed: '..\%condition:get_src(), 0); end" + compile [assume %condition or barf %msg] to {..} + statements:"if not \(%condition as lua expr) then error(\(%msg as lua expr), 0); end" # Literals -compile [yes] to "true" -compile [no] to "false" -compile [nothing, nil, null] to "nil" +immediately + compile [yes] to {expr:"true"} + compile [no] to {expr:"false"} + compile [nothing, nil, null] to {expr:"nil"} diff --git a/lib/operators.nom b/lib/operators.nom index 32e29db..8be0ea0 100644 --- a/lib/operators.nom +++ b/lib/operators.nom @@ -16,138 +16,131 @@ immediately %key st in %obj, %key nd in %obj, %key rd in %obj, %key th in %obj, ..to lua> ".." - local obj_lua = \(%obj as lua); + local obj_lua = \(%obj as lua expr); if not obj_lua:sub(-1,-1):match("[a-zA-Z)]") then obj_lua = "("..obj_lua..")"; end - local key_lua = \(%key as lua); + local key_lua = \(%key as lua expr); local key_attr = (key_lua:match("'([a-zA-Z][a-zA-Z0-9]*)'") or key_lua:match('"([a-zA-Z][a-zA-Z0-9]*)"')); if key_attr then - return obj_lua.."."..key_attr; + return {expr=obj_lua.."."..key_attr}; elseif key_lua:sub(1,1) == "[" then key_lua = " "..key_lua.." "; end - return obj_lua.."["..key_lua.."]"; + return {expr=obj_lua.."["..key_lua.."]"}; # Comparison Operators immediately - compile [%x < %y] to "(\(%x as lua) < \(%y as lua))" - compile [%x > %y] to "(\(%x as lua) > \(%y as lua))" - compile [%x <= %y] to "(\(%x as lua) <= \(%y as lua))" - compile [%x >= %y] to "(\(%x as lua) >= \(%y as lua))" + compile [%x < %y] to {expr:"(\(%x as lua expr) < \(%y as lua expr))"} + compile [%x > %y] to {expr:"(\(%x as lua expr) > \(%y as lua expr))"} + compile [%x <= %y] to {expr:"(\(%x as lua expr) <= \(%y as lua expr))"} + compile [%x >= %y] to {expr:"(\(%x as lua expr) >= \(%y as lua expr))"} compile [%a is %b, %a = %b, %a == %b] to lua> ".." local safe = {Text=true, Number=true}; local a_lua, b_lua = nomsu:tree_to_lua(\%a).expr, nomsu:tree_to_lua(\%b).expr; if safe[\%a.type] or safe[\%b.type] then - return "("..a_lua.." == "..b_lua..")"; + return {expr="("..a_lua.." == "..b_lua..")"}; else - return "utils.equivalent("..a_lua..", "..b_lua..")"; + return {expr="utils.equivalent("..a_lua..", "..b_lua..")"}; end compile [%a isn't %b, %a is not %b, %a not= %b, %a != %b] to lua> ".." local safe = {Text=true, Number=true}; local a_lua, b_lua = nomsu:tree_to_lua(\%a).expr, nomsu:tree_to_lua(\%b).expr; if safe[\%a.type] or safe[\%b.type] then - return "("..a_lua.." ~= "..b_lua..")"; + return {expr="("..a_lua.." ~= "..b_lua..")"}; else - return "(not utils.equivalent("..a_lua..", "..b_lua.."))"; + return {expr="(not utils.equivalent("..a_lua..", "..b_lua.."))"}; end # For strict identity checking, use (%x's id) is (%y's id) - compile [%'s id, id of %] to "nomsu.ids[\(% as lua)]" + compile [%'s id, id of %] to {expr:"nomsu.ids[\(% as lua expr)]"} # Variable assignment operator immediately - lua> ".." - nomsu:define_compile_action("%var <- %value", \(__line_no__), function(\%var, \%value) - local lua = {}; - lua.statements = ("%s = %s;"):format( - assert(nomsu:tree_to_lua(\%var).expr, "Invalid target for assignment: "..\%var.src), - assert(nomsu:tree_to_lua(\%value).expr, "Invalid value for assignment: "..\%value.src)); - if \%var.type == "Var" then - lua.locals = {nomsu:tree_to_lua(\%var).expr}; - end - return lua; - end, \(__src__ 1)); + compile [%var <- %value] to + lua> "local \%var_lua = nomsu:tree_to_lua(\%var);" + assume (%var_lua's "expr") or barf "Invalid target for assignment: \(%var's source code)" + lua> "local \%value_lua = nomsu:tree_to_lua(\%value);" + assume (%value_lua's "expr") or barf "Invalid value for assignment: \(%value's source code)" + return {..} + statements:"\(%var_lua's "expr") = \(%value_lua's "expr");" + locals: =lua "(\%var.type == 'Var' and {\%var_lua.expr} or nil)" - lua> ".." - nomsu:define_compile_action("export %var <- %value", \(__line_no__), function(\%var, \%value) - local lua = {}; - lua.statements = ("%s = %s;"):format( - assert(nomsu:tree_to_lua(\%var).expr, "Invalid target for assignment: "..\%var.src), - assert(nomsu:tree_to_lua(\%value).expr, "Invalid value for assignment: "..\%value.src)); - return lua; - end, \(__src__ 1)); +immediately + compile [export %var <- %value] to + %var_lua <- (%var as lua) + assume (%var_lua's "expr") or barf "Invalid target for assignment: \(%var's source code)" + %value_lua <- (%value as lua) + assume (%value_lua's "expr") or barf "Invalid value for assignment: \(%value's source code)" + return {statements:"\(%var_lua's "expr") = \(%value_lua's "expr");"} - lua> ".." - nomsu:define_compile_action("with %assignments %body", \(__line_no__), function(\%assignments, \%body) - local body_lua = nomsu:tree_to_lua(\%body); - local declarations = ""; - local leftover_locals = {}; - for _, body_local in ipairs(body_lua.locals or {}) do - leftover_locals[body_local] = true; - end - assert(\%assignments.type == "List", - "Expected a List for the assignments part of 'with' statement, not "..\%assignments.src); + compile [with %assignments %body] to + %body_lua <- (%body as lua) + %locals <- [] + %declarations <- [] + %leftover_locals <- (=lua "{unpack(\%body_lua.locals or {})}") + assume ((%assignments' "type") is "List") or barf ".." + Expected a List for the assignments part of 'with' statement, not \(%assignments' source code) + lua> ".." for i, item in ipairs(\%assignments.value) do if item.type == "Var" then local var = nomsu:tree_to_lua(item).expr; - leftover_locals[var] = nil; - declarations = declarations.."local "..var..";\\n "; + utils.remove_from_list(\%leftover_locals, var); + table.insert(\%locals, var); else - assert(item.type == "FunctionCall" and #item.value == 3 and item.value[2].src == "<-", - "'with' statement expects entries of the form: '%var <- %value', not: "..item.src); + if not (item.type == "FunctionCall" and #item.value == 3 and item.value[2].value == "<-") then + error("'with' statement expects entries of the form: '%var <- %value', not: "..item:get_src()); + end local target, value = item.value[1], item.value[3]; + local target_lua = nomsu:tree_to_lua(target); + if not target_lua.expr then error("Invalid target for assignment: "..target:get_src()); end + local value_lua = nomsu:tree_to_lua(value); + if not value_lua.expr then error("Invalid value for assignment: "..value:get_src()); end if target.type == "Var" then - local var = nomsu:tree_to_lua(target).expr; - leftover_locals[var] = nil; - declarations = declarations..(("local %s = %s;\\n "):format( - var, assert(nomsu:tree_to_lua(value).expr, "Invalid value for assignment: "..value.src))); - else - declarations = declarations..(("%s = %s;\\n "):format( - assert(nomsu:tree_to_lua(target).expr, "Invalid target for assignment: "..target.src), - assert(nomsu:tree_to_lua(value).expr, "Invalid value for assignment: "..value.src))); + utils.remove_from_list(\%leftover_locals, target_lua.expr); end + table.insert(\%declarations, (("local %s = %s;\\n "):format( + target_lua.expr, value_lua.expr))); end end - local code = ([[ - do - %s%s - end]]):format(declarations, body_lua.statements or (body_lua.expr..";")); - return {statements=code, locals=utils.keys(leftover_locals)}; - end, \(__src__ 1)); - - lua> ".." - nomsu:define_compile_action("exporting %exported %body", \(__line_no__), function(\%exported, \%body) - local body_lua = nomsu:tree_to_lua(\%body); - local declarations = ""; - local leftover_locals = {}; - for _, body_local in ipairs(body_lua.locals or {}) do - leftover_locals[body_local] = true; + local locals_code = ""; + if #\%locals > 0 then + locals_code = "\\nlocal "..table.concat(\%locals, ", ")..";"; + end + local declaration_code = ""; + if #\%declarations > 0 then + declaration_code = "\\n"..table.concat(\%declarations, "\\n"); end - assert(\%exported.type == "List", - "Expected a List for the export part of 'exporting' statement, not "..\%exported.src); + return {locals=\%leftover_locals, statements=([[ + do%s%s + %s + end]]):format(locals_code, declaration_code, \%body_lua.statements or (\%body_lua.expr..";"))}; + + compile [exporting %exported %body] to + %body_lua <- (%body as lua) + %leftover_locals <- (=lua "{unpack(\%body_lua.locals or {})}") + assume ((%exported's "type") = "List") or barf ".." + Expected a List for the export part of 'exporting' statement, not \(%exported's source code) + lua> ".." for i, item in ipairs(\%exported.value) do - assert(item.type == "Var", "exporting statement expects Vars, not: "..item.src); + if item.type ~= "Var" then + error("'exporting' statement expects Vars, not: "..item:get_src()); + end local var = nomsu:tree_to_lua(item).expr; - leftover_locals[var] = nil; + utils.remove_from_list(leftover_locals, var); end - local code = ([[ - do - %s%s - end]]):format(declarations, body_lua.statements or (body_lua.expr..";")); - return {statements=code, locals=utils.keys(leftover_locals)}; - end, \(__src__ 1)); + return {locals:%leftover_locals, statements:=lua "\%body_lua.statements or (\%body_lua.expr..';')"} immediately # Math Operators - compile [%x + %y] to "(\(%x as lua) + \(%y as lua))" - compile [%x - %y] to "(\(%x as lua) - \(%y as lua))" - compile [%x * %y] to "(\(%x as lua) * \(%y as lua))" - compile [%x / %y] to "(\(%x as lua) / \(%y as lua))" - compile [%x ^ %y] to "(\(%x as lua) ^ \(%y as lua))" - compile [%x wrapped around %y, %x mod %y] to "(\(%x as lua) % \(%y as lua))" + compile [%x + %y] to {expr:"(\(%x as lua expr) + \(%y as lua expr))"} + compile [%x - %y] to {expr:"(\(%x as lua expr) - \(%y as lua expr))"} + compile [%x * %y] to {expr:"(\(%x as lua expr) * \(%y as lua expr))"} + compile [%x / %y] to {expr:"(\(%x as lua expr) / \(%y as lua expr))"} + compile [%x ^ %y] to {expr:"(\(%x as lua expr) ^ \(%y as lua expr))"} + compile [%x wrapped around %y, %x mod %y] to {expr:"(\(%x as lua expr) % \(%y as lua expr))"} # 3-part chained comparisons # (uses a lambda to avoid re-evaluating middle value, while still being an expression) @@ -162,22 +155,22 @@ immediately # TODO: optimize for common case where x,y,z are all either variables or number literals # Boolean Operators - compile [%x and %y] to "(\(%x as lua) and \(%y as lua))" - compile [%x or %y] to "(\(%x as lua) or \(%y as lua))" + compile [%x and %y] to {expr:"(\(%x as lua expr) and \(%y as lua expr))"} + compile [%x or %y] to {expr:"(\(%x as lua expr) or \(%y as lua expr))"} # Bitwise Operators - compile [%a OR %b, %a | %b] to "bit32.bor(\(%a as lua), \(%b as lua))" - compile [%a XOR %b] to "bit32.bxor(\(%a as lua), \(%b as lua))" - compile [%a AND %b, %a & %b] to "bit32.band(\(%a as lua), \(%b as lua))" - compile [NOT %, ~ %] to "bit32.bnot(\(% as lua))" - compile [%x LSHIFT %shift, %x << %shift] to "bit32.lshift(\(%x as lua), \(%shift as lua))" - compile [%x RSHIFT %shift, %x >>> %shift] to "bit32.rshift(\(%x as lua), \(%shift as lua))" - compile [%x ARSHIFT %shift, %x >> %shift] to "bit32.arshift(\(%x as lua), \(%shift as lua))" + compile [%a OR %b, %a | %b] to {expr:"bit32.bor(\(%a as lua expr), \(%b as lua expr))"} + compile [%a XOR %b] to {expr:"bit32.bxor(\(%a as lua expr), \(%b as lua expr))"} + compile [%a AND %b, %a & %b] to {expr:"bit32.band(\(%a as lua expr), \(%b as lua expr))"} + compile [NOT %, ~ %] to {expr:"bit32.bnot(\(% as lua expr))"} + compile [%x LSHIFT %shift, %x << %shift] to {expr:"bit32.lshift(\(%x as lua expr), \(%shift as lua expr))"} + compile [%x RSHIFT %shift, %x >>> %shift] to {expr:"bit32.rshift(\(%x as lua expr), \(%shift as lua expr))"} + compile [%x ARSHIFT %shift, %x >> %shift] to {expr:"bit32.arshift(\(%x as lua expr), \(%shift as lua expr))"} # TODO: implement OR, XOR, AND for multiple operands? # Unary operators - compile [- %] to "(- \(% as lua))" - compile [not %] to "(not \(% as lua))" + compile [- %] to {expr:"(- \(% as lua expr))"} + compile [not %] to {expr:"(not \(% as lua expr))"} # Update operators immediately @@ -188,4 +181,4 @@ immediately parse [<- %var ^ %] as: %var <- (%var ^ %) parse [<- %var and %] as: %var <- (%var and %) parse [<- %var or %] as: %var <- (%var or %) - parse [wrap %var around %] as: "\(%var as lua) = \(%var as lua) % \(% as lua);" + parse [wrap %var around %] as: %var <- (%var wrapped around %) diff --git a/lib/text.nom b/lib/text.nom index 0bd7a8a..982db63 100644 --- a/lib/text.nom +++ b/lib/text.nom @@ -13,41 +13,43 @@ action [%texts joined with %glue] parse [joined %texts, %texts joined] as: %texts joined with "" compile [capitalized %text capitalized] to - "(\(%text as lua)):gsub('%l', string.upper, 1)" + {expr:"(\(%text as lua expr)):gsub('%l', string.upper, 1)"} compile [%text with %sub instead of %patt, %text s/%patt/%sub] to - "((\(%text as lua)):gsub(\(%patt as lua), \(%sub as lua)))" + {expr:"((\(%text as lua expr)):gsub(\(%patt as lua expr), \(%sub as lua expr)))"} -compile [indented %text, %text indented] to "\%text:gsub('\\n','\\n'..(' '))" -compile [dedented %obj, %obj dedented] to "nomsu:dedent(\(%obj as lua))" -compile [%text indented %n times] to "\%text:gsub('\\n','\\n'..(' '):rep(\%n))" +compile [indented %text, %text indented] to {expr:"\%text:gsub('\\n','\\n'..(' '))"} +compile [dedented %obj, %obj dedented] to {expr:"nomsu:dedent(\(%obj as lua expr))"} +compile [%text indented %n times] to {expr:"\%text:gsub('\\n','\\n'..(' '):rep(\%n))"} # Text literals -lua do> ".." - local escapes = { - nl="\\\\n", newline="\\\\n", tab="\\\\t", bell="\\\\a", cr="\\\\r", ["carriage return"]="\\\\r", - backspace="\\\\b", ["form feed"]="\\\\f", formfeed="\\\\f", ["vertical tab"]="\\\\v", - }; - local colors = { - ["reset color"]="\\\\27[0m", bright="\\\\27[1m", dim="\\\\27[2m", underscore="\\\\27[4m", - blink="\\\\27[5m", inverse="\\\\27[7m", hidden="\\\\27[8m", - - black="\\\\27[30m", red="\\\\27[31m", green="\\\\27[32m", yellow="\\\\27[33m", blue="\\\\27[34m", - magenta="\\\\27[35m", cyan="\\\\27[36m", white="\\\\27[37m", - - ["on black"]="\\\\27[40m", ["on red"]="\\\\27[41m", ["on green"]="\\\\27[42m", ["on yellow"]="\\\\27[43m", - ["on blue"]="\\\\27[44m", ["on magenta"]="\\\\27[45m", ["on cyan"]="\\\\27[46m", ["on white"]="\\\\27[47m", - }; - for name, e in pairs(escapes) do - local lua = "'"..e.."'"; - nomsu:define_compile_action(name, \(__line_no__), function() return {expr=text}; end, \(__src__ 1)); - end - for name, c in pairs(colors) do - local color = "'"..c.."'"; - local reset = "'"..colors["reset color"].."'"; - nomsu:define_compile_action(name, \(__line_no__), function() return {expr=color}; end, \(__src__ 1)); - nomsu:define_compile_action(name.." %", \(__line_no__), function(\%) - return {expr=color..".."..nomsu:tree_to_lua(\%).expr..".."..reset}; - end, \(__src__ 1)); +lua> ".." + do + local escapes = { + nl="\\\\n", newline="\\\\n", tab="\\\\t", bell="\\\\a", cr="\\\\r", ["carriage return"]="\\\\r", + backspace="\\\\b", ["form feed"]="\\\\f", formfeed="\\\\f", ["vertical tab"]="\\\\v", + }; + for name, e in pairs(escapes) do + local lua = "'"..e.."'"; + nomsu:define_compile_action(name, \(!! code location !!), function() return {expr=text}; end); + end + local colors = { + ["reset color"]="\\\\27[0m", bright="\\\\27[1m", dim="\\\\27[2m", underscore="\\\\27[4m", + blink="\\\\27[5m", inverse="\\\\27[7m", hidden="\\\\27[8m", + + black="\\\\27[30m", red="\\\\27[31m", green="\\\\27[32m", yellow="\\\\27[33m", blue="\\\\27[34m", + magenta="\\\\27[35m", cyan="\\\\27[36m", white="\\\\27[37m", + + ["on black"]="\\\\27[40m", ["on red"]="\\\\27[41m", ["on green"]="\\\\27[42m", ["on yellow"]="\\\\27[43m", + ["on blue"]="\\\\27[44m", ["on magenta"]="\\\\27[45m", ["on cyan"]="\\\\27[46m", ["on white"]="\\\\27[47m", + }; + for name, c in pairs(colors) do + local color = "'"..c.."'"; + local reset = "'"..colors["reset color"].."'"; + nomsu:define_compile_action(name, \(!! code location !!), function() return {expr=color}; end); + nomsu:define_compile_action(name.." %", \(!! code location !!), function(\%) + return {expr=color..".."..nomsu:tree_to_lua(\%).expr..".."..reset}; + end); + end end |
