diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-01-07 18:45:27 -0800 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-01-07 18:45:27 -0800 |
| commit | 568a44ef191e1f4072d379700e3b6f599150a92b (patch) | |
| tree | c58e74b5f7539a0055d72d8ff40ef6f499aef9ea /lib | |
| parent | c92e5fbc81e57ada43f2c17792e500da5b708bee (diff) | |
Reworking some stuff so that functions only allow expressions to be
return values with either an explicit "return" statement or if they're
the only line in the function, and the line is an expression.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/collections.nom | 24 | ||||
| -rw-r--r-- | lib/control_flow.nom | 4 | ||||
| -rw-r--r-- | lib/operators.nom | 15 | ||||
| -rw-r--r-- | lib/utils2.nom | 39 |
4 files changed, 40 insertions, 42 deletions
diff --git a/lib/collections.nom b/lib/collections.nom index b97c164..9939c9b 100644 --- a/lib/collections.nom +++ b/lib/collections.nom @@ -51,7 +51,7 @@ compile [%list ->* %indices] to: %ret = "\(%list as lua)" for %index in (%indices's "value"): %ret join= "[\(%index as lua)]" - "\%ret" + return "\%ret" # Assignment compile [..] @@ -76,35 +76,35 @@ rule [flatten %lists] =: for %list in %lists: for %item in %list: add %item to %flat - %flat + return %flat rule [dict %items] =: %dict = [] for %pair in %items: %dict -> (%pair -> 1) = (%pair -> 2) - %dict + return %dict rule [entries in %dict] =: %entries = [] for %k = %v in %dict: add {key=%k, value=%v} to %entries - %entries + return %entries rule [keys in %dict] =: %keys = [] for %k = %v in %dict: add %k to %keys - %keys + return %keys rule [values in %dict] =: %values = [] for %k = %v in %dict: add %v to %values - %values + return %values # List Comprehension compile [%expression for %item in %iterable] to: assert ((%item's "type") == "Var") ".." List comprehension has the wrong type for the loop variable. Expected Var, but got: \(%item's "type") - ".." + return ".." (function(nomsu, vars); local comprehension = {}; for i,item in ipairs(\(%iterable as lua)) do; @@ -120,7 +120,7 @@ compile [%expression for %key = %value in %iterable] to: List comprehension has the wrong type for the key loop variable. Expected Var, but got: \(%key's "type") assert ((%value's "type") == "Var") ".." List comprehension has the wrong type for the value loop variable. Expected Var, but got: \(%value's "type") - ".." + return ".." (function(nomsu, vars); local comprehension = {}; for key,value in pairs(\(%iterable as lua)) do; @@ -133,11 +133,11 @@ compile [%expression for %key = %value in %iterable] to: rule [%items sorted] =: %copy = (% for all %items) sort %copy - %copy + return %copy rule [%items sorted by %key] =: %copy = (% for all %items) sort %copy by %key - %copy + return %copy rule [unique %items] =: keys in (dict ([%,yes] for all %items)) @@ -165,7 +165,7 @@ rule [chain %dict to %fallback] =: compile [%key = %value for %item in %iterable] to: assert ((%item's "type") == "Var") ".." Dict comprehension has the wrong type for the loop variable. Expected Var, but got: \(%item's "type") - ".." + return ".." (function(nomsu, vars); local comprehension = {}; for i,value in ipairs(\(%iterable as lua)) do; @@ -181,7 +181,7 @@ compile [%key = %value for %src_key = %src_value in %iterable] to: Dict comprehension has the wrong type for the key loop variable. Expected Var, but got: \(%src_key's "type") assert ((%src_value's "type") == "Var") ".." Dict comprehension has the wrong type for the value loop variable. Expected Var, but got: \(%src_value's "type") - ".." + return ".." (function(nomsu, vars); local comprehension = {}; for key,value in pairs(\(%iterable as lua)) do; diff --git a/lib/control_flow.nom b/lib/control_flow.nom index 72a2ad1..33f72ab 100644 --- a/lib/control_flow.nom +++ b/lib/control_flow.nom @@ -210,7 +210,7 @@ compile [when %body] to code: if (%result != ""): %result join= "\nend" - %result + return %result # Switch statement compile [when %branch_value == ? %body] to code: @@ -259,7 +259,7 @@ compile [when %branch_value == ? %body] to code: ..\%result end end --when == ? - %result + return %result # Try/except compile [..] diff --git a/lib/operators.nom b/lib/operators.nom index 8ea7aad..59a9ad2 100644 --- a/lib/operators.nom +++ b/lib/operators.nom @@ -12,8 +12,8 @@ compile [phi, PHI, golden ratio] to: "((1+math.sqrt(5))/2)" compile [nop, pass] to code: "" # Ternary operator -#.. Note: this uses a function instead of (condition and if_expr or else_expr) - because that breaks if %if_expr is falsey. +#.. Note: this uses a function instead of "(condition and if_expr or else_expr)" + because that breaks if %if_expr is falsey, e.g. "x < 5 and false or 99" compile [..] %when_true_expr if %condition else %when_false_expr %when_true_expr if %condition otherwise %when_false_expr @@ -27,6 +27,17 @@ compile [..] return \(%when_false_expr as lua); end end)(nomsu, vars) +parse [..] + %true if %x == %y else %false, %true if %x == %y otherwise %false + %false unless %x == %y else %true, %false unless %x == %y otherwise %true +..as: + %true if (%x == %y) else %false + +parse [..] + %true if %x != %y else %false, %true if %x != %y otherwise %false + %false unless %x != %y else %true, %false unless %x != %y otherwise %true +..as: + %true if (%x != %y) else %false # Indexing: compile [%obj'%key, %obj's %key, %obj -> %key] to: "(\(%obj as lua))[\(%key as lua)]" diff --git a/lib/utils2.nom b/lib/utils2.nom index 89a0767..2fa29f7 100644 --- a/lib/utils2.nom +++ b/lib/utils2.nom @@ -6,16 +6,12 @@ require "lib/collections.nom" compile [say %str] to: - if ((%str's "type") == "String"): - "nomsu:writeln(\(%str as lua))" - ..else: - "nomsu:writeln(nomsu:stringify(\(%str as lua)))" + "nomsu:writeln(\(%str as lua))" if ((%str's "type") == "String") + ..else "nomsu:writeln(nomsu:stringify(\(%str as lua)))" compile [do %action] to code: - if ((%action's "type") == "Thunk"): - %action as lua statements - ..else: - "(\(%action as lua))(nomsu, vars);" + (%action as lua statements) if ((%action's "type") == "Thunk") + ..else "(\(%action as lua))(nomsu, vars);" # With statement compile [with %assignments %action] to code: @@ -33,7 +29,7 @@ compile [with %assignments %action] to code: "local old_value\(%->"i") = \((%->"var") as lua); \((%->"var") as lua) = \((%->"value") as lua);" ..for all %data ..with glue "\n " - ".." + return ".." do \%setup local fell_through = false; @@ -51,26 +47,17 @@ parse [with %thing = %value %action] as: with [%thing = %value] %action # Any/all/none compile [all of %items, all %items] to: - if (%items' "type") == "List": - "(\(join ((% as lua) for all (%items' "value")) with glue " and "))" - ..else: - "nomsu.utils.all(\(%items as lua))" + "(\(join ((% as lua) for all (%items' "value")) with glue " and "))" + ..if (%items' "type") == "List" else "nomsu.utils.all(\(%items as lua))" parse [not all of %items, not all %items] as: not (all of %items) compile [any of %items, any %items] to: - if (%items' "type") == "List": - "(\(join ((% as lua) for all (%items' "value")) with glue " or "))" - ..else: - "nomsu.utils.any(\(%items as lua))" + "(\(join ((% as lua) for all (%items' "value")) with glue " or "))" + ..if (%items' "type") == "List" else "nomsu.utils.any(\(%items as lua))" parse [none of %items, none %items] as: not (any of %items) - compile [sum of %items, sum %items] to: - if (%items' "type") == "List": - "(\(join ((% as lua) for all (%items' "value")) with glue " + "))" - ..else: - "nomsu.utils.sum(\(%items as lua))" + "(\(join ((% as lua) for all (%items' "value")) with glue " + "))" + ..if (%items' "type") == "List" else "nomsu.utils.sum(\(%items as lua))" compile [product of %items, product %items] to: - if (%items' "type") == "List": - "(\(join ((% as lua) for all (%items' "value")) with glue " * "))" - ..else: - "nomsu.utils.product(\(%items as lua))" + "(\(join ((% as lua) for all (%items' "value")) with glue " * "))" + ..if (%items' "type") == "List" else "nomsu.utils.product(\(%items as lua))" |
