From 9a8f4096eafb67df0199c1f5822259fae11acde1 Mon Sep 17 00:00:00 2001 From: bog Date: Sun, 24 Sep 2023 15:54:57 +0200 Subject: [PATCH] ADD: ref! to mutate array elements. --- examples/array.fk | 12 +++++++++++- libstd/fun.cpp | 14 ++++++++++++++ libstd/fun.hpp | 1 + libstd/lib.cpp | 1 + libstd/macro.cpp | 2 +- src/Array.cpp | 6 ++++++ src/Array.hpp | 1 + 7 files changed, 35 insertions(+), 2 deletions(-) diff --git a/examples/array.fk b/examples/array.fk index e4c65f6..cb555bf 100644 --- a/examples/array.fk +++ b/examples/array.fk @@ -17,4 +17,14 @@ (assert= 4 (second (produce))) ($ arr [2 'bim' produce false]) -(assert= 6 (ref ((ref arr 2)) 2)) \ No newline at end of file +(assert= 6 (ref ((ref arr 2)) 2)) + +($ hello [2 4 6]) +(assert= 4 (ref hello 1)) +(ref! hello 1 12) +(assert= 12 (ref hello 1)) + +($ hello2 [[5 7][9 2]]) +(assert= 9 (ref hello2 1 0)) +(ref! (ref hello2 1) 0 34) +(assert= 34 (ref hello2 1 0)) \ No newline at end of file diff --git a/libstd/fun.cpp b/libstd/fun.cpp index 1565b00..c86c8c9 100644 --- a/libstd/fun.cpp +++ b/libstd/fun.cpp @@ -339,6 +339,20 @@ namespace fkstd return val->at(std::get(args.back()->value())); } + STDRET array_set(Loc, Module& mod, STDARGS args) + { + auto ref_val = args[0]->value(); + size_t ref = std::get(ref_val); + + int index = std::get(args[1]->value()); + + std::shared_ptr val = + std::get>(mod.vm()->load_global(ref)); + + val->set(index, args[2]); + return args[2]; + } + STDRET string_cat(Loc loc, Module&, STDARGS args) { if (args.empty()) diff --git a/libstd/fun.hpp b/libstd/fun.hpp index fad8a65..b7be0eb 100644 --- a/libstd/fun.hpp +++ b/libstd/fun.hpp @@ -30,6 +30,7 @@ namespace fkstd STDRET bool_not(Loc loc, Module& mod, STDARGS args); STDRET array_ref(Loc loc, Module& mod, STDARGS args); + STDRET array_set(Loc loc, Module& mod, STDARGS args); STDRET string_cat(Loc loc, Module& mod, STDARGS args); STDRET string_dup(Loc loc, Module& mod, STDARGS args); diff --git a/libstd/lib.cpp b/libstd/lib.cpp index 80bfbfd..00a2115 100644 --- a/libstd/lib.cpp +++ b/libstd/lib.cpp @@ -29,6 +29,7 @@ extern "C" void lib(Module& mod) mod.register_function("not", fkstd::bool_not); mod.register_function("ref", fkstd::array_ref); + mod.register_function("ref!", fkstd::array_set); mod.register_function("cat", fkstd::string_cat); mod.register_function("dup", fkstd::string_dup); mod.register_function("char-at", fkstd::char_at); diff --git a/libstd/macro.cpp b/libstd/macro.cpp index 1b71ee8..3935e56 100644 --- a/libstd/macro.cpp +++ b/libstd/macro.cpp @@ -9,7 +9,7 @@ namespace fkstd { auto ident = node->child(1)->repr(); compiler.compile_prog(node->child(2), program); - std::cout << "push " << node->child(2)->string() << std::endl; + auto entry = compiler.sym()->find(ident); if (entry) diff --git a/src/Array.cpp b/src/Array.cpp index 6f6cd03..148c987 100644 --- a/src/Array.cpp +++ b/src/Array.cpp @@ -22,6 +22,12 @@ namespace fk return m_data.at(index); } + void Array::set(size_t index, std::shared_ptr value) + { + assert(index < size()); + m_data[index] = value; + } + std::shared_ptr Array::pop() { assert(size() > 0); diff --git a/src/Array.hpp b/src/Array.hpp index 8a65797..f045bcf 100644 --- a/src/Array.hpp +++ b/src/Array.hpp @@ -17,6 +17,7 @@ namespace fk void push(std::shared_ptr constant); std::shared_ptr at(size_t index) const; + void set(size_t index, std::shared_ptr value); std::shared_ptr pop(); std::string string() const;