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 { struct value {
value_type type; value_type type{value_type::nil};
union { value() = default;
bool boolean;
coral::i64 integer; value(bool boolean) {
this->data[1] = 0xff * boolean;
this->type = value_type::boolean;
}
coral::f64 scalar; value(coral::i64 integer) {
(*reinterpret_cast<coral::u64 *>(this->data)) =
(*reinterpret_cast<coral::u64 *>(&integer));
coral::vector2 vector2; this->type = value_type::integer;
}
coral::vector3 vector3; value(coral::f64 scalar) {
(*reinterpret_cast<coral::u64 *>(this->data)) =
(*reinterpret_cast<coral::u64 *>(&scalar));
void * object; this->type = value_type::scalar;
} as; }
coral::optional<coral::u16> as_u16() const { value(coral::vector2 vector2) {
if ((this->type == value_type::integer) && (*reinterpret_cast<coral::u64 *>(this->data)) =
(this->as.integer >= 0) && (this->as.integer <= coral::u16_max)) { (*reinterpret_cast<coral::u64 *>(&vector2));
return static_cast<coral::u16>(this->as.integer); 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 {}; return {};
} }
};
value nil = { private:
.type = value_type::nil, coral::u8 data[12]{0};
.as = {.object = nullptr},
}; };
}; };

View File

@ -57,7 +57,7 @@ struct bytecode {
} }
kym::value execute(kym::vm & vm, coral::slice<kym::value const> const & arguments) { kym::value execute(kym::vm & vm, coral::slice<kym::value const> const & arguments) {
return kym::nil; return {};
} }
private: private:
@ -66,7 +66,7 @@ struct bytecode {
export namespace kym { export namespace kym {
value default_call(vm & owning_vm, void * userdata, coral::slice<value const> const & arguments) { 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) { 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) { value get_field(coral::slice<char const> const & field_name) {
return nil; return {};
} }
private: private:
@ -139,14 +139,14 @@ export namespace kym {
value compile(coral::slice<char const> const & source) { value compile(coral::slice<char const> const & source) {
bytecode * source_bytecode = new (*this->allocator) bytecode{allocator}; 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) { if (!source_bytecode->compile(tokenizer{source}, [&](coral::slice<char const> error_message) {
this->log(error_message); this->log(error_message);
})) { })) {
this->allocator->deallocate(source_bytecode); this->allocator->deallocate(source_bytecode);
return nil; return {};
} }
return this->new_object([this, source_bytecode](bound_object & object) { 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) { 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) { 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(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) { vm.with_object(script.call({}), [&](kym::bound_object config) {
coral::u16 const width{config.get_field("width").as_u16().value_or(0)}; 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, if (width == 0) return system.log(app::log_level::error,
"failed to decode `width` property of config"); "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, if (height == 0) return system.log(app::log_level::error,
"failed to decode `height` property of config"); "failed to decode `height` property of config");