From 9816b1b1ad3cce16317ba8d63afa08749fe08e47 Mon Sep 17 00:00:00 2001 From: Chris Hinsley Date: Thu, 5 Oct 2017 13:45:34 +0100 Subject: [PATCH] hash_map now have get and set methods --- class/hash_map/hash_map.inc | 2 ++ class/hash_map/hash_map.vp | 70 +++++++++++++++++++++++++++++++++++++ class/lisp/env_get.vp | 35 ------------------- class/lisp/env_set.vp | 39 --------------------- class/lisp/func_repl.vp | 20 +++++------ class/lisp/func_set.vp | 8 +++-- class/lisp/func_setq.vp | 8 +++-- class/lisp/lisp.inc | 2 -- class/lisp/make.inc | 2 -- class/lisp/repl_eval.vp | 7 +++- 10 files changed, 100 insertions(+), 93 deletions(-) delete mode 100644 class/lisp/env_get.vp delete mode 100644 class/lisp/env_set.vp diff --git a/class/hash_map/hash_map.inc b/class/hash_map/hash_map.inc index 05204267e..c06699460 100644 --- a/class/hash_map/hash_map.inc +++ b/class/hash_map/hash_map.inc @@ -8,6 +8,8 @@ (def-method 'copy 'class/hash_map/copy) (def-method 'insert 'class/hash_map/insert 'static '(r0 r1 r2) '(r0 r1 r2)) (def-method 'search 'class/hash_map/search 'static '(r0 r1) '(r0 r1 r2)) +(def-method 'set 'class/hash_map/set 'static '(r0 r1 r2) '(r0 r1)) +(def-method 'get 'class/hash_map/get 'static '(r0 r1) '(r0 r1)) (def-method 'get_parent 'class/hash_map/get_parent 'static '(r0) '(r0 r1)) (def-method 'set_parent 'class/hash_map/set_parent 'static '(r0 r1) '(r0)) diff --git a/class/hash_map/hash_map.vp b/class/hash_map/hash_map.vp index d994fc80c..156e0d446 100644 --- a/class/hash_map/hash_map.vp +++ b/class/hash_map/hash_map.vp @@ -206,6 +206,8 @@ ;r0 = hash_map object ;r1 = 0, else iterator ;r2 = bucket vector + ;trashes + ;all but r0 (def-struct 'local) (ptr 'this) @@ -228,3 +230,71 @@ (vp-ret) (def-func-end) + +(def-func 'class/hash_map/set) + ;inputs + ;r0 = hash_map object + ;r1 = key object + ;r2 = value object + ;outputs + ;r0 = hash_map object + ;r1 = 0 if not found, else value object + ;trashes + ;all but r0 + + (def-struct 'local) + (ptr 'this) + (ptr 'key) + (ptr 'value) + (def-struct-end) + + (vp-alloc local_size) + (f-entry 'hash_map 'set '(r0 r1 r2)) + + (assign '(r2) '((rsp local_value))) + (f-call 'hash_map 'search '(r0 r1) '(r0 r1 _)) + (vpif '(r1 != 0)) + (assign '(r0) '((rsp local_this))) + (vp-cpy-rr r1 r2) + (f-call 'ref 'ref '((rsp local_value)) '(r0)) + (f-call 'ref 'ref '(r0) '(r0)) + (f-call 'pair 'set_second '((r2 0) r0)) + (assign '((rsp local_this) (rsp local_value)) '(r0 r1)) + (endif) + + (f-exit 'hash_map 'set '(r0 r1)) + (vp-free local_size) + (vp-ret) + +(def-func-end) + +(def-func 'class/hash_map/get) + ;inputs + ;r0 = hash_map object + ;r1 = key object + ;outputs + ;r0 = hash_map object + ;r1 = 0 if not found, else value object + ;trashes + ;all but r0 + + (def-struct 'local) + (ptr 'this) + (ptr 'key) + (def-struct-end) + + (vp-alloc local_size) + (f-entry 'hash_map 'get '(r0 r1)) + + (f-call 'hash_map 'search '(r0 r1) '(r0 r1 _)) + (vpif '(r1 != 0)) + (assign '(r0) '((rsp local_this))) + (f-call 'pair 'ref_second '((r1 0)) '(_ r1)) + (assign '((rsp local_this)) '(r0)) + (endif) + + (f-exit 'hash_map 'get '(r0 r1)) + (vp-free local_size) + (vp-ret) + +(def-func-end) diff --git a/class/lisp/env_get.vp b/class/lisp/env_get.vp deleted file mode 100644 index d94585cae..000000000 --- a/class/lisp/env_get.vp +++ /dev/null @@ -1,35 +0,0 @@ -(import 'sys/func.inc) -(import 'class/pair/pair.inc) -(import 'class/hash_map/hash_map.inc) -(import 'class/lisp/lisp.inc) - -(def-func 'class/lisp/env_get) - ;inputs - ;r0 = lisp object - ;r1 = symbol - ;outputs - ;r0 = lisp object - ;r1 = value - - (ptr 'this 'symbol) - (pptr 'iter) - - (push-scope) - (f-entry 'lisp 'env_get {this, symbol}) - - (f-call 'hash_map 'search {this->lisp_environment, symbol} {_, iter, _}) - (errorifnot {iter} 'error) - (f-call 'pair 'ref_second {*iter} {_, symbol}) - -(vp-label 'exit) - (f-exit 'lisp 'env_get {this, symbol}) - (return) - -(errorcases -(vp-label 'error) - (f-call 'lisp 'repl_error {this, "", error_msg_symbol_not_bound, symbol} {_, symbol}) - (goto 'exit)) - - (pop-scope-syms) - -(def-func-end) diff --git a/class/lisp/env_set.vp b/class/lisp/env_set.vp deleted file mode 100644 index 363edbfd4..000000000 --- a/class/lisp/env_set.vp +++ /dev/null @@ -1,39 +0,0 @@ -(import 'sys/func.inc) -(import 'class/pair/pair.inc) -(import 'class/hash_map/hash_map.inc) -(import 'class/lisp/lisp.inc) - -(def-func 'class/lisp/env_set) - ;inputs - ;r0 = lisp object - ;r1 = environment object - ;r2 = symbol - ;r3 = value - ;outputs - ;r0 = lisp object - ;r1 = value - - (ptr 'this 'env 'symbol 'value) - (pptr 'iter) - - (push-scope) - (f-entry 'lisp 'env_set {this, env, symbol, value}) - - (f-call 'hash_map 'search {env, symbol} {_, iter, _}) - (errorifnot {iter} 'error) - (f-call 'ref 'ref {value}) - (f-call 'ref 'ref {value}) - (f-call 'pair 'set_second {*iter, value}) - -(vp-label 'exit) - (f-exit 'lisp 'env_set {this, value}) - (return) - -(errorcases -(vp-label 'error) - (f-call 'lisp 'repl_error {this, "env_set", error_msg_symbol_not_bound, symbol} {_, value}) - (goto 'exit)) - - (pop-scope-syms) - -(def-func-end) diff --git a/class/lisp/func_repl.vp b/class/lisp/func_repl.vp index 4a1069848..24133053a 100644 --- a/class/lisp/func_repl.vp +++ b/class/lisp/func_repl.vp @@ -35,15 +35,15 @@ (errorifnot {old_file} 'error3)) ;push old file and line, set to this stream info - (f-call 'lisp 'env_get {this, this->lisp_sym_stream_name} {_, old_file}) - (f-call 'lisp 'env_get {this, this->lisp_sym_stream_line} {_, old_line}) - (f-call 'lisp 'env_set {this, this->lisp_environment, this->lisp_sym_stream_name, value} {_, value}) - (f-call 'ref 'deref {value}) + (f-call 'hash_map 'get {this->lisp_environment, this->lisp_sym_stream_name} {_, old_file}) + (f-call 'hash_map 'get {this->lisp_environment, this->lisp_sym_stream_line} {_, old_line}) + (f-call 'hash_map 'set {this->lisp_environment, this->lisp_sym_stream_name, value} {_, value}) + (f-call 'ref 'deref_if {value}) (f-call 'boxed_long 'create {} {value}) (f-call 'boxed_long 'set_value {value, 1}) - (f-call 'lisp 'env_set {this, this->lisp_environment, this->lisp_sym_stream_line, value} {_, ast}) + (f-call 'hash_map 'set {this->lisp_environment, this->lisp_sym_stream_line, value} {_, ast}) (f-call 'ref 'deref {value}) - (f-call 'ref 'deref {ast}) + (f-call 'ref 'deref_if {ast}) (f-call 'stream 'read_char {stream} {_, char}) (loop-start) @@ -71,12 +71,12 @@ (loop-end) ;pop old file and line info - (f-call 'lisp 'env_set {this, this->lisp_environment, this->lisp_sym_stream_name, old_file} {_, value}) + (f-call 'hash_map 'set {this->lisp_environment, this->lisp_sym_stream_name, old_file} {_, value}) (f-call 'ref 'deref {old_file}) - (f-call 'ref 'deref {value}) - (f-call 'lisp 'env_set {this, this->lisp_environment, this->lisp_sym_stream_line, old_line} {_, value}) + (f-call 'ref 'deref_if {value}) + (f-call 'hash_map 'set {this->lisp_environment, this->lisp_sym_stream_line, old_line} {_, value}) (f-call 'ref 'deref {old_line}) - (f-call 'ref 'deref {value}) + (f-call 'ref 'deref_if {value}) (f-call 'ref 'ref {this->lisp_sym_t} {value}) diff --git a/class/lisp/func_set.vp b/class/lisp/func_set.vp index 399d1e270..eff925251 100644 --- a/class/lisp/func_set.vp +++ b/class/lisp/func_set.vp @@ -1,4 +1,5 @@ (import 'sys/func.inc) +(import 'class/hash_map/hash_map.inc) (import 'class/lisp/lisp.inc) (def-func 'class/lisp/func_set) @@ -31,9 +32,9 @@ (f-call 'lisp 'repl_eval {this, iter_begin[ptr_size * 1]} {_, val}) (errorif (cat {val->obj_vtable == @} (f-path 'class 'error)) 'exit) (assign {val} {eval}) - (f-call 'lisp 'env_set {this, env, var, eval} {_, val}) + (f-call 'hash_map 'set {env, var, eval} {_, val}) (f-call 'ref 'deref {eval}) - (errorif (cat {val->obj_vtable == @} (f-path 'class 'error)) 'exit) + (errorifnot {val} 'error4) (assign {&iter_begin[ptr_size * 2]} {iter_begin}) (loop-until {iter_begin == iter_end}) @@ -50,6 +51,9 @@ (goto 'exit) (vp-label 'error3) (f-call 'lisp 'repl_error {this, "(set env var val ...)", error_msg_not_a_symbol, args} {_, val}) + (goto 'exit) +(vp-label 'error4) + (f-call 'lisp 'repl_error {this, "(set env var val ...)", error_msg_symbol_not_bound, args} {_, val}) (goto 'exit)) (pop-scope-syms) diff --git a/class/lisp/func_setq.vp b/class/lisp/func_setq.vp index 772f1e1a9..c5aef4c5a 100644 --- a/class/lisp/func_setq.vp +++ b/class/lisp/func_setq.vp @@ -1,4 +1,5 @@ (import 'sys/func.inc) +(import 'class/hash_map/hash_map.inc) (import 'class/lisp/lisp.inc) (def-func 'class/lisp/func_setq) @@ -28,9 +29,9 @@ (f-call 'lisp 'repl_eval {this, iter_begin[ptr_size * 1]} {_, val}) (errorif (cat {val->obj_vtable == @} (f-path 'class 'error)) 'exit) (assign {val} {eval}) - (f-call 'lisp 'env_set {this, this->lisp_environment, var, eval} {_, val}) + (f-call 'hash_map 'set {this->lisp_environment, var, eval} {_, val}) (f-call 'ref 'deref {eval}) - (errorif (cat {val->obj_vtable == @} (f-path 'class 'error)) 'exit) + (errorifnot {val} 'error3) (assign {&iter_begin[ptr_size * 2]} {iter_begin}) (loop-until {iter_begin == iter_end}) @@ -44,6 +45,9 @@ (goto 'exit) (vp-label 'error2) (f-call 'lisp 'repl_error {this, "(setq var val ...)", error_msg_not_a_symbol, args} {_, val}) + (goto 'exit) +(vp-label 'error3) + (f-call 'lisp 'repl_error {this, "(setq var val ...)", error_msg_symbol_not_bound, args} {_, val}) (goto 'exit)) (pop-scope-syms) diff --git a/class/lisp/lisp.inc b/class/lisp/lisp.inc index b28783eb9..59de90ca5 100644 --- a/class/lisp/lisp.inc +++ b/class/lisp/lisp.inc @@ -11,8 +11,6 @@ (def-method 'env_push 'class/lisp/env_push 'static '(r0) '(r0)) (def-method 'env_pop 'class/lisp/env_pop 'static '(r0) '(r0)) -(def-method 'env_set 'class/lisp/env_set 'static '(r0 r1 r2 r3) '(r0 r1)) -(def-method 'env_get 'class/lisp/env_get 'static '(r0 r1) '(r0 r1)) (def-method 'env_bind 'class/lisp/env_bind 'static '(r0 r1 r2) '(r0 r1)) (def-method 'env_args_set 'class/lisp/env_args_set 'static '(r0 r1 r2 r3) '(r0)) (def-method 'env_args_type 'class/lisp/env_args_type 'static '(r0 r1 r2 r3) '(r0 r1)) diff --git a/class/lisp/make.inc b/class/lisp/make.inc index a5bf5bc5b..090a82f7b 100644 --- a/class/lisp/make.inc +++ b/class/lisp/make.inc @@ -1,10 +1,8 @@ (import 'class/lisp/built_in_func.vp) (import 'class/lisp/deinit.vp) (import 'class/lisp/env_bind.vp) -(import 'class/lisp/env_get.vp) (import 'class/lisp/env_pop.vp) (import 'class/lisp/env_push.vp) -(import 'class/lisp/env_set.vp) (import 'class/lisp/env_args_set.vp) (import 'class/lisp/env_args_type.vp) (import 'class/lisp/func_add.vp) diff --git a/class/lisp/repl_eval.vp b/class/lisp/repl_eval.vp index 54185977d..2c22f6219 100644 --- a/class/lisp/repl_eval.vp +++ b/class/lisp/repl_eval.vp @@ -1,6 +1,7 @@ (import 'sys/func.inc) (import 'class/symbol/symbol.inc) (import 'class/boxed_ptr/boxed_ptr.inc) +(import 'class/hash_map/hash_map.inc) (import 'class/lisp/lisp.inc) (def-func 'class/lisp/repl_eval) @@ -21,7 +22,8 @@ (switch) (case (cat {func == @} (f-path 'class 'symbol))) ;eval to symbol value - (f-call 'lisp 'env_get {this, form} {_, value}) + (f-call 'hash_map 'get {this->lisp_environment, form} {_, value}) + (errorifnot {value} 'error2) (break) (case (cat {func == @} (f-path 'class 'vector))) ;apply function, eval args if needed @@ -65,6 +67,9 @@ (errorcases (vp-label 'error1) (f-call 'lisp 'repl_error {this, "(lambda vars body)", error_msg_not_a_lambda, form} {_, value}) + (goto 'exit) +(vp-label 'error2) + (f-call 'lisp 'repl_error {this, "(eval sym)", error_msg_symbol_not_bound, form} {_, value}) (goto 'exit)) (pop-scope-syms)