aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/collections.nom24
-rw-r--r--core/control_flow.nom9
-rw-r--r--core/coroutines.nom1
-rw-r--r--core/math.nom7
-rw-r--r--core/metaprogramming.nom18
-rw-r--r--core/operators.nom11
-rw-r--r--core/scopes.nom1
-rw-r--r--core/text.nom13
8 files changed, 16 insertions, 68 deletions
diff --git a/core/collections.nom b/core/collections.nom
index 5f00358..99c84ca 100644
--- a/core/collections.nom
+++ b/core/collections.nom
@@ -11,7 +11,6 @@ use "core/operators.nom"
# Indexing
test:
assume ((2 nd to last in [1, 2, 3, 4, 5]) is 4)
-
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
@@ -25,7 +24,6 @@ parse [first in %list] as %list.1
# Membership testing
test:
assume (3 is in [1, 2, 3, 4, 5])
-
action [%item is in %list, %list contains %item, %list has %item]:
for %key = %value in %list:
if (%key is %item): return (yes)
@@ -33,7 +31,6 @@ action [%item is in %list, %list contains %item, %list has %item]:
test:
assume (99 isn't in [1, 2, 3])
-
action [..]
%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
@@ -44,12 +41,11 @@ action [..]
test:
assume ({x:no} has key "x")
-
parse [%list has key %index, %list has index %index] as (%list.%index != (nil))
+
test:
assume ({x:no} doesn't have key "y")
assume (not ({x:no} doesn't have key "x"))
-
parse [..]
%list doesn't have key %index, %list does not have key %index
%list doesn't have index %index, %list does not have index %index
@@ -66,7 +62,6 @@ test:
assume ((last in %list) is 5)
remove index 1 from %list
assume ((first in %list) is 2)
-
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))")
@@ -86,7 +81,6 @@ compile [remove index %index from %list] to (..)
# List Comprehension
test:
assume (((% * %) for % in [1, 2, 3]) == [1, 4, 9])
-
parse [%expression for %item in %iterable] as (..)
result of:
%comprehension = []
@@ -108,13 +102,11 @@ parse [..]
test:
assume (((% * %) for % in 1 to 3) == [1, 4, 9])
-
parse [%expression for %var in %start to %stop] as (..)
%expression for %var in %start to %stop via 1
test:
assume (("\%k,\%v" for %k = %v in {x:1}) == ["x,1"])
-
parse [..]
%expression for %key = %value in %iterable
%expression for %key %value in %iterable
@@ -128,7 +120,6 @@ parse [..]
# Dict comprehensions
test:
assume (((% * %) = % for % in [1, 2, 3]) == {1:1, 4:2, 9:3})
-
parse [%key = %value for %item in %iterable, %key %value for %item in %iterable] as
..(..)
result of:
@@ -139,7 +130,6 @@ parse [%key = %value for %item in %iterable, %key %value for %item in %iterable]
test:
assume ((%k = (%v * %v) for %k = %v in {x:1, y:2, z:3}) == {x:1, y:4, z:9})
-
parse [..]
%key = %value for %src_key = %src_value in %iterable
%key %value for %src_key %src_value in %iterable
@@ -164,7 +154,6 @@ parse [..]
test:
assume (((% * %) = % for % in 1 to 3) == {1:1, 4:2, 9:3})
-
parse [..]
%key = %value for %item in %start to %stop
%key %value for %item in %start to %stop
@@ -172,7 +161,6 @@ parse [..]
test:
assume (([[1, 2], [3, 4]] flattened) == [1, 2, 3, 4])
-
action [%lists flattened]:
%flat = []
for %list in %lists:
@@ -181,15 +169,14 @@ action [%lists flattened]:
test:
assume ((entries in {x:1}) == [{key:"x", value:1}])
-
parse [entries in %dict] as ({key:%k, value:%v} for %k = %v in %dict)
+
test:
assume ((keys in {x:1}) == ["x"])
-
parse [keys in %dict, keys of %dict] as (%k for %k = %v in %dict)
+
test:
assume ((values in {x:1}) == [1])
-
parse [values in %dict, values of %dict] as (%v for %k = %v in %dict)
# Metatable stuff
@@ -197,13 +184,11 @@ test:
%t = {}
set %t 's metatable to {__tostring:[%] -> "XXX"}
assume ("\%t" == "XXX")
-
compile [set %dict 's metatable to %metatable] to (..)
Lua "setmetatable(\(%dict as lua expr), \(%metatable as lua expr));"
test:
assume (({} with fallback % -> (% + 1)).10 == 11)
-
compile [%dict with fallback %key -> %value] to (..)
Lua value ".."
setmetatable(\(%dict as lua expr), {__index=function(self, \(%key as lua expr))
@@ -222,7 +207,6 @@ test:
%keys = {1:999, 2:0, 3:50}
sort %x by % = %keys.%
assume (%x == [2, 3, 1])
-
compile [sort %items] to (Lua "table.sort(\(%items as lua expr));")
parse [sort %items by %item = %key_expr, sort %items by %item -> %key_expr] as (..)
do:
@@ -233,7 +217,6 @@ parse [sort %items by %item = %key_expr, sort %items by %item -> %key_expr] as (
test:
assume ((sorted [3, 1, 2]) == [1, 2, 3])
-
action [%items sorted, sorted %items]:
%copy = (% for % in %items)
sort %copy
@@ -247,7 +230,6 @@ parse [%items sorted by %item = %key, %items sorted by %item -> %key] as (..)
test:
assume ((unique [1, 2, 1, 3, 2, 3]) == [1, 2, 3])
-
action [unique %items]:
%unique = []
%seen = {}
diff --git a/core/control_flow.nom b/core/control_flow.nom
index 24e367d..0552771 100644
--- a/core/control_flow.nom
+++ b/core/control_flow.nom
@@ -16,7 +16,6 @@ compile [do nothing] to (Lua "")
test:
if (no):
barf "conditional fail"
-
compile [if %condition %if_body] to (..)
Lua ".."
if \(%condition as lua expr) then
@@ -26,7 +25,6 @@ compile [if %condition %if_body] to (..)
test:
unless (yes):
barf "conditional fail"
-
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
@@ -46,7 +44,6 @@ compile [..]
test:
assume ((1 if (yes) else 2) == 1)
assume ((1 if (no) else 2) == 2)
-
compile [..]
%when_true_expr if %condition else %when_false_expr
%when_true_expr if %condition otherwise %when_false_expr
@@ -82,7 +79,6 @@ test:
%i += 1
unless (%i == 10): go to %loop
assume (%i == 10)
-
compile [=== %label ===, --- %label ---, *** %label ***] to (..)
Lua "::label_\(%label as lua identifier)::"
@@ -112,7 +108,6 @@ test:
barf "Failed to 'do next repeat'"
assume (%x == 30)
-
compile [do next repeat] to (Lua "goto continue_repeat")
compile [stop repeating] to (Lua "goto stop_repeat")
compile [repeat while %condition %body] to:
@@ -138,11 +133,11 @@ compile [repeat while %condition %body] to:
parse [repeat %body] as (repeat while (yes) %body)
parse [repeat until %condition %body] as (repeat while (not %condition) %body)
+
test:
%x = 0
repeat 10 times: %x += 1
assume (%x == 10)
-
compile [repeat %n times %body] to:
%lua = (..)
Lua ".."
@@ -466,7 +461,6 @@ test:
..and if it barfs: do nothing
assume (%d.x == "good")
-
compile [do %action then always %final_action] to (..)
Lua ".."
do
@@ -485,6 +479,7 @@ test:
# Inline thunk:
compile [result of %body] to (Lua value "\(compile as ([] -> %body))()")
+
test:
%t = [1, [2, [[3], 4], 5, [[[6]]]]]
%flat = []
diff --git a/core/coroutines.nom b/core/coroutines.nom
index 1ebb2db..1cc5a71 100644
--- a/core/coroutines.nom
+++ b/core/coroutines.nom
@@ -14,7 +14,6 @@ test:
for % in coroutine %co: add % to %nums
assume (%nums == [4, 5, 6, 6, 6]) or barf "Coroutine iteration failed"
-
compile [coroutine %body, generator %body] to (..)
Lua value ".."
(function()
diff --git a/core/math.nom b/core/math.nom
index 5337d8c..3fad8f0 100644
--- a/core/math.nom
+++ b/core/math.nom
@@ -14,7 +14,6 @@ test:
math constants failed
%nan = (NaN)
assume (%nan != %nan) or barf "NaN failed"
-
compile [infinity, inf] to (Lua value "math.huge")
compile [not a number, NaN, nan] to (Lua value "(0/0)")
compile [pi, Pi, PI] to (Lua value "math.pi")
@@ -25,8 +24,8 @@ compile [e] to (Lua value "math.exp(1)")
# Functions:
test:
assume (("5" as a number) == 5)
-
compile [% as a number, % as number] to (Lua value "tonumber(\(% as lua expr))")
+
test:
assume (..)
all of [..]
@@ -34,7 +33,6 @@ test:
arc tangent 5, arc tangent 5 / 10, hyperbolic sine 5, hyperbolic cosine 5
hyperbolic tangent 5, e^ 5, ln 5, log base 2 of 5, floor 5, ceiling 5, round 5
..or barf "math functions failed"
-
compile [absolute value %, | % |, abs %] to (..)
Lua value "math.abs(\(% as lua expr))"
@@ -63,10 +61,10 @@ compile [log % base %base, log base %base of %] to (..)
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)")
+
test:
assume ((463 to the nearest 100) == 500) or barf "rounding failed"
assume ((2.6 to the nearest 0.25) == 2.5) or barf "rounding failed"
-
action [%n to the nearest %rounder] (..)
=lua "(\%rounder)*math.floor((\%n / \%rounder) + .5)"
@@ -107,7 +105,6 @@ compile [max of %items, biggest of %items, largest of %items, highest of %items]
test:
assume ((min of [3, -4, 1, 2] by % = (% * %)) == 1)
assume ((max of [3, -4, 1, 2] by % = (% * %)) == -4)
-
parse [min of %items by %item = %value_expr] as (..)
result of:
set {%best:nil, %best_key:nil}
diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom
index 4168c82..9acc97e 100644
--- a/core/metaprogramming.nom
+++ b/core/metaprogramming.nom
@@ -32,11 +32,9 @@ lua> ".."
test:
compile [five] to (Lua value "5")
-
test:
assume ((five) == 5) or barf "Compile to expression failed."
compile [loc x] to (Lua "local _x = 99;")
-
test:
lua> "do"
loc x
@@ -46,11 +44,9 @@ test:
compile [asdf] to:
%tmp = ""
return (Lua %tmp)
-
test:
asdf
assume (%tmp is (nil)) or barf "compile to is leaking variables"
-
lua> ".."
nomsu.COMPILE_ACTIONS["compile % to %"] = function(nomsu, tree, \%actions, \%body)
local \%args = {"nomsu", "tree", unpack(table.map(\%actions[1]:get_args(), function(a) return tostring(nomsu:compile(\
@@ -96,7 +92,6 @@ test:
parse [baz %] as (foo %)
assume ((foo 1) == "outer")
-
compile [local action %actions %body] to (..)
lua> ".."
local fn_name = "A"..string.as_lua_id(\%actions[1].stub)
@@ -124,11 +119,9 @@ compile [local action %actions %body] to (..)
test:
action [baz1]: return "baz1"
action [baz2] "baz2"
-
test:
assume ((baz1) == "baz1")
assume ((baz2) == "baz2")
-
compile [action %actions %body] to (..)
lua> ".."
local lua = \(compile as (local action %actions %body))
@@ -137,15 +130,14 @@ compile [action %actions %body] to (..)
test:
assume ((action (say %)) == (=lua "A_say_1"))
-
compile [action %action] to (Lua value (%action as lua id))
+
test:
parse [swap %x and %y] as (..)
do:
%tmp = %x
%x = %y
%y = %tmp
-
test:
set {%1:1, %2:2}
swap %1 and %2
@@ -155,7 +147,6 @@ test:
swap %tmp and %tmp2
assume ((%tmp == 2) and (%tmp2 == 1)) or barf ".."
'parse % as %' variable mangling failed.
-
compile [parse %actions as %body] to (..)
lua> ".."
local replacements = {}
@@ -207,7 +198,6 @@ compile [remove action %action] to (..)
test:
assume ("\(\(foo %x) as nomsu)" == "foo %x") or barf ".."
action source code failed.
-
compile [%tree as nomsu] to (..)
Lua value "nomsu:tree_to_nomsu(\(%tree as lua expr))"
@@ -276,18 +266,16 @@ test:
one
"two"
..== "\"one\\n\\\"two\\\"\""
-
compile [quote %s] to (Lua value "repr(\(%s as lua expr))")
+
test:
assume ((type of {}) == "table") or barf "type of failed."
-
compile [type of %obj] to (Lua value "type(\(%obj as lua expr))")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test:
assume ((parse "foo %") == \(foo %))
-
compile [parse %text] to (Lua value "nomsu:parse(\(%text as lua expr))")
compile [parse %text from %filename] to (..)
Lua value ".."
@@ -301,7 +289,6 @@ test:
run \:
external %passed = (yes)
assume %passed
-
compile [run %nomsu_code] to (..)
Lua value ".."
nomsu:run(\(%nomsu_code as lua expr), \(..)
@@ -310,7 +297,6 @@ compile [run %nomsu_code] to (..)
test:
assume ((\(5 + 5) as value) == 10) or barf "%tree as value failed."
-
action [run tree %tree, %tree as value] (lua> "return nomsu:run(\%tree)")
compile [compile %block, compiled %block, %block compiled] to (..)
Lua value "nomsu:compile(\(%block as lua))"
diff --git a/core/operators.nom b/core/operators.nom
index fbb8325..e3c71b0 100644
--- a/core/operators.nom
+++ b/core/operators.nom
@@ -22,7 +22,6 @@ compile [%a isn't %b, %a is not %b, %a not= %b, %a != %b] to (..)
# For strict identity checking, use (%x's id) is (%y's id)
test:
assume (([] == []) and (([]'s id) != ([]'s id)))
-
lua> ".."
do
local new_uuid = require('uuid')
@@ -117,7 +116,6 @@ test:
set global x local y
assume ((%foozle == "inner") and (%y == "outer")) or barf "external failed."
-
compile [external %var = %value] to:
%var_lua = (%var as lua)
assume %var_lua.is_value or barf "Invalid target for assignment: \%var"
@@ -135,7 +133,6 @@ test:
set global x local y
assume ((%foozle == "inner") and (%y == "outer")) or barf ".."
'with external' failed.
-
compile [with external %externs %body] to:
%body_lua = (%body as lua statements)
lua> ".."
@@ -151,7 +148,6 @@ test:
assume (%x == 1) or barf "'with' scoping failed"
assume (%z == (nil)) or barf "'with' scoping failed"
-
compile [with %assignments %body] to:
%lua = (%body as lua statements)
lua> ".."
@@ -189,7 +185,6 @@ compile [with %assignments %body] to:
# Math Operators
test:
assume ((5 wrapped around 2) == 1) or barf "mod not working"
-
compile [%x wrapped around %y, %x mod %y] to (..)
Lua value "(\(%x as lua expr) % \(%y as lua expr))"
@@ -204,7 +199,6 @@ test:
assume (0 <= (one) <= 2) or barf "Three-way chained comparison failed."
assume (%calls == 1) or barf ".."
Three-way comparison evaluated middle value multiple times
-
parse [%x < %y < %z] as (..)
call ([%a, %b, %c] -> ((%a < %b) and (%b < %c))) with [%x, %y, %z]
@@ -236,7 +230,6 @@ test:
assume (((no) and (barfer)) == (no))
assume ((no) or (yes))
assume ((yes) or (barfer))
-
compile [%x and %y] to (Lua value "(\(%x as lua expr) and \(%y as lua expr))")
compile [%x or %y] to (Lua value "(\(%x as lua expr) or \(%y as lua expr))")
@@ -292,12 +285,11 @@ compile [%x ARSHIFT %shift, %x >> %shift] to (..)
test:
assume ((- 5) == -5)
assume ((not (yes)) == (no))
-
compile [- %] to (Lua value "(- \(% as lua expr))")
compile [not %] to (Lua value "(not \(% as lua expr))")
+
test:
assume ((length of [1, 2, 3]) == 3)
-
compile [length of %list, len %list, || %list ||] to (..)
Lua value "(#\(%list as lua expr))"
@@ -314,7 +306,6 @@ test:
assume (%x == 4) or barf "*= failed"
wrap %x around 3
assume (%x == 1) or barf "wrap around failed"
-
parse [%var += %] as (%var = (%var + %))
parse [%var -= %] as (%var = (%var - %))
parse [%var *= %] as (%var = (%var * %))
diff --git a/core/scopes.nom b/core/scopes.nom
index 3dfe473..3456bc0 100644
--- a/core/scopes.nom
+++ b/core/scopes.nom
@@ -20,7 +20,6 @@ test:
assume ((foo) == "inner foo")
assume ((foo) == "outer foo")
-
compile [with local %locals %body, with local %locals do %body] to:
%body_lua = (%body as lua statements)
if %locals.type is:
diff --git a/core/text.nom b/core/text.nom
index 5ec7b23..395012f 100644
--- a/core/text.nom
+++ b/core/text.nom
@@ -21,7 +21,6 @@ test:
..gap
..== "nogap"
parse [アクション %spec %body] as (action %spec %body)
-
test:
%こんにちは = "こんにちは"
アクション [% と言う] "\(%)世界"
@@ -32,7 +31,6 @@ test:
test:
assume ((["x", "y"] joined with ",") == "x,y") or barf "joined with failed"
assume ((["x", "y"] joined) == "xy") or barf "joined failed"
-
action [%texts joined with %glue] (..)
lua> ".."
local text_bits = {}
@@ -40,15 +38,16 @@ action [%texts joined with %glue] (..)
return table.concat(text_bits, \%glue)
parse [joined %texts, %texts joined] as (%texts joined with "")
+
test:
assume ((byte 2 of "BAR") == 65)
assume ((bytes 1 to 2 of "BAR") == [66, 65])
-
compile [byte %i of %text] to (..)
Lua value "(\(%text as lua expr)):byte(\(%i as lua expr))"
compile [bytes %start to %stop of %text] to (..)
- Lua value "list{(\(%text as lua expr)):byte(\(%start as lua expr), \(%stop as lua expr))}"
+ Lua value ".."
+ list{(\(%text as lua expr)):byte(\(%start as lua expr), \(%stop as lua expr))}
test:
assume (("asdf" capitalized) == "Asdf")
@@ -63,7 +62,6 @@ compile [uppercase %text, %text uppercase] to (..)
test:
assume (("asdf" with "X" instead of "s") == "aXdf") or barf ".."
substitution failed
-
compile [..]
%text with %sub instead of %patt, %text with %patt replaced by %sub
%text s/ %patt / %sub
@@ -78,7 +76,6 @@ test:
one
two
..== ["one", "two"]
-
action [lines in %text, lines of %text] (..)
lua> ".."
local result = list{}
@@ -99,7 +96,9 @@ compile [%expr for %match in %text matching %patt] to (..)
Lua value ".."
(function()
local ret = list{}
- for \(%match as lua expr) in (\(%text as lua expr)):gmatch(\(%patt as lua expr)) do
+ for \(%match as lua expr) in (\(%text as lua expr)):gmatch(\(..)
+ %patt as lua expr
+ ..) do
ret[#ret+1] = \(%expr as lua statements)
end
return ret