diff options
Diffstat (limited to 'lib/core')
| -rw-r--r-- | lib/core/collections.nom | 77 | ||||
| -rw-r--r-- | lib/core/control_flow.nom | 130 | ||||
| -rw-r--r-- | lib/core/coroutines.nom | 20 | ||||
| -rw-r--r-- | lib/core/errors.nom | 21 | ||||
| -rw-r--r-- | lib/core/id.nom | 29 | ||||
| -rw-r--r-- | lib/core/init.nom | 7 | ||||
| -rw-r--r-- | lib/core/io.nom | 11 | ||||
| -rw-r--r-- | lib/core/math.nom | 27 | ||||
| -rw-r--r-- | lib/core/metaprogramming.nom | 84 | ||||
| -rw-r--r-- | lib/core/operators.nom | 48 | ||||
| -rw-r--r-- | lib/core/text.nom | 13 | ||||
| -rw-r--r-- | lib/core/things.nom | 26 | ||||
| -rw-r--r-- | lib/core/time.nom | 17 |
13 files changed, 259 insertions, 251 deletions
diff --git a/lib/core/collections.nom b/lib/core/collections.nom index 02f9302..e9da70c 100644 --- a/lib/core/collections.nom +++ b/lib/core/collections.nom @@ -1,5 +1,6 @@ -#!/usr/bin/env nomsu -V6.15.13.8 -# +#!/usr/bin/env nomsu -V7.0.0 + +### This file contains code that supports manipulating and using collections like lists and dictionaries. @@ -9,11 +10,11 @@ use "core/operators" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# List functionality: +### List functionality: test: $list = [1, 2, 3, 4, 5] $visited = {} - for $i = $x in $list: + for ($i = $x) in $list: $visited.$i = (yes) assume ($visited == {.1, .2, .3, .4, .5}) $visited = {} @@ -24,7 +25,7 @@ test: assume (($list, first) == 1) assume ($list, has 3) assume (($list, index of 3) == 3) - assume ((#$list) == 5) + assume (#$list == 5) $list, add 6 assume (($list, last) == 6) $list, pop @@ -33,11 +34,11 @@ test: assume (($list, first) == 2) assume (([1, 2] + [3, 4]) == [1, 2, 3, 4]) -# Dict functionality +### Dict functionality test: $dict = {.x = 1, .y = 2, .z = 3} - assume (#$dict) == 3 - assume [: for $k = $v in {.x = 1}: add {.key = $k, .value = $v}] == + assume #$dict == 3 + assume [: for ($k = $v) in {.x = 1}: add {.key = $k, .value = $v}] == [{.key = "x", .value = 1}] assume ({.x = 1, .y = 1} + {.y = 10, .z = 10}) == {.x = 1, .y = 11, .z = 10} assume ({.x = 1, .y = 1} - {.y = 10, .z = 10}) == {.x = 1, .y = -9, .z = -10} @@ -45,54 +46,54 @@ test: assume ({.x = 1, .y = 1} & {.y = 10, .z = 10}) == {.y = 1} assume ({.x = 1, .y = 1} ~ {.y = 10, .z = 10}) == {.x = 1, .z = 10} - # Set compliments: + ### Set compliments: assume (~{.x}).y assume ((~{.x}).x == (nil)) - $sc = (~{.x, .y}) + $sc = ~{.x, .y} $sc.y = 99 - # For now, whether $sc.y == 99 or $sc.y == (yes) is unspecified, + ### For now, whether $sc.y == 99 or $sc.y == (yes) is unspecified, (but the actual behavior is (yes)) assume ($sc.y and (not $sc.x)) - assume ($sc == ((~{.x, .y}) | {.y = 99})) + assume ($sc == (~{.x, .y} | {.y = 99})) - # Both sets: + ### Both sets: assume (({.x, .y} & {.y, .z}) == {.y}) assume (({.x, .y} | {.y, .z}) == {.x, .y, .z}) assume (({.x, .y} ~ {.y, .z}) == {.z, .x}) - # Mixed: - assume (({.x, .y} & (~{.y, .z})) == {.x}) - assume (({.x, .y} | (~{.y, .z})) == (~{.z})) - assume (({.x, .y} ~ (~{.y, .z})) == (~{.x})) + ### Mixed: + assume (({.x, .y} & ~{.y, .z}) == {.x}) + assume (({.x, .y} | ~{.y, .z}) == ~{.z}) + assume (({.x, .y} ~ ~{.y, .z}) == ~{.x}) - # Mixed reversed: - assume (((~{.y, .z}) & {.x, .y}) == {.x}) - assume (((~{.y, .z}) | {.x, .y}) == (~{.z})) - assume (((~{.y, .z}) ~ {.x, .y}) == (~{.x})) + ### Mixed reversed: + assume ((~{.y, .z} & {.x, .y}) == {.x}) + assume ((~{.y, .z} | {.x, .y}) == ~{.z}) + assume ((~{.y, .z} ~ {.x, .y}) == ~{.x}) - # Both set compliments: - assume (((~{.x, .y}) & (~{.y, .z})) == (~{.x, .y, .z})) - assume (((~{.x, .y}) | (~{.y, .z})) == (~{.y})) - assume (((~{.x, .y}) ~ (~{.y, .z})) == {.x, .z}) + ### Both set compliments: + assume ((~{.x, .y} & ~{.y, .z}) == ~{.x, .y, .z}) + assume ((~{.x, .y} | ~{.y, .z}) == ~{.y}) + assume ((~{.x, .y} ~ ~{.y, .z}) == {.x, .z}) test: assume ((entries in {.x = 1}) == [{.key = "x", .value = 1}]) -(entries in $dict) parses as [ - : for $k = $v in $dict: - add {.key = $k, .value = $v} -] +(entries in $dict) parses as + [: for ($k = $v) in $dict: add {.key = $k, .value = $v}] test: assume ((keys in {.x = 1}) == ["x"]) -[keys in $dict, keys of $dict] all parse as [: for $k = $v in $dict: add $k] +[keys in $dict, keys of $dict] all parse as [: for ($k = $v) in $dict: add $k] test: assume ((values in {.x = 1}) == [1]) -[values in $dict, values of $dict] all parse as [: for $k = $v in $dict: add $v] -# Metatable stuff +[values in $dict, values of $dict] all parse as + [: for ($k = $v) in $dict: add $v] + +### Metatable stuff test: $t = {} set $t's metatable to {.__tostring = ($ -> "XXX")} @@ -118,12 +119,12 @@ test: end)(\($dict as lua expr)) ") -# Sorting +### Sorting test: $x = [3, 1, 2] sort $x assume ($x == [1, 2, 3]) - sort $x by $ = (- $) + sort $x by $ = -$ assume ($x == [3, 2, 1]) $keys = {.1 = 999, .2 = 0, .3 = 50} sort $x by $ = $keys.$ @@ -164,13 +165,13 @@ external: $seen.$ = (yes) return $unique -# Ranges: +### Ranges: test: $r = (3 to 5) assume ($r.2 == 4) assume ($r is "a Range") assume ((1 to 10, backwards) == (10 to 1 by -1)) - assume ((#(1 to 10 by 2)) == 5) + assume (#(1 to 10 by 2) == 5) $visited = [] for $ in (1 to 10 by 2): $visited, add $ @@ -211,7 +212,7 @@ $range_mt = { ($self.first == $other.first) and ($self.last == $other.last) and ($self.step == $other.step) - .backwards = (for $self ($self.last to $self.first by (- $self.step))) + .backwards = (for $self ($self.last to $self.first by -$self.step)) .__inext = $(inext), .__next = $(inext) .as_text = for $self: @@ -231,4 +232,4 @@ external: setmetatable {.first = $first, .last = $last, .step = $step} $range_mt ($first to $last) means - setmetatable {.first = $first, .last = $last, .step = 1} $range_mt + setmetatable {.first = $first, .last = $last, .step = 1} $range_mt
\ No newline at end of file diff --git a/lib/core/control_flow.nom b/lib/core/control_flow.nom index fafc45d..481507f 100644 --- a/lib/core/control_flow.nom +++ b/lib/core/control_flow.nom @@ -1,5 +1,6 @@ -#!/usr/bin/env nomsu -V6.15.13.8 -# +#!/usr/bin/env nomsu -V7.0.0 + +### This file contains compile-time actions that define basic control flow structures like "if" statements and loops. @@ -8,12 +9,12 @@ use "core/operators" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# No-Op +### No-Op test: do nothing (do nothing) compiles to "" -# Conditionals +### Conditionals test: if (no): fail "conditional fail" @@ -48,8 +49,8 @@ test: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# Conditional expression (ternary operator) -# Note: this uses a function instead of "(condition and if_expr or else_expr)" +### Conditional expression (ternary operator) +### Note: this uses a function instead of "(condition and if_expr or else_expr)" because that breaks if $if_expr is falsey, e.g. "x < 5 and false or 99" test: assume ((1 if (yes) else 2) == 1) @@ -61,7 +62,7 @@ test: $when_false_expr unless $condition else $when_true_expr $when_false_expr unless $condition then $when_true_expr ] all compile to: - # If $when_true_expr is guaranteed to be truthy, we can use Lua's idiomatic + ### 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, .List, .Dict, .Number}.($when_true_expr.type): return Lua (" @@ -69,7 +70,7 @@ test: ..\($when_false_expr as lua expr)) ") ..else: - # Otherwise, need to do an anonymous inline function (yuck, too bad lua + ### Otherwise, need to do an anonymous inline function (yuck, too bad lua doesn't have a proper ternary operator!) To see why this is necessary consider: (random()<.5 and false or 99) return Lua (" @@ -84,7 +85,7 @@ test: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# GOTOs +### GOTOs test: $i = 0 --- $loop --- @@ -112,7 +113,7 @@ test: ) ") -# Basic loop control +### Basic loop control (stop $var) compiles to: if $var: return Lua "goto stop_\($var as lua identifier)" @@ -128,7 +129,7 @@ test: (---stop $var ---) compiles to "::stop_\($var as lua identifier)::" (---next $var ---) compiles to "::continue_\($var as lua identifier)::" -# While loops +### While loops test: $x = 0 repeat while ($x < 10): $x += 1 @@ -149,7 +150,7 @@ test: \($body as lua) ") - if ($body has subtree \(do next)): + if ($body, contains `(do next)): $lua, add "\n ::continue::" $lua, add "\nend --while-loop" @@ -160,12 +161,12 @@ test: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# For-each loop (lua's "ipairs()") +### For-each loop (lua's "ipairs()") (for $var in $iterable $body) compiles to: unless $var: at (this tree) fail "No var here" - # This uses Lua's approach of only allowing loop-scoped variables in a loop + ### This uses Lua's approach of only allowing loop-scoped variables in a loop if (($var.type == "Action") and ($var.stub == "1 =")): [$key, $value] = [$var.1, $var.3] ..else: @@ -174,7 +175,7 @@ test: unless $value: at (this tree) fail "No value here" - # Numeric loop: + ### Numeric loop: if (($iterable.type == "Action") and (($iterable, get stub) == "1 to")): [$start, $stop] = [$iterable.1, $iterable.3] $loop = @@ -191,7 +192,7 @@ test: go to (loop set) - # Numeric loop with step: + ### Numeric loop with step: if (($iterable.type == "Action") and (($iterable, get stub) == "1 to 2 by")): [$start, $stop, $step] = [$iterable.1, $iterable.3, $iterable.5] $loop = @@ -208,21 +209,19 @@ test: go to (loop set) - # for $ in (...): + ### for $ in (...): if $key: $loop = 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 ") ..else: $loop = - Lua (" - for _i,\($value as lua identifier) in _ipairs(\($iterable as lua expr)) do - ") - + Lua "for _i,\($value as lua identifier) in _ipairs(\($iterable as lua expr)) do" --- (loop set) --- - # TODO: don't always wrap in block + ### TODO: don't always wrap in block $lua = Lua (" do -- for-loop @@ -230,37 +229,31 @@ test: \; ") $lua, add ($body as lua) - if ($body has subtree \(do next)): + if ($body, contains `(do next)): $lua, add "\n ::continue::" - if ($key and ($body has subtree \(do next $key))): - $lua, add "\n " (\(---next $key ---) as lua) + if ($key and ($body, contains ("Action" tree with "do" "next" $key))): + $lua, add "\n " (("Action" tree with "---" "next" $key "---") as lua) - if ($body has subtree \(do next $value)): - $lua, add "\n " (\(---next $value ---) as lua) + if ($body, contains ("Action" tree with "do" "next" $value)): + $lua, add "\n " (("Action" tree with "---" "next" $value "---") as lua) $lua, add "\n end" - if ($key and ($body has subtree \(stop $key))): - $lua, add "\n " (\(---stop $key ---) as lua) + if ($key and ($body, contains ("Action" tree with "stop" $key))): + $lua, add "\n " (("Action" tree with "---" "stop" $key "---") as lua) - if ($body has subtree \(stop $value)): - $lua, add "\n " (\(---stop $value ---) as lua) + if ($body, contains ("Action" tree with "stop" $value)): + $lua, add "\n " (("Action" tree with "---" "stop" $value "---") as lua) $lua, add "\nend -- for-loop" - $lua, remove free vars [($value as lua identifier, text), $key and ($key as lua identifier, text)] + $lua, remove free vars + [($value as lua identifier, text), $key and ($key as lua identifier, text)] return $lua -# TODO: remove these shims: -(for $var in $iterable at $i $body) parses as - for ($i = $var) in $iterable $body - -(for $k = $v in $iterable $body) parses as - for ($k = $v) in $iterable $body - test: $d = {.a = 10, .b = 20, .c = 30, .d = 40, .e = 50} $result = [] - for $k = $v in $d: + for ($k = $v) in $d: if ($k == "a"): do next $k @@ -270,7 +263,7 @@ test: $result, add "\$k = \$v" assume (($result sorted) == ["c = 30", "d = 40", "e = 50"]) -# Numeric range for loops +### Numeric range for loops test: assume ([: for $ in (1 to 5): add $] == [1, 2, 3, 4, 5]) assume ([: for $ in (1 to 5 by 2): add $] == [1, 3, 5]) @@ -286,16 +279,7 @@ test: stop $outer assume ($nums == [1, -2, 3, -2, 3, 4, 3, 4, 5]) -# TODO: These are shims, and should be phased out: -[ - for $var in $start to $stop by $step $body - for $var in $start to $stop via $step $body -] all parse as (for $var in ($start to $stop by $step) $body) - -(for $var in $start to $stop $body) parses as - for $var in ($start to $stop) $body - -# repeat $n times is a shorthand: +### repeat $n times is a shorthand: test: $x = 0 repeat 5 times: @@ -319,7 +303,7 @@ test: else: fail "bad conditional" -# Multi-branch conditional (if..elseif..else) +### Multi-branch conditional (if..elseif..else) (when $body) compiles to: $code = (Lua "") $clause = "if" @@ -332,7 +316,7 @@ test: for $line in $body: unless - (($line.type == "Action") and ((#$line) >= 2)) and + (($line.type == "Action") and (#$line >= 2)) and $line.(#$line) is "Block" syntax tree ..: at $line fail (" @@ -341,14 +325,14 @@ test: ..or "else" followed by a block. ") $action = $line.(#$line) - if (($line.1 == "else") and ((#$line) == 2)): + if (($line.1 == "else") and (#$line == 2)): unless $else_allowed: at $line fail (" Compile error: You can't have two 'else' blocks. Hint: Merge all of the 'else' blocks together. ") - unless ((#"\$code") > 0): + unless (#"\$code" > 0): at $line fail (" Compile error: You can't have an 'else' block without a preceding condition. Hint: If you want the code in this block to always execute, you don't need a conditional \ @@ -359,14 +343,14 @@ test: $else_allowed = (no) ..else: $code, add $clause " " - for $i in 1 to ((#$line) - 1): + for $i in (1 to (#$line - 1)): if ($i > 1): $code, add " or " $code, add ($line.$i as lua expr) $code, add " then\n " ($action as lua) $clause = "\nelseif" - if ((#"\$code") == 0): + if (#"\$code" == 0): at $body fail (" Compile error: 'if' block has an empty body. Hint: This means nothing would happen, so the 'if' block should be deleted. @@ -389,7 +373,7 @@ test: else: fail "bad switch statement" -# Switch statement +### Switch statement [if $branch_value is $body, when $branch_value is $body] all compile to: $code = (Lua "") $clause = "if" @@ -403,7 +387,7 @@ test: for $line in $body: unless - (($line.type == "Action") and ((#$line) >= 2)) and + (($line.type == "Action") and (#$line >= 2)) and $line.(#$line) is "Block" syntax tree ..: at $line fail (" @@ -411,14 +395,14 @@ test: Hint: Each line should contain expressions followed by a block, or "else" followed by a block. ") $action = $line.(#$line) - if (($line.1 == "else") and ((#$line) == 2)): + if (($line.1 == "else") and (#$line == 2)): unless $else_allowed: at $line fail (" Compile error: You can't have two 'else' blocks. Hint: Merge all of the 'else' blocks together. ") - unless ((#"\$code") > 0): + unless (#"\$code" > 0): at $line fail (" Compile error: You can't have an 'else' block without a preceding condition. Hint: If you want the code in this block to always execute, you don't need a conditional \ @@ -429,14 +413,14 @@ test: $else_allowed = (no) ..else: $code, add $clause " " - for $i in 1 to ((#$line) - 1): + for $i in (1 to (#$line - 1)): if ($i > 1): $code, add " or " $code, add "\(mangle "branch value") == " ($line.$i as lua expr) $code, add " then\n " ($action as lua) $clause = "\nelseif" - if ((#"\$code") == 0): + if (#"\$code" == 0): at $body fail (" Compile error: 'if' block has an empty body. Hint: This means nothing would happen, so the 'if' block should be deleted. @@ -450,7 +434,7 @@ test: end -- if $ is... ") -# Do/finally +### Do/finally (do $action) compiles to (" do \($action as lua) @@ -460,8 +444,8 @@ test: test: assume ((result of: return 99) == 99) -# Inline thunk: -(result of $body) compiles to "\(\(-> $body) as lua)()" +### Inline thunk: +(result of $body) compiles to "\(("Action" tree with "->" $body) as lua)()" test: $t = [1, [2, [[3], 4], 5, [[[6]]]]] $flat = [] @@ -473,7 +457,7 @@ test: $flat, add $ assume (sorted $flat) == [1, 2, 3, 4, 5, 6] -# Recurion control flow +### Recurion control flow (recurse $v on $x) compiles to Lua "table.insert(_stack_\($v as lua expr), \($x as lua expr))" @@ -487,14 +471,14 @@ test: \($body as lua) ") - if ($body has subtree \(do next)): + if ($body, contains `(do next)): $lua, add "\n ::continue::" - if ($body has subtree \(do next $var)): - $lua, add "\n \(\(---next $var ---) as lua)" + if ($body, contains ("Action" tree with "do" "next" $var)): + $lua, add "\n \(("Action" tree with "---" "next" $var "---") as lua)" $lua, add "\n end -- Recursive loop" - if ($body has subtree \(stop $var)): - $lua, add "\n \(\(---stop $var ---) as lua)" + if ($body, contains ("Action" tree with "stop" $var)): + $lua, add "\n \(("Action" tree with "---" "stop" $var "---") as lua)" $lua, add "\nend -- Recursive scope" return $lua diff --git a/lib/core/coroutines.nom b/lib/core/coroutines.nom index b151a4b..a11d6ee 100644 --- a/lib/core/coroutines.nom +++ b/lib/core/coroutines.nom @@ -1,5 +1,6 @@ -#!/usr/bin/env nomsu -V6.15.13.8 -# +#!/usr/bin/env nomsu -V7.0.0 + +### This file defines the code that creates and manipulates coroutines use "core/metaprogramming" @@ -16,11 +17,8 @@ test: repeat 3 times: yield 6 $nums = [] - for $ in (coroutine from $fn): - $nums, add $ - + for $ in (coroutine from $fn): $nums, add $ assume ($nums == [4, 5, 6, 6, 6]) - $d = {.x = 0} $co = coroutine: @@ -35,7 +33,6 @@ test: [$ok, $val] = (co) assume ($ok == (yes)) assume ($val == 5) - $t = [] $i = 1 for $ in @@ -47,8 +44,7 @@ test: ..: $t.$i = $ $i += 1 - assume ($t == [4,5,nil,6]) - + assume ($t == [4, 5, nil, 6]) $t = [] for ($k = $) in coroutine: @@ -58,11 +54,11 @@ test: yield 6 ..: $t, add {.key = $k, .value = $} + assume $t == [ {.key = 1, .value = 4}, {.key = 2, .value = 5}, {.key = 3}, {.key = 4, .value = 6} ] - -(coroutine $body) parses as (coroutine from (-> $body)) +(coroutine $body) parses as (coroutine from ->$body) external: ($ is a dead coroutine) means - ((lua type of $) == "thread") and ((coroutine status of $) == "dead") + ((lua type of $) == "thread") and ((coroutine status of $) == "dead")
\ No newline at end of file diff --git a/lib/core/errors.nom b/lib/core/errors.nom index a05c7d4..20751fa 100644 --- a/lib/core/errors.nom +++ b/lib/core/errors.nom @@ -1,5 +1,6 @@ -#!/usr/bin/env nomsu -V6.15.13.8 -# +#!/usr/bin/env nomsu -V7.0.0 + +### This file contains basic error reporting code use "core/metaprogramming" @@ -63,8 +64,7 @@ use "core/control_flow" if not _1_is(_a, _b) then _a = type_of(_a) == 'Text' and _a:as_lua() or _1_as_text(_a) at_1_fail(\(quote "\($condition.1.source)"), - "Assumption failed: This value (".._a..") was expected to be "..\ - .._b..", but wasn't.") + "Assumption failed: This value (".._a..") was expected to be ".._b..", but wasn't.") end end ") @@ -76,8 +76,7 @@ use "core/control_flow" if _1_is(_a, _b) then _a = type_of(_a) == 'Text' and _a:as_lua() or _1_as_text(_a) at_1_fail(\(quote "\($condition.1.source)"), - "Assumption failed: This value (".._a..") was expected to not be \ - ..".._b..", but it was.") + "Assumption failed: This value (".._a..") was expected to not be ".._b..", but it was.") end end ") @@ -104,21 +103,21 @@ test: unless $worked: fail "'try' failed to recover from failure" -# Try/except +### Try/except [ try $action if it succeeds $success if it fails with $msg $fallback try $action if it fails with $msg $fallback if it succeeds $success ] all compile to: $success_lua = ($success as lua) - if ((#"\$success_lua") > 0): + if (#"\$success_lua" > 0): $success_lua, add "\n" $success_lua, prepend "-- Success:\n" $success_lua, add "if not _fell_through then return table.unpack(_result, 2) end" $fallback_lua = ($fallback as lua) - if ((#"\$fallback_lua") > 0): + if (#"\$fallback_lua" > 0): $msg_lua = ($msg as lua expr) - if ((#"\$msg_lua") > 0): + if (#"\$msg_lua" > 0): $fallback_lua, prepend "\n\$msg_lua = _result[2]\n" if ($msg_lua, text, is lua id): $fallback_lua, add free vars [($msg_lua, text)] @@ -181,4 +180,4 @@ test: if not _results[1] then error(_results[2], 0) end if not _fell_through then return table.unpack(_results, 2) end end -") +")
\ No newline at end of file diff --git a/lib/core/id.nom b/lib/core/id.nom index 936fd40..7324eea 100644 --- a/lib/core/id.nom +++ b/lib/core/id.nom @@ -1,5 +1,6 @@ -#!/usr/bin/env nomsu -V6.15.13.8 -# +#!/usr/bin/env nomsu -V7.0.0 + +### A simple UUID function based on RFC 4122: http://www.ietf.org/rfc/rfc4122.txt use "core/metaprogramming" @@ -35,27 +36,29 @@ set $id_by_obj's metatable to { external: (uuid) means: - # Set all the other bits to randomly (or pseudo-randomly) chosen values. + ### Set all the other bits to randomly (or pseudo-randomly) chosen values. $bytes = [ - # time-low, time-mid, time-high-and-version + ### time-low, time-mid, time-high-and-version randint (2 ^ (4 * 8)), randint (2 ^ (2 * 8)), randint (2 ^ (2 * 8 - 4)) - # clock-seq-and-reserved, clock-seq-low + + ### clock-seq-and-reserved, clock-seq-low randint (2 ^ (1 * 8 - 2)), randint (2 ^ (1 * 8)), randint (2 ^ (3 * 8)) - # node + + ### node randint (2 ^ (3 * 8)) ] - # Set the four most significant bits (bits 12 through 15) of the - # time_hi_and_version field to the 4-bit version number from - # Section 4.1.3. + ### Set the four most significant bits (bits 12 through 15) of the + ### time_hi_and_version field to the 4-bit version number from + ### Section 4.1.3. $bytes.3 += 0x4000 - # Set the two most significant bits (bits 6 and 7) of the - # clock_seq_hi_and_reserved to zero and one, respectively. + ### Set the two most significant bits (bits 6 and 7) of the + ### clock_seq_hi_and_reserved to zero and one, respectively. $bytes.4 += 0xC0 return (=lua "('%08x-%04x-%04x-%02x%02x-%6x%6x'):format(unpack(\$bytes))") - # For strict identity checking, use ($x's id) == ($y's id) + ### For strict identity checking, use ($x's id) == ($y's id) test: assume (([] == []) and ((id of []) != (id of []))) seed random with 0 @@ -64,4 +67,4 @@ external: seed random with 0 assume ((id of $x) != (id of [])) seed random - [id of $, $'s id, $'id] all mean $id_by_obj.$ + [id of $, $'s id, $'id] all mean $id_by_obj.$
\ No newline at end of file diff --git a/lib/core/init.nom b/lib/core/init.nom index 41362c7..84fd3cd 100644 --- a/lib/core/init.nom +++ b/lib/core/init.nom @@ -1,5 +1,6 @@ -#!/usr/bin/env nomsu -V6.15.13.8 -# Export everything +#!/usr/bin/env nomsu -V7.0.0 + +### Export everything export "core/metaprogramming" export "core/operators" export "core/control_flow" @@ -11,4 +12,4 @@ export "core/id" export "core/io" export "core/text" export "core/things" -export "core/time" +export "core/time"
\ No newline at end of file diff --git a/lib/core/io.nom b/lib/core/io.nom index df52c92..c4f74d9 100644 --- a/lib/core/io.nom +++ b/lib/core/io.nom @@ -1,5 +1,6 @@ -#!/usr/bin/env nomsu -V6.15.13.8 -# +#!/usr/bin/env nomsu -V7.0.0 + +### This file contains basic input/output code use "core/metaprogramming" @@ -10,14 +11,12 @@ use "core/control_flow" external: (say (*extra arguments*)) means: - for $ in 1 to (select "#" (*extra arguments*)): + for $ in (1 to (select "#" (*extra arguments*))): $arg = (select $ (*extra arguments*)) $io.write ($arg as text) $io.write "\n" $io.flush() - (say $message inline) means ($io.write $message) - (ask $prompt) means: $io.write $prompt - return ($io.read()) + return ($io.read())
\ No newline at end of file diff --git a/lib/core/math.nom b/lib/core/math.nom index 3aab2b5..9b53385 100644 --- a/lib/core/math.nom +++ b/lib/core/math.nom @@ -1,5 +1,6 @@ -#!/usr/bin/env nomsu -V6.15.13.8 -# +#!/usr/bin/env nomsu -V7.0.0 + +### This file defines some common math literals and functions use "core/metaprogramming" @@ -11,7 +12,7 @@ use "core/collections" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ external: - # Literals: + ### Literals: test: unless (all of [inf, NaN, pi, tau, golden ratio, e]): fail "math constants failed" @@ -25,7 +26,7 @@ external: (golden ratio) compiles to "((1+math.sqrt(5))/2)" (e) compiles to "math.exp(1)" - # Functions: + ### Functions: test: assume (("5" as a number) == 5) $($ as a number) = $(tonumber $) @@ -33,7 +34,7 @@ external: test: unless all of [ - abs 5, | 5 |, sqrt 5, √ 5, sine 5, cosine 5, tangent 5, arc sine 5, arc cosine 5 + abs 5, | 5 |, sqrt 5, √5, sine 5, cosine 5, tangent 5, arc sine 5, arc cosine 5 arc tangent 5, arc tangent 5 / 10, hyperbolic sine 5, hyperbolic cosine 5 hyperbolic tangent 5, e^ 5, ln 5, log 5 base 2, floor 5, ceiling 5, round 5 ] @@ -43,7 +44,7 @@ external: [$(absolute value $), $(absolute value of $), $(| $ |), $(abs $)] = [$math.abs, $math.abs, $math.abs, $math.abs] - [$(square root $), $(square root of $), $(√ $), $(sqrt $)] = + [$(square root $), $(square root of $), $(√$), $(sqrt $)] = [$math.sqrt, $math.sqrt, $math.sqrt, $math.sqrt] [$(sine $), $(sin $)] = [$math.sin, $math.sin] @@ -67,7 +68,7 @@ external: unless ((2.6 to the nearest 0.25) == 2.5): fail "rounding failed" ($n to the nearest $rounder) means ($rounder * (floor ($n / $rounder + 0.5))) - # Any/all + ### Any/all [all of $items, all $items] all mean: for $ in $items: unless $: @@ -81,7 +82,7 @@ external: return (no) [none of $items, none $items] all parse as (not (any of $items)) - # Sum/product + ### Sum/product [sum of $items, sum $items] all mean: $total = 0 for $ in $items: @@ -94,9 +95,9 @@ external: $prod *= $ return $prod - [avg of $items, average of $items] all mean ((sum of $items) / (#$items)) + [avg of $items, average of $items] all mean ((sum of $items) / #$items) - # Min/max + ### Min/max [min of $items, smallest of $items, lowest of $items] all mean: $best = (nil) for $ in $items: @@ -186,13 +187,13 @@ external: ($nums mixed by $amount) means: $ = ($amount clamped between 0 and 1) - $i = (1 + ($ * ((#$nums) - 1))) - if ((floor $i) == (#$nums)): + $i = (1 + ($ * (#$nums - 1))) + if ((floor $i) == #$nums): return $nums.(floor $i) [$lo, $hi] = [$nums.(floor $i), $nums.(floor ($i + 1))] return ($lo to $hi mixed by ($i mod 1)) - # Random functions + ### Random functions (seed random with $) means: lua> (" math.randomseed(\$); diff --git a/lib/core/metaprogramming.nom b/lib/core/metaprogramming.nom index 8fe09ca..7bf04f2 100644 --- a/lib/core/metaprogramming.nom +++ b/lib/core/metaprogramming.nom @@ -1,5 +1,6 @@ -#!/usr/bin/env nomsu -V6.15.13.8 -# +#!/usr/bin/env nomsu -V7.0.0 + +### This File contains actions for making actions and compile-time actions and some helper functions to make that easier. @@ -55,6 +56,28 @@ lua> (" end COMPILE_RULES["->"] = COMPILE_RULES["1 ->"] COMPILE_RULES["for"] = COMPILE_RULES["1 ->"] + + COMPILE_RULES["`"] = function(\(nomsu environment), _tree, escaped) + local function escape(t) + if t.type == "Action" and t:get_stub() == "`" and #t == 2 then + return \(nomsu environment):compile(t[2]) + else + local bits = {} + table.insert(bits, "type="..t.type:as_lua()) + if t.source then + table.insert(bits, "source="..t.source:as_lua()) + end + for i,b in ipairs(t) do + table.insert(bits, lua_type_of(b) == 'string' and b:as_lua() or escape(b)) + end + local lua = LuaCode:from(t.source, "SyntaxTree{") + lua:concat_add(bits, ", ") + lua:add("}") + return lua + end + end + return escape(escaped) + end ") ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -95,7 +118,8 @@ lua> (" (\$action.type == "EscapedNomsu" and \$action[1].type == "Action") or \$action.type == "MethodCall") then at_1_fail(\$action.source, "Compile error: ".. - "This first argument to (* compiles to *) is neither an action nor an escaped action (it's a "..\$action.type.."). ".. + "This first argument to (* compiles to *) is neither an action nor an escaped \ + ..action (it's a "..\$action.type.."). ".. "Hint: This should probably be an action like:\\n" .."(foo $x) compiles to \\"(\\\\($x as lua) + 1)\\"") end @@ -104,25 +128,23 @@ lua> (" if a.type == "EscapedNomsu" then \$args:add(a[1]) end end return LuaCode("COMPILE_RULES[", \($action as lua), ":get_stub()] = ", - \(\($args -> $body) as lua)) + \(`(`$args -> `$body) as lua)) else for _,a in ipairs(\$action:get_args()) do \$args:add(a) end return LuaCode("COMPILE_RULES[", \$action:get_stub():as_lua(), - "] = ", \(\($args -> $body) as lua)) + "] = ", \(`(`$args -> `$body) as lua)) end end ") ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -(` $) compiles to (Lua (=lua "\$ or SyntaxTree{type='Action'}", as lua)) - ($actions all compile to $body) compiles to: lua> (" if \$actions.type ~= "List" then at_1_fail(\$actions, "Compile error: This should be a list of actions.") end - local lua = \(\($actions.1 compiles to $body) as lua) + local lua = \(`(`$actions.1 compiles to `$body) as lua) local \$args = a_List{"\(nomsu environment)", "\(this tree)", unpack(\$actions[1]:get_args())} local \$compiled_args = a_List{"\(nomsu environment)", "\(this tree)"}; for i=3,#\$args do \$compiled_args[i] = \(nomsu environment):compile(\$args[i]) end @@ -165,7 +187,6 @@ test: ($action means $body) compiles to: lua> (" - local lua = LuaCode() if \$action.type == "MethodCall" then lua:add(\(nomsu environment):compile(\$action[1]), ".", \$action[2]:get_stub():as_lua_id()) @@ -175,15 +196,16 @@ test: else at_1_fail(\$action, "Compile error: This is not an action or method call.") end - lua:add(" = ", \(\($action -> $body) as lua), ";") + lua:add(" = ", \(`(`$action -> `$body) as lua), ";") return lua ") ($actions all mean $body) compiles to: lua> (" - local lua = \(\($actions.1 means $body) as lua) + local lua = \(`(`$actions.1 means `$body) as lua) local first_def = (\$actions[1].type == "MethodCall" - and LuaCode(\(nomsu environment):compile(\$actions[1][1]), ".", \$actions[1][2]:get_stub():as_lua_id()) + and LuaCode(\(nomsu environment):compile(\$actions[1][1]), ".", \$actions[1][2]:get_\ + ..stub():as_lua_id()) or LuaCode(\$actions[1]:get_stub():as_lua_id())) local \$args = a_List(\$actions[1]:get_args()) for i=2,#\$actions do @@ -199,7 +221,7 @@ test: if \$args == \$alias_args then lua:add(" = ", first_def, ";") else - lua:add(" = ", \(\($alias_args -> $actions.1) as lua), ";") + lua:add(" = ", \(`(`$alias_args -> `$actions.1) as lua), ";") end end return lua @@ -301,15 +323,13 @@ test: local \$new_body = LuaCode:from(\$body.source, "local mangle = mangler()", "\\nreturn ", make_tree(\$body)) - local ret = \(\($actions all compile to $new_body) as lua) - return ret + return \(`(`$actions all compile to `$new_body) as lua) ") ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [$action parses as $body] all parse as ([$action] all parse as $body) - -((nomsu environment), $tree as lua expr) means: +(nomsu environment, $tree as lua expr) means: lua> (" local tree_lua = \(nomsu environment):compile(\$tree) if \$tree.type == 'Block' and #\$tree > 1 then @@ -318,9 +338,8 @@ test: return tree_lua ") -# Need to make sure the proper environment is used for compilation (i.e. the caller's environment) -($tree as lua expr) compiles to - =lua "SyntaxTree{type='MethodCall', \(\(nomsu environment)), \(\($tree as lua expr))}" +### Need to make sure the proper environment is used for compilation (i.e. the caller's environment) +($tree as lua expr) compiles to `((nomsu environment), `$tree as lua expr) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -337,11 +356,11 @@ external: ") test: - (num args (*extra arguments*)) means (#(*extra arguments*)) + (num args (*extra arguments*)) means #(*extra arguments*) assume (num args 1 2 3) == 3 (extra args (*extra arguments*)) means [*extra arguments*] assume (extra args 1 2 3) == [1, 2, 3] - (third arg (*extra arguments*)) means ((*extra arguments*).3) + (third arg (*extra arguments*)) means (*extra arguments*).3 assume (third arg 5 6 7 8) == 7 (*extra arguments*) compiles to "..." @@ -405,7 +424,7 @@ external: local lua_type = \(lua type of $) return 'a '..lua_type:capitalized() ") - + ($ is $type) means: lua> (" local class = getmetatable(\$) @@ -425,13 +444,15 @@ external: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ test: - assume ("Action" tree with "foo" ("Var" tree with "x")) == \(foo \$x) + assume ("Action" tree with "foo" ("Var" tree with "x")) == `(foo $x) external: ($type tree with (*extra arguments*)) means SyntaxTree (=lua "{type=\$type, ...}") + ($type tree from $source) means SyntaxTree (=lua "{type=\$type, source=\$source}") + ($type tree from $source with (*extra arguments*)) means SyntaxTree (=lua "{type=\$type, source=\$source, ...}") @@ -440,7 +461,7 @@ test: return 100 200 300 assume (select 2 (foo)) == 200 -# Return statement is wrapped in a do..end block because Lua is unhappy if you +### Return statement is wrapped in a do..end block because Lua is unhappy if you put code after a return statement, unless you wrap it in a block. (return (*extra arguments*)) compiles to: lua> (" @@ -453,10 +474,10 @@ test: return lua ") -# Convenience helper: -(return Lua (*extra arguments*)) compiles to \(return \(Lua (*extra arguments*))) +### Convenience helper: +(return Lua (*extra arguments*)) compiles to `(return (Lua `(*extra arguments*))) -# Literals +### Literals (yes) compiles to "(true)" (no) compiles to "(false)" [nothing, nil, null] all compile to "(nil)" @@ -466,7 +487,7 @@ test: (at compilation $expr) compiles to: lua> (" - local value = \(nomsu environment):run(\(\(return $expr))) + local value = \(nomsu environment):run(\(`(return `$expr))) if lua_type_of(value) == 'table' or lua_type_of(value) == 'string' and value.as_lua then return LuaCode(value:as_lua()) else @@ -490,8 +511,9 @@ test: return lua ") -~~~~ -# TODO: Remove shim +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +### TODO: Remove shim ($tree with $t -> $replacement) parses as $tree, with ($t -> $replacement) diff --git a/lib/core/operators.nom b/lib/core/operators.nom index 6f1fd78..187d2c7 100644 --- a/lib/core/operators.nom +++ b/lib/core/operators.nom @@ -1,5 +1,6 @@ -#!/usr/bin/env nomsu -V6.15.13.8 -# +#!/usr/bin/env nomsu -V7.0.0 + +### This file contains definitions of operators like "+" and "and". use "core/metaprogramming" @@ -9,7 +10,7 @@ use "core/metaprogramming" test: assume (all [1 < 2, 2 > 1, 1 <= 2, 2 >= 1, 1 == 1, 1 != 2]) -# Comparison Operators +### Comparison Operators ($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))" @@ -33,7 +34,7 @@ test: unless (($x == 4) and ($y == 5)): fail "unpacking failed" -# Variable assignment operator +### Variable assignment operator ($var = $value) compiles to: lua> (" local lua = LuaCode() @@ -113,7 +114,7 @@ test: end -- 'with' block ") -# Math Operators +### Math Operators test: unless ((5 wrapped around 2) == 1): fail "mod not working" @@ -121,8 +122,8 @@ test: [$x wrapped around $y, $x mod $y, $x % $y] all compile to "((\($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) +### 3-part chained comparisons +### (uses a lambda to avoid re-evaluating middle value, while still being an expression) test: $calls = 0 (one) means: @@ -144,8 +145,8 @@ test: ($x > $y >= $z) parses as ((($a $b $c) -> (($a > $b) and ($b >= $c))) $x $y $z) ($x >= $y >= $z) parses as ((($a $b $c) -> (($a >= $b) and ($b >= $c))) $x $y $z) -# TODO: optimize for common case where x,y,z are all either variables or number literals -# Boolean Operators +### TODO: optimize for common case where x,y,z are all either variables or number literals +### Boolean Operators test: (barfer) means (fail "short circuiting failed") assume (((no) and (barfer)) == (no)) @@ -156,8 +157,8 @@ test: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# Bitwise Operators -# These can break if running the precompiled code from another Lua version: +### Bitwise Operators +### These can break if running the precompiled code from another Lua version: lua> (" if \(at compilation $(LUA VERSION)) ~= \$(LUA VERSION) then \( @@ -169,19 +170,19 @@ lua> (" end ") -# TODO: implement OR, XOR, AND for multiple operands? +### TODO: implement OR, XOR, AND for multiple operands? test: - assume ((~ (~ 5)) == 5) + assume (~(~5) == 5) assume ((1 | 4) == 5) assume ((1 ~ 3) == 2) assume ((1 & 3) == 1) assume ((1 << 2) == 4) assume ((4 >> 2) == 1) -# Lua 5.3 introduced bit operators like | and &. Use them when possible, otherwise +### Lua 5.3 introduced bit operators like | and &. Use them when possible, otherwise fall back to bit.bor(), bit.band(), etc. lua> "if \((is jit) or ($(LUA API) == "Lua 5.2")) then" -[NOT $, ~ $] all compile to "Bit.bnot(\($ as lua expr))" +[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))" @@ -198,7 +199,7 @@ lua> "if \((is jit) or ($(LUA API) == "Lua 5.2")) then" "Bit.rshift(\($x as lua expr), \($shift as lua expr))" lua> "else" -[NOT $, ~ $] all compile to "(~(\($ as lua expr)))" +[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))" @@ -210,16 +211,17 @@ lua> "else" lua> "end" -# Unary operators +### Unary operators test: - assume ((- 5) == -5) + assume (-(5) == -5) assume ((not (yes)) == (no)) -(- $) compiles to "(-(\($ as lua expr)))" +-$ compiles to "(-(\($ as lua expr)))" (not $) compiles to "(not \($ as lua expr))" test: assume ((size of [1, 2, 3]) == 3) - assume ((#[1, 2, 3]) == 3) -# Length + assume (#[1, 2, 3] == 3) + +### Length [#$list, size of $list] all compile to: lua> (" local list_lua = \($list as lua expr) @@ -228,11 +230,11 @@ test: end return LuaCode("(#", list_lua, ")") ") -($list is empty) parses as ((#$list) == 0) +($list is empty) parses as (#$list == 0) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# Update operators +### Update operators test: $x = 1 $x += 1 diff --git a/lib/core/text.nom b/lib/core/text.nom index d7719b3..42f2e41 100644 --- a/lib/core/text.nom +++ b/lib/core/text.nom @@ -1,5 +1,6 @@ -#!/usr/bin/env nomsu -V6.15.13.8 -# +#!/usr/bin/env nomsu -V7.0.0 + +### This file contains some definitions of text escape sequences, including ANSI console color codes. @@ -62,17 +63,17 @@ test: external: ($num as hex) means: if ($num < 0): - return ("-0x%X", formatted with (- $num)) + return ("-0x%X", formatted with -$num) ..else: return ("0x%X", formatted with $num) -# Text literals +### Text literals $escapes = { .nl = "\n", .newline = "\n", .tab = "\t", .bell = "\a", .cr = "\r" ."carriage return" = "\r", .backspace = "\b", ."form feed" = "\f" .formfeed = "\f", ."vertical tab" = "\v" } -for $name = $str in $escapes: +for ($name = $str) in $escapes: with [$lua = (Lua (quote $str))]: - $(COMPILE RULES).$name = (-> $lua) + $(COMPILE RULES).$name = ->$lua
\ No newline at end of file diff --git a/lib/core/things.nom b/lib/core/things.nom index 6ba21cb..e83f88d 100644 --- a/lib/core/things.nom +++ b/lib/core/things.nom @@ -1,7 +1,8 @@ -#!/usr/bin/env nomsu -V6.15.13.8 -# - A library for simple object oriented programming. +#!/usr/bin/env nomsu -V7.0.0 +### + A library for simple object oriented programming. + use "core/metaprogramming" use "core/operators" use "core/control_flow" @@ -34,7 +35,7 @@ test: assume $b != (a Buffer {.bits = []}) (a Comma Buffer) is (a Buffer) with [$bits]: ($self, as text) means ($bits, joined with ",") - ($self, number of commas) means ((#$bits) - 1) + ($self, number of commas) means (#$bits - 1) $csv = (a Comma Buffer) assume $csv.is_a_buffer assume ($csv is "a Comma Buffer") @@ -45,7 +46,7 @@ test: assume "\$csv" == "x,y" assume ($csv, number of commas) == 1 (a Vec) is (a thing) with [$x, $y]: - ($self, + $other) means (Vec ($x + $other.x) ($y + $other.y)) + ($self, +$other) means (Vec ($x + $other.x) ($y + $other.y)) ($self, length) means (sqrt ($x * $x + $y * $y)) (Vec $x $y) means (a Vec {.x = $x, .y = $y}) assume ((Vec 1 2) + (Vec 10 10)) == (Vec 11 12) @@ -70,7 +71,6 @@ external: $class.__tostring = ($ -> "\($.__type) \($ as text like a dict)") $class.__eq = ({}'s metatable).__eq $class.__len = ({}'s metatable).__len - set $class's metatable to { .__index = $parent, .__tostring = ($class -> $class.__type) .__call = @@ -84,19 +84,15 @@ external: if $(initialize $): initialize $class - for $stub = $metamethod in $METAMETHOD_MAP: + for ($stub = $metamethod) in $METAMETHOD_MAP: if $class.($stub, as lua id): $class.$metamethod = $class.($stub, as lua id) return $class - $(a thing) = ((nil) class named "thing") - ($classname is $parent with $vars $class_body) compiles to: unless ($vars.type == "List"): - at $vars fail (" - Compile error: This is not a list of variables. - ") + at $vars fail "Compile error: This is not a list of variables." $class_id = ($classname.stub, as lua id) $class_body and= $class_body, with @@ -106,9 +102,10 @@ external: return "IndexChain" tree with ("Var" tree with "self") "Index" tree with ("Text" tree with $v.1) - + if ($parent.type == "Action"): $parent = ("Var" tree with $parent) + $lua = Lua (" \$class_id = _1_class_named(\($parent as lua id), \(quote $classname.stub)\( @@ -122,5 +119,6 @@ external: ) if $class_body else "" )) ") + $lua, add free vars [$class_id] - return $lua + return $lua
\ No newline at end of file diff --git a/lib/core/time.nom b/lib/core/time.nom index 63ff594..0a82fa5 100644 --- a/lib/core/time.nom +++ b/lib/core/time.nom @@ -1,5 +1,6 @@ -#!/usr/bin/env nomsu -V6 -# +#!/usr/bin/env nomsu -V7.0.0 + +### This file defines time-related actions. use "core/metaprogramming" @@ -14,23 +15,23 @@ external: (a Time) is (a thing) with [ $year, $month, $day, $hour, $min, $sec, $wday, $yday, $isdst ]: - ($self, + $other) means: + ($self, +$other) means: if (($self is "a Time") and ($other is "a Time")): fail (" Type error: Cannot add two times together. Hint: One of these two should be a number, not a time. ") - if ($other is not "a Number"): + if ($other isn'tt "a Number"): $other = ($os.time $other) - if ($self is not "a Number"): + if ($self isn't "a Number"): $self = ($os.time $self) return (a Time ($os.date "*t" ($self + $other, rounded))) - ($self, - $other) means: - if ($self is not "a Time"): + ($self, -$other) means: + if ($self isn't "a Time"): fail "Type error: Cannot subtract a Time from something that isn't a Time." $self = ($os.time $self) if ($other is "a Time"): @@ -45,7 +46,7 @@ external: return (a Time ($os.date "*t")) [sleep for $t seconds, sleep for $t second] all mean: - # Lua does not come with a sleep() function, only an os.clock() function, + ### Lua does not come with a sleep() function, only an os.clock() function, so this busy-loop is necessary for cross-platform compatibility. $deadline = (($os.clock()) + $t) repeat while (($os.clock()) < $deadline): do nothing |
