Tidy up Kym library

This commit is contained in:
kayomn 2023-02-20 15:27:14 +00:00
parent f20e220e35
commit c36bf5b76c
3 changed files with 51 additions and 27 deletions

View File

@ -15,35 +15,50 @@ export namespace kym {
};
struct value {
value_type type;
value_type type{value_type::nil};
union {
bool boolean;
value() = default;
coral::i64 integer;
coral::f64 scalar;
coral::vector2 vector2;
coral::vector3 vector3;
void * object;
} as;
coral::optional<coral::u16> as_u16() const {
if ((this->type == value_type::integer) &&
(this->as.integer >= 0) && (this->as.integer <= coral::u16_max)) {
return static_cast<coral::u16>(this->as.integer);
value(bool boolean) {
this->data[1] = 0xff * boolean;
this->type = value_type::boolean;
}
value(coral::i64 integer) {
(*reinterpret_cast<coral::u64 *>(this->data)) =
(*reinterpret_cast<coral::u64 *>(&integer));
this->type = value_type::integer;
}
value(coral::f64 scalar) {
(*reinterpret_cast<coral::u64 *>(this->data)) =
(*reinterpret_cast<coral::u64 *>(&scalar));
this->type = value_type::scalar;
}
value(coral::vector2 vector2) {
(*reinterpret_cast<coral::u64 *>(this->data)) =
(*reinterpret_cast<coral::u64 *>(&vector2));
this->type = value_type::vector2;
}
value(coral::vector3 const & vector3) {
coral::copy(this->data, coral::as_bytes(&vector3));
this->type = value_type::vector3;
}
coral::optional<coral::i64> as_integer() const {
if (this->type == value_type::integer)
return (*reinterpret_cast<coral::i64 const *>(&this->data));
return {};
}
};
value nil = {
.type = value_type::nil,
.as = {.object = nullptr},
private:
coral::u8 data[12]{0};
};
};

View File

@ -57,7 +57,7 @@ struct bytecode {
}
kym::value execute(kym::vm & vm, coral::slice<kym::value const> const & arguments) {
return kym::nil;
return {};
}
private:
@ -66,7 +66,7 @@ struct bytecode {
export namespace kym {
value default_call(vm & owning_vm, void * userdata, coral::slice<value const> const & arguments) {
return nil;
return {};
}
coral::expected<coral::usize, coral::io_error> default_stringify(vm & owning_vm, void * userdata, coral::writer & output) {
@ -103,7 +103,7 @@ export namespace kym {
}
value get_field(coral::slice<char const> const & field_name) {
return nil;
return {};
}
private:
@ -139,14 +139,14 @@ export namespace kym {
value compile(coral::slice<char const> const & source) {
bytecode * source_bytecode = new (*this->allocator) bytecode{allocator};
if (source_bytecode == nullptr) return nil;
if (source_bytecode == nullptr) return {};
if (!source_bytecode->compile(tokenizer{source}, [&](coral::slice<char const> error_message) {
this->log(error_message);
})) {
this->allocator->deallocate(source_bytecode);
return nil;
return {};
}
return this->new_object([this, source_bytecode](bound_object & object) {
@ -165,7 +165,7 @@ export namespace kym {
}
value new_object(coral::callable<void(bound_object &)> const & then) {
return nil;
return {};
}
void with_object(value object_value, coral::callable<void(bound_object &)> const & then) {

View File

@ -38,13 +38,22 @@ extern "C" int main(int argc, char const * const * argv) {
}
vm.with_object(vm.compile(coral::slice{script_source.begin(), script_source.end()}.as_chars()), [&](kym::bound_object & script) {
vm.with_object(script.call({}), [&](kym::bound_object & config) {
coral::u16 const width{config.get_field("width").as_u16().value_or(0)};
vm.with_object(script.call({}), [&](kym::bound_object config) {
constexpr auto as_u16 = [](coral::i64 value) -> coral::optional<coral::i16> {
if ((value >= 0) && (value <= coral::u16_max))
return static_cast<coral::u16>(value);
return {};
};
coral::u16 const width{config.get_field("width").
as_integer().map(as_u16).value_or(0)};
if (width == 0) return system.log(app::log_level::error,
"failed to decode `width` property of config");
coral::u16 const height{config.get_field("height").as_u16().value_or(0)};
coral::u16 const height{config.get_field("height").
as_integer().map(as_u16).value_or(0)};
if (height == 0) return system.log(app::log_level::error,
"failed to decode `height` property of config");