aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2018-11-09 14:36:15 -0800
committerBruce Hill <bruce@bruce-hill.com>2018-11-09 14:37:22 -0800
commit7d2b7199d87930096b7fd799709fe0105d51eccb (patch)
treef33de61cb44357497758bb08ec6993753d8baa43 /core
parentba96cdfa07cea15ada62f8f89b2563de1286a0de (diff)
Lots of cleanups, including expanded use of (... compiles to "text
literal") shorthand, deprecating Lua value, and more use of Lua "..." with text interpolations.
Diffstat (limited to 'core')
-rw-r--r--core/collections.nom33
-rw-r--r--core/control_flow.nom181
-rw-r--r--core/coroutines.nom21
-rw-r--r--core/errors.nom60
-rw-r--r--core/io.nom4
-rw-r--r--core/math.nom88
-rw-r--r--core/metaprogramming.nom5
-rw-r--r--core/operators.nom78
-rw-r--r--core/text.nom24
9 files changed, 225 insertions, 269 deletions
diff --git a/core/collections.nom b/core/collections.nom
index 684f3f6..9a7dbe4 100644
--- a/core/collections.nom
+++ b/core/collections.nom
@@ -151,26 +151,25 @@ test:
%t = {}
set %t 's metatable to {__tostring:[%] -> "XXX"}
assume ("\%t" == "XXX")
-(set %dict 's metatable to %metatable) compiles to (..)
- Lua "setmetatable(\(%dict as lua expr), \(%metatable as lua expr));"
+(set %dict 's metatable to %metatable) compiles to "\
+ ..setmetatable(\(%dict as lua expr), \(%metatable as lua expr));"
-[% 's metatable, % 'metatable] all compile to (..)
- Lua value "getmetatable(\(% as lua expr))"
+[% 's metatable, % 'metatable] all compile to "\
+ ..getmetatable(\(% as lua expr))"
test:
assume (({} with fallback % -> (% + 1)).10 == 11)
-(%dict with fallback %key -> %value) compiles to (..)
- Lua value "\
- ..(function(d)
- local mt = {}
- for k,v in pairs(getmetatable(d) or {}) do mt[k] = v end
- mt.__index = function(self, \(%key as lua expr))
- local value = \(%value as lua expr)
- self[\(%key as lua expr)] = value
- return value
- end
- return setmetatable(d, mt)
- end)(\(%dict as lua expr))"
+(%dict with fallback %key -> %value) compiles to "\
+ ..(function(d)
+ local mt = {}
+ for k,v in pairs(getmetatable(d) or {}) do mt[k] = v end
+ mt.__index = function(self, \(%key as lua expr))
+ local value = \(%value as lua expr)
+ self[\(%key as lua expr)] = value
+ return value
+ end
+ return setmetatable(d, mt)
+ end)(\(%dict as lua expr))"
# Sorting
test:
@@ -182,7 +181,7 @@ test:
%keys = {1:999, 2:0, 3:50}
sort %x by % = %keys.%
assume (%x == [2, 3, 1])
-(sort %items) compiles to (Lua "table.sort(\(%items as lua expr));")
+(sort %items) compiles to "table.sort(\(%items as lua expr));"
[..]
sort %items by %item = %key_expr, sort %items by %item -> %key_expr
..all parse as (..)
diff --git a/core/control_flow.nom b/core/control_flow.nom
index f7a8423..236c9a8 100644
--- a/core/control_flow.nom
+++ b/core/control_flow.nom
@@ -12,19 +12,16 @@ use "core/errors.nom"
# No-Op
test: do nothing
-(do nothing) compiles to (Lua "")
+(do nothing) compiles to ""
# Conditionals
test:
if (no):
barf "conditional fail"
-(if %condition %if_body) compiles to:
- %lua = (Lua "if ")
- %lua::append (%condition as lua expr)
- %lua::append " then\n "
- %lua::append (%if_body as lua statements)
- %lua::append "\nend"
- return %lua
+(if %condition %if_body) compiles to "\
+ ..if \(%condition as lua expr) then
+ \(%if_body as lua statements)
+ end"
test:
unless (yes):
@@ -32,15 +29,12 @@ test:
(unless %condition %unless_body) parses as (if (not %condition) %unless_body)
[..]
if %condition %if_body else %else_body, unless %condition %else_body else %if_body
-..all compile to:
- %lua = (Lua "if ")
- %lua::append (%condition as lua expr)
- %lua::append " then\n "
- %lua::append (%if_body as lua statements)
- %lua::append "\nelse\n "
- %lua::append (%else_body as lua statements)
- %lua::append "\nend"
- return %lua
+..all compile to "\
+ ..if \(%condition as lua expr) then
+ \(%if_body as lua statements)
+ else
+ \(%else_body as lua statements)
+ end"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -60,7 +54,7 @@ test:
equivalent of a conditional expression: (cond and if_true or if_false)
if {Text:yes, List:yes, Dict:yes, Number:yes}.(%when_true_expr.type):
return (..)
- Lua value "\
+ Lua "\
..(\(%condition as lua expr) and \(%when_true_expr as lua expr) or \(..)
%when_false_expr as lua expr
..)"
@@ -69,7 +63,7 @@ test:
doesn't have a proper ternary operator!)
To see why this is necessary consider: (random()<.5 and false or 99)
return (..)
- Lua value "\
+ Lua "\
..((function()
if \(%condition as lua expr) then
return \(%when_true_expr as lua expr)
@@ -91,17 +85,15 @@ test:
%i -= 1
unless (%i == 0): go to (Loop)
assume (%i == 0)
-[=== %label ===, --- %label ---, *** %label ***] all compile to (..)
- Lua "\
- ..::label_\(..)
- (%label.stub if (%label.type == "Action") else %label) as lua identifier
- ..::"
-
-(go to %label) compiles to (..)
- Lua "\
- ..goto label_\(..)
- (%label.stub if (%label.type == "Action") else %label) as lua identifier
- .."
+[=== %label ===, --- %label ---, *** %label ***] all compile to "\
+ ..::label_\(..)
+ (%label.stub if (%label.type == "Action") else %label) as lua identifier
+ ..::"
+
+(go to %label) compiles to "\
+ ..goto label_\(..)
+ (%label.stub if (%label.type == "Action") else %label) as lua identifier
+ .."
# Basic loop control
(stop %var) compiles to:
@@ -114,10 +106,10 @@ test:
return (..)
Lua "goto continue_\((%var.stub if (%var.type == "action") else %var) as lua identifier)"
..else: return (Lua "goto continue")
-[===stop %var ===, ---stop %var ---, ***stop %var ***] all compile to (..)
- Lua "::stop_\((%var.stub if (%var.type == "action") else %var) as lua identifier)::"
-[===next %var ===, ---next %var ---, ***next %var ***] all compile to (..)
- Lua "::continue_\((%var.stub if (%var.type == "action") else %var) as lua identifier)::"
+[===stop %var ===, ---stop %var ---, ***stop %var ***] all compile to "\
+ ..::stop_\((%var.stub if (%var.type == "action") else %var) as lua identifier)::"
+[===next %var ===, ---next %var ---, ***next %var ***] all compile to "\
+ ..::continue_\((%var.stub if (%var.type == "action") else %var) as lua identifier)::"
# While loops
test:
@@ -139,8 +131,8 @@ test:
barf "Failed to 'do next repeat'"
assume (%x == 30)
-(do next repeat) compiles to (Lua "goto continue_repeat")
-(stop repeating) compiles to (Lua "goto stop_repeat")
+(do next repeat) compiles to "goto continue_repeat"
+(stop repeating) compiles to "goto stop_repeat"
(repeat while %condition %body) compiles to:
%lua = (..)
Lua "\
@@ -155,8 +147,7 @@ test:
if (%body has subtree \(stop repeating)):
%inner_lua = %lua
%lua = (Lua "do -- scope of 'stop repeating' label\n ")
- %lua::append %inner_lua
- %lua::append "\
+ %lua::append %inner_lua "\
..
::stop_repeat::
end -- end of 'stop repeating' label scope"
@@ -183,8 +174,7 @@ test:
if (%body has subtree \(stop repeating)):
%inner_lua = %lua
%lua = (Lua "do -- scope of 'stop repeating' label\n ")
- %lua::append %inner_lua
- %lua::append "\
+ %lua::append %inner_lua "\
..
::stop_repeat::
end -- end of 'stop repeating' label scope"
@@ -226,20 +216,17 @@ test:
%step as lua expr
.. do"
- %lua::append "\n "
- %lua::append (%body as lua statements)
+ %lua::append "\n " (%body as lua statements)
if (%body has subtree \(do next)):
%lua::append "\n ::continue::"
if (%body has subtree \(do next %var)):
- %lua::append "\n "
- %lua::append (what (===next %var ===) compiles to)
+ %lua::append "\n " (what (===next %var ===) compiles to)
%lua::append "\nend --numeric for-loop"
if (%body has subtree \(stop %var)):
%inner_lua = %lua
%lua = (Lua "do -- scope for stopping for-loop\n ")
- %lua::append %inner_lua
- %lua::append "\n "
+ %lua::append %inner_lua "\n "
%lua::append (what (===stop %var ===) compiles to)
%lua::append "\nend -- end of scope for stopping for-loop"
@@ -273,15 +260,13 @@ test:
if (%body has subtree \(do next)):
%lua::append "\n ::continue::"
if (%body has subtree \(do next %var)):
- %lua::append "\n "
- %lua::append (what (===next %var ===) compiles to)
+ %lua::append "\n " (what (===next %var ===) compiles to)
%lua::append "\nend --foreach-loop"
if (%body has subtree \(stop %var)):
%inner_lua = %lua
%lua = (Lua "do -- scope for stopping for-loop\n ")
- %lua::append %inner_lua
- %lua::append "\n "
+ %lua::append %inner_lua "\n "
%lua::append (what (===stop %var ===) compiles to)
%lua::append "\nend -- end of scope for stopping for-loop"
@@ -296,15 +281,13 @@ test:
if (%body has subtree \(do next)):
%lua::append "\n ::continue::"
if (%body has subtree \(do next %var)):
- %lua::append "\n "
- %lua::append (what (===next %var ===) compiles to)
+ %lua::append "\n " (what (===next %var ===) compiles to)
%lua::append "\nend --foreach-loop"
if (%body has subtree \(stop %var)):
%inner_lua = %lua
%lua = (Lua "do -- scope for stopping for-loop\n ")
- %lua::append %inner_lua
- %lua::append "\n "
+ %lua::append %inner_lua "\n "
%lua::append (what (===stop %var ===) compiles to)
%lua::append "\nend -- end of scope for stopping for-loop"
@@ -335,34 +318,27 @@ test:
%iterable as lua expr
..) do"
- %lua::append "\n "
- %lua::append (%body as lua statements)
+ %lua::append "\n " (%body as lua statements)
if (%body has subtree \(do next)):
%lua::append "\n ::continue::"
if (%body has subtree \(do next %key)):
- %lua::append "\n "
- %lua::append (what (===next %key ===) compiles to)
+ %lua::append "\n " (what (===next %key ===) compiles to)
if (%body has subtree \(do next %value)):
- %lua::append "\n "
- %lua::append (what (===next %value ===) compiles to)
+ %lua::append "\n " (what (===next %value ===) compiles to)
%lua::append "\nend --foreach-loop"
%stop_labels = (Lua "")
if (%body has subtree \(stop %key)):
- %stop_labels::append "\n"
- %stop_labels::append (what (===stop %key ===) compiles to)
+ %stop_labels::append "\n" (what (===stop %key ===) compiles to)
if (%body has subtree \(stop %value)):
- %stop_labels::append "\n"
- %stop_labels::append (what (===stop %value ===) compiles to)
+ %stop_labels::append "\n" (what (===stop %value ===) compiles to)
if ((size of "\%stop_labels") > 0):
%inner_lua = %lua
%lua = (Lua "do -- scope for stopping for % = % loop\n ")
- %lua::append %inner_lua
- %lua::append %stop_labels
- %lua::append "\nend"
+ %lua::append %inner_lua %stop_labels "\nend"
return %lua
@@ -411,19 +387,16 @@ test:
..need a conditional block around it. Otherwise, make sure the 'else' \
..block comes last."
- %code::append "\nelse\n "
- %code::append (%action as lua statements)
+ %code::append "\nelse\n " (%action as lua statements)
%else_allowed = (no)
..else:
- %code::append %clause
- %code::append " "
+ %code::append %clause " "
for %i in 1 to ((size of %line) - 1):
if (%i > 1):
%code::append " or "
%code::append (%line.%i as lua expr)
- %code::append " then\n "
- %code::append (%action as lua statements)
+ %code::append " then\n " (%action as lua statements)
%clause = "\nelseif"
if ((size of "\%code") == 0):
@@ -472,42 +445,34 @@ test:
..need a conditional block around it. Otherwise, make sure the 'else' \
..block comes last."
- %code::append "\nelse\n "
- %code::append (%action as lua statements)
+ %code::append "\nelse\n " (%action as lua statements)
%else_allowed = (no)
..else:
- %code::append %clause
- %code::append " "
+ %code::append %clause " "
for %i in 1 to ((size of %line) - 1):
if (%i > 1):
%code::append " or "
- %code::append "\(mangle "branch value") == "
- %code::append (%line.%i as lua expr)
+ %code::append "\(mangle "branch value") == " (%line.%i as lua expr)
- %code::append " then\n "
- %code::append (%action as lua statements)
+ %code::append " then\n " (%action as lua statements)
%clause = "\nelseif"
if ((size of "\%code") == 0):
compile error at %body "'if' block has an empty body."
..hint "This means nothing would happen, so the 'if' block should be deleted."
%code::append "\nend --when"
- %lua = (..)
+ return (..)
Lua "\
..do --if % is...
- local \(mangle "branch value") = "
- %lua::append (%branch_value as lua expr)
- %lua::append "\n "
- %lua::append %code
- %lua::append "\nend --if % is..."
- return %lua
+ local \(mangle "branch value") = \(%branch_value as lua expr)
+ \%code
+ end -- if % is..."
# Do/finally
-(do %action) compiles to:
- %lua = (Lua "do\n ")
- %lua::append (%action as lua statements)
- %lua::append "\nend -- do"
- return %lua
+(do %action) compiles to "\
+ ..do
+ \(%action as lua statements)
+ end -- do"
test:
%d = {}
@@ -519,29 +484,24 @@ test:
assume (%d.x == "good")
(do %action then always %final_action) compiles to:
define mangler
- %lua = (..)
+ return (..)
Lua "\
..do
local \(mangle "fell_through") = false
- local \(mangle "ok"), \(mangle "ret") = pcall(function()"
- %lua::append "\n "
- %lua::append (%action as lua statements)
- %lua::append "\
- .. \(mangle "fell_through") = true
- end)"
- %lua::append "\n "
- %lua::append (%final_action as lua statements)
- %lua::append "\
- .. if not \(mangle "ok") then error(ret, 0) end
- if not \(mangle "fell_through") then return ret end
- end"
- return %lua
+ local \(mangle "ok"), \(mangle "ret") = pcall(function()
+ \(%action as lua statements)
+ \(mangle "fell_through") = true
+ end)
+ \(%final_action as lua statements)
+ if not \(mangle "ok") then error(ret, 0) end
+ if not \(mangle "fell_through") then return ret end
+ end"
test:
assume ((result of (: return 99)) == 99)
# Inline thunk:
-(result of %body) compiles to (Lua value "\(what ([] -> %body) compiles to)()")
+(result of %body) compiles to "\(what ([] -> %body) compiles to)()"
test:
%t = [1, [2, [[3], 4], 5, [[[6]]]]]
@@ -564,9 +524,8 @@ test:
..do
local \(mangle "stack \(%var.1)") = List{\(%structure as lua expr)}
while #\(mangle "stack \(%var.1)") > 0 do
- \(%var as lua expr) = table.remove(\(mangle "stack \(%var.1)"), 1)"
- %lua::append "\n "
- %lua::append (%body as lua statements)
+ \(%var as lua expr) = table.remove(\(mangle "stack \(%var.1)"), 1)
+ \(%body as lua statements)"
if (%body has subtree \(do next)):
%lua::append "\n ::continue::"
if (%body has subtree \(do next %var)):
diff --git a/core/coroutines.nom b/core/coroutines.nom
index f03cfc8..782c20e 100644
--- a/core/coroutines.nom
+++ b/core/coroutines.nom
@@ -16,16 +16,13 @@ test:
for % in coroutine %co: %nums::add %
assume (%nums == [4, 5, 6, 6, 6]) or barf "Coroutine iteration failed"
-[coroutine %body, generator %body] all compile to (..)
- Lua value "\
- ..(function()
- \(%body as lua statements)
- end)"
+[coroutine %body, generator %body] all compile to "\
+ ..(function()
+ \(%body as lua statements)
+ end)"
-(->) compiles to (Lua value "coroutine.yield(true)")
-(-> %) compiles to (Lua value "coroutine.yield(true, \(% as lua expr))")
-(for % in coroutine %co %body) compiles to (..)
- Lua "\
- ..for junk,\(% as lua expr) in coroutine.wrap(\(%co as lua expr)) do
- \(%body as lua statements)
- end"
+(-> %) compiles to "coroutine.yield(true, \((% as lua expr) if % else "nil"))"
+(for % in coroutine %co %body) compiles to "\
+ ..for _junk,\(% as lua expr) in coroutine.wrap(\(%co as lua expr)) do
+ \(%body as lua statements)
+ end"
diff --git a/core/errors.nom b/core/errors.nom
index 74c0a54..b2222ee 100644
--- a/core/errors.nom
+++ b/core/errors.nom
@@ -6,11 +6,11 @@ use "core/metaprogramming.nom"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-(barf %msg) compiles to (Lua "error(\(=lua "\%msg and \(%msg as lua expr) or 'nil'"), 0);")
-(compile error at %tree %msg) compiles to (..)
- Lua "nomsu:compile_error(\(%tree as lua expr), \(%msg as lua expr))"
-(compile error at %tree %msg hint %hint) compiles to (..)
- Lua "nomsu:compile_error(\(%tree as lua expr), \(%msg as lua expr), \(%hint as lua expr))"
+(barf %msg) compiles to "error(\(=lua "\%msg and \(%msg as lua expr) or 'nil'"), 0);"
+(compile error at %tree %msg) compiles to "\
+ ..nomsu:compile_error(\(%tree as lua expr), \(%msg as lua expr))"
+(compile error at %tree %msg hint %hint) compiles to "\
+ ..nomsu:compile_error(\(%tree as lua expr), \(%msg as lua expr), \(%hint as lua expr))"
(assume %condition) compiles to:
lua> "\
@@ -36,11 +36,10 @@ use "core/metaprogramming.nom"
end
end"
-(assume %condition or barf %message) compiles to (..)
- Lua "\
- ..if not \(%condition as lua expr) then
- error(\(%message as lua expr), 0)
- end"
+(assume %condition or barf %message) compiles to "\
+ ..if not \(%condition as lua expr) then
+ error(\(%message as lua expr), 0)
+ end"
test:
try (barf) and if it succeeds: barf "try failed."
@@ -59,29 +58,28 @@ test:
[..]
try %action and if it succeeds %success or if it barfs %msg %fallback
try %action and if it barfs %msg %fallback or if it succeeds %success
-..all compile to (..)
- Lua "\
- ..do
- local fell_through = false
- local err, erred = nil, false
- local ok, ret = xpcall(function()
- \(%action as lua statements)
- fell_through = true
- end, function(\(=lua "\%fallback and \(%msg as lua expr) or ''"))
- local ok, ret = pcall(function()
- \((=lua "\%fallback or \%msg") as lua statements)
- end)
- if not ok then err, erred = ret, true end
+..all compile to "\
+ ..do
+ local fell_through = false
+ local err, erred = nil, false
+ local ok, ret = xpcall(function()
+ \(%action as lua statements)
+ fell_through = true
+ end, function(\(=lua "\%fallback and \(%msg as lua expr) or ''"))
+ local ok, ret = pcall(function()
+ \((=lua "\%fallback or \%msg") as lua statements)
end)
- if ok then
- \(%success as lua statements)
- if not fell_through then
- return ret
- end
- elseif erred then
- error(err, 0)
+ if not ok then err, erred = ret, true end
+ end)
+ if ok then
+ \(%success as lua statements)
+ if not fell_through then
+ return ret
end
- end"
+ elseif erred then
+ error(err, 0)
+ end
+ end"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/core/io.nom b/core/io.nom
index e12e4eb..edeeab4 100644
--- a/core/io.nom
+++ b/core/io.nom
@@ -17,9 +17,9 @@ use "core/metaprogramming.nom"
(ask %prompt) compiles to:
lua> "\
..if \%prompt.type == "Text" then
- return LuaCode.Value(tree.source, "(io.write(", \(%prompt as lua expr), ") and io.read())");
+ return LuaCode(tree.source, "(io.write(", \(%prompt as lua expr), ") and io.read())");
else
- return LuaCode.Value(tree.source, "(io.write(tostring(", \(..)
+ return LuaCode(tree.source, "(io.write(tostring(", \(..)
%prompt as lua expr
.., ")) and io.read())");
end"
diff --git a/core/math.nom b/core/math.nom
index 36d1d90..1c7306e 100644
--- a/core/math.nom
+++ b/core/math.nom
@@ -16,18 +16,18 @@ test:
..math constants failed"
%nan = (NaN)
assume (%nan != %nan) or barf "NaN failed"
-[infinity, inf] all compile to (Lua value "math.huge")
-[not a number, NaN, nan] all compile to (Lua value "(0/0)")
-[pi, Pi, PI] all compile to (Lua value "math.pi")
-[tau, Tau, TAU] all compile to (Lua value "(2*math.pi)")
-(golden ratio) compiles to (Lua value "((1+math.sqrt(5))/2)")
-(e) compiles to (Lua value "math.exp(1)")
+[infinity, inf] all compile to "math.huge"
+[not a number, NaN, nan] all compile to "(0/0)"
+[pi, Pi, PI] all compile to "math.pi"
+[tau, Tau, TAU] all compile to "(2*math.pi)"
+(golden ratio) compiles to "((1+math.sqrt(5))/2)"
+(e) compiles to "math.exp(1)"
# Functions:
test:
assume (("5" as a number) == 5)
-[% as a number, % as number] all compile to (..)
- Lua value "tonumber(\(% as lua expr))"
+((% as a number)'s meaning) = ((tonumber %)'s meaning)
+((% as number)'s meaning) = ((tonumber %)'s meaning)
test:
assume (..)
@@ -37,40 +37,40 @@ test:
hyperbolic tangent 5, e^ 5, ln 5, log base 2 of 5, floor 5, ceiling 5, round 5
..or barf "math functions failed"
[absolute value %, | % |, abs %] all compile to (..)
- Lua value "math.abs(\(% as lua expr))"
+ "math.abs(\(% as lua expr))"
[square root %, square root of %, √ %, sqrt %] all compile to (..)
- Lua value "math.sqrt(\(% as lua expr))"
-
-[sine %, sin %] all compile to (Lua value "math.sin(\(% as lua expr))")
-[cosine %, cos %] all compile to (Lua value "math.cos(\(% as lua expr))")
-[tangent %, tan %] all compile to (Lua value "math.tan(\(% as lua expr))")
-[arc sine %, asin %] all compile to (Lua value "math.asin(\(% as lua expr))")
-[arc cosine %, acos %] all compile to (Lua value "math.acos(\(% as lua expr))")
-[arc tangent %, atan %] all compile to (Lua value "math.atan(\(% as lua expr))")
+ "math.sqrt(\(% as lua expr))"
+
+[sine %, sin %] all compile to "math.sin(\(% as lua expr))"
+[cosine %, cos %] all compile to "math.cos(\(% as lua expr))"
+[tangent %, tan %] all compile to "math.tan(\(% as lua expr))"
+[arc sine %, asin %] all compile to "math.asin(\(% as lua expr))"
+[arc cosine %, acos %] all compile to "math.acos(\(% as lua expr))"
+[arc tangent %, atan %] all compile to "math.atan(\(% as lua expr))"
[arc tangent %y / %x, atan2 %y %x] all compile to (..)
- Lua value "math.atan2(\(%y as lua expr), \(%x as lua expr))"
+ "math.atan2(\(%y as lua expr), \(%x as lua expr))"
[hyperbolic sine %, sinh %] all compile to (..)
- Lua value "math.sinh(\(% as lua expr))"
+ "math.sinh(\(% as lua expr))"
[hyperbolic cosine %, cosh %] all compile to (..)
- Lua value "math.cosh(\(% as lua expr))"
+ "math.cosh(\(% as lua expr))"
[hyperbolic tangent %, tanh %] all compile to (..)
- Lua value "math.tanh(\(% as lua expr))"
+ "math.tanh(\(% as lua expr))"
-[e^ %, exp %] all compile to (Lua value "math.exp(\(% as lua expr))")
+[e^ %, exp %] all compile to "math.exp(\(% as lua expr))"
[natural log %, ln %, log %] all compile to (..)
- Lua value "math.log(\(% as lua expr))"
+ "math.log(\(% as lua expr))"
[log % base %base, log base %base of %] all compile to (..)
- Lua value "math.log(\(% as lua expr), \(%base as lua expr))"
+ "math.log(\(% as lua expr), \(%base as lua expr))"
-(floor %) compiles to (Lua value "math.floor(\(% as lua expr))")
-[ceiling %, ceil %] all compile to (Lua value "math.ceil(\(% as lua expr))")
+(floor %) compiles to "math.floor(\(% as lua expr))"
+[ceiling %, ceil %] all compile to "math.ceil(\(% as lua expr))"
[round %, % rounded] all compile to (..)
- Lua value "math.floor(\(% as lua expr) + .5)"
+ "math.floor(\(% as lua expr) + .5)"
test:
assume ((463 to the nearest 100) == 500) or barf "rounding failed"
@@ -86,8 +86,11 @@ externally [all of %items, all %items] all mean:
[all of %items, all %items] all compile to:
unless (%items.type is "List"):
return %tree
- %clauses = (((% as lua expr)::text) for % in %items)
- return (Lua value "(\(%clauses::joined with " and "))")
+ if ((size of %items) == 0): return (Lua "true")
+ %lua = (Lua "(")
+ %lua::add ((% as lua expr) for % in %items) joined with " and "
+ %lua::append ")"
+ return %lua
[not all of %items, not all %items] all parse as (not (all of %items))
externally [any of %items, any %items] all mean:
@@ -97,8 +100,11 @@ externally [any of %items, any %items] all mean:
[any of %items, any %items] all compile to:
unless (%items.type is "List"):
return %tree
- %clauses = (((% as lua expr)::text) for % in %items)
- return (Lua value "(\(%clauses::joined with " or "))")
+ if ((size of %items) == 0): return (Lua "false")
+ %lua = (Lua "(")
+ %lua::add ((% as lua expr) for % in %items) joined with " or "
+ %lua::append ")"
+ return %lua
[none of %items, none %items] all parse as (not (any of %items))
# Sum/product
@@ -109,8 +115,11 @@ externally [sum of %items, sum %items] all mean:
[sum of %items, sum %items] all compile to:
unless (%items.type is "List"):
return %tree
- %clauses = (((% as lua expr)::text) for % in %items)
- return (Lua value "(\(%clauses::joined with " + "))")
+ if ((size of %items) == 0): return (Lua "0")
+ %lua = (Lua "(")
+ %lua::add ((% as lua expr) for % in %items) joined with " + "
+ %lua::append ")"
+ return %lua
externally [product of %items, product %items] all mean:
%prod = 1
@@ -119,8 +128,11 @@ externally [product of %items, product %items] all mean:
[product of %items, product %items] all compile to:
unless (%items.type is "List"):
return %tree
- %clauses = (((% as lua expr)::text) for % in %items)
- return (Lua value "(\(%clauses::joined with " * "))")
+ if ((size of %items) == 0): return (Lua "1")
+ %lua = (Lua "(")
+ %lua::add ((% as lua expr) for % in %items) joined with " * "
+ %lua::append ")"
+ return %lua
externally [avg of %items, average of %items] all mean (..)
(sum of %items) / (size of %items)
@@ -212,12 +224,12 @@ externally (seed random with %) means:
for i=1,20 do math.random(); end"
(seed random) parses as (seed random with (=lua "os.time()"))
-[random number, random, rand] all compile to (Lua value "math.random()")
+[random number, random, rand] all compile to "math.random()"
[random int %n, random integer %n, randint %n] all compile to (..)
- Lua value "math.random(\(%n as lua expr))"
+ "math.random(\(%n as lua expr))"
[random from %low to %high, random number from %low to %high, rand %low %high] all \
-..compile to (Lua value "math.random(\(%low as lua expr), \(%high as lua expr))")
+..compile to "math.random(\(%low as lua expr), \(%high as lua expr))"
externally [..]
random choice from %elements, random choice %elements, random %elements
diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom
index be7450a..2ae8b1b 100644
--- a/core/metaprogramming.nom
+++ b/core/metaprogramming.nom
@@ -71,9 +71,8 @@ lua> "\
if \%body.type == "Text" then
\%body = SyntaxTree{source=\%body.source, type="Action", "Lua", \%body}
end
- local lua = LuaCode(tree.source, "compile.action[", \%action.stub:as_lua(),
+ return LuaCode(tree.source, "compile.action[", \%action.stub:as_lua(),
"] = ", \(what (%args -> %body) compiles to))
- return lua
end"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -332,7 +331,7 @@ test:
..one
"two""
..== "\"one\\n\\\"two\\\"\""
-(quote %s) compiles to (Lua "tostring(\(%s as lua expr)):as_lua()")
+(quote %s) compiles to "tostring(\(%s as lua expr)):as_lua()"
test:
assume (lua type of {}) == "table"
diff --git a/core/operators.nom b/core/operators.nom
index 1d8e63f..66185f7 100644
--- a/core/operators.nom
+++ b/core/operators.nom
@@ -11,15 +11,14 @@ test:
assume (all [1 < 2, 2 > 1, 1 <= 2, 2 >= 1, 1 == 1, 1 != 2])
# Comparison Operators
-(%x < %y) compiles to (Lua value "(\(%x as lua expr) < \(%y as lua expr))")
-(%x > %y) compiles to (Lua value "(\(%x as lua expr) > \(%y as lua expr))")
-(%x <= %y) compiles to (Lua value "(\(%x as lua expr) <= \(%y as lua expr))")
-(%x >= %y) compiles to (Lua value "(\(%x as lua expr) >= \(%y as lua expr))")
-[%a is %b, %a == %b] all compile to (..)
- Lua value "(\(%a as lua expr) == \(%b as lua expr))"
+(%x < %y) compiles to "(\(%x as lua expr) < \(%y as lua expr))"
+(%x > %y) compiles to "(\(%x as lua expr) > \(%y as lua expr))"
+(%x <= %y) compiles to "(\(%x as lua expr) <= \(%y as lua expr))"
+(%x >= %y) compiles to "(\(%x as lua expr) >= \(%y as lua expr))"
+[%a is %b, %a == %b] all compile to "(\(%a as lua expr) == \(%b as lua expr))"
-[%a isn't %b, %a is not %b, %a not= %b, %a != %b] all compile to (..)
- Lua value "(\(%a as lua expr) ~= \(%b as lua expr))"
+[%a isn't %b, %a is not %b, %a not= %b, %a != %b] all compile to "\
+ ..(\(%a as lua expr) ~= \(%b as lua expr))"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -45,6 +44,7 @@ test:
assume ((%y == 10) and (%x == 20)) or barf "swapping vars failed."
# Simultaneous mutli-assignments like: x,y,z = 1,x,3;
+# TODO: deprecate?
(set %assignments) compiles to:
assume (%assignments.type is "Dict") or barf "\
..Expected a Dict for the assignments part of '<- %' statement, not \%assignments"
@@ -143,7 +143,7 @@ test:
test:
assume ((5 wrapped around 2) == 1) or barf "mod not working"
[%x wrapped around %y, %x mod %y] all compile to (..)
- Lua value "((\(%x as lua expr)) % (\(%y as lua expr)))"
+ "((\(%x as lua expr)) % (\(%y as lua expr)))"
# 3-part chained comparisons
# (uses a lambda to avoid re-evaluating middle value, while still being an expression)
@@ -187,8 +187,8 @@ test:
assume (((no) and (barfer)) == (no))
assume ((no) or (yes))
assume ((yes) or (barfer))
-(%x and %y) compiles to (Lua value "(\(%x as lua expr) and \(%y as lua expr))")
-(%x or %y) compiles to (Lua value "(\(%x as lua expr) or \(%y as lua expr))")
+(%x and %y) compiles to "(\(%x as lua expr) and \(%y as lua expr))"
+(%x or %y) compiles to "(\(%x as lua expr) or \(%y as lua expr))"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -204,49 +204,39 @@ test:
# Lua 5.3 introduced bit operators like | and &. Use them when possible, otherwise
fall back to bit.bor(), bit.band(), etc.
-%use_bitops = ((is jit) or ((Lua version) == "Lua 5.2"))
-[NOT %, ~ %] all compile to (..)
- Lua value (..)
- (%use_bitops and "bit.bnot(\(% as lua expr))") or "~(\(% as lua expr))"
-
-[%a OR %b, %a | %b] all compile to (..)
- Lua value (..)
- (%use_bitops and "bit.bor(\(%a as lua expr), \(%b as lua expr))") or "\
- ..(\(%a as lua expr) | \(%b as lua expr))"
-
-[%a XOR %b, %a ~ %b] all compile to (..)
- Lua value (..)
- (%use_bitops and "bit.bxor(\(%a as lua expr), \(%b as lua expr))") or "\
- ..(\(%a as lua expr) ~ \(%b as lua expr))"
-
-[%a AND %b, %a & %b] all compile to (..)
- Lua value (..)
- (%use_bitops and "bit.band(\(%a as lua expr), \(%b as lua expr))") or "\
- ..(\(%a as lua expr) & \(%b as lua expr))"
-
-[%x LSHIFT %shift, %x << %shift] all compile to (..)
- Lua value (..)
- (%use_bitops and "bit.lshift(\(%x as lua expr), \(%shift as lua expr))") or "\
- ..(\(%x as lua expr) << \(%shift as lua expr))"
-
-[%x RSHIFT %shift, %x >> %shift] all compile to (..)
- Lua value (..)
- (%use_bitops and "bit.rshift(\(%x as lua expr), \(%shift as lua expr))") or "\
- ..(\(%x as lua expr) >> \(%shift as lua expr))"
+lua> "if \((is jit) or ((Lua version) == "Lua 5.2")) then"
+[NOT %, ~ %] all compile to "bit.bnot(\(% as lua expr))"
+[%x OR %y, %x | %y] all compile to "bit.bor(\(%x as lua expr), \(%y as lua expr))"
+[%x XOR %y, %x ~ %y] all compile to "bit.bxor(\(%x as lua expr), \(%y as lua expr))"
+[%x AND %y, %x & %y] all compile to "bit.band(\(%x as lua expr), \(%y as lua expr))"
+[%x LSHIFT %shift, %x << %shift] all compile to "\
+ ..bit.lshift(\(%x as lua expr), \(%shift as lua expr))"
+[%x RSHIFT %shift, %x >> %shift] all compile to "\
+ ..bit.rshift(\(%x as lua expr), \(%shift as lua expr))"
+lua> "else"
+[NOT %, ~ %] all compile to "~(\(% as lua expr))"
+[%x OR %y, %x | %y] all compile to "(\(%x as lua expr) | \(%y as lua expr))"
+[%x XOR %y, %x ~ %y] all compile to "(\(%x as lua expr) ~ \(%y as lua expr))"
+[%x AND %y, %x & %y] all compile to "(\(%x as lua expr) & \(%y as lua expr))"
+[%x LSHIFT %shift, %x << %shift] all compile to "\
+ ..(\(%x as lua expr) << \(%shift as lua expr))"
+[%x RSHIFT %shift, %x >> %shift] all compile to "\
+ ..(\(%x as lua expr) >> \(%shift as lua expr))"
+lua> "end"
# Unary operators
test:
assume ((- 5) == -5)
assume ((not (yes)) == (no))
-(- %) compiles to (Lua value "(- \(% as lua expr))")
-(not %) compiles to (Lua value "(not \(% as lua expr))")
+(- %) compiles to "(- \(% as lua expr))"
+(not %) compiles to "(not \(% as lua expr))"
test:
assume ((size of [1, 2, 3]) == 3)
[size of %list, size of %list, size of %list, size of %list] all compile to (..)
- Lua value "(#\(%list as lua expr))"
+ "(#\(%list as lua expr))"
-(%list is empty) compiles to (Lua value "(#\(%list as lua expr) == 0)")
+(%list is empty) compiles to "(#\(%list as lua expr) == 0)"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/core/text.nom b/core/text.nom
index 38fa08b..1e297aa 100644
--- a/core/text.nom
+++ b/core/text.nom
@@ -29,17 +29,19 @@ test:
%こんにちは = "こんにちは"
(% と言う) とは "\(%)世界"
assume (%こんにちは と言う) == "こんにちは世界"
-(%expr for %match in %text matching %patt) compiles to (..)
- Lua value "\
- ..(function()
- local ret = _List{}
- 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
- end)()"
+(%expr for %match in %text matching %patt) compiles to:
+ define mangler
+ return (..)
+ Lua "\
+ ..(function()
+ local \(mangle "comprehension") = _List{}
+ for \(%match as lua expr) in (\(%text as lua expr)):gmatch(\(..)
+ %patt as lua expr
+ ..) do
+ \(mangle "comprehension")[#\(mangle "comprehension")+1] = \(%expr as lua statements)
+ end
+ return \(mangle "comprehension")
+ end)()"
test:
assume "\n" == (newline)