diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2017-09-20 05:02:53 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2017-09-20 05:02:53 -0700 |
| commit | 0750d642624b2262afdb4dd17b275a16e96971b5 (patch) | |
| tree | 0d526e928115238862375441f5e4176638d07eb4 | |
| parent | 18365e02b14686e9ad26d1fbc5ac19984b73da6b (diff) | |
Updated all the code to work with the latest nomsu.
| -rw-r--r-- | examples/parser_tests.nom | 63 | ||||
| -rw-r--r-- | examples/sample_code.nom | 33 | ||||
| -rw-r--r-- | examples/tutorial.nom | 29 | ||||
| -rw-r--r-- | lib/core.nom | 49 | ||||
| -rw-r--r-- | nomsu.lua | 2 | ||||
| -rwxr-xr-x | nomsu.moon | 2 |
6 files changed, 62 insertions, 116 deletions
diff --git a/examples/parser_tests.nom b/examples/parser_tests.nom index 5cf64d3..b1eb23b 100644 --- a/examples/parser_tests.nom +++ b/examples/parser_tests.nom @@ -1,48 +1,38 @@ require "lib/core.nom" -test: say "foo" ..yields ".." +test: say "foo" +..yields ".." |Call [say %]: | "foo" -test: say (4) ..yields ".." +test: say (foo) +..yields ".." |Call [say %]: - | 4 + | Call [foo]! test: - rule: fart ..=: say "poot" + rule [fart] =: say "poot" ..yields ".." |Call [rule % = %]: - | Thunk: + | List: | Call [fart]! | Thunk: | Call [say %]: | "poot" -test: - rule: doublefart ..=: - say "poot" - say "poot" +test: say (subexpressions work) ..yields ".." - |Call [rule % = %]: - | Thunk: - | Call [doublefart]! - | Thunk: - | Call [say %]: - | "poot" - | Call [say %]: - | "poot" - -test: say (subexpressions work) ..yields ".." |Call [say %]: | Call [subexpressions work]! -test: say ["lists", "work"] ..yields ".." +test: say ["lists", "work"] +..yields ".." |Call [say %]: | List: | "lists" | "work" -test: say [] ..yields ".." +test (: say []) yields ".." |Call [say %]: | <Empty List> @@ -71,28 +61,6 @@ test: | 3 | 4 -test: - say both.. - "hello" - and "world" -..yields ".." - |Call [say both % and %]: - | "hello" - | "world" - -test: - say both .. - "a list:" - and [..] - 1,2,(three),(4) -..yields ".." - |Call [say both % and %]: - | "a list:" - | List: - | 1 - | 2 - | Call [three]! - | 4 test: if 1: yes @@ -106,7 +74,7 @@ test: | Call [no]! test: - if 1: yes ..else: no + if 1 (: yes) else: no ..yields ".." |Call [if % % else %]: | 1 @@ -173,10 +141,11 @@ test: say (1 + (-(2 * 3))) test: if %x: x - ..else: if %y: - y ..else: - z + if %y: + y + ..else: + z ..yields ".." |Call [if % % else %]: | Var[x] diff --git a/examples/sample_code.nom b/examples/sample_code.nom index f8315a8..e2b3a62 100644 --- a/examples/sample_code.nom +++ b/examples/sample_code.nom @@ -97,40 +97,31 @@ say (..) 3 * 4 when: - when %x == 1: + ? %x == 1: say "one" - when %x == 2: + ? %x == 2: say "two" - when %x: + ? %x: say "nonzero" - else: + ?: say "???" -when: - * %x == 1: +when %x: + == 1: say "one" - * %x == 2: + == 2: say "two" - * %x: - say "nonzero" - *: - say "???" - -when %x ==: - * 1: - say "one" - * 2: - say "two" - *: + ==: say "???" if %x: say "one" -..else: if %y: - say "two" ..else: - say "three" + if %y: + say "two" + ..else: + say "three" say ".." diff --git a/examples/tutorial.nom b/examples/tutorial.nom index 55e558b..306a0af 100644 --- a/examples/tutorial.nom +++ b/examples/tutorial.nom @@ -3,7 +3,7 @@ continue until dedent # Import files like so: -require "core.nom" +require "lib/core.nom" # Numbers: 23 @@ -114,8 +114,8 @@ do: # Function definition: rule [say both %first and also %second] =: - # Function arguments are accessed just like variables say %first + # Function arguments are accessed just like variables say %second # The last line of a function is the return value @@ -192,29 +192,16 @@ say (2 ++ 3) "say both %first and also %second", and a code block to a function called "rule % %" that takes two arguments. -# Line continuations work by either ending a line with ".." and continuing with an indented block: -say.. - both "Tom" and - also - "Sawyer" - -# Or by starting the next line with ".." +# Line continuations work by starting the next line with ".." say both "Bruce" ..and also "Lee" -# This can be mixed and matched: -say both.. - "Rick" -..and also.. - "Moranis" - -# And combined with the block forms of literals: +# This can be combined with the block forms of literals: say both ".." |Four score and seven years ago our fathers brought forth, upon this continent, |a new nation, conceived in liberty, and dedicated to the proposition that |"all men are created equal" -..and also.. - "-- Abraham Lincoln" +..and also "-- Abraham Lincoln" rule [my favorite number] =: 21 + 2 @@ -223,7 +210,7 @@ say (my favorite number) # There's a multi-line indented block form for subexpressions too: say (..) my favorite - number + ..number # Block strings can interpolate values by enclosing an expression in a pair of \s say ".."|My favorite number is \my favorite number\! @@ -231,7 +218,7 @@ say ".."|My favorite number is \my favorite number\! say ".." |My favorite number is still \(..) my favorite - number + ..number ..\, but this time it uses an indented subexpression! rule [sing %starting-bottles bottles of beer] =: @@ -278,7 +265,7 @@ say ".."|The square root of 2 is \square root of 2\ # "macro block %" is for defining macros that produce blocks of code, not values macro block [unless %condition %body] =: ".." |if not (\%condition as lua expr\) then - | \(lua expr "vars.body.value.value") as lua block\ + | \(lua expr "vars.body.value") as lua block\ |end unless (1 > 10): diff --git a/lib/core.nom b/lib/core.nom index c1bd663..f0cad0d 100644 --- a/lib/core.nom +++ b/lib/core.nom @@ -356,22 +356,19 @@ macro block [when %body] =: for %statement in (lua expr "vars.body.value.value"): %func-call =: lua expr "vars.statement.value" if ((lua expr "vars['func-call'].type") != "FunctionCall"): - error "Invalid format for 'when' statement" + error "Invalid format for 'when' statement. Only '?' blocks are allowed." %tokens =: lua expr "vars['func-call'].value" %q =: lua expr "vars.tokens[1]" if (((lua expr "vars.q.type") != "Word") or ((lua expr "vars.q.value") != "?")): - error "Invalid format for 'when' statement" + error "Invalid format for 'when' statement. Lines must begin with '?'" %thunk =: lua expr "vars.tokens[#vars.tokens]" if ((lua expr "vars.thunk.type") != "Thunk"): - error "Invalid format for 'when' statement" + error "Invalid format for 'when' statement. Lines must have a body." %condition_bits =: [] for %i in (2 up to (lua expr "#vars.tokens")): lua block "table.insert(vars['condition_bits'], vars.tokens[vars.i])" - %condition =: dict [..] - ["type",lua expr "vars['func-call'].type"] - ["src",lua expr "vars['func-call'].src"] - ["value", %condition_bits] - if ((lua expr "#vars.condition.value") == 0): + + if (lua expr "#vars.condition_bits == 0"): %result concat=: ".." | |do @@ -380,6 +377,13 @@ macro block [when %body] =: | return ret |end ..else: + if (lua expr "#vars.condition_bits == 1 and vars.condition_bits[1].type ~= 'Word'"): + %condition =: lua expr "vars.condition_bits[1]" + ..else: + %condition =: dict [..] + ["type",lua expr "vars['func-call'].type"] + ["src",lua expr "vars['func-call'].src"] + ["value", %condition_bits] %result concat=: ".." | |if \%condition as lua expr\ then @@ -387,6 +391,7 @@ macro block [when %body] =: | \(lua expr "vars.thunk.value") as lua block\ | return ret |end + %result # Switch statement @@ -395,14 +400,14 @@ macro block [when %branch-value %body] =: for %statement in (lua expr "vars.body.value.value"): %func-call =: lua expr "vars.statement.value" if ((lua expr "vars['func-call'].type") != "FunctionCall"): - error "Invalid format for 'when' statement" + error "Invalid format for 'when' statement. Only == blocks are allowed." %tokens =: lua expr "vars['func-call'].value" %eq =: lua expr "vars.tokens[1]" if (((lua expr "vars.eq.type") != "Word") or ((lua expr "vars.eq.value") != "==")): - error "Invalid format for 'when' statement" + error "Invalid format for 'when' statement. Lines must begin with '=='" %thunk =: lua expr "vars.tokens[#vars.tokens]" if ((lua expr "vars.thunk.type") != "Thunk"): - error "Invalid format for 'when' statement" + error "Invalid format for 'when' statement. Lines must have a body." %condition_bits =: [] for %i in (2 up to (lua expr "#vars.tokens")): lua block "table.insert(vars.condition_bits, vars.tokens[vars.i])" @@ -416,25 +421,19 @@ macro block [when %branch-value %body] =: |end ..else: if (lua expr "#vars.condition_bits == 1 and vars.condition_bits[1].type ~= 'Word'"): - %result concat=: ".." - | - |if compiler.utils.equivalent(branch_value, \(lua expr "vars.condition_bits[1]") as lua expr\) then - | local ret - | \(lua expr "vars.thunk.value") as lua block\ - | return ret - |end + %condition =: (lua expr "vars.condition_bits[1]") ..else: %condition =: dict [..] ["type",lua expr "vars['func-call'].type"] ["src",lua expr "vars['func-call'].src"] ["value", %condition_bits] - %result concat=: ".." - | - |if compiler.utils.equivalent(branch_value, \%condition as lua expr\) then - | local ret - | \(lua expr "vars.thunk.value") as lua block\ - | return ret - |end + %result concat=: ".." + | + |if compiler.utils.equivalent(branch_condition, \%condition as lua expr\) then + | local ret + | \(lua expr "vars.thunk.value") as lua block\ + | return ret + |end %result # List Comprehension @@ -321,7 +321,7 @@ do (dedent / longstring_error)) |} }) -> Longstring longstring_line <- "|" {| ({("\\" / (!string_interpolation [^%nl]))+} / string_interpolation)* |} longstring_error <- (({.+} ("" -> "Error while parsing Longstring")) => error) - string_interpolation <- "\" %ws? (inline_functioncall / expression) %ws? "\" + string_interpolation <- "\" %ws? (((inline_functioncall / expression) dotdot?) / dotdot) %ws? "\" number <- ({ {"-"? (([0-9]+ "." [0-9]+) / ("." [0-9]+) / ([0-9]+)) } }) -> Number @@ -229,7 +229,7 @@ class NomsuCompiler (dedent / longstring_error)) |} }) -> Longstring longstring_line <- "|" {| ({("\\" / (!string_interpolation [^%nl]))+} / string_interpolation)* |} longstring_error <- (({.+} ("" -> "Error while parsing Longstring")) => error) - string_interpolation <- "\" %ws? (inline_functioncall / expression) %ws? "\" + string_interpolation <- "\" %ws? (((inline_functioncall / expression) dotdot?) / dotdot) %ws? "\" number <- ({ {"-"? (([0-9]+ "." [0-9]+) / ("." [0-9]+) / ([0-9]+)) } }) -> Number |
