From a08f72259801d95bf83843a7c8666cfc5b1a38bd Mon Sep 17 00:00:00 2001 From: bog Date: Sun, 24 Sep 2023 12:17:42 +0200 Subject: [PATCH] REF: adding ref function for arrays deref. --- examples/array.fk | 12 ++++++------ libstd/fun.cpp | 25 +++++++++++++++++++++++++ libstd/fun.hpp | 2 ++ libstd/lib.cpp | 1 + src/Compiler.cpp | 13 ++++--------- src/VM.cpp | 25 ------------------------- src/VM.hpp | 2 ++ 7 files changed, 40 insertions(+), 40 deletions(-) diff --git a/examples/array.fk b/examples/array.fk index 193eae0..e4c65f6 100644 --- a/examples/array.fk +++ b/examples/array.fk @@ -1,13 +1,13 @@ -(assert= 8 ([2 6 2 8 9] 3)) -(assert= 9 ([[3 6] [9 2]] 1 0)) +(assert= 8 (ref [2 6 2 8 9] 3)) +(assert= 9 (ref [[3 6] [9 2]] 1 0)) ($ a ['a' [[2 true] 2.3]]) -(assert= 'a' (a 0)) -(assert= 2 (a 1 0 0)) +(assert= 'a' (ref a 0)) +(assert= 2 (ref a 1 0 0)) ($ (second arr) - (arr 1)) + (ref arr 1)) (assert= 6 (second [2 6])) @@ -17,4 +17,4 @@ (assert= 4 (second (produce))) ($ arr [2 'bim' produce false]) -(assert= 6 (((arr 2)) 2)) \ No newline at end of file +(assert= 6 (ref ((ref arr 2)) 2)) \ No newline at end of file diff --git a/libstd/fun.cpp b/libstd/fun.cpp index 8734cb9..756fe6f 100644 --- a/libstd/fun.cpp +++ b/libstd/fun.cpp @@ -195,4 +195,29 @@ namespace fkstd { NUM_BINOP(!=); } + + STDRET array_ref(Loc loc, Module& mod, STDARGS args) + { + auto ref_val = args[0]->value(); + size_t ref = std::get(ref_val); + + std::shared_ptr val = + std::get>(mod.vm()->load_global(ref)); + + for (size_t i=1; i(args[i]->value()); + + auto ref_val = val->at(k)->value(); + size_t ref = std::get(ref_val); + + auto arr = + std::get>(mod.vm()->load_global(ref)); + + val = arr; + + } + + return val->at(std::get(args.back()->value())); + } } diff --git a/libstd/fun.hpp b/libstd/fun.hpp index 9ff3b7d..433f1d1 100644 --- a/libstd/fun.hpp +++ b/libstd/fun.hpp @@ -20,6 +20,8 @@ namespace fkstd STDRET ge(Loc loc, Module& mod, STDARGS args); STDRET eq(Loc loc, Module& mod, STDARGS args); STDRET ne(Loc loc, Module& mod, STDARGS args); + + STDRET array_ref(Loc loc, Module& mod, STDARGS args); } #endif diff --git a/libstd/lib.cpp b/libstd/lib.cpp index 1a42902..7c91963 100644 --- a/libstd/lib.cpp +++ b/libstd/lib.cpp @@ -18,6 +18,7 @@ extern "C" void lib(Module& mod) mod.register_function(">=", fkstd::ge); mod.register_function("=", fkstd::eq); mod.register_function("<>", fkstd::ne); + mod.register_function("ref", fkstd::array_ref); mod.register_macro("!", fkstd::set_addr); mod.register_macro("assert-static-fail", fkstd::assert_static_fail); diff --git a/src/Compiler.cpp b/src/Compiler.cpp index 9d40a7b..55baf2d 100644 --- a/src/Compiler.cpp +++ b/src/Compiler.cpp @@ -218,12 +218,7 @@ namespace fk compile_prog(node->child(i), prog); } - if (node->child(0)->type() == NODE_ARRAY) - { - prog->add(OP_CALL_REF, node->size() - 1); - break; - } - else if (node->child(0)->type() == NODE_LAMBDA + if (node->child(0)->type() == NODE_LAMBDA || node->child(0)->type() == NODE_CALL || node->child(0)->type() == NODE_NS) { @@ -236,10 +231,9 @@ namespace fk entry && entry->is_global() == false) { if (entry->node() - && entry->node()->child(1)->type() != NODE_ARRAY - && entry->node()->type() != NODE_ARRAY) + && entry->node() + && entry->node()->type() == NODE_LAMBDA) { - std::cout << "-> " << entry->node()->string() << std::endl; size_t arity = entry->node()->child(0)->size(); if (arity != node->size() - 1) { @@ -264,6 +258,7 @@ namespace fk break; } } + std::cerr << "cannot call node '" << node->string() << "'" << std::endl; abort(); diff --git a/src/VM.cpp b/src/VM.cpp index 3f19fc6..29dc1fe 100644 --- a/src/VM.cpp +++ b/src/VM.cpp @@ -310,31 +310,6 @@ namespace fk m_pc++; break; } - else if (std::get_if>(&val)) - { - std::shared_ptr val = - std::get>(load_global(ref)); - - for (size_t i=0; i(args[i]->value()); - auto ref_val = val->at(k)->value(); - - size_t ref = std::get(ref_val); - - auto arr = - std::get>(load_global(ref)); - - val = arr; - - } - - push(frame().program->add - (val->at(std::get(args.back()->value())))); - - m_pc++; - break; - } auto lambda = std::get>(load_global(ref)); diff --git a/src/VM.hpp b/src/VM.hpp index 640cf03..39f186a 100644 --- a/src/VM.hpp +++ b/src/VM.hpp @@ -23,6 +23,8 @@ namespace fk }; class Module; + FK_ERROR(execution_error); + class VM { public: