Updated all the code to work with the latest nomsu.

This commit is contained in:
Bruce Hill 2017-09-20 05:02:53 -07:00
parent 18365e02b1
commit 0750d64262
6 changed files with 62 additions and 116 deletions

View File

@ -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,7 +141,8 @@ test: say (1 + (-(2 * 3)))
test:
if %x:
x
..else: if %y:
..else:
if %y:
y
..else:
z

View File

@ -97,37 +97,28 @@ say (..)
3 * 4
when:
when %x == 1:
? %x == 1:
say "one"
when %x == 2:
? %x == 2:
say "two"
? %x:
say "nonzero"
?:
say "???"
when %x:
say "nonzero"
else:
say "???"
when:
* %x == 1:
== 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:
..else:
if %y:
say "two"
..else:
say "three"

View File

@ -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):

View File

@ -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,13 +421,7 @@ 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"]
@ -430,7 +429,7 @@ macro block [when %branch-value %body] =:
["value", %condition_bits]
%result concat=: ".."
|
|if compiler.utils.equivalent(branch_value, \%condition as lua expr\) then
|if compiler.utils.equivalent(branch_condition, \%condition as lua expr\) then
| local ret
| \(lua expr "vars.thunk.value") as lua block\
| return ret

View File

@ -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

View File

@ -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