Skip to content

Commit

Permalink
Made true & false the tags themselves.
Browse files Browse the repository at this point in the history
  • Loading branch information
shin1m committed Feb 24, 2024
1 parent 54ed1c0 commit 3746d6f
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 117 deletions.
2 changes: 1 addition & 1 deletion doc/TermExpressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Relational, equality, and identity operators return either `true` or `false`.

and: identity ('&' identity)* ;

Bitwise operators (`&`, `^`, and `|`) do bitwise operations if both the operands are integers, do boolean operations if both the operands are booleans, throws an exception otherwise.
Bitwise operators (`&`, `^`, and `|`) do bitwise operations if both the operands are integers, do boolean operations if the left operand is boolean, throws an exception otherwise.

## Xor Expressions

Expand Down
27 changes: 7 additions & 20 deletions include/xemmai/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,16 @@ struct t_operand
e_tag__TEMPORARY
};

static inline const t_pvalue v_null{};
static inline const t_pvalue v_true{true};
static inline const t_pvalue v_false{false};

t_tag v_tag;
union
{
intptr_t v_integer;
double v_float;
const t_pvalue* v_value;
t_object* v_value;
size_t v_index;
};

t_operand(nullptr_t) : v_tag(e_tag__LITERAL), v_value(&v_null)
{
}
t_operand(bool a_value) : v_tag(e_tag__LITERAL), v_value(a_value ? &v_true : &v_false)
t_operand(bool a_value) : v_tag(e_tag__LITERAL), v_value(reinterpret_cast<t_object*>(a_value ? e_tag__TRUE : e_tag__FALSE))
{
}
t_operand(intptr_t a_value) : v_tag(e_tag__INTEGER), v_integer(a_value)
Expand All @@ -97,7 +90,7 @@ struct t_operand
t_operand(double a_value) : v_tag(e_tag__FLOAT), v_float(a_value)
{
}
t_operand(t_svalue& a_value) : v_tag(e_tag__LITERAL), v_value(reinterpret_cast<const t_pvalue*>(&a_value))
t_operand(t_object* a_value) : v_tag(e_tag__LITERAL), v_value(a_value)
{
}
t_operand() : v_tag(e_tag__TEMPORARY)
Expand Down Expand Up @@ -571,8 +564,7 @@ struct XEMMAI__LOCAL t_emit
}
t_emit& operator<<(bool a_operand)
{
v_code->v_instructions.push_back(reinterpret_cast<void*>(a_operand ? 1 : 0));
return *this;
return *this << (a_operand ? 1 : 0);
}
t_emit& operator<<(double a_operand)
{
Expand All @@ -585,14 +577,9 @@ struct XEMMAI__LOCAL t_emit
for (size_t i = 0; i < sizeof(double) / sizeof(void*); ++i) v_code->v_instructions.push_back(v1[i]);
return *this;
}
t_emit& operator<<(const t_pvalue& a_operand)
{
v_code->v_instructions.push_back(const_cast<t_pvalue*>(&a_operand));
return *this;
}
t_emit& operator<<(t_svalue& a_operand)
t_emit& operator<<(t_object* a_operand)
{
v_code->v_instructions.push_back(&a_operand);
v_code->v_instructions.push_back(a_operand);
return *this;
}
t_emit& operator<<(t_label& a_label)
Expand Down Expand Up @@ -663,7 +650,7 @@ t_operand t_literal<T>::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool
if (a_clear) return {};
if (a_tail) {
a_emit.f_emit_safe_point(this);
a_emit << static_cast<t_instruction>(t_emit::f_instruction_of<T>() + e_instruction__RETURN_BOOLEAN - e_instruction__BOOLEAN) << v_value;
a_emit << static_cast<t_instruction>(t_emit::f_instruction_of<T>() + e_instruction__RETURN_NUL - e_instruction__NUL) << v_value;
} else {
a_emit << t_emit::f_instruction_of<T>() << a_emit.v_stack << v_value;
}
Expand Down
10 changes: 3 additions & 7 deletions include/xemmai/boolean.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,17 @@ struct t_type_of<bool> : t_uninstantiatable<t_bears<bool>>
{
static bool f_as(auto&& a_object)
{
return a_object && (a_object.f_tag() != e_tag__BOOLEAN || a_object.f_boolean());
return a_object.f_boolean();
}
static bool f_is(t_object* a_object)
{
return true;
}
};

