From c80478a2cb25bf3c07ee42f03475e08ff8487d39 Mon Sep 17 00:00:00 2001 From: Felix Brendel Date: Sun, 6 Oct 2019 18:57:15 +0200 Subject: [PATCH] hashmaps can now be printed and keys deleted --- bin/pre.slime | 10 ++++++++++ bin/pre.slime.expanded | 28 ++++++++++++++++++++++++++++ src/built_ins.cpp | 6 ++++++ src/ftb | 2 +- src/io.cpp | 10 +++++++++- 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/bin/pre.slime b/bin/pre.slime index 76da091..300060f 100644 --- a/bin/pre.slime +++ b/bin/pre.slime @@ -7,6 +7,16 @@ (define-syntax (pe expr) `(print ',expr "evaluates to" ,expr)) +(define the-empty-stream ()) + +(define (stream-null? s) (if s t ())) + +(define-syntax (delay expr) + `(,lambda () ,expr)) + +(define (force promise) + (promise)) + (define-syntax (when condition . body) :doc "Special form for when multiple actions should be done if a condition is true. diff --git a/bin/pre.slime.expanded b/bin/pre.slime.expanded index 2cb2607..39cde8e 100644 --- a/bin/pre.slime.expanded +++ b/bin/pre.slime.expanded @@ -6,6 +6,34 @@ (define-syntax (pe expr) `(print ',expr "evaluates to" ,expr)) +(define the-empty-stream ()) + +(define (stream-null? s) (if s t ())) + +(define-syntax (delay expr) `(,lambda () ,expr)) + +(define (force promise) (promise)) + +(define (pair-stream oject expression) (pair object (delay expression))) + +(define stream-first first) + +(define (stream-rest stream) (force (rest stream))) + +(define (stream-ref s n) (if (= n 0) (stream-first s) (stream-ref (stream-rest s) (- n 1)))) + +(define (stream-filter pred stream) (cond ((stream-null? stream) the-empty-stream) ((pred (stream-first stream)) (pair-stream (stream-first stream) (stream-filter pred (stream-rest stream)))) (else (stream-filter pred (stream-rest stream))))) + +(define (stream-map proc s) (if (stream-null? s) the-empty-stream (pair-stream (proc (stream-first s)) (stream-map proc (stream-rest s))))) + +(define (stream-for-each proc s) (if (stream-null? s) 'done (begin (proc (stream-first s)) (stream-for-each proc (stream-rest s))))) + +(define (stream-enumerate-interval low high) (if (> low high) the-empty-stream (pair-stream low (stream-enumerate-interval (+ low 1) high)))) + +(define (prime? x) (define (prime-helper x k) (cond ((= x k) t) ((= (% x k) 0) ()) (else (prime-helper x (+ k 1))))) (cond ((= x 1) ()) ((= x 2) t) (else (prime-helper x 2)))) + +(define (a) (stream-first (stream-rest (stream-filter prime? (stream-enumerate-interval 10000 1020))))) + (define-syntax (when condition . body) :doc "Special form for when multiple actions should be done if a\ncondition is true.\n\n{{{example_start}}}\n(when (not ())\n (print "Hello ")\n (print "from ")\n (print "when!"))\n\n(when ()\n (print "Goodbye ")\n (print "World!"))\n{{{example_end}}}\n" (if (= (rest body) ()) `(if ,condition (unquote-splicing body) nil) `(if ,condition (begin (unquote-splicing body)) nil))) (define-syntax (unless condition . body) :doc "Special form for when multiple actions should be done if a\ncondition is false." (if (= (rest body) ()) `(if ,condition nil (unquote-splicing body)) `(if ,condition nil (begin (unquote-splicing body))))) diff --git a/src/built_ins.cpp b/src/built_ins.cpp index 9314d2e..b032d7b 100644 --- a/src/built_ins.cpp +++ b/src/built_ins.cpp @@ -848,6 +848,12 @@ proc load_built_ins_into_environment() -> void { hm_set(hm->value.hashMap, key, value); return Memory::nil; }; + define((hash-map-delete! hm key), "TODO") { + fetch(hm, key); + try assert_type(hm, Lisp_Object_Type::HashMap); + hm_delete_object(hm->value.hashMap, key); + return Memory::nil; + }; define((vector . args), "TODO") { fetch(args); Lisp_Object* ret; diff --git a/src/ftb b/src/ftb index 94ad64f..9c5512b 160000 --- a/src/ftb +++ b/src/ftb @@ -1 +1 @@ -Subproject commit 94ad64f6bb3a91247e8266217a5a0ab3a93c5d11 +Subproject commit 9c5512b1825ad838af7926587922f2083273a601 diff --git a/src/io.cpp b/src/io.cpp index b27615a..9fd7042 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -284,7 +284,15 @@ proc print(Lisp_Object* node, bool print_repr = false, FILE* file = stdout) -> v case (Lisp_Object_Type::Continuation): fputs("[continuation]", file); break; case (Lisp_Object_Type::CFunction): fputs("[C-function]", file); break; case (Lisp_Object_Type::Pointer): fputs("[pointer]", file); break; - case (Lisp_Object_Type::HashMap): fputs("[hashmap]", file); break; + case (Lisp_Object_Type::HashMap): { + for_lisp_obj_hash_map (node->value.hashMap) { + fputs(" ", file); + print(key, true, file); + fputs(" -> ", file); + print((Lisp_Object*)value, true, file); + fputs("\n", file); + } + } break; case (Lisp_Object_Type::String): { if (print_repr) { putc('\"', file);