1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 19:19:53 +00:00

Add underscore to repl

This commit is contained in:
bakpakin 2018-01-13 10:34:59 -05:00
parent 921f087c96
commit 94a4970053
3 changed files with 14 additions and 2 deletions

View File

@ -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 */ /* simple repl */
static int repl() { static int repl() {
DstBuffer b; DstBuffer b;
dst_buffer_init(&b, 256); dst_buffer_init(&b, 256);
set_underscore(dst_wrap_nil());
for (;;) { for (;;) {
int c; int c;
DstParseResult res; DstParseResult res;
@ -120,6 +128,7 @@ static int repl() {
dst_puts(dst_formatc("runtime error: %S\n", dst_to_string(ret))); dst_puts(dst_formatc("runtime error: %S\n", dst_to_string(ret)));
} else { } else {
dst_puts(dst_formatc("%v\n", ret)); dst_puts(dst_formatc("%v\n", ret));
set_underscore(ret);
} }
} else { } else {
dst_puts(dst_formatc("compile error: %S\n", cres.error)); dst_puts(dst_formatc("compile error: %S\n", cres.error));

View File

@ -378,7 +378,7 @@ Dst dst_loadstl(int flags) {
if (dst_checktype(ret, DST_TABLE)) { if (dst_checktype(ret, DST_TABLE)) {
DstTable *v = dst_table(1); DstTable *v = dst_table(1);
dst_table_put(v, dst_csymbolv("value"), ret); 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; return ret;
} }

View File

@ -72,7 +72,7 @@
(assert (= 7 (| 3 4)) "bit or") (assert (= 7 (| 3 4)) "bit or")
(assert (= 0 (& 3 4)) "bit and") (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 zero 0)
(var one 1) (var one 1)
(var two 2) (var two 2)
@ -84,6 +84,9 @@
(var f91 nil) (var f91 nil)
(varset! f91 (fn [n] (if (> n 100) (- n 10) (f91 (f91 (+ n 11)))))) (varset! f91 (fn [n] (if (> n 100) (- n 10) (f91 (f91 (+ n 11))))))
(assert (= 91 (f91 10)), "f91(10) = 91") (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 100)), "f91(100) = 91")
(assert (= 91 (f91 101)), "f91(101) = 91") (assert (= 91 (f91 101)), "f91(101) = 91")
(assert (= 92 (f91 102)), "f91(102) = 92") (assert (= 92 (f91 102)), "f91(102) = 92")