static t_object* f__string(bool a_self)
static t_object* f__string(const t_pvalue& a_self)
{
return t_string::f_instantiate(a_self ? L"true"sv : L"false"sv);
}
static intptr_t f__hash(bool a_self)
{
return a_self ? 1 : 0;
return t_string::f_instantiate(a_self.f_boolean() ? L"true"sv : L"false"sv);
}
XEMMAI__LOCAL static void f_define();

Expand Down
6 changes: 4 additions & 2 deletions include/xemmai/fiber.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ inline t_pvalue t_value<T_tag>::f_##a_name() const\
auto p = static_cast<t_object*>(*this);\
switch (reinterpret_cast<uintptr_t>(p)) {\
case e_tag__NULL:\
case e_tag__BOOLEAN:\
case e_tag__FALSE:\
case e_tag__TRUE:\
f_throw(L"not supported."sv);\
case e_tag__INTEGER:\
return a_operator(v_integer);\
Expand All @@ -287,7 +288,8 @@ inline t_pvalue t_value<T_tag>::f_complement() const
case e_tag__INTEGER:
return ~v_integer;
case e_tag__NULL:
case e_tag__BOOLEAN:
case e_tag__FALSE:
case e_tag__TRUE:
case e_tag__FLOAT:
f_throw(L"not supported."sv);
default:
Expand Down
3 changes: 2 additions & 1 deletion include/xemmai/float.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ struct t_type_of<double> : t_derivable<t_bears<double>, t_derived_primitive<doub
if (!std::is_same_v<typename t_fundamental<T>::t_type, double>) return reinterpret_cast<uintptr_t>(a_object) >= e_tag__OBJECT && a_object->f_type()->f_derives<typename t_fundamental<T>::t_type>();
switch (reinterpret_cast<uintptr_t>(a_object)) {
case e_tag__NULL:
case e_tag__BOOLEAN:
case e_tag__FALSE:
case e_tag__TRUE:
return false;
case e_tag__INTEGER:
case e_tag__FLOAT:
Expand Down
24 changes: 14 additions & 10 deletions include/xemmai/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ XEMMAI__PORTABLE__ALWAYS_INLINE inline t_type* t_value<T_tag>::f_type() const
switch (reinterpret_cast<uintptr_t>(p)) {
case e_tag__NULL:
return f_global()->f_type<std::nullptr_t>();
case e_tag__BOOLEAN:
case e_tag__FALSE:
case e_tag__TRUE:
return f_global()->f_type<bool>();
case e_tag__INTEGER:
return f_global()->f_type<intptr_t>();
Expand Down Expand Up @@ -282,8 +283,9 @@ inline t_pvalue t_value<T_tag>::f_##a_name() const\
switch (reinterpret_cast<uintptr_t>(p)) {\
case e_tag__NULL:\
return t_type_of<std::nullptr_t>::f__##a_name(*this);\
case e_tag__BOOLEAN:\
return t_type_of<bool>::f__##a_name(v_boolean);\
case e_tag__FALSE:\
case e_tag__TRUE:\
return t_type_of<bool>::f__##a_name(f_as<bool>(*this));\
case e_tag__INTEGER:\
return t_type_of<intptr_t>::f__##a_name(v_integer);\
case e_tag__FLOAT:\
Expand Down Expand Up @@ -315,7 +317,8 @@ inline t_pvalue t_value<T_tag>::f_##a_name(const t_pvalue& a_value) const\
auto p = static_cast<t_object*>(*this);\
switch (reinterpret_cast<uintptr_t>(p)) {\
case e_tag__NULL:\
case e_tag__BOOLEAN:\
case e_tag__FALSE:\
case e_tag__TRUE:\
f_throw(L"not supported."sv);\
case e_tag__INTEGER:\
return t_type_of<intptr_t>::f__##a_name(v_integer, a_value);\
Expand All @@ -336,7 +339,8 @@ inline t_pvalue t_value<T_tag>::f_##a_name(const t_pvalue& a_value) const\
f_check<intptr_t>(a_value, L"argument0");\
return static_cast<uintptr_t>(v_integer) a_operator f_as<intptr_t>(a_value);\
case e_tag__NULL:\
case e_tag__BOOLEAN:\
case e_tag__FALSE:\
case e_tag__TRUE:\
case e_tag__FLOAT:\
f_throw(L"not supported."sv);\
default:\
Expand All @@ -350,9 +354,9 @@ inline t_pvalue t_value<T_tag>::f_##a_name(const t_pvalue& a_value) const\
auto p = static_cast<t_object*>(*this);\
switch (reinterpret_cast<uintptr_t>(p)) {\
case e_tag__NULL:\
case e_tag__FALSE:\
case e_tag__TRUE:\
return a_operator(p == a_value.v_p);\
case e_tag__BOOLEAN:\
return a_operator(p == a_value.v_p && v_boolean == a_value.v_boolean);\
case e_tag__INTEGER:\
return t_type_of<intptr_t>::f__##a_name(v_integer, a_value);\
case e_tag__FLOAT:\
Expand All @@ -367,9 +371,9 @@ inline t_pvalue t_value<T_tag>::f_##a_name(const t_pvalue& a_value) const\
{\
auto p = static_cast<t_object*>(*this);\
switch (reinterpret_cast<uintptr_t>(p)) {\
case e_tag__BOOLEAN:\
if (a_value.f_tag() != e_tag__BOOLEAN) [[unlikely]] f_throw_type_error<bool>(L"argument0");\
return v_boolean a_operator a_value.v_boolean;\
case e_tag__FALSE:\
case e_tag__TRUE:\
return f_as<bool>(*this) a_operator f_as<bool>(a_value);\
case e_tag__INTEGER:\
f_check<intptr_t>(a_value, L"argument0");\
return v_integer a_operator f_as<intptr_t>(a_value);\
Expand Down
3 changes: 2 additions & 1 deletion include/xemmai/integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ struct t_type_of<intptr_t> : t_derivable<t_bears<intptr_t>, t_derived_primitive<
case e_tag__INTEGER:
return true;
case e_tag__NULL:
case e_tag__BOOLEAN:
case e_tag__FALSE:
case e_tag__TRUE:
case e_tag__FLOAT:
return false;
default:
Expand Down
7 changes: 3 additions & 4 deletions include/xemmai/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,19 @@ struct t_module
struct t_script : t_module::t_body
{
std::wstring v_path;
std::deque<t_svalue> v_slots;
std::deque<t_slot> v_slots;
std::mutex v_mutex;

t_script(std::wstring_view a_path) : v_path(a_path)
{
}
virtual void f_scan(t_scan a_scan);
t_svalue& f_slot(t_object* a_p)
t_object* f_slot(t_object* a_p)
{
v_mutex.lock();
auto& p = v_slots.emplace_back();
v_mutex.unlock();
p = a_p;
return p;
return p = a_p;
}
};

Expand Down
3 changes: 2 additions & 1 deletion include/xemmai/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ struct t_type::t_cast<T*>
switch (reinterpret_cast<uintptr_t>(a_object)) {
case e_tag__NULL:
return true;
case e_tag__BOOLEAN:
case e_tag__FALSE:
case e_tag__TRUE:
case e_tag__INTEGER:
case e_tag__FLOAT:
return false;
Expand Down
2 changes: 1 addition & 1 deletion include/xemmai/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class XEMMAI__LOCAL t_parser
{
f_throw(a_message, v_lexer.f_at());
}
t_svalue& f_symbol() const
t_object* f_symbol() const
{
return v_module.f_slot(t_symbol::f_instantiate(v_lexer.f_value()));
}
Expand Down
19 changes: 5 additions & 14 deletions include/xemmai/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ using t_type = t_type_of<t_object>;
enum t_tag
{
e_tag__NULL,
e_tag__BOOLEAN,
e_tag__FALSE,
e_tag__TRUE,
e_tag__INTEGER,
e_tag__FLOAT,
e_tag__OBJECT
Expand Down Expand Up @@ -236,7 +237,6 @@ class t_value : public T_tag

union
{
bool v_boolean;
intptr_t v_integer;
double v_float;
};
Expand All @@ -251,7 +251,7 @@ class t_value : public T_tag

public:
using T_tag::T_tag;
t_value(bool a_value) : T_tag(reinterpret_cast<t_object*>(e_tag__BOOLEAN)), v_boolean(a_value)
t_value(bool a_value) : T_tag(reinterpret_cast<t_object*>(a_value ? e_tag__TRUE : e_tag__FALSE))
{
}
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
Expand Down Expand Up @@ -289,8 +289,6 @@ class t_value : public T_tag
auto p = f_tag();
if (p != a_value.f_tag()) return false;
switch (p) {
case e_tag__BOOLEAN:
return v_boolean == a_value.v_boolean;
case e_tag__INTEGER:
return v_integer == a_value.v_integer;
case e_tag__FLOAT:
Expand All @@ -305,7 +303,7 @@ class t_value : public T_tag
}
bool f_boolean() const
{
return v_boolean;
return f_tag() >= e_tag__TRUE;
}
intptr_t f_integer() const;
double f_float() const;
Expand Down Expand Up @@ -344,14 +342,7 @@ class t_value : public T_tag
t_value<t_pointer> f_minus() const;
t_value<t_pointer> f_not() const
{
switch (reinterpret_cast<uintptr_t>(f_tag())) {
case e_tag__NULL:
return true;
case e_tag__BOOLEAN:
return !v_boolean;
default:
return false;
}
return f_tag() < e_tag__TRUE;
}
t_value<t_pointer> f_complement() const;
t_value<t_pointer> f_multiply(const t_value<t_pointer>& a_value) const;
Expand Down
17 changes: 10 additions & 7 deletions src/ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -717,15 +717,18 @@ t_operand t_scope_put::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool
a_emit << e_instruction__FLOAT << v_variable.v_index << operand.v_float;
break;
case t_operand::e_tag__LITERAL:
switch (operand.v_value->f_tag()) {
switch (reinterpret_cast<uintptr_t>(operand.v_value)) {
case e_tag__NULL:
a_emit << e_instruction__NUL << v_variable.v_index;
break;
case e_tag__BOOLEAN:
a_emit << e_instruction__BOOLEAN << v_variable.v_index << operand.v_value->f_boolean();
case e_tag__FALSE:
a_emit << e_instruction__BOOLEAN << v_variable.v_index << 0;
break;
case e_tag__TRUE:
a_emit << e_instruction__BOOLEAN << v_variable.v_index << 1;
break;
default:
a_emit << e_instruction__INSTANCE << v_variable.v_index << *operand.v_value;
a_emit << e_instruction__INSTANCE << v_variable.v_index << operand.v_value;
}
break;
case t_operand::e_tag__VARIABLE:
Expand Down Expand Up @@ -870,7 +873,7 @@ t_operand t_unary::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_cl
if (!a_tail) a_emit << a_emit.v_stack;
switch (operand.v_tag) {
case t_operand::e_tag__LITERAL:
a_emit << *operand.v_value;
a_emit << operand.v_value;
break;
case t_operand::e_tag__VARIABLE:
a_emit << operand.v_index;
Expand Down Expand Up @@ -1071,7 +1074,7 @@ t_operand t_binary::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_c
a_emit << left.v_float;
break;
case t_operand::e_tag__LITERAL:
a_emit << *left.v_value;
a_emit << left.v_value;
break;
case t_operand::e_tag__VARIABLE:
a_emit << left.v_index;
Expand All @@ -1085,7 +1088,7 @@ t_operand t_binary::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_c
a_emit << right.v_float;
break;
case t_operand::e_tag__LITERAL:
a_emit << *right.v_value;
a_emit << right.v_value;
break;
case t_operand::e_tag__VARIABLE:
a_emit << right.v_index;
Expand Down
2 changes: 1 addition & 1 deletion src/boolean.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace xemmai
void t_type_of<bool>::f_define()
{
t_define{f_global()}
(f_global()->f_symbol_string(), t_member<t_object*(*)(bool), f__string>())
(f_global()->f_symbol_string(), t_member<t_object*(*)(const t_pvalue&), f__string>())
.f_derive<bool, t_object>();
}

Expand Down
Loading

0 comments on commit 3746d6f

Please sign in to comment.