diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/collections.nom | 56 | ||||
| -rw-r--r-- | core/control_flow.nom | 126 | ||||
| -rw-r--r-- | core/math.nom | 16 | ||||
| -rw-r--r-- | core/metaprogramming.nom | 72 | ||||
| -rw-r--r-- | core/operators.nom | 30 | ||||
| -rw-r--r-- | core/text.nom | 2 |
6 files changed, 151 insertions, 151 deletions
diff --git a/core/collections.nom b/core/collections.nom index d4d4c5b..97bc7ad 100644 --- a/core/collections.nom +++ b/core/collections.nom @@ -9,18 +9,18 @@ use "core/operators.nom" # List/dict functions: # Indexing -immediately: +immediately 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: Lua value "utils.nth_to_last(\(%list as lua expr), \(%index as lua expr))" -immediately: +immediately 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 -immediately: +immediately action [%item is in %list, %list contains %item, %list has %item] for %key = %value in %list if (%key is %item): return (yes) @@ -30,12 +30,12 @@ immediately: %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 is %item): return (no) return (yes) -immediately: +immediately # 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 Lua value ".." @@ -47,21 +47,21 @@ immediately: %list doesn't have index %index, %list does not have index %index ..to: Lua value "((\(%list as lua expr))[ \(%index as lua expr)] == nil)" - compile [number of keys in %list] to: + compile [number of keys in %list] to Lua value "utils.size(\(%list as lua expr))" - compile [append %item to %list, add %item to %list, to %list add %item, to %list append %item] to: + compile [append %item to %list, add %item to %list, to %list add %item, to %list append %item] to Lua "table.insert(\(%list as lua expr), \(%item as lua expr))" - compile [pop from %list, remove last from %list] to: + compile [pop from %list, remove last from %list] to Lua "table.remove(\(%list as lua expr))" - compile [remove index %index from %list] to: + compile [remove index %index from %list] to Lua "table.remove(\(%list as lua expr), \(%index as lua expr))" # List Comprehension -immediately: - compile [%expression for %item in %iterable] to: +immediately + compile [%expression for %item in %iterable] to assume (%item.type is "Var") or barf ".." List comprehension has the wrong type for the loop variable. Expected Var, but got: \(%item.type) return @@ -78,7 +78,7 @@ immediately: compile [..] %expression for %index from %start to %stop via %step %expression for %index from %start to %stop by %step - ..to: + ..to assume (%index.type is "Var") or barf ".." List comprehension has the wrong type for the loop variable. Expected Var, but got: \(%index.type) return @@ -98,7 +98,7 @@ immediately: ..as: %expression for % from %start to %stop via %step parse [%expression for all %start to %stop] as: %expression for all %start to %stop via 1 - compile [%expression for %key = %value in %iterable] to: + compile [%expression for %key = %value in %iterable] to assume (%key.type is "Var") or barf ".." List comprehension has the wrong type for the key loop variable. Expected Var, but got: \(%key.type) assume (%value.type is "Var") or barf ".." @@ -114,8 +114,8 @@ immediately: end)() # Dict comprehensions -immediately: - compile [%key = %value for %item in %iterable] to: +immediately + compile [%key = %value for %item in %iterable] to assume (%item.type is "Var") or barf ".." Dict comprehension has the wrong type for the loop variable. Expected Var, but got: \(%item.type) # Note: it's important to have the space after "[" to prevent confusion if %key is a string @@ -130,7 +130,7 @@ immediately: end)() parse [%key = %value for all %iterable] as: %key = %value for % in %iterable - compile [%key = %value for %src_key = %src_value in %iterable] to: + compile [%key = %value for %src_key = %src_value in %iterable] to assume (%src_key.type is "Var") or barf ".." Dict comprehension has the wrong type for the key loop variable. Expected Var, but got: \(%src_key.type) assume (%src_value.type is "Var") or barf ".." @@ -146,11 +146,11 @@ immediately: return comprehension; end)() -immediately: - action [%lists flattened]: +immediately + action [%lists flattened] %flat <- [] - for %list in %lists: - for %item in %list: + for %list in %lists + for %item in %list add %item to %flat return %flat @@ -158,8 +158,8 @@ immediately: parse [keys in %dict] as: %k for %k = %v in %dict parse [values in %dict] as: %v for %k = %v in %dict -# Sorting: -immediately: +# Sorting +immediately compile [sort %items] to: Lua "table.sort(\(%items as lua expr));" compile [sort %items by %key_expr] to Lua ".." @@ -167,26 +167,26 @@ immediately: return \(%key_expr as lua expr); end); -immediately: - action [%items sorted, sorted %items]: +immediately + action [%items sorted, sorted %items] %copy <- (% for all %items) sort %copy return %copy - action [%items sorted by %key]: + action [%items sorted by %key] %copy <- (% for all %items) sort %copy by %key return %copy - action [unique %items]: + action [unique %items] %unique <- [] %seen <- {} - for all %items: + for all %items unless: % in %seen add % to %unique (% in %seen) <- (yes) return %unique -immediately: +immediately # Metatable stuff compile [set %dict's metatable to %metatable] to Lua "setmetatable(\(%dict as lua expr), \(%metatable as lua expr));" diff --git a/core/control_flow.nom b/core/control_flow.nom index 35e5fb5..b51ecdf 100644 --- a/core/control_flow.nom +++ b/core/control_flow.nom @@ -7,19 +7,19 @@ use "core/text.nom" use "core/operators.nom" # No-Op -immediately: +immediately compile [do nothing] to: Lua "" # Conditionals -immediately: - compile [if %condition %if_body] to: +immediately + compile [if %condition %if_body] to Lua ".." if \(%condition as lua expr) then \(%if_body as lua statements) 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: + compile [if %condition %if_body else %else_body, unless %condition %else_body else %if_body] to Lua ".." if \(%condition as lua expr) then \(%if_body as lua statements) @@ -36,18 +36,18 @@ immediately %when_true_expr if %condition otherwise %when_false_expr %when_false_expr unless %condition else %when_true_expr %when_false_expr unless %condition then %when_true_expr - ..to: + ..to #.. 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.type in {Text:yes, List:yes, Dict:yes, Number:yes} - return: + return Lua value ".." (\(%condition as lua expr) and \(%when_true_expr as lua expr) or \(%when_false_expr as lua expr)) - ..else: + ..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: + return Lua value ".." (function() if \(%condition as lua expr) then @@ -58,20 +58,20 @@ immediately end)() # GOTOs -immediately: +immediately compile [=== %label ===, --- %label ---, *** %label ***] to Lua "::label_\(%label as lua identifier)::;" compile [go to %label] to Lua "goto label_\(%label as lua identifier);" # Basic loop control -immediately: +immediately compile [do next] to: Lua "continue;" compile [stop] to: Lua "break;" # Helper function -immediately: - compile [if %tree has subtree %subtree where %condition %body] to: +immediately + compile [if %tree has subtree %subtree where %condition %body] to Lua ".." for \(%subtree as lua expr) in coroutine.wrap(function() nomsu:walk_tree(\(%tree as lua expr)) end) do if Types.is_node(\(%subtree as lua expr)) then @@ -83,7 +83,7 @@ immediately: end # While loops -immediately: +immediately compile [do next repeat] to: Lua "goto continue_repeat;" compile [stop repeating] to: Lua "goto stop_repeat;" compile [repeat while %condition %body] to @@ -91,14 +91,14 @@ immediately: Lua ".." while \(%condition as lua expr) do \(%body as lua statements) - if %body has subtree % where: + if %body has subtree % where (%.type = "Action") and ((%'s stub) is "do next repeat") - ..: + .. to %lua write "\n ::continue_repeat::;" to %lua write "\nend --while-loop" - if %body has subtree % where: + if %body has subtree % where (%.type = "Action") and ((%'s stub) is "stop repeating") - ..: + .. %lua <- Lua ".." do -- scope of "stop repeating" label @@ -111,7 +111,7 @@ immediately: compile [..] repeat %n times %body - ..to: + ..to %lua <- Lua ".." for i=1,\(%n as lua expr) do @@ -120,10 +120,10 @@ immediately: (%.type = "Action") and ((%'s stub) is "do next repeat") ..: to %lua write "\n ::continue_repeat::;" to %lua write "\nend --numeric for-loop" - if %body has subtree % where: + if %body has subtree % where (%.type = "Action") and ((%'s stub) is "stop repeating") - ..: - %lua <-: + .. + %lua <- Lua ".." do -- scope of "stop repeating" label \%lua @@ -131,37 +131,37 @@ immediately: end -- end of "stop repeating" label scope return %lua -# For loop control flow: -immediately: +# For loop control flow +immediately compile [stop %var] to Lua "goto stop_\(%var as lua identifier);" compile [do next %var] to Lua "goto continue_\(%var as lua identifier);" # Numeric range for loops -immediately: +immediately compile [..] for %var from %start to %stop by %step %body for %var from %start to %stop via %step %body - ..to: + ..to # This uses Lua's approach of only allowing loop-scoped variables in a loop assume (%var.type is "Var") or barf "Loop expected variable, not: \(%var's source code)" %lua <- Lua ".." for \(%var as lua expr)=\(%start as lua expr),\(%stop as lua expr),\(%step as lua expr) do \(%body as lua statements) - if %body has subtree % where: + if %body has subtree % where (%.type = "Action") and ((%'s stub) is "do next %") and %.value.3.value is %var.value ..: to %lua write "\n ::continue_\(%var as lua identifier)::;" to %lua write "\nend --numeric for-loop" - if %body has subtree % where: - (%.type = "Action") and: - ((%'s stub) is "stop %") and: + if %body has subtree % where + (%.type = "Action") and + ((%'s stub) is "stop %") and %.value.2.value is %var.value - ..: + .. %lua <- Lua ".." do -- scope for stopping for-loop @@ -171,7 +171,7 @@ immediately: return %lua -immediately: +immediately 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 @@ -180,25 +180,25 @@ immediately: parse [for all %start to %stop %body] as: for all %start to %stop via 1 %body # For-each loop (lua's "ipairs()") -immediately: - compile [for %var in %iterable %body] to: +immediately + compile [for %var in %iterable %body] to # This uses Lua's approach of only allowing loop-scoped variables in a loop assume (%var.type is "Var") or barf "Loop expected variable, not: \(%var's source code)" %lua <- Lua ".." for i,\(%var as lua identifier) in ipairs(\(%iterable as lua expr)) do \(%body as lua statements) - if %body has subtree % where: + if %body has subtree % where (%.type = "Action") and ((%'s stub) is "do next %") and %.value.3.value is %var.value ..: to %lua write (Lua "\n ::continue_\(%var as lua identifier)::;") to %lua write "\nend --foreach-loop" - if %body has subtree % where: + if %body has subtree % where (%.type = "Action") and ((%'s stub) is "stop %") and %.value.2.value is %var.value - ..: + .. %lua <- Lua ".." do -- scope for stopping for-loop @@ -210,8 +210,8 @@ immediately: parse [for all %iterable %body] as: for % in %iterable %body # Dict iteration (lua's "pairs()") -immediately: - compile [for %key = %value in %iterable %body] to: +immediately + compile [for %key = %value in %iterable %body] to # This uses Lua's approach of only allowing loop-scoped variables in a loop assume (%key.type is "Var") or barf "Loop expected variable, not: \(%key's source code)" assume (%value.type is "Var") or barf "Loop expected variable, not: \(%value's source code)" @@ -219,13 +219,13 @@ immediately: Lua ".." for \(%key as lua identifier),\(%value as lua identifier) in pairs(\(%iterable as lua expr)) do \(%body as lua statements) - if %body has subtree % where: + if %body has subtree % where (%.type = "Action") and ((%'s stub) is "do next %") and %.value.3.value is %key.value ..: to %lua write (Lua "\n ::continue_\(%key as lua identifier)::;") - if %body has subtree % where: + if %body has subtree % where (%.type = "Action") and ((%'s stub) is "do next %") and %.value.3.value is %value.value @@ -233,13 +233,13 @@ immediately: to %lua write "\nend --foreach-loop" %stop_labels <- (Lua "") - if %body has subtree % where: + if %body has subtree % where (%.type = "Action") and ((%'s stub) is "stop %") and %.value.2.value is %key.value ..: to %stop_labels write "\n::stop_\(%key as lua identifier)::;" - if %body has subtree % where: + if %body has subtree % where (%.type = "Action") and ((%'s stub) is "stop %") and %.value.2.value is %value.value @@ -255,13 +255,13 @@ immediately: return %lua # Switch statement/multi-branch if -immediately: - compile [when %body] to: +immediately + compile [when %body] to %code <- (Lua "") %fallthroughs <- [] %is_first <- (yes) %seen_else <- (no) - for %func_call in %body.value: + for %func_call in %body.value assume (%func_call.type is "Action") or barf ".." Invalid format for 'when' statement. Only '*' blocks are allowed. %tokens <- %func_call.value @@ -269,7 +269,7 @@ immediately: %star: %tokens.1 %condition: %tokens.2 %action: %tokens.3 - ..: + .. assume ((%star and (%star.type is "Word")) and (%star.value is "*")) or barf ".." Invalid format for 'when' statement. Lines must begin with '*' assume %condition or barf ".." @@ -283,11 +283,11 @@ immediately: to %code write "\nelse\n " to %code write: %action as lua statements %seen_else <- (yes) - ..else: + ..else assume (not %seen_else) or barf "'else' clause needs to be last in 'when' block" lua> "table.insert(\%fallthroughs, \(%condition as lua expr));" to %code write "\("if" if %is_first else "\nelseif") " - for %i=%condition in %fallthroughs: + for %i=%condition in %fallthroughs if (%i > 1): to %code write " or " to %code write %condition to %code write " then\n " @@ -302,17 +302,17 @@ immediately: return %code # Switch statement -immediately: - compile [when %branch_value = ? %body, when %branch_value is ? %body] to: +immediately + compile [when %branch_value = ? %body, when %branch_value is ? %body] to %code <- (Lua "") %fallthroughs <- [] %is_first <- (yes) %seen_else <- (no) - for %func_call in %body.value: + for %func_call in %body.value assume (%func_call.type is "Action") or barf ".." Invalid format for 'when' statement. Only '*' blocks are allowed. %tokens <- %func_call.value - with {%star:%tokens.1, %condition:%tokens.2, %action:%tokens.3}: + with {%star:%tokens.1, %condition:%tokens.2, %action:%tokens.3} assume ((%star and (%star.type is "Word")) and (%star.value is "*")) or barf ".." Invalid format for 'when' statement. Lines must begin with '*' assume %condition or barf ".." @@ -325,7 +325,7 @@ immediately: assume (not %is_first) or barf "'else' clause cannot be first in 'when % = ?' block" to %code write "\nelse\n " to %code write: %action as lua statements - ..else: + ..else assume (not %seen_else) or barf "'else' clause needs to be last in 'when % = ?' block" to %code write "\("if" if %is_first else "\nelseif") " lua> "table.insert(\%fallthroughs, \(%condition as lua expr));" @@ -334,7 +334,7 @@ immediately: to %code write " or " if: (%.type is "Text") or (%.type is "Number") to %code write "branch_value == \%" - ..else: + ..else to %code write "utils.equivalent(branch_value, \%)" to %code write "then\n " to %code write (%action as lua statements) @@ -354,11 +354,11 @@ immediately: return %code # Try/except -immediately: +immediately compile [..] try %action and if it succeeds %success or if it barfs %fallback try %action and if it barfs %fallback or if it succeeds %success - ..to: + ..to Lua ".." do local fell_through = false; @@ -375,24 +375,24 @@ immediately: return ret; end end - parse [try %action] as: + parse [try %action] as try %action and if it succeeds: do nothing ..or if it barfs: do nothing - parse [try %action and if it barfs %fallback] as: + parse [try %action and if it barfs %fallback] as try %action and if it succeeds: do nothing ..or if it barfs %fallback - parse [try %action and if it succeeds %success] as: + parse [try %action and if it succeeds %success] as try %action and if it succeeds %success or if it barfs: do nothing -# Do/finally: -immediately: - compile [do %action] to: +# Do/finally +immediately + compile [do %action] to Lua ".." do \(%action as lua statements) end --do - compile [do %action then always %final_action] to: + compile [do %action then always %final_action] to Lua ".." do local fell_through = false; diff --git a/core/math.nom b/core/math.nom index 0df8f16..75e41b4 100644 --- a/core/math.nom +++ b/core/math.nom @@ -34,37 +34,37 @@ compile [log % base %base, log_%base %, log base %base %] to: Lua value "math.lo compile [floor %] to: Lua value "math.floor(\(% as lua expr))" compile [ceiling %, ceil %] to: Lua value "math.ceil(\(% as lua expr))" compile [round %, % rounded] to: Lua value "math.floor(\(% as lua expr) + .5)" -action [%n to the nearest %rounder]: +action [%n to the nearest %rounder] =lua "(\%rounder)*math.floor((\%n / \%rounder) + .5)" # Any/all/none -compile [all of %items, all %items] to: +compile [all of %items, all %items] to unless: (%items' "type") is "List" return: Lua value "utils.all(\(%items as lua expr))" %clauses <- [] for all (%items' "value"): lua> "table.insert(\%clauses, \(% as lua expr));" return: Lua value "(\(%clauses joined with " and "))" parse [not all of %items, not all %items] as: not (all of %items) -compile [any of %items, any %items] to: +compile [any of %items, any %items] to unless: (%items' "type") is "List" return: Lua value "utils.any(\(%items as lua expr))" %clauses <- [] for all (%items' "value"): lua> "table.insert(\%clauses, \(% as lua expr));" return: Lua value "(\(%clauses joined with " or "))" parse [none of %items, none %items] as: not (any of %items) -compile [sum of %items, sum %items] to: +compile [sum of %items, sum %items] to unless: (%items' "type") is "List" return: Lua value "utils.sum(\(%items as lua expr))" %clauses <- [] for all (%items' "value"): lua> "table.insert(\%clauses, \(% as lua expr));" return: Lua value "(\(%clauses joined with " + "))" -compile [product of %items, product %items] to: +compile [product of %items, product %items] to unless: (%items' "type") is "List" return: Lua value "utils.product(\(%items as lua expr))" %clauses <- [] for all (%items' "value"): lua> "table.insert(\%clauses, \(% as lua expr));" return: Lua value "(\(%clauses joined with " * "))" -action [avg of %items, average of %items]: +action [avg of %items, average of %items] =lua "(utils.sum(\%items)/#\%items)" compile [min of %items, smallest of %items, lowest of %items] to Lua value "utils.min(\(%items as lua expr))" @@ -82,7 +82,7 @@ compile [max of %items by %value_expr] to end) # Random functions -action [seed random with %]: +action [seed random with %] lua> ".." math.randomseed(\%); for i=1,20 do math.random(); end @@ -91,5 +91,5 @@ compile [random number, random, rand] to: Lua value "math.random()" compile [random int %n, random integer %n, randint %n] to: Lua value "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 expr), \(%high as lua expr))" -action [random choice from %elements, random choice %elements, random %elements]: +action [random choice from %elements, random choice %elements, random %elements] =lua "\%elements[math.random(#\%elements)]" diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index f03f8d5..9722afd 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -3,7 +3,7 @@ functions to make that easier. # Compile-time action to make compile-time actions: -immediately: +immediately lua> ".." nomsu:define_compile_action("compile %actions to %lua", \(!! code location !!), function(tree, \%actions, \%lua) local lua = Lua(tree.source, "nomsu:define_compile_action("); @@ -31,15 +31,15 @@ immediately: end local body_lua = \%lua:as_lua(nomsu); body_lua:convert_to_statements("return "); - body_lua:remove_free_vars(args); + body_lua:remove_free_vars(unpack(args)); body_lua:declare_locals(); lua:append(")\\n ", body_lua, "\\nend);"); return lua; end); # Compile-time action to make actions -immediately: - compile [action %actions %body] to: +immediately + compile [action %actions %body] to lua> ".." local lua = Lua(tree.source, "nomsu:define_action("); local stubs = {}; @@ -63,14 +63,14 @@ immediately: end local body_lua = \%body:as_lua(nomsu); body_lua:convert_to_statements("return "); - body_lua:remove_free_vars(args); + body_lua:remove_free_vars(unpack(args)); body_lua:declare_locals(); lua:append(")\\n ", body_lua, "\\nend);") return lua; -# Macro to make nomsu macros: -immediately: - compile [parse %shorthand as %longhand] to: +# Macro to make nomsu macros +immediately + compile [parse %shorthand as %longhand] to lua> ".." local lua = Lua(tree.source, "nomsu:define_compile_action("); local stubs = {}; @@ -104,7 +104,7 @@ immediately: ]]); return lua; -action [remove action %stub]: +action [remove action %stub] lua> ".." local fn = ACTIONS[\%stub]; local metadata = nomsu.action_metadata[fn]; @@ -114,11 +114,11 @@ action [remove action %stub]: end ACTIONS[\%stub] = nil; -immediately: - action [%tree as lua]: +immediately + action [%tree as lua] =lua "\%tree:as_lua(nomsu)" - action [%tree as lua expr]: + action [%tree as lua expr] lua> ".." local lua = \%tree:as_lua(nomsu); if not lua.is_value then @@ -126,7 +126,7 @@ immediately: end return lua; - action [%tree as lua statements]: + action [%tree as lua statements] lua> ".." local lua = \%tree:as_lua(nomsu); lua:convert_to_statements(); @@ -138,37 +138,37 @@ immediately: compile [declare locals %locals in %tree] to Lua "\(%tree as lua expr):declare_locals(\(%locals as lua expr));" - compile [remove free vars %vars from %tree] to: + compile [remove free vars %vars from %tree] to Lua "\(%tree as lua expr):remove_free_vars(unpack(\(%vars as lua expr)));" - action [%tree as value]: + action [%tree as value] =lua "nomsu:tree_to_value(\%tree)" - action [%tree's stub]: + action [%tree's stub] =lua "nomsu:tree_to_stub(\%tree)" -immediately: +immediately parse [%var <-write %code] as: lua> "\%var:append(\%code);" parse [to %var write %code] as: lua> "\%var:append(\%code);" -immediately: - action [%tree's source code, %tree' source code]: +immediately + action [%tree's source code, %tree' source code] =lua "\%tree.source:get_text()" compile [repr %obj] to: Lua value "repr(\(%obj as lua expr))" compile [%obj as text] to: Lua value "tostring(\(%obj as lua expr))" compile [type of %obj] to: Lua value "type(\(%obj as lua expr))" -immediately: +immediately compile [nomsu] to: Lua value "nomsu" compile [%var as lua identifier] to: Lua value "nomsu:var_to_lua_identifier(\(%var as lua expr))" -action [action %names metadata]: +action [action %names metadata] =lua "nomsu.action_metadata[ACTIONS[\%names]]" # Get the source code for a function -action [help %action]: +action [help %action] lua> ".." local metadata = \(action %action metadata); if not metadata then @@ -178,20 +178,20 @@ action [help %action]: end # Compiler tools -immediately: - compile [run %code] to: +immediately + compile [run %code] to Lua value "nomsu:run(Nomsu(\(=lua "tostring(tree.source)"), \(%code as lua expr)))" parse [enable debugging] as: lua> "nomsu.debug = true;" parse [disable debugging] as: lua> "nomsu.debug = false;" -immediately: - compile [show lua %block] to: +immediately + compile [show lua %block] to lua> ".." local \%lua = \%block:as_lua(nomsu); return Lua(\%block.source, "print(", repr(tostring(\%lua)), ");"); -immediately: - compile [say %message] to: +immediately + compile [say %message] to lua> ".." if \%message.type == "Text" then return Lua(tree.source, "print(", \(%message as lua expr), ");"); @@ -199,42 +199,42 @@ immediately: return Lua(tree.source, "print(stringify(", \(%message as lua expr), "));"); end -immediately: +immediately compile [source] to: Lua value (=lua "tree.source") "tree.source" #.. - immediately: + immediately action [Lua %]: Lua (=lua "tree.source") % action [Lua value %]: Lua value (=lua "tree.source") % # Return -immediately: +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: Lua "do return; end" compile [return %return_value] to: Lua "do return \(%return_value as lua expr); end" # Error functions -immediately: +immediately compile [barf] to: Lua "error(nil, 0);" compile [barf %msg] to: Lua "error(\(%msg as lua expr), 0);" - compile [assume %condition] to: + compile [assume %condition] to lua> "local \%assumption = 'Assumption failed: '..tostring(\%condition.source:get_text());" - return: + return Lua ".." if not \(%condition as lua expr) then error(\(repr %assumption), 0); end # TODO: factor this out and replace with "unless %condition: barf %message" - compile [assume %condition or barf %message] to: + compile [assume %condition or barf %message] to Lua ".." if not \(%condition as lua expr) then error(\(%message as lua expr), 0); end # Literals -immediately: +immediately compile [yes] to: Lua value "true" compile [no] to: Lua value "false" compile [nothing, nil, null] to: Lua value "nil" diff --git a/core/operators.nom b/core/operators.nom index 073936e..a9deec1 100644 --- a/core/operators.nom +++ b/core/operators.nom @@ -3,8 +3,8 @@ use "core/metaprogramming.nom" -# Indexing: -immediately: +# Indexing +immediately #.. NOTE!!! It's critical that there are spaces around %key if it's a string, otherwise, Lua will get confused and interpret %obj[[[foo]]] as %obj("[foo]") instead of %obj[ "foo" ]. @@ -17,13 +17,13 @@ immediately: ..as: %obj.%key # Comparison Operators -immediately: +immediately compile [%x < %y] to: Lua value "(\(%x as lua expr) < \(%y as lua expr))" compile [%x > %y] to: Lua value "(\(%x as lua expr) > \(%y as lua expr))" compile [%x <= %y] to: Lua value "(\(%x as lua expr) <= \(%y as lua expr))" compile [%x >= %y] to: Lua value "(\(%x as lua expr) >= \(%y as lua expr))" # TODO: optimize case of [%x,%y] = [1,2] - compile [%a is %b, %a = %b, %a == %b] to: + compile [%a is %b, %a = %b, %a == %b] to lua> ".." local safe = {Text=true, Number=true}; local a_lua, b_lua = \%a:as_lua(nomsu), \%b:as_lua(nomsu); @@ -32,7 +32,7 @@ immediately: else return Lua.Value(tree.source, "utils.equivalent(", a_lua, ", ", b_lua, ")"); end - compile [%a isn't %b, %a is not %b, %a not= %b, %a != %b] to: + 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 = \%a:as_lua(nomsu), \%b:as_lua(nomsu); @@ -45,8 +45,8 @@ immediately: compile [%'s id, id of %] to: Lua value "nomsu.ids[\(% as lua expr)]" # Variable assignment operator -immediately: - compile [%var <- %value] to: +immediately + compile [%var <- %value] to lua> "local \%var_lua = \%var:as_lua(nomsu);" assume %var_lua.is_value or barf "Invalid target for assignment: \(%var's source code)" lua> "local \%value_lua = \%value:as_lua(nomsu);" @@ -58,9 +58,9 @@ immediately: end return lua; -immediately: +immediately # Simultaneous mutli-assignments like: x,y,z = 1,x,3; - compile [<- %assignments] to: + compile [<- %assignments] to assume ((%assignments' "type") is "Dict") or barf ".." Expected a Dict for the assignments part of '<- %' statement, not \(%assignments' source code) lua> ".." @@ -83,20 +83,20 @@ immediately: end return Lua(tree.source, lhs, " = ", rhs, ";"); -immediately: - compile [export %var <- %value] to: +immediately + compile [export %var <- %value] to %var_lua <- (%var as lua) assume %var_lua.is_value or barf "Invalid target for assignment: \(%var's source code)" %value_lua <- (%value as lua) assume %value_lua.is_value or barf "Invalid value for assignment: \(%value's source code)" return: Lua "\(%var_lua) = \(%value_lua);" - compile [exporting %exported %body] to: + compile [exporting %exported %body] to %body_lua <- (%body as lua statements) lua> "\%body_lua:remove_free_vars(unpack(\(%exported.value)));" return %body_lua - compile [with %assignments %body] to: + compile [with %assignments %body] to %lua <- (%body as lua statements) lua> ".." local lhs, rhs = Lua(tree.source), Lua(\%assignments.source); @@ -130,7 +130,7 @@ immediately: \%lua end -- 'with' block -immediately: +immediately # Math Operators compile [%x + %y] to: Lua value "(\(%x as lua expr) + \(%y as lua expr))" compile [%x - %y] to: Lua value "(\(%x as lua expr) - \(%y as lua expr))" @@ -171,7 +171,7 @@ immediately: compile [length of %list] to: Lua value "(#\(%list as lua expr))" # Update operators -immediately: +immediately parse [%var + <- %, %var +<- %] as: %var <- (%var + %) parse [%var - <- %, %var -<- %] as: %var <- (%var - %) parse [%var * <- %, %var *<- %] as: %var <- (%var * %) diff --git a/core/text.nom b/core/text.nom index 71e1ce1..835ef87 100644 --- a/core/text.nom +++ b/core/text.nom @@ -5,7 +5,7 @@ use "core/metaprogramming.nom" # Text functions -action [%texts joined with %glue]: +action [%texts joined with %glue] lua> ".." local text_bits = {} for i,bit in ipairs(\%texts) do text_bits[i] = stringify(bit) end |
