diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2018-12-30 23:58:47 -0800 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2018-12-30 23:58:47 -0800 |
| commit | 5059c081ac624ede925d5256885509ddd17181c6 (patch) | |
| tree | 0457d135e5193126bf73466bfaae09df94ab8a7d /core | |
| parent | ec69ee6f62016a2edcc13e3183cbe4cadd75717f (diff) | |
Auto-upgraded code.
Diffstat (limited to 'core')
| -rw-r--r-- | core/collections.nom | 13 | ||||
| -rw-r--r-- | core/control_flow.nom | 70 | ||||
| -rw-r--r-- | core/errors.nom | 9 | ||||
| -rw-r--r-- | core/id.nom | 4 | ||||
| -rw-r--r-- | core/math.nom | 18 | ||||
| -rw-r--r-- | core/metaprogramming.nom | 27 | ||||
| -rw-r--r-- | core/operators.nom | 30 | ||||
| -rw-r--r-- | core/text.nom | 9 |
8 files changed, 92 insertions, 88 deletions
diff --git a/core/collections.nom b/core/collections.nom index 77da968..5697af9 100644 --- a/core/collections.nom +++ b/core/collections.nom @@ -38,9 +38,8 @@ test: $dict = {.x = 1, .y = 2, .z = 3} assume (size of $dict) == 3 assume [: for $ in {.x = 1}: add $] == [{.key = "x", .value = 1}] - assume [: for $k = $v in {.x = 1}: add {.key = $k, .value = $v}] == [ - {.key = "x", .value = 1} - ] + 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 = 1, .z = 10} assume ({.x = 1, .y = 1} & {.y = 10, .z = 10}) == {.y = 1} @@ -62,9 +61,8 @@ externally ($lists flattened) means: 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"]) @@ -81,10 +79,9 @@ test: assume ("\$t" == "XXX") (set $dict's metatable to $metatable) compiles to -.."setmetatable(\($dict as lua expr), \($metatable as lua expr));" + "setmetatable(\($dict as lua expr), \($metatable as lua expr));" [$'s metatable, $'metatable] all compile to "getmetatable(\($ as lua expr))" - test: assume (({} with fallback $ -> ($ + 1)).10 == 11) diff --git a/core/control_flow.nom b/core/control_flow.nom index b2db945..12bb9c6 100644 --- a/core/control_flow.nom +++ b/core/control_flow.nom @@ -30,8 +30,9 @@ test: barf "conditional fail" (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 (" +[ + if $condition $if_body else $else_body, unless $condition $else_body else $if_body +] all compile to (" if \($condition as lua expr) then \($if_body as lua) else @@ -58,7 +59,10 @@ test: equivalent of a conditional expression: (cond and if_true or if_false) if {.Text, .List, .Dict, .Number}.($when_true_expr.type): return - Lua "(\($condition as lua expr) and \($when_true_expr as lua expr) or \($when_false_expr as lua expr))" + Lua (" + (\($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 doesn't have a proper ternary operator!) @@ -135,9 +139,15 @@ test: assume ($x == 20) (repeat while $condition $body) compiles to: - $lua = (Lua "while \($condition as lua expr) do\n \($body as lua)") + $lua = + Lua (" + while \($condition as lua expr) do + \($body as lua) + ") + if ($body has subtree \(do next)): $lua, add "\n ::continue::" + $lua, add "\nend --while-loop" return $lua @@ -173,7 +183,10 @@ test: ] all compile to: # This uses Lua's approach of only allowing loop-scoped variables in a loop $lua = - Lua "for \($var as lua identifier)=\($start as lua expr),\($stop as lua expr),\($step as lua expr) do" + Lua (" + for \($var as lua identifier)=\($start as lua expr),\($stop as lua expr),\ + ..\($step as lua expr) do + ") $lua, add "\n " ($body as lua) if ($body has subtree \(do next)): $lua, add "\n ::continue::" @@ -265,7 +278,10 @@ test: [for $key = $value in $iterable $body, for $key $value in $iterable $body] ..all compile to: $lua = - Lua "for \($key as lua identifier),\($value as lua identifier) in pairs(\($iterable as lua expr)) do" + Lua (" + for \($key as lua identifier),\($value as lua identifier) in pairs(\ + ..\($iterable as lua expr)) do + ") $lua, add "\n " ($body as lua) if ($body has subtree \(do next)): $lua, add "\n ::continue::" @@ -314,7 +330,7 @@ test: $else_allowed = (yes) unless ($body.type is "Block"): compile error at $body "'if' expected a Block, but got a \($body.type)." - .."Perhaps you forgot to put a ':' after 'if'?" + "Perhaps you forgot to put a ':' after 'if'?" for $line in $body: unless @@ -329,7 +345,7 @@ test: if (($line.1 is "else") and ((size of $line) == 2)): unless $else_allowed: compile error at $line "You can't have two 'else' blocks." - .."Merge all of the 'else' blocks together." + "Merge all of the 'else' blocks together." unless ((size of "\$code") > 0): compile error at $line @@ -338,12 +354,7 @@ test: ..around it. Otherwise, make sure the 'else' block comes last. ") - $code, add (" - - else - \; - ") ($action as lua) - + $code, add "\nelse\n " ($action as lua) $else_allowed = (no) ..else: $code, add $clause " " @@ -351,20 +362,14 @@ test: if ($i > 1): $code, add " or " $code, add ($line.$i as lua expr) - - $code, add (" - then - \; - ") ($action as lua) - + $code, add " then\n " ($action as lua) $clause = "\nelseif" if ((size of "\$code") == 0): compile error at $body "'if' block has an empty body." - .."This means nothing would happen, so the 'if' block should be deleted." + "This means nothing would happen, so the 'if' block should be deleted." $code, add "\nend --when" - return $code test: @@ -389,7 +394,7 @@ test: define mangler unless ($body.type is "Block"): compile error at $body "'if' expected a Block, but got a \($body.type)" - .."Perhaps you forgot to put a ':' after the 'is'?" + "Perhaps you forgot to put a ':' after the 'is'?" for $line in $body: unless @@ -403,7 +408,7 @@ test: if (($line.1 is "else") and ((size of $line) == 2)): unless $else_allowed: compile error at $line "You can't have two 'else' blocks." - .."Merge all of the 'else' blocks together." + "Merge all of the 'else' blocks together." unless ((size of "\$code") > 0): compile error at $line @@ -412,12 +417,7 @@ test: ..around it. Otherwise, make sure the 'else' block comes last. ") - $code, add (" - - else - \; - ") ($action as lua) - + $code, add "\nelse\n " ($action as lua) $else_allowed = (no) ..else: $code, add $clause " " @@ -425,20 +425,14 @@ test: if ($i > 1): $code, add " or " $code, add "\(mangle "branch value") == " ($line.$i as lua expr) - - $code, add (" - then - \; - ") ($action as lua) - + $code, add " then\n " ($action as lua) $clause = "\nelseif" if ((size of "\$code") == 0): compile error at $body "'if' block has an empty body." - .."This means nothing would happen, so the 'if' block should be deleted." + "This means nothing would happen, so the 'if' block should be deleted." $code, add "\nend --when" - return Lua (" do --if % is... diff --git a/core/errors.nom b/core/errors.nom index 9cf7a23..edcfffa 100644 --- a/core/errors.nom +++ b/core/errors.nom @@ -7,10 +7,12 @@ use "core/metaprogramming.nom" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (barf $msg) compiles to -.."error(\(=lua "\$msg and \($msg as lua expr) or 'nil'"), 0);" + "error(\(=lua "\$msg and \($msg as lua expr) or 'nil'"), 0);" (assume $condition) compiles to: - lua> "local \$assumption = 'Assumption failed: '..tostring((\$condition):get_source_code())" + lua> (" + local \$assumption = 'Assumption failed: '..tostring((\$condition):get_source_code()) + ") return Lua (" @@ -29,7 +31,8 @@ use "core/metaprogramming.nom" do local \(mangle "a"), \(mangle "b") = \($a as lua expr), \($b as lua expr) if \(mangle "a") ~= \(mangle "b") then - error(\(quote "\$assumption").."\\n"..tostring(\(mangle "a")).." != "..tostring(\(mangle "b")), 0) + error(\(quote "\$assumption").."\\n"..tostring(\(mangle "a")).." != "..tostring(\ + ..\(mangle "b")), 0) end end ") diff --git a/core/id.nom b/core/id.nom index bc9a96c..a450288 100644 --- a/core/id.nom +++ b/core/id.nom @@ -16,7 +16,8 @@ $obj_by_id = {} set $obj_by_id's metatable to {.__mode = "v"} $id_by_obj = {} set $id_by_obj's metatable to { - .__mode = "k", .__index = ( + .__mode = "k" + .__index = for ($self $key): if ($key == (nil)): return $self.$nil_surrogate @@ -30,7 +31,6 @@ set $id_by_obj's metatable to { $self.$key = $id $obj_by_id.$id = $key return $id - ) } externally (uuid) means: diff --git a/core/math.nom b/core/math.nom index d456bff..05b7dd8 100644 --- a/core/math.nom +++ b/core/math.nom @@ -13,10 +13,8 @@ use "core/collections.nom" # Literals: test: assume (all of [inf, NaN, pi, tau, golden ratio, e]) or barf - .."math constants failed" - + "math constants failed" $nan = (NaN) - assume ($nan != $nan) or barf "NaN failed" [infinity, inf] all compile to "math.huge" [not a number, NaN, nan] all compile to "(0/0)" @@ -42,29 +40,23 @@ test: [absolute value $, | $ |, abs $] all compile to "math.abs(\($ as lua expr))" [square root $, square root of $, √ $, sqrt $] all compile to -.."math.sqrt(\($ 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 -.."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 "math.sinh(\($ as lua expr))" - [hyperbolic cosine $, cosh $] all compile to "math.cosh(\($ as lua expr))" [hyperbolic tangent $, tanh $] all compile to "math.tanh(\($ as lua expr))" [e^ $, exp $] all compile to "math.exp(\($ as lua expr))" [natural log $, ln $, log $] all compile to "math.log(\($ as lua expr))" [log $ base $base, log base $base of $] all compile to -.."math.log(\($ as lua expr), \($base as lua expr))" - + "math.log(\($ as lua expr), \($base 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 "math.floor(\($ as lua expr) + .5)" test: @@ -204,7 +196,7 @@ externally (seed random with $) means: (seed random) parses as (seed random with (=lua "os.time()")) [random number, random, rand] all compile to "math.random()" [random int $n, random integer $n, randint $n] all compile to -.."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 "math.random(\($low as lua expr), \($high as lua expr))" diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index 6dea00f..180614b 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -210,13 +210,11 @@ test: [$1, $2] = [1, 2] swap $1 and $2 assume (($1 == 2) and ($2 == 1)) or barf - .."'parse % as %' failed on 'swap % and %'" - + "'parse % as %' failed on 'swap % and %'" [$tmp, $tmp2] = [1, 2] - swap $tmp and $tmp2 assume (($tmp == 2) and ($tmp2 == 1)) or barf - .."'parse % as %' variable mangling failed." + "'parse % as %' variable mangling failed." ($actions all parse as $body) compiles to: lua> (" @@ -310,7 +308,12 @@ externally ($ is $kind syntax tree) means ($tree with $t -> $replacement) compiles to (" \($tree as lua expr):map(function(\($t as lua expr)) - \(=lua "\$replacement.type == 'Block' and \($replacement as lua) or 'return '..\($replacement as lua expr)") + \( + =lua (" + \$replacement.type == 'Block' and \($replacement as lua) or 'return '..\ + ..\($replacement as lua expr) + ") + ) end) ") @@ -360,7 +363,13 @@ externally (match $tree with $patt) means: ") test: - assume ((quote "one\n\"two\"") == "\"one\\n\\\"two\\\"\"") + assume + ( + quote (" + one + "two" + ") + ) == "\"one\\n\\\"two\\\"\"" (quote $s) compiles to "tostring(\($s as lua expr)):as_lua()" test: @@ -400,7 +409,7 @@ test: assume (run \(return \(\(5) + \(5)))) == 10 (run $nomsu_code) compiles to "run_1_in(\($nomsu_code as lua expr), _ENV)" [compile $block, compiled $block, $block compiled] all compile to -.."compile(\($block as lua))" + "compile(\($block as lua))" test: (foo) means: @@ -443,4 +452,6 @@ test: ") externally (Nomsu version) means: - return "\(Nomsu syntax version).\(core version).\(Nomsu compiler version).\(lib version)" + return (" + \(Nomsu syntax version).\(core version).\(Nomsu compiler version).\(lib version) + ") diff --git a/core/operators.nom b/core/operators.nom index 8dfac09..9b095d0 100644 --- a/core/operators.nom +++ b/core/operators.nom @@ -17,7 +17,7 @@ test: ($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 -.."(\($a as lua expr) ~= \($b as lua expr))" + "(\($a as lua expr) ~= \($b as lua expr))" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -91,11 +91,13 @@ test: $y = "inner" set global x local y assume (($foozle == "inner") and ($y == "outer")) or barf - .."'with external' failed." + "'with external' failed." (with external $externs $body) compiles to: $body_lua = ($body as lua) - lua> "\$body_lua:remove_free_vars(table.map(\$externs, function(v) return compile(v):text() end))" + lua> (" + \$body_lua:remove_free_vars(table.map(\$externs, function(v) return compile(v):text() end)) + ") return $body_lua test: @@ -120,6 +122,7 @@ test: \$defs:add("local ", item_lua, ";") end ") + return Lua (" do @@ -133,7 +136,7 @@ test: assume ((5 wrapped around 2) == 1) or barf "mod not working" [$x wrapped around $y, $x mod $y] all compile to -.."((\($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) @@ -144,10 +147,8 @@ test: return 1 assume (0 <= (one) <= 2) or barf "Three-way chained comparison failed." assume ($calls == 1) or barf - .."Three-way comparison evaluated middle value multiple times" - + "Three-way comparison evaluated middle value multiple times" ($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) ($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) @@ -183,31 +184,30 @@ test: 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))" + "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))" + "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))" + "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))" + "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))" + "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 as lua expr) << \($shift as lua expr))" [$x RSHIFT $shift, $x >> $shift] all compile to -.."(\($x as lua expr) >> \($shift as lua expr))" + "(\($x as lua expr) >> \($shift as lua expr))" lua> "end" diff --git a/core/text.nom b/core/text.nom index b5b22b3..97f560e 100644 --- a/core/text.nom +++ b/core/text.nom @@ -27,7 +27,14 @@ test: assume ("asdf", capitalized) == "Asdf" assume ("asdf", uppercase) == "ASDF" assume ("asdf", with "s" -> "X") == "aXdf" - assume ("one\ntwo\n", lines) == ["one", "two", ""] + assume + (" + one + two + + "), lines + ..== ["one", "two", ""] + ($spec とは $body) parses as ($spec means $body) test: |
