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/Loader.hpp"
|
||||||
|
#include "src/Logger.hpp"
|
||||||
|
|
||||||
|
GRINO_ERROR(assertion_error);
|
||||||
|
|
||||||
extern "C" void lib(grino::Loader& loader)
|
extern "C" void lib(grino::Loader& loader)
|
||||||
{
|
{
|
||||||
|
@ -14,6 +17,22 @@ extern "C" void lib(grino::Loader& loader)
|
||||||
|
|
||||||
std::cout << ss.str() << std::endl;
|
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: {
|
case NODE_BOOL: {
|
||||||
std::string repr = node->repr();
|
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_instr(OPCODE_LOAD_CONST,
|
||||||
program.push_constant(value));
|
program.push_constant(value));
|
||||||
|
|
|
@ -39,7 +39,9 @@ namespace grino
|
||||||
void Loader::add_native(std::string const& name, native_t native)
|
void Loader::add_native(std::string const& name, native_t native)
|
||||||
{
|
{
|
||||||
size_t addr = m_vm.heap_size();
|
size_t addr = m_vm.heap_size();
|
||||||
m_vm.set_heap(addr, grino::Value::make_native_function(native));
|
grino::Loc loc {"???", 0};
|
||||||
m_sym_table.declare_object(grino::Loc {"???", 1}, name, addr);
|
|
||||||
|
m_vm.set_heap(addr, grino::Value::make_native_function(loc, native));
|
||||||
|
m_sym_table.declare_object(loc, name, addr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,9 @@
|
||||||
#include "Loc.hpp"
|
#include "Loc.hpp"
|
||||||
|
|
||||||
#define LOG_TYPE(G) \
|
#define LOG_TYPE(G) \
|
||||||
G(LOG_ERROR)
|
G(LOG_ERROR), \
|
||||||
|
G(LOG_ASSERT),
|
||||||
|
|
||||||
|
|
||||||
namespace grino
|
namespace grino
|
||||||
{
|
{
|
||||||
|
|
|
@ -93,7 +93,7 @@ namespace grino
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return Loc {"???", 1};
|
return Loc {"???", 0};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,7 +50,7 @@ namespace grino
|
||||||
|
|
||||||
case OPCODE_LOAD_OBJ: {
|
case OPCODE_LOAD_OBJ: {
|
||||||
size_t addr = *instr.param;
|
size_t addr = *instr.param;
|
||||||
auto ref = Value::make_ref(addr);
|
auto ref = Value::make_ref(Loc {"???", 0}, addr);
|
||||||
push(program.push_constant(ref));
|
push(program.push_constant(ref));
|
||||||
m_pc++;
|
m_pc++;
|
||||||
} break;
|
} break;
|
||||||
|
|
|
@ -2,33 +2,34 @@
|
||||||
|
|
||||||
namespace grino
|
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;
|
value->m_type = TYPE_NIL;
|
||||||
return value;
|
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_type = TYPE_BOOL;
|
||||||
value->m_bool_val = val;
|
value->m_bool_val = val;
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*static*/
|
/*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_type = TYPE_FUNCTION;
|
||||||
value->m_function_val = std::make_shared<Function>(val);
|
value->m_function_val = std::make_shared<Function>(val);
|
||||||
return value;
|
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_type = TYPE_REF;
|
||||||
value->m_ref_val = val;
|
value->m_ref_val = val;
|
||||||
return value;
|
return value;
|
||||||
|
@ -65,4 +66,9 @@ namespace grino
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*explicit*/ Value::Value(Loc const& loc)
|
||||||
|
: m_loc { loc }
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,20 +4,24 @@
|
||||||
#include "commons.hpp"
|
#include "commons.hpp"
|
||||||
#include "types.hpp"
|
#include "types.hpp"
|
||||||
#include "Function.hpp"
|
#include "Function.hpp"
|
||||||
|
#include "Loc.hpp"
|
||||||
|
|
||||||
namespace grino
|
namespace grino
|
||||||
{
|
{
|
||||||
class Value
|
class Value
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static std::shared_ptr<Value> make_nil();
|
static std::shared_ptr<Value> make_nil(Loc const& loc);
|
||||||
static std::shared_ptr<Value> make_bool(bool val);
|
static std::shared_ptr<Value> make_bool(Loc const& loc, bool val);
|
||||||
static std::shared_ptr<Value> make_native_function(native_t val);
|
static std::shared_ptr<Value> make_native_function(Loc const& loc,
|
||||||
static std::shared_ptr<Value> make_ref(size_t val);
|
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;
|
virtual ~Value() = default;
|
||||||
|
|
||||||
|
Loc loc() const { return m_loc; }
|
||||||
TypeType type() const { return m_type; }
|
TypeType type() const { return m_type; }
|
||||||
bool as_bool() const { return *m_bool_val; }
|
bool as_bool() const { return *m_bool_val; }
|
||||||
std::shared_ptr<Function> as_function() const { return m_function_val; }
|
std::shared_ptr<Function> as_function() const { return m_function_val; }
|
||||||
|
@ -27,6 +31,7 @@ namespace grino
|
||||||
bool equals(Value const& other) const;
|
bool equals(Value const& other) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Loc m_loc;
|
||||||
TypeType m_type = TYPE_NIL;
|
TypeType m_type = TYPE_NIL;
|
||||||
std::optional<bool> m_bool_val;
|
std::optional<bool> m_bool_val;
|
||||||
std::shared_ptr<Function> m_function_val;
|
std::shared_ptr<Function> m_function_val;
|
||||||
|
|
Loading…
Reference in New Issue