From 3e89092833a6d407e711fe4ae5f44474ff34cf64 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 1 Jan 2019 17:15:51 -0800 Subject: Some changes to the error API, a fix for statement block parsing, and replacing ((foo 1 baz)'s meaning) with $(foo 1 baz). --- core/collections.nom | 8 ++++--- core/control_flow.nom | 22 +++++++++---------- core/coroutines.nom | 7 ++++-- core/errors.nom | 44 ++++++++++++++++++++++++-------------- core/id.nom | 2 +- core/io.nom | 2 +- core/math.nom | 24 ++++++++++----------- core/metaprogramming.nom | 53 +++++++++++++++++++++++++--------------------- core/operators.nom | 55 +++++++++++++++++++++++++++++++----------------- core/text.nom | 2 +- 10 files changed, 129 insertions(+), 90 deletions(-) (limited to 'core') diff --git a/core/collections.nom b/core/collections.nom index 6aac861..18b1af6 100644 --- a/core/collections.nom +++ b/core/collections.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V6.13.12.8 +#!/usr/bin/env nomsu -V6.14 # This file contains code that supports manipulating and using collections like lists and dictionaries. @@ -60,8 +60,10 @@ 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"]) diff --git a/core/control_flow.nom b/core/control_flow.nom index f066767..5c90bcc 100644 --- a/core/control_flow.nom +++ b/core/control_flow.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V6.13.12.8 +#!/usr/bin/env nomsu -V6.14 # This file contains compile-time actions that define basic control flow structures like "if" statements and loops. @@ -16,7 +16,7 @@ test: # Conditionals test: if (no): - barf "conditional fail" + fail "conditional fail" (if $condition $if_body) compiles to (" if \($condition as lua expr) then @@ -26,7 +26,7 @@ test: test: unless (yes): - barf "conditional fail" + fail "conditional fail" (unless $condition $unless_body) parses as (if (not $condition) $unless_body) [ @@ -134,7 +134,7 @@ test: $x += 1 if (yes): do next - barf "Failed to 'do next'" + fail "Failed to 'do next'" assume ($x == 20) (repeat while $condition $body) compiles to: @@ -311,16 +311,16 @@ test: test: when: (1 == 2) (100 < 0): - barf "bad conditional" + fail "bad conditional" (1 == 0) (1 == 1) $not_a_variable.x: do nothing (1 == 1): - barf "bad conditional" + fail "bad conditional" (1 == 2): - barf "bad conditional" + fail "bad conditional" else: - barf "bad conditional" + fail "bad conditional" # Multi-branch conditional (if..elseif..else) (when $body) compiles to: @@ -374,16 +374,16 @@ test: test: if 5 is: 1 2 3: - barf "bad switch statement" + fail "bad switch statement" 4 5: do nothing 5 6: - barf "bad switch statement" + fail "bad switch statement" else: - barf "bad switch statement" + fail "bad switch statement" # Switch statement [if $branch_value is $body, when $branch_value is $body] all compile to: diff --git a/core/coroutines.nom b/core/coroutines.nom index 1a164a9..4b4639a 100644 --- a/core/coroutines.nom +++ b/core/coroutines.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V6.13.12.8 +#!/usr/bin/env nomsu -V6.14 # This file defines the code that creates and manipulates coroutines @@ -18,7 +18,10 @@ test: $nums = [] for $ in coroutine $co: $nums, add $ - assume ($nums == [4, 5, 6, 6, 6]) or barf "Coroutine iteration failed" + + unless ($nums == [4, 5, 6, 6, 6]): + fail "Coroutine iteration failed" + $d = {.x = 0} $co2 = coroutine: diff --git a/core/errors.nom b/core/errors.nom index 0b63b5b..bd9e1c4 100644 --- a/core/errors.nom +++ b/core/errors.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V6.13.12.8 +#!/usr/bin/env nomsu -V6.14 # This file contains basic error reporting code @@ -7,9 +7,7 @@ use "core/operators.nom" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -(fail $msg) compiles to - "error(\(($msg as lua expr) if $msg else "nil"), 0);" - +(fail $msg) compiles to "error(\(($msg as lua expr) if $msg else "nil"), 0);" (assume $condition) compiles to: lua> (" local \$assumption = 'Assumption failed: '..tostring((\$condition):get_source_code()) @@ -41,26 +39,34 @@ use "core/operators.nom" test: try: fail $worked = (no) - try: fail - ..if it fails: + try: + fail "xx" + ..if it fails with $failure: $worked = (yes) ..if it succeeds: fail "'try' incorrectly ran success case." - + assume $failure == "xx" unless $worked: fail "'try' failed to recover from failure" + # Try/except [ - try $action if it succeeds $success if it fails $fallback - try $action if it fails $fallback if it succeeds $success + 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): $success_lua, add "\n" + 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" + $success_lua, + add "if not _fell_through then return table.unpack(_result, 2) end" $fallback_lua = ($fallback as lua) if ((#"\$fallback_lua") > 0): - $fallback_lua, prepend "\nlocal function failure() return _result[2] end\n" + $msg_lua = ($msg as lua expr) + 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)] $fallback_lua, prepend "-- Failure:" return Lua (" @@ -83,14 +89,20 @@ test: (try $action) parses as try $action if it succeeds (do nothing) if it fails (do nothing) -(try $action if it fails $msg $fallback) parses as - try $action if it succeeds (do nothing) if it fails $msg $fallback +(try $action if it fails $fallback) parses as + try $action if it succeeds (do nothing) if it fails $fallback + +(try $action if it fails with $msg $fallback) parses as + try $action if it succeeds (do nothing) if it fails with $msg $fallback (try $action if it succeeds $success) parses as try $action if it succeeds $success if it fails (do nothing) (try $action if it fails $fallback if it succeeds $success) parses as - try $action if it succeeds $success if it fails $fallback + try $action if it fails with (=lua "") $fallback if it succeeds $success + +(try $action if it succeeds $success if it fails $fallback) parses as + try $action if it succeeds $success if it fails with (=lua "") $fallback test: $success = (no) @@ -117,7 +129,7 @@ test: end ") -~~~ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (barf $) parses as (fail $) (assume $1 or barf $2) parses as (unless $1: fail $2) diff --git a/core/id.nom b/core/id.nom index d5f747a..61ffeaa 100644 --- a/core/id.nom +++ b/core/id.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V6.13.12.8 +#!/usr/bin/env nomsu -V6.14 # A simple UUID function based on RFC 4122: http://www.ietf.org/rfc/rfc4122.txt diff --git a/core/io.nom b/core/io.nom index 6333292..f1abe32 100644 --- a/core/io.nom +++ b/core/io.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V6.13.12.8 +#!/usr/bin/env nomsu -V6.14 # This file contains basic input/output code diff --git a/core/math.nom b/core/math.nom index 04388cb..f7a0afb 100644 --- a/core/math.nom +++ b/core/math.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V6.13.12.8 +#!/usr/bin/env nomsu -V6.14 # This file defines some common math literals and functions @@ -12,10 +12,11 @@ use "core/collections.nom" # Literals: test: - assume (all of [inf, NaN, pi, tau, golden ratio, e]) or barf - "math constants failed" + unless (all of [inf, NaN, pi, tau, golden ratio, e]): + fail "math constants failed" $nan = (NaN) - assume ($nan != $nan) or barf "NaN failed" + unless ($nan != $nan): + fail "NaN failed" [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" @@ -26,19 +27,18 @@ test: # Functions: test: assume (("5" as a number) == 5) -(($ as a number)'s meaning) = ((tonumber $)'s meaning) -(($ as number)'s meaning) = ((tonumber $)'s meaning) +external $($ as a number) = $(tonumber $) +external $($ as number) = $(tonumber $) test: - assume + unless all of [ 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 base 2 of 5, floor 5, ceiling 5, round 5 ] - ..or barf "math functions failed" - + ..: + fail "math functions failed" [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))" [sine $, sin $] all compile to "math.sin(\($ as lua expr))" @@ -60,8 +60,8 @@ test: [ceiling $, ceil $] all compile to "math.ceil(\($ as lua expr))" [round $, $ rounded] all compile to "math.floor(\($ as lua expr) + .5)" test: - assume ((463 to the nearest 100) == 500) or barf "rounding failed" - assume ((2.6 to the nearest 0.25) == 2.5) or barf "rounding failed" + unless ((463 to the nearest 100) == 500): fail "rounding failed" + unless ((2.6 to the nearest 0.25) == 2.5): fail "rounding failed" externally ($n to the nearest $rounder) means =lua "(\$rounder)*math.floor((\$n / \$rounder) + .5)" diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index b4297dd..c950e97 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -1,9 +1,9 @@ -#!/usr/bin/env nomsu -V6.13.12.8 +#!/usr/bin/env nomsu -V6.14 # This File contains actions for making actions and compile-time actions and some helper functions to make that easier. -lua> "NOMSU_CORE_VERSION = 13" +lua> "NOMSU_CORE_VERSION = 14" lua> "NOMSU_LIB_VERSION = 8" lua> (" do @@ -62,22 +62,27 @@ test: (five) compiles to "5" test: - assume ((five) == 5) or barf "Compile to expression failed." + unless ((five) == 5): + fail "Compile to expression failed." (loc x) compiles to "local x = 99;" test: lua> "do" loc x - assume ($x is 99) or barf "Compile to statements with locals failed." + unless ($x is 99): + fail "Compile to statements with locals failed." lua> "end" - assume ($x is (nil)) or barf "Failed to properly localize a variable." + unless ($x is (nil)): + fail "Failed to properly localize a variable." + (asdf) compiles to: $tmp = "" return (Lua $tmp) test: asdf - assume ($tmp is (nil)) or barf "compile to is leaking variables" + unless ($tmp is (nil)): + fail "compile to is leaking variables" lua> (" compile.action["1 compiles to"] = function(compile, \$action, \$body) @@ -121,12 +126,17 @@ lua> (" test: (foo $x) means "outer" - with [(foo $)'s meaning]: + with [$(foo $)]: (foo $x) means: $y = ($x + 1) return $y - assume ((foo 10) == 11) or barf "Action didn't work." - assume ($y is (nil)) or barf "Action leaked a local into globals." + + unless ((foo 10) == 11): + fail "Action didn't work." + + unless ($y is (nil)): + fail "Action leaked a local into globals." + (baz $) parses as (foo $) assume ((foo 1) == "outer") @@ -195,10 +205,6 @@ test: return lua ") -test: - assume (((say $)'s meaning) == (=lua "say")) - -($action's meaning) compiles to (Lua ($action, get stub, as lua id)) test: (swap $x and $y) parses as do: @@ -209,12 +215,12 @@ test: test: [$1, $2] = [1, 2] swap $1 and $2 - assume (($1 == 2) and ($2 == 1)) or barf - "'parse $ as $' failed on 'swap $ and $'" + unless (($1 == 2) and ($2 == 1)): + fail "'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." + unless (($tmp == 2) and ($tmp2 == 1)): + fail "'parse $ as $' variable mangling failed." ($actions all parse as $body) compiles to: lua> (" @@ -227,10 +233,10 @@ test: end local function make_tree(t) if SyntaxTree:is_instance(t) and t.type == "Var" then - if replacements[t[1]] then - return replacements[t[1]] + if replacements[t:as_var()] then + return replacements[t:as_var()] else - return "SyntaxTree{mangle("..t[1]:as_lua().."), type="..t.type:as_lua()..", \ + return "SyntaxTree{mangle("..t:as_var():as_lua().."), type="..t.type:as_lua()..", \ ..source="..tostring(t.source):as_lua().."}" end elseif SyntaxTree:is_instance(t) then @@ -265,7 +271,6 @@ test: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [$action parses as $body] all parse as ([$action] all parse as $body) - externally ($tree as lua expr) means: lua> (" local tree_lua = compile(\$tree) @@ -320,7 +325,7 @@ externally ($tree with vars $replacements) means =lua (" \$tree:map(function(\$t) if \$t.type == "Var" then - return \$replacements[\$t[1]] + return \$replacements[\$t:as_var()] end end) ") @@ -328,7 +333,7 @@ externally ($tree with vars $replacements) means (tree $tree with vars $replacements) compiles to (" \(=lua "(\$tree):as_lua()"):map(function(t) if t.type == "Var" then - return \($replacements as lua expr)[t[1]] + return \($replacements as lua expr)[t:as_var()] end end) ") @@ -344,7 +349,7 @@ externally ($tree with vars $replacements) means externally (match $tree with $patt) means: lua> (" - if \$patt.type == "Var" then return Dict{[\$patt[1]]=\$tree} end + if \$patt.type == "Var" then return Dict{[\$patt:as_var()]=\$tree} end if \$patt.type == "Action" and \$patt:get_stub() ~= \$tree:get_stub() then return nil end if #\$patt ~= #\$tree then return nil end local matches = Dict{} diff --git a/core/operators.nom b/core/operators.nom index 6c089a6..fa37a11 100644 --- a/core/operators.nom +++ b/core/operators.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V6.13.12.8 +#!/usr/bin/env nomsu -V6.14 # This file contains definitions of operators like "+" and "and". @@ -24,12 +24,15 @@ test: $x = 10 assume ($x == 10) [$x, $y] = [10, 20] - assume (($x == 10) and ($y == 20)) or barf "mutli-assignment failed." + unless (($x == 10) and ($y == 20)): + fail "mutli-assignment failed." [$x, $y] = [$y, $x] - assume (($y == 10) and ($x == 20)) or barf "swapping vars failed." + unless (($y == 10) and ($x == 20)): + fail "swapping vars failed." $vals = [4, 5] [$x, $y] = (unpack $vals) - assume (($x == 4) and ($y == 5)) or barf "unpacking failed" + unless (($x == 4) and ($y == 5)): + fail "unpacking failed" # Variable assignment operator ($var = $value) compiles to: @@ -80,7 +83,7 @@ test: external $foozle = "inner" $y = "inner" set global x local y - assume (($foozle == "inner") and ($y == "outer")) or barf "external failed." + unless (($foozle == "inner") and ($y == "outer")): fail "external failed." (external $var = $value) compiles to "\($var as lua) = \($value as lua)" test: [$foozle, $y] = ["outer", "outer"] @@ -89,8 +92,8 @@ test: $foozle = "inner" $y = "inner" set global x local y - assume (($foozle == "inner") and ($y == "outer")) or barf - "'with external' failed." + unless (($foozle == "inner") and ($y == "outer")): + fail "'with external' failed." (with external $externs $body) compiles to: $body_lua = ($body as lua) @@ -104,10 +107,17 @@ test: with [$z, $x = 999]: assume $z == (nil) $z = 999 - assume ($z == 999) or barf "'with' failed." - assume ($x == 999) or barf "'with' assignment failed." - assume ($x == 1) or barf "'with' scoping failed" - assume ($z == (nil)) or barf "'with' scoping failed" + unless ($z == 999): + fail "'with' failed." + + unless ($x == 999): + fail "'with' assignment failed." + + unless ($x == 1): + fail "'with' scoping failed" + + unless ($z == (nil)): + fail "'with' scoping failed" (with $assignments $body) compiles to: lua> (" @@ -132,7 +142,8 @@ test: # Math Operators test: - assume ((5 wrapped around 2) == 1) or barf "mod not working" + unless ((5 wrapped around 2) == 1): + fail "mod not working" [$x wrapped around $y, $x mod $y] all compile to "((\($x as lua expr)) % (\($y as lua expr)))" @@ -144,9 +155,12 @@ test: (one) means: external $calls = ($calls + 1) 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" + + unless (0 <= (one) <= 2): + fail "Three-way chained comparison failed." + + unless ($calls == 1): + fail "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) @@ -159,7 +173,7 @@ test: # TODO: optimize for common case where x,y,z are all either variables or number literals # Boolean Operators test: - (barfer) means (barf "short circuiting failed") + (barfer) means (fail "short circuiting failed") assume (((no) and (barfer)) == (no)) assume ((no) or (yes)) assume ((yes) or (barfer)) @@ -228,11 +242,14 @@ test: test: $x = 1 $x += 1 - assume ($x == 2) or barf "+= failed" + unless ($x == 2): + fail "+= failed" $x *= 2 - assume ($x == 4) or barf "*= failed" + unless ($x == 4): + fail "*= failed" wrap $x around 3 - assume ($x == 1) or barf "wrap around failed" + unless ($x == 1): + fail "wrap around failed" ($var += $) parses as ($var = (($var or 0) + $)) ($var -= $) parses as ($var = (($var or 0) - $)) ($var *= $) parses as ($var = (($var or 1) * $)) diff --git a/core/text.nom b/core/text.nom index 80f5ac9..ad57498 100644 --- a/core/text.nom +++ b/core/text.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V6.13.12.8 +#!/usr/bin/env nomsu -V6.14 # This file contains some definitions of text escape sequences, including ANSI console color codes. -- cgit v1.2.3