aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/collections.nom134
-rw-r--r--core/control_flow.nom159
-rw-r--r--core/coroutines.nom8
-rw-r--r--core/errors.nom3
-rw-r--r--core/io.nom7
-rw-r--r--core/math.nom30
-rw-r--r--core/metaprogramming.nom25
-rw-r--r--core/operators.nom19
-rw-r--r--core/scopes.nom23
-rw-r--r--core/text.nom10
10 files changed, 290 insertions, 128 deletions
diff --git a/core/collections.nom b/core/collections.nom
index 428ed4d..f74b24d 100644
--- a/core/collections.nom
+++ b/core/collections.nom
@@ -1,4 +1,4 @@
-#!/usr/bin/env nomsu -V2.4.4.3
+#!/usr/bin/env nomsu -V2.5.4.3
#
This file contains code that supports manipulating and using collections like lists
and dictionaries.
@@ -9,10 +9,12 @@ use "core/operators.nom"
# List/dict functions:
# Indexing
-test: assume ((2 nd to last in [1, 2, 3, 4, 5]) is 4)
+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
+ %index st to last in %list, %index nd to last in %list
+ %index rd to last in %list, %index th to last in %list
..to (Lua value "utils.nth_to_last(\(%list as lua expr), \(%index as lua expr))")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -21,24 +23,34 @@ parse [last in %list] as (1 st to last in %list)
parse [first in %list] as %list.1
# Membership testing
-test: assume (3 is in [1, 2, 3, 4, 5])
+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)
+ for %key = %value in %list:
+ if (%key is %item): return (yes)
+
return (no)
-test: assume (99 isn't in [1, 2, 3])
+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
..:
- for %key = %value in %list: if (%key is %item): return (no)
+ for %key = %value in %list:
+ if (%key is %item): return (no)
+
return (yes)
-test: assume ({x: no} has key "x")
+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"))
+ 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
@@ -58,12 +70,12 @@ test:
assume ((first in %list) is 2)
compile [..]
- append %item to %list, add %item to %list, to %list add %item
- to %list append %item
+ 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))")
compile [add %item to %list at index %i] to (..)
- Lua "table.insert(\(%list as lua expr), \(%i as lua expr), \(%item as lua expr))"
+ Lua ".."
+ table.insert(\(%list as lua expr), \(%i as lua expr), \(%item as lua expr))
compile [pop from %list, remove last from %list] to (..)
Lua value "table.remove(\(%list as lua expr))"
@@ -74,11 +86,15 @@ compile [remove index %index from %list] to (..)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# List Comprehension
-test: assume (((% * %) for % in [1, 2, 3]) == [1, 4, 9])
+test:
+ assume (((% * %) for % in [1, 2, 3]) == [1, 4, 9])
+
parse [%expression for %item in %iterable] as (..)
result of:
%comprehension = []
- for %item in %iterable: add %expression to %comprehension
+ for %item in %iterable:
+ add %expression to %comprehension
+
return %comprehension
parse [..]
@@ -87,37 +103,49 @@ parse [..]
..as (..)
result of:
%comprehension = []
- for %index in %start to %stop via %step: add %expression to %comprehension
+ for %index in %start to %stop via %step:
+ add %expression to %comprehension
+
return %comprehension
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-test: assume (((% * %) for % in 1 to 3) == [1, 4, 9])
+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"])
+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
..as (..)
result of:
%comprehension = []
- for %key = %value in %iterable: add %expression to %comprehension
+ for %key = %value in %iterable:
+ add %expression to %comprehension
+
return %comprehension
# 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 (..)
+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:
%comprehension = {}
- for %item in %iterable: %comprehension.%key = %value
+ for %item in %iterable:
+ %comprehension.%key = %value
+
return %comprehension
test:
- assume ((%k = (%v * %v) for %k = %v in {x: 1, y: 2, z: 3}) == {x: 1, y: 4, z: 9})
+ 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
@@ -125,7 +153,9 @@ parse [..]
..as (..)
result of:
%comprehension = {}
- for %src_key = %src_value in %iterable: %comprehension.%key = %value
+ for %src_key = %src_value in %iterable:
+ %comprehension.%key = %value
+
return %comprehension
parse [..]
@@ -134,40 +164,56 @@ parse [..]
..as (..)
result of:
%comprehension = {}
- for %item in %start to %stop via %step: %comprehension.%key = %value
+ for %item in %start to %stop via %step:
+ %comprehension.%key = %value
+
return %comprehension
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-test: assume (((% * %) = % for % in 1 to 3) == {1: 1, 4: 2, 9: 3})
+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
..as (%key = %value for %item in %start to %stop via 1)
-test: assume (([[1, 2], [3, 4]] flattened) == [1, 2, 3, 4])
+test:
+ assume (([[1, 2], [3, 4]] flattened) == [1, 2, 3, 4])
+
action [%lists flattened]:
%flat = []
- for %list in %lists: for %item in %list: add %item to %flat
+ for %list in %lists:
+ for %item in %list: add %item to %flat
+
return %flat
-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"])
+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])
+test:
+ assume ((values in {x:1}) == [1])
+
parse [values in %dict, values of %dict] as (%v for %k = %v in %dict)
# Metatable stuff
test:
%t = {}
- set %t 's metatable to {__tostring: [%] -> "XXX"}
+ 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)
+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))
@@ -184,7 +230,7 @@ test:
assume (%x == [1, 2, 3])
sort %x by % = (- %)
assume (%x == [3, 2, 1])
- %keys = {1: 999, 2: 0, 3: 50}
+ %keys = {1:999, 2:0, 3:50}
sort %x by % = %keys.%
assume (%x == [2, 3, 1])
@@ -196,7 +242,9 @@ parse [sort %items by %item = %key_expr, sort %items by %item -> %key_expr] as (
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-test: assume ((sorted [3, 1, 2]) == [1, 2, 3])
+test:
+ assume ((sorted [3, 1, 2]) == [1, 2, 3])
+
action [%items sorted, sorted %items]:
%copy = (% for % in %items)
sort %copy
@@ -208,9 +256,15 @@ parse [%items sorted by %item = %key, %items sorted by %item -> %key] as (..)
sort %copy by %item = %key
return %copy
-test: assume ((unique [1, 2, 1, 3, 2, 3]) == [1, 2, 3])
+test:
+ assume ((unique [1, 2, 1, 3, 2, 3]) == [1, 2, 3])
+
action [unique %items]:
%unique = []
%seen = {}
- for % in %items: unless %seen.% (: add % to %unique; %seen.% = (yes))
+ for % in %items:
+ unless %seen.%:
+ add % to %unique
+ %seen.% = (yes)
+
return %unique \ No newline at end of file
diff --git a/core/control_flow.nom b/core/control_flow.nom
index 5719d72..63d7900 100644
--- a/core/control_flow.nom
+++ b/core/control_flow.nom
@@ -1,4 +1,4 @@
-#!/usr/bin/env nomsu -V2.3.4.3
+#!/usr/bin/env nomsu -V2.5.4.3
#
This file contains compile-time actions that define basic control flow structures
like "if" statements and loops.
@@ -13,11 +13,20 @@ test: do nothing
compile [do nothing] to (Lua "")
# Conditionals
-test: if (no): barf "conditional fail"
+test:
+ if (no):
+ barf "conditional fail"
+
compile [if %condition %if_body] to (..)
- Lua "if \(%condition as lua expr) then\n \(%if_body as lua statements)\nend"
+ Lua ".."
+ if \(%condition as lua expr) then
+ \(%if_body as lua statements)
+ end
+
+test:
+ unless (yes):
+ barf "conditional fail"
-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
@@ -42,10 +51,12 @@ compile [..]
..to (..)
# If %when_true_expr is guaranteed to be truthy, we can use Lua's idiomatic
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):
+ if {Text:yes, List:yes, Dict:yes, Number:yes}.(%when_true_expr.type):
return (..)
Lua value ".."
- (\(%condition as lua expr) and \(%when_true_expr as lua expr) or \(%when_false_expr as lua expr))
+ (\(%condition as lua expr) and \(%when_true_expr as lua expr) or \(..)
+ %when_false_expr as lua expr
+ ..)
..else:
# Otherwise, need to do an anonymous inline function (yuck, too bad lua
@@ -61,6 +72,7 @@ compile [..]
end
end)())
+
# GOTOs
compile [=== %label ===, --- %label ---, *** %label ***] to (..)
Lua "::label_\(%label as lua identifier)::"
@@ -91,7 +103,9 @@ compile [do next repeat] to (Lua "goto continue_repeat")
compile [stop repeating] to (Lua "goto stop_repeat")
compile [repeat while %condition %body] to:
%lua = (..)
- Lua "while \(%condition as lua expr) do\n \(%body as lua statements)"
+ Lua ".."
+ while \(%condition as lua expr) do
+ \(%body as lua statements)
if (..)
%body has subtree % where ((%.type == "Action") and (%.stub is "do next repeat"))
@@ -115,7 +129,9 @@ parse [repeat %body] as (repeat while (yes) %body)
parse [repeat until %condition %body] as (repeat while (not %condition) %body)
compile [repeat %n times %body] to:
%lua = (..)
- Lua "for i=1,\(%n as lua expr) do\n \(%body as lua statements)"
+ Lua ".."
+ for i=1,\(%n as lua expr) do
+ \(%body as lua statements)
if (..)
%body has subtree % where ((%.type == "Action") and (%.stub is "do next repeat"))
@@ -154,10 +170,14 @@ compile [..]
..to:
# This uses Lua's approach of only allowing loop-scoped variables in a loop
- unless (%var.type is "Var"): compile error at %var.source "Loop expected variable, not: %s"
+ unless (%var.type is "Var"):
+ compile error at %var.source "Loop expected variable, not: %s"
+
%lua = (..)
Lua ".."
- for \(%var as lua expr)=\(%start as lua expr),\(%stop as lua expr),\(%step as lua expr) do
+ for \(%var as lua expr)=\(%start as lua expr),\(%stop as lua expr),\(..)
+ %step as lua expr
+ .. do
\(%body as lua statements)
if (..)
@@ -185,11 +205,14 @@ compile [..]
parse [for %var in %start to %stop %body] as (..)
for %var in %start to %stop via 1 %body
+
# For-each loop (lua's "ipairs()")
compile [for %var in %iterable %body] to:
# This uses Lua's approach of only allowing loop-scoped variables in a loop
- unless (%var.type is "Var"): compile error at %var.source "Loop expected variable, not: %s"
+ unless (%var.type is "Var"):
+ compile error at %var.source "Loop expected variable, not: %s"
+
%lua = (..)
Lua ".."
for i,\(%var as lua identifier) in ipairs(\(%iterable as lua expr)) do
@@ -199,8 +222,7 @@ compile [for %var in %iterable %body] to:
%body has subtree % where (..)
(%.type == "Action") and ((%.stub is "do next %") and (%.(3).1 == %var.1))
..:
- to %lua write (..)
- Lua "\n\(compile as (===next %var ===))"
+ to %lua write (Lua "\n\(compile as (===next %var ===))")
to %lua write "\nend --foreach-loop"
if (..)
@@ -218,30 +240,35 @@ compile [for %var in %iterable %body] to:
# Dict iteration (lua's "pairs()")
-compile [for %key = %value in %iterable %body, for %key %value in %iterable %body]
+compile [..]
+ for %key = %value in %iterable %body, for %key %value in %iterable %body
..to:
# This uses Lua's approach of only allowing loop-scoped variables in a loop
- unless (%key.type is "Var"): compile error at %key.source "Loop expected variable, not: %s"
- unless (%value.type is "Var"): compile error at %value.source "Loop expected variable, not: %s"
+ unless (%key.type is "Var"):
+ compile error at %key.source "Loop expected variable, not: %s"
+
+ unless (%value.type is "Var"):
+ compile error at %value.source "Loop expected variable, not: %s"
+
%lua = (..)
Lua ".."
- for \(%key as lua identifier),\(%value as lua identifier) in pairs(\(%iterable as lua expr)) do
+ for \(%key as lua identifier),\(%value as lua identifier) in pairs(\(..)
+ %iterable as lua expr
+ ..) do
\(%body as lua statements)
if (..)
%body has subtree % where (..)
(%.type == "Action") and ((%.stub is "do next %") and (%.(3).1 == %key.1))
..:
- to %lua write (..)
- Lua "\n\(compile as (===next %key ===))"
+ to %lua write (Lua "\n\(compile as (===next %key ===))")
if (..)
%body has subtree % where (..)
(%.type == "Action") and ((%.stub is "do next %") and (%.(3).1 == %value.1))
..:
- to %lua write (..)
- Lua "\n\(compile as (===next %value ===))"
+ to %lua write (Lua "\n\(compile as (===next %value ===))")
to %lua write "\nend --foreach-loop"
%stop_labels = (Lua "")
@@ -259,7 +286,10 @@ compile [for %key = %value in %iterable %body, for %key %value in %iterable %bod
if ((length of "\%stop_labels") > 0):
%lua = (..)
- Lua "do -- scope for stopping for % = % loop\n \%lua\%stop_labels\nend"
+ Lua ".."
+ do -- scope for stopping for % = % loop
+ \%lua\%stop_labels
+ end
return %lua
@@ -270,22 +300,32 @@ compile [if %body, when %body] to:
%code = (Lua "")
%clause = "if"
%else_allowed = (yes)
- unless (%body.type is "Block"): compile error at %body.source "'if' expected a Block, but got: %s"
+ unless (%body.type is "Block"):
+ compile error at %body.source "'if' expected a Block, but got: %s"
+
for %line in %body:
unless (..)
- ((%line.type is "Action") and ((length of %line) >= 2))
- ..and (%line.(length of %line) is "Block" syntax tree)
+ ((%line.type is "Action") and ((length of %line) >= 2)) and (..)
+ %line.(length of %line) is "Block" syntax tree
..:
compile error at %line.source ".."
- Invalid line for 'if', each line should contain conditional expressions \
- ..followed by a block, or "else" followed by a block:
+ Invalid line for 'if', each line should contain conditional expressions followed by a block, or "else" followed by a block:
%s
+
%action = %line.(length of %line)
if ((%line.1 is "else") and ((length of %line) == 2)):
- unless %else_allowed: compile error at %line.source "Can't have two 'else' blocks"
+ unless %else_allowed:
+ compile error at %line.source "Can't have two 'else' blocks"
+
unless ((length of "\%code") > 0):
- compile error at %line.source "Can't have an 'else' block without a preceeding condition"
- to %code write "\nelse\n \(%action as lua statements)"
+ compile error at %line.source ".."
+ Can't have an 'else' block without a preceeding condition
+
+ to %code write ".."
+
+ else
+ \(%action as lua statements)
+
%else_allowed = (no)
..else:
to %code write "\%clause "
@@ -294,37 +334,56 @@ compile [if %body, when %body] to:
compile error at %line.source ".."
Invalid condition for 'if' statement:
%s
- if (%i > 1): to %code write " or "
+
+ if (%i > 1):
+ to %code write " or "
+
to %code write (%line.%i as lua expr)
- to %code write " then\n \(%action as lua statements)"
+
+ to %code write ".."
+ then
+ \(%action as lua statements)
+
%clause = "\nelseif"
if ((length of "\%code") == 0):
compile error at %body.source "'if' block has an empty body"
+
to %code write "\nend --when"
return %code
+
# Switch statement
compile [if %branch_value is %body, when %branch_value is %body] to:
%code = (Lua "")
%clause = "if"
%else_allowed = (yes)
- unless (%body.type is "Block"): compile error at %body.source "'if' expected a Block, but got: %s"
+ unless (%body.type is "Block"):
+ compile error at %body.source "'if' expected a Block, but got: %s"
+
for %line in %body:
unless (..)
- ((%line.type is "Action") and ((length of %line) >= 2))
- ..and (%line.(length of %line) is "Block" syntax tree)
+ ((%line.type is "Action") and ((length of %line) >= 2)) and (..)
+ %line.(length of %line) is "Block" syntax tree
..:
compile error at %line.source ".."
- Invalid line for 'if % is % %', each line should contain expressions \
- ..followed by a block, or "else" followed by a block:
+ Invalid line for 'if % is % %', each line should contain expressions followed by a block, or "else" followed by a block:
%s
+
%action = %line.(length of %line)
if ((%line.1 is "else") and ((length of %line) == 2)):
- unless %else_allowed: compile error at %line.source "Can't have two 'else' blocks"
+ unless %else_allowed:
+ compile error at %line.source "Can't have two 'else' blocks"
+
unless ((length of "\%code") > 0):
- compile error at %line.source "Can't have an 'else' block without a preceeding condition"
- to %code write "\nelse\n \(%action as lua statements)"
+ compile error at %line.source ".."
+ Can't have an 'else' block without a preceeding condition
+
+ to %code write ".."
+
+ else
+ \(%action as lua statements)
+
%else_allowed = (no)
..else:
to %code write "\%clause "
@@ -333,13 +392,21 @@ compile [if %branch_value is %body, when %branch_value is %body] to:
compile error at %line.source ".."
Invalid condition for 'if' statement:
%s
- if (%i > 1): to %code write " or "
+
+ if (%i > 1):
+ to %code write " or "
+
to %code write "branch_value == \(%line.%i as lua expr)"
- to %code write " then\n \(%action as lua statements)"
+
+ to %code write ".."
+ then
+ \(%action as lua statements)
+
%clause = "\nelseif"
if ((length of "\%code") == 0):
compile error at %body.source "'if % is % %' block has an empty body"
+
to %code write "\nend --when"
return (..)
Lua ".."
@@ -348,9 +415,13 @@ compile [if %branch_value is %body, when %branch_value is %body] to:
\%code
end --if % is
+
# Do/finally
compile [do %action] to (..)
- Lua "do\n \(%action as lua statements)\nend --do"
+ Lua ".."
+ do
+ \(%action as lua statements)
+ end --do
compile [do %action then always %final_action] to (..)
Lua ".."
@@ -385,4 +456,4 @@ compile [for %var in recursive %structure %body] to (..)
\(compile as (===next %var ===))
end
\(compile as (===stop %var ===))
- end
+ end \ No newline at end of file
diff --git a/core/coroutines.nom b/core/coroutines.nom
index bff115d..52467c1 100644
--- a/core/coroutines.nom
+++ b/core/coroutines.nom
@@ -1,10 +1,14 @@
-#!/usr/bin/env nomsu -V2.4.4.3
+#!/usr/bin/env nomsu -V2.5.4.3
#
This file defines the code that creates and manipulates coroutines
use "core/metaprogramming.nom"
+
compile [coroutine %body, generator %body] to (..)
- Lua value "(function()\n \(%body as lua statements)\nend)"
+ Lua value ".."
+ (function()
+ \(%body as lua statements)
+ end)
compile [->] to (Lua value "coroutine.yield(true)")
compile [-> %] to (Lua value "coroutine.yield(true, \(% as lua expr))")
diff --git a/core/errors.nom b/core/errors.nom
index 5dfed1e..2744208 100644
--- a/core/errors.nom
+++ b/core/errors.nom
@@ -1,8 +1,9 @@
-#!/usr/bin/env nomsu -V2.4.4.3
+#!/usr/bin/env nomsu -V2.5.4.3
#
This file contains basic error reporting code
use "core/metaprogramming.nom"
+
compile [traceback] to (Lua value "debug.traceback()")
compile [traceback %] to (Lua value "debug.traceback('', \(% as lua expr))")
compile [barf] to (Lua "error(nil, 0);")
diff --git a/core/io.nom b/core/io.nom
index b06ebb3..0b24ca5 100644
--- a/core/io.nom
+++ b/core/io.nom
@@ -1,8 +1,9 @@
-#!/usr/bin/env nomsu -V2.4.4.3
+#!/usr/bin/env nomsu -V2.5.4.3
#
This file contains basic input/output code
use "core/metaprogramming.nom"
+
compile [say %message] to (..)
lua> ".."
if \%message.type == "Text" then
@@ -16,5 +17,7 @@ compile [ask %prompt] to (..)
if \%prompt.type == "Text" then
return LuaCode.Value(tree.source, "(io.write(", \(%prompt as lua expr), ") and io.read())");
else
- return LuaCode.Value(tree.source, "(io.write(tostring(", \(%prompt as lua expr), ")) and io.read())");
+ return LuaCode.Value(tree.source, "(io.write(tostring(", \(..)
+ %prompt as lua expr
+ .., ")) and io.read())");
end \ No newline at end of file
diff --git a/core/math.nom b/core/math.nom
index e0c58a3..95ed6b9 100644
--- a/core/math.nom
+++ b/core/math.nom
@@ -1,4 +1,4 @@
-#!/usr/bin/env nomsu -V2.4.4.3
+#!/usr/bin/env nomsu -V2.5.4.3
#
This file defines some common math literals and functions
@@ -34,9 +34,7 @@ compile [arc tangent %y / %x, atan2 %y %x] to (..)
Lua value "math.atan2(\(%y as lua expr), \(%x as lua expr))"
compile [hyperbolic sine %, sinh %] to (Lua value "math.sinh(\(% as lua expr))")
-compile [hyperbolic cosine %, cosh %] to (..)
- Lua value "math.cosh(\(% as lua expr))"
-
+compile [hyperbolic cosine %, cosh %] to (Lua value "math.cosh(\(% as lua expr))")
compile [hyperbolic tangent %, tanh %] to (..)
Lua value "math.tanh(\(% as lua expr))"
@@ -87,41 +85,45 @@ action [avg of %items, average of %items] (=lua "(utils.sum(\%items)/#\%items)")
compile [min of %items, smallest of %items, lowest of %items] to (..)
Lua value "utils.min(\(%items as lua expr))"
-compile [max of %items, biggest of %items, largest of %items, highest of %items]
-..to (Lua value "utils.max(\(%items as lua expr))")
+compile [max of %items, biggest of %items, largest of %items, highest of %items] to
+..(Lua value "utils.max(\(%items as lua expr))")
parse [min of %items by %item = %value_expr] as (..)
result of:
- set {%best: nil, %best_key: nil}
+ set {%best:nil, %best_key:nil}
for %item in %items:
%key = %value_expr
if ((%best == (nil)) or (%key < %best_key)):
- set {%best: %item, %best_key: %key}
+ set {%best:%item, %best_key:%key}
return %best
parse [max of %items by %item = %value_expr] as (..)
result of:
- set {%best: nil, %best_key: nil}
+ set {%best:nil, %best_key:nil}
for %item in %items:
%key = %value_expr
if ((%best == (nil)) or (%key > %best_key)):
- set {%best: %item, %best_key: %key}
+ set {%best:%item, %best_key:%key}
return %best
# Random functions
action [seed random with %] (..)
- lua> "math.randomseed(\%);\nfor i=1,20 do math.random(); end"
+ lua> ".."
+ math.randomseed(\%);
+ for i=1,20 do math.random(); end
parse [seed random] as (seed random with (=lua "os.time()"))
compile [random number, random, rand] to (Lua value "math.random()")
compile [random int %n, random integer %n, randint %n] to (..)
Lua value "math.random(\(%n as lua expr))"
-compile [random from %low to %high, random number from %low to %high, rand %low %high]
+compile [..]
+ random from %low to %high, random number from %low to %high
+ rand %low %high
..to (Lua value "math.random(\(%low as lua expr), \(%high as lua expr))")
-action [random choice from %elements, random choice %elements, random %elements]
-..(=lua "\%elements[math.random(#\%elements)]") \ No newline at end of file
+action [random choice from %elements, random choice %elements, random %elements] (..)
+ =lua "\%elements[math.random(#\%elements)]" \ No newline at end of file
diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom
index 9e51d7d..d8d3d77 100644
--- a/core/metaprogramming.nom
+++ b/core/metaprogramming.nom
@@ -1,4 +1,4 @@
-#!/usr/bin/env nomsu -V2.4.4.3
+#!/usr/bin/env nomsu -V2.5.4.3
#
This File contains actions for making actions and compile-time actions and some helper
functions to make that easier.
@@ -32,13 +32,14 @@ lua> ".."
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(a)) end))}
+ local \%args = {"nomsu", "tree", unpack(table.map(\%actions[1]:get_args(), function(a) return tostring(nomsu:compile(\
+ ..a)) end))}
local lua = LuaCode(tree.source, "nomsu.COMPILE_ACTIONS[", repr(\%actions[1].stub),
"] = ", \(compile as (%args -> %body)))
for i=2,#\%actions do
local alias = \%actions[i]
- local \%alias_args = {"nomsu", "tree", unpack(table.map(alias:get_args(), function(a) return tostring(nomsu:compile(a)) \
- ..end))}
+ local \%alias_args = {"nomsu", "tree", unpack(table.map(alias:get_args(), function(a) return tostring(nomsu:compile(\
+ ..a)) end))}
lua:append("\\nnomsu.COMPILE_ACTIONS[", repr(alias.stub), "] = ")
if utils.equivalent(\%args, \%alias_args) then
lua:append("nomsu.COMPILE_ACTIONS[", repr(\%actions[1].stub), "]")
@@ -108,8 +109,7 @@ compile [parse %actions as %body] to (..)
elseif replacements[t[1]] then
return replacements[t[1]]
else
- return t.type.."("..repr(tostring(t.source))..", "..repr(t[1].." \\0").."..string.format('%X', \
- ..__MANGLE_INDEX))"
+ return t.type.."("..repr(tostring(t.source))..", "..repr(t[1].." \\0").."..string.format('%X', __MANGLE_INDEX))"
end
end
local \%new_body = LuaCode(\%body.source,
@@ -191,7 +191,8 @@ compile [%lua <-write %code, to %lua write %code] to (..)
Lua "\(%lua as lua expr):append(\(%code as lua expr));"
compile [to %lua write %code joined by %glue] to (..)
- Lua "\(%lua as lua expr):concat_append(\(%code as lua expr), \(%glue as lua expr));"
+ Lua ".."
+ \(%lua as lua expr):concat_append(\(%code as lua expr), \(%glue as lua expr));
compile [quote %s] to (Lua value "repr(\(%s as lua expr))")
compile [type of %obj] to (Lua value "type(\(%obj as lua expr))")
@@ -200,11 +201,15 @@ compile [parse %text] to (..)
compile [parse %text from %filename] to (..)
Lua value ".."
- nomsu:parse(NomsuCode(Source(\(%filename as lua expr), 1, #\(%text as lua expr)), \(%text as lua expr)))
+ nomsu:parse(NomsuCode(Source(\(%filename as lua expr), 1, #\(%text as lua expr)), \(..)
+ %text as lua expr
+ ..))
compile [run %nomsu_code] to (..)
Lua value ".."
- nomsu:run(\(%nomsu_code as lua expr), \(=lua "repr(tostring(\(%nomsu_code.source)))"))
+ nomsu:run(\(%nomsu_code as lua expr), \(..)
+ =lua "repr(tostring(\(%nomsu_code.source)))"
+ ..)
action [run tree %tree, %tree as value] (lua> "return nomsu:run(\%tree)")
compile [compile %block, compiled %block, %block compiled] to (..)
@@ -240,4 +245,4 @@ compile [with local compile actions %body] to (..)
action [Nomsu version]:
use "lib/version.nom"
return ".."
- \(Nomsu syntax version).\(core version).\(Nomsu compiler version).\(lib version)
+ \(Nomsu syntax version).\(core version).\(Nomsu compiler version).\(lib version) \ No newline at end of file
diff --git a/core/operators.nom b/core/operators.nom
index bce91c4..d0e3915 100644
--- a/core/operators.nom
+++ b/core/operators.nom
@@ -1,4 +1,4 @@
-#!/usr/bin/env nomsu -V2.4.4.3
+#!/usr/bin/env nomsu -V2.5.4.3
#
This file contains definitions of operators like "+" and "and".
@@ -75,9 +75,13 @@ compile [set %assignments] to:
end
end)
local target_lua = \(%target as lua)
- if not target_lua.is_value then error("Invalid target for assignment: "..\(%target as text)) end
+ if not target_lua.is_value then error("Invalid target for assignment: "..\(..)
+ %target as text
+ ..) end
local value_lua = \(%value as lua)
- if not value_lua.is_value then error("Invalid value for assignment: "..\(%value as text)) end
+ if not value_lua.is_value then error("Invalid value for assignment: "..\(..)
+ %value as text
+ ..) end
if \%target.type == "Var" then
lhs:add_free_vars({tostring(target_lua)})
end
@@ -135,7 +139,10 @@ compile [with %assignments %body] to:
\%lua:prepend("local ", lhs, " = ", rhs, ";\\n")
return (..)
- Lua "do\n \%lua\nend -- 'with' block"
+ Lua ".."
+ do
+ \%lua
+ end -- 'with' block
# Math Operators
@@ -199,7 +206,9 @@ compile [%x ARSHIFT %shift, %x >> %shift] to (..)
# Unary operators
compile [- %] to (Lua value "(- \(% as lua expr))")
compile [not %] to (Lua value "(not \(% as lua expr))")
-test: assume ((length of [1, 2, 3]) == 3)
+test:
+ assume ((length of [1, 2, 3]) == 3)
+
compile [length of %list, || %list ||] to (Lua value "(#\(%list as lua expr))")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/core/scopes.nom b/core/scopes.nom
index 30bdea0..37405e5 100644
--- a/core/scopes.nom
+++ b/core/scopes.nom
@@ -1,4 +1,4 @@
-#!/usr/bin/env nomsu -V2.4.4.3
+#!/usr/bin/env nomsu -V2.5.4.3
#
This file contains definitions pertaining to variable scoping
@@ -6,18 +6,29 @@ use "core/metaprogramming.nom"
use "core/operators.nom"
use "core/collections.nom"
use "core/control_flow.nom"
+
compile [with local %locals %body, with local %locals do %body] to:
%body_lua = (%body as lua statements)
if %locals.type is:
"Dict":
%body_lua = (..)
- Lua "\(compile as (<- %locals))\n\%body_lua"
+ Lua ".."
+ \(compile as (<- %locals))
+ \%body_lua
declare locals ("\(%.1 as lua)" for % in %locals) in %body_lua
- "List": declare locals ("\(% as lua)" for % in %locals) in %body_lua
- "Var" "Action": declare locals ["\(%locals as lua)"] in %body_lua
- else: compile error at %locals.source "Unexpected locals: %s"
+ "List":
+ declare locals ("\(% as lua)" for % in %locals) in %body_lua
+
+ "Var" "Action":
+ declare locals ["\(%locals as lua)"] in %body_lua
+
+ else:
+ compile error at %locals.source "Unexpected locals: %s"
return (..)
- Lua "do\n \%body_lua\nend" \ No newline at end of file
+ Lua ".."
+ do
+ \%body_lua
+ end \ No newline at end of file
diff --git a/core/text.nom b/core/text.nom
index dc67ec5..641db14 100644
--- a/core/text.nom
+++ b/core/text.nom
@@ -1,4 +1,4 @@
-#!/usr/bin/env nomsu -V2.4.4.3
+#!/usr/bin/env nomsu -V2.5.4.3
#
This file contains some definitions of text escape sequences, including ANSI console
color codes.
@@ -20,7 +20,8 @@ compile [..]
%text with %sub instead of %patt, %text with %patt replaced by %sub
%text s/ %patt / %sub
..to (..)
- Lua value "((\(%text as lua expr)):gsub(\(%patt as lua expr), \(%sub as lua expr)))"
+ Lua value ".."
+ ((\(%text as lua expr)):gsub(\(%patt as lua expr), \(%sub as lua expr)))
action [lines in %text, lines of %text] (..)
lua> ".."
@@ -49,7 +50,8 @@ compile [%expr for %match in %text matching %patt] to (..)
end)()
compile [%text matches %pattern] to (..)
- Lua value "(\(%text as lua expr):match(\(%pattern as lua expr)) and true or false)"
+ Lua value ".."
+ (\(%text as lua expr):match(\(%pattern as lua expr)) and true or false)
# Text literals
@@ -66,4 +68,4 @@ lua> ".."
return LuaCode.Value(tree.source, lua)
end
end
- end
+ end \ No newline at end of file