From 20b5e60557a25ef48888579593acb62efd51de65 Mon Sep 17 00:00:00 2001 From: bog Date: Mon, 11 Sep 2023 13:46:41 +0200 Subject: [PATCH] ADD: assert core function. --- lib/core.cpp | 21 ++++++++++++++++++++- src/Compiler.cpp | 2 +- src/Loader.cpp | 6 ++++-- src/Logger.hpp | 6 ++++-- src/Parser.cpp | 2 +- src/VM.cpp | 2 +- src/Value.cpp | 22 ++++++++++++++-------- src/Value.hpp | 15 ++++++++++----- 8 files changed, 55 insertions(+), 21 deletions(-) diff --git a/lib/core.cpp b/lib/core.cpp index 374fcb4..930b47e 100644 --- a/lib/core.cpp +++ b/lib/core.cpp @@ -1,4 +1,7 @@ #include "../src/Loader.hpp" +#include "src/Logger.hpp" + +GRINO_ERROR(assertion_error); extern "C" void lib(grino::Loader& loader) { @@ -14,6 +17,22 @@ extern "C" void lib(grino::Loader& loader) std::cout << ss.str() << std::endl; - return grino::Value::make_nil(); + return grino::Value::make_nil(args.back()->loc()); + }); + + loader.add_native("assert", [](auto args){ + + for (auto value: args) + { + if (!value->as_bool()) + { + grino::Logger logger; + logger.log(grino::LOG_ASSERT, + value->loc(), + "assertion failed"); + } + } + + return grino::Value::make_bool(args.front()->loc(), true); }); } diff --git a/src/Compiler.cpp b/src/Compiler.cpp index e5f414c..58cf75b 100644 --- a/src/Compiler.cpp +++ b/src/Compiler.cpp @@ -40,7 +40,7 @@ namespace grino case NODE_BOOL: { std::string repr = node->repr(); - auto value = Value::make_bool(repr == "true"); + auto value = Value::make_bool(node->loc(), repr == "true"); program.push_instr(OPCODE_LOAD_CONST, program.push_constant(value)); diff --git a/src/Loader.cpp b/src/Loader.cpp index 1bb1555..157f764 100644 --- a/src/Loader.cpp +++ b/src/Loader.cpp @@ -39,7 +39,9 @@ namespace grino void Loader::add_native(std::string const& name, native_t native) { size_t addr = m_vm.heap_size(); - m_vm.set_heap(addr, grino::Value::make_native_function(native)); - m_sym_table.declare_object(grino::Loc {"???", 1}, name, addr); + grino::Loc loc {"???", 0}; + + m_vm.set_heap(addr, grino::Value::make_native_function(loc, native)); + m_sym_table.declare_object(loc, name, addr); } } diff --git a/src/Logger.hpp b/src/Logger.hpp index d5b4eed..da8d70c 100644 --- a/src/Logger.hpp +++ b/src/Logger.hpp @@ -5,8 +5,10 @@ #include "src/mutils.hpp" #include "Loc.hpp" -#define LOG_TYPE(G) \ - G(LOG_ERROR) +#define LOG_TYPE(G) \ + G(LOG_ERROR), \ + G(LOG_ASSERT), + namespace grino { diff --git a/src/Parser.cpp b/src/Parser.cpp index 33600a4..d088c99 100644 --- a/src/Parser.cpp +++ b/src/Parser.cpp @@ -93,7 +93,7 @@ namespace grino } else { - return Loc {"???", 1}; + return Loc {"???", 0}; } } diff --git a/src/VM.cpp b/src/VM.cpp index ebb054f..7819cce 100644 --- a/src/VM.cpp +++ b/src/VM.cpp @@ -50,7 +50,7 @@ namespace grino case OPCODE_LOAD_OBJ: { size_t addr = *instr.param; - auto ref = Value::make_ref(addr); + auto ref = Value::make_ref(Loc {"???", 0}, addr); push(program.push_constant(ref)); m_pc++; } break; diff --git a/src/Value.cpp b/src/Value.cpp index fa34411..9259bdc 100644 --- a/src/Value.cpp +++ b/src/Value.cpp @@ -2,33 +2,34 @@ namespace grino { - /*static*/ std::shared_ptr Value::make_nil() + /*static*/ std::shared_ptr Value::make_nil(Loc const& loc) { - auto value = std::make_shared(); + auto value = std::make_shared(loc); value->m_type = TYPE_NIL; return value; } - /*static*/ std::shared_ptr Value::make_bool(bool val) + /*static*/ std::shared_ptr Value::make_bool(Loc const& loc, bool val) { - auto value = std::make_shared(); + auto value = std::make_shared(loc); value->m_type = TYPE_BOOL; value->m_bool_val = val; return value; } /*static*/ - std::shared_ptr Value::make_native_function(native_t val) + std::shared_ptr Value::make_native_function(Loc const& loc, + native_t val) { - auto value = std::make_shared(); + auto value = std::make_shared(loc); value->m_type = TYPE_FUNCTION; value->m_function_val = std::make_shared(val); return value; } - /*static*/ std::shared_ptr Value::make_ref(size_t val) + /*static*/ std::shared_ptr Value::make_ref(Loc const& loc, size_t val) { - auto value = std::make_shared(); + auto value = std::make_shared(loc); value->m_type = TYPE_REF; value->m_ref_val = val; return value; @@ -65,4 +66,9 @@ namespace grino abort(); } } + + /*explicit*/ Value::Value(Loc const& loc) + : m_loc { loc } + { + } } diff --git a/src/Value.hpp b/src/Value.hpp index 87bae81..7b212a7 100644 --- a/src/Value.hpp +++ b/src/Value.hpp @@ -4,20 +4,24 @@ #include "commons.hpp" #include "types.hpp" #include "Function.hpp" +#include "Loc.hpp" namespace grino { class Value { public: - static std::shared_ptr make_nil(); - static std::shared_ptr make_bool(bool val); - static std::shared_ptr make_native_function(native_t val); - static std::shared_ptr make_ref(size_t val); + static std::shared_ptr make_nil(Loc const& loc); + static std::shared_ptr make_bool(Loc const& loc, bool val); + static std::shared_ptr make_native_function(Loc const& loc, + native_t val); + static std::shared_ptr make_ref(Loc const& loc, + size_t val); - explicit Value() = default; + explicit Value(Loc const& loc); virtual ~Value() = default; + Loc loc() const { return m_loc; } TypeType type() const { return m_type; } bool as_bool() const { return *m_bool_val; } std::shared_ptr as_function() const { return m_function_val; } @@ -27,6 +31,7 @@ namespace grino bool equals(Value const& other) const; private: + Loc m_loc; TypeType m_type = TYPE_NIL; std::optional m_bool_val; std::shared_ptr m_function_val;