From 1fd5a0be12fb0496ca2ba109bcebd78f252011f1 Mon Sep 17 00:00:00 2001 From: Ian Piumarta Date: Thu, 30 May 2024 18:06:08 +0900 Subject: [PATCH] New reference output. --- test.ref | 9 +++------ test.txt | 27 +++++++++++++++------------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/test.ref b/test.ref index a0e9871..272cd0f 100644 --- a/test.ref +++ b/test.ref @@ -147,13 +147,10 @@ f(); f 102 ( 40 ) 41 32 { 123 32 p 112 r 114 i 105 n 110 t 116 ( 40 _ 95 _ 95 e 101 n 110 v 118 _ 95 _ 95 ( 40 ) 41 ) 41 32 } 125 10 f 102 ( 40 ) 41 ; 59 10 -<> - | operation: 15 - | 0: 6 - | 1: 7 42 42 -6 * 7 +42 +42 MACRO table <> | assert: <> | | environment: nil @@ -261,4 +258,4 @@ AST eval => 42 3: if (n < 2) nil else n * factorial(n - 1) 2: factorial(5) 1: { factorial(5) } - 0: try { factorial(5) } catch (e) { { if (!eval(e.__function__ == "*")) { print("\nassertion failed: ", "e.__function__ == \"*\"", "\n"); exit(1) } }; { if (!eval(e.__kind__ == "type error")) { print("\nassertion failed: ", "e.__kind__ == \"type error\"", "\n"); exit(1) } }; { if (!eval(e.__message__ == "illegal operand types")) { print("\nassertion failed: ", "e.__message__ == \"illegal operand types\"", "\n"); exit(1) } }; { if (!eval(e.operand1 == 2)) { print("\nassertion failed: ", "e.operand1 == 2", "\n"); exit(1) } }; { if (!eval(e.operand2 == nil)) { print("\nassertion failed: ", "e.operand2 == nil", "\n"); exit(1) } }; print(e.__function__, ": ", e.__kind__, ": ", e.__message__, ": ", typeName(e.operand1), " and ", typeName(e.operand2), "\n"); let w = 2; let j = i; while (j /= 10 >= 0) { w += 1 }; for (i from len(e) - 1 to 0) print(pad(w, codeString(i)), ": ", codeString(e[i]), "\n") } + 0: try { factorial(5) } catch (e) { { if (!eval(e.__function__ == "*")) { print("\nassertion failed: ", "e.__function__ == \"*\"", "\n"); exit(1) } }; { if (!eval(e.__kind__ == "type error")) { print("\nassertion failed: ", "e.__kind__ == \"type error\"", "\n"); exit(1) } }; { if (!eval(e.__message__ == "illegal operand types")) { print("\nassertion failed: ", "e.__message__ == \"illegal operand types\"", "\n"); exit(1) } }; { if (!eval(e.operand1 == 2)) { print("\nassertion failed: ", "e.operand1 == 2", "\n"); exit(1) } }; { if (!eval(e.operand2 == nil)) { print("\nassertion failed: ", "e.operand2 == nil", "\n"); exit(1) } }; print(e.__function__, ": ", e.__kind__, ": ", e.__message__, ": ", typeName(e.operand1), " and ", typeName(e.operand2), "\n"); backtrace(e) } diff --git a/test.txt b/test.txt index b204433..0255044 100644 --- a/test.txt +++ b/test.txt @@ -152,8 +152,8 @@ assert((0b1110 & 0b0111) == 0b0110); nt = 0; nf = 0; -t() { nt = nt + 1; #t } -f() { nf = nf + 1; nil } +t() { global nt += 1; #t } +f() { global nf += 1; nil } refute(f() || f()); assert(nt == 0); assert(nf == 2); assert(f() || t()); assert(nt == 1); assert(nf == 3); @@ -388,16 +388,23 @@ factorial(n) { if (n < 2) nil else n * factorial(n-1) } typeName(x) { if (!x) return ""; - let name = "?"; - let level = 1; + name = "?"; + level = 1; while (x && !x.allKeys().includes(#__name__)) x = x.__delegate__; if (x.allKeys().includes(#__name__)) name = x.__name__.asString(); "<" * level + name + ">" * level; } -pad(w, i) { - while (len(i) < w) i = " " + i; - i; +backtrace(e) { + pad(w, i) { + while (len(i) < w) i = " " + i; + i; + } + w = 2; + j = len(e); + while ((j /= 10) > 0) { ++w; } // w = 1+log10(j) + for (i from len(e) - 1 to 0) + print(pad(w, codeString(i)), ": ", codeString(e[i]), "\n"); } try { @@ -410,9 +417,5 @@ catch (e) { assert(e.operand1 == 2); assert(e.operand2 == nil); print(e.__function__, ": ", e.__kind__, ": ", e.__message__, ": ", typeName(e.operand1), " and ", typeName(e.operand2), "\n"); - let w = 2; - let j = i; - while ( (j /= 10) > 0) { ++w; } - for (i from len(e) - 1 to 0) - print(pad(w, codeString(i)), ": ", codeString(e[i]), "\n"); + backtrace(e); }