ADD: assert core function.
parent
314fbc0bd6
commit
20b5e60557
21
lib/core.cpp
21
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<assertion_error>(grino::LOG_ASSERT,
|
||||
value->loc(),
|
||||
"assertion failed");
|
||||
}
|
||||
}
|
||||
|
||||
return grino::Value::make_bool(args.front()->loc(), true);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -93,7 +93,7 @@ namespace grino
|
|||
}
|
||||
else
|
||||
{
|
||||
return Loc {"???", 1};
|
||||
return Loc {"???", 0};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -2,33 +2,34 @@
|
|||
|
||||
namespace grino
|
||||
{
|
||||
/*static*/ std::shared_ptr<Value> Value::make_nil()
|
||||
/*static*/ std::shared_ptr<Value> Value::make_nil(Loc const& loc)
|
||||
{
|
||||
auto value = std::make_shared<Value>();
|
||||
auto value = std::make_shared<Value>(loc);
|
||||
value->m_type = TYPE_NIL;
|
||||
return value;
|
||||
}
|
||||
|
||||
/*static*/ std::shared_ptr<Value> Value::make_bool(bool val)
|
||||
/*static*/ std::shared_ptr<Value> Value::make_bool(Loc const& loc, bool val)
|
||||
{
|
||||
auto value = std::make_shared<Value>();
|
||||
auto value = std::make_shared<Value>(loc);
|
||||
value->m_type = TYPE_BOOL;
|
||||
value->m_bool_val = val;
|
||||
return value;
|
||||
}
|
||||
|
||||
/*static*/
|
||||
std::shared_ptr<Value> Value::make_native_function(native_t val)
|
||||
std::shared_ptr<Value> Value::make_native_function(Loc const& loc,
|
||||
native_t val)
|
||||
{
|
||||
auto value = std::make_shared<Value>();
|
||||
auto value = std::make_shared<Value>(loc);
|
||||
value->m_type = TYPE_FUNCTION;
|
||||
value->m_function_val = std::make_shared<Function>(val);
|
||||
return value;
|
||||
}
|
||||
|
||||
/*static*/ std::shared_ptr<Value> Value::make_ref(size_t val)
|
||||
/*static*/ std::shared_ptr<Value> Value::make_ref(Loc const& loc, size_t val)
|
||||
{
|
||||
auto value = std::make_shared<Value>();
|
||||
auto value = std::make_shared<Value>(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 }
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Value> make_nil();
|
||||
static std::shared_ptr<Value> make_bool(bool val);
|
||||
static std::shared_ptr<Value> make_native_function(native_t val);
|
||||
static std::shared_ptr<Value> make_ref(size_t val);
|
||||
static std::shared_ptr<Value> make_nil(Loc const& loc);
|
||||
static std::shared_ptr<Value> make_bool(Loc const& loc, bool val);
|
||||
static std::shared_ptr<Value> make_native_function(Loc const& loc,
|
||||
native_t val);
|
||||
static std::shared_ptr<Value> 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<Function> 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<bool> m_bool_val;
|
||||
std::shared_ptr<Function> m_function_val;
|
||||
|
|
Loading…
Reference in New Issue