From 94a4970053ec5af3ed017e8efadceaded9b2c234 Mon Sep 17 00:00:00 2001 From: bakpakin Date: Sat, 13 Jan 2018 10:34:59 -0500 Subject: [PATCH] Add underscore to repl --- client/main.c | 9 +++++++++ core/stl.c | 2 +- dsttest/suite0.dst | 5 ++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/client/main.c b/client/main.c index 1501b2cf..d126b67d 100644 --- a/client/main.c +++ b/client/main.c @@ -73,10 +73,18 @@ static void bshift(DstBuffer *buf, int32_t delta) { } } +/* Wrap a value to add to the env */ +static void set_underscore(Dst val) { + DstTable *t = dst_table(0); + dst_table_put(t, dst_csymbolv("value"), val); + dst_put(env, dst_csymbolv("_"), dst_wrap_table(t)); +} + /* simple repl */ static int repl() { DstBuffer b; dst_buffer_init(&b, 256); + set_underscore(dst_wrap_nil()); for (;;) { int c; DstParseResult res; @@ -120,6 +128,7 @@ static int repl() { dst_puts(dst_formatc("runtime error: %S\n", dst_to_string(ret))); } else { dst_puts(dst_formatc("%v\n", ret)); + set_underscore(ret); } } else { dst_puts(dst_formatc("compile error: %S\n", cres.error)); diff --git a/core/stl.c b/core/stl.c index 07aa20b9..54c6de7d 100644 --- a/core/stl.c +++ b/core/stl.c @@ -378,7 +378,7 @@ Dst dst_loadstl(int flags) { if (dst_checktype(ret, DST_TABLE)) { DstTable *v = dst_table(1); dst_table_put(v, dst_csymbolv("value"), ret); - dst_put(ret, dst_csymbolv("-env-"), dst_wrap_table(v)); + dst_put(ret, dst_csymbolv("_env"), dst_wrap_table(v)); } return ret; } diff --git a/dsttest/suite0.dst b/dsttest/suite0.dst index 699cc2a1..2c26b073 100644 --- a/dsttest/suite0.dst +++ b/dsttest/suite0.dst @@ -72,7 +72,7 @@ (assert (= 7 (| 3 4)) "bit or") (assert (= 0 (& 3 4)) "bit and") -# Set global variables to prevent some compiler optimizations that defeat point of the test +# Set global variables to prevent some possible compiler optimizations that defeat point of the test (var zero 0) (var one 1) (var two 2) @@ -84,6 +84,9 @@ (var f91 nil) (varset! f91 (fn [n] (if (> n 100) (- n 10) (f91 (f91 (+ n 11)))))) (assert (= 91 (f91 10)), "f91(10) = 91") +(assert (= 91 (f91 11)), "f91(11) = 91") +(assert (= 91 (f91 20)), "f91(20) = 91") +(assert (= 91 (f91 31)), "f91(31) = 91") (assert (= 91 (f91 100)), "f91(100) = 91") (assert (= 91 (f91 101)), "f91(101) = 91") (assert (= 92 (f91 102)), "f91(102) = 92")