Skip to content

Commit

Permalink
Refactored a little.
Browse files Browse the repository at this point in the history
  • Loading branch information
shin1m committed May 14, 2023
1 parent 34bea00 commit 1dce95e
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 136 deletions.
6 changes: 3 additions & 3 deletions include/xemmai/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -660,16 +660,16 @@ template<typename T>
t_operand t_literal<T>::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_clear)
{
if (a_operand) return v_value;
if (a_clear) return t_operand();
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_B - e_instruction__BOOLEAN) << v_value;
a_emit << static_cast<t_instruction>(t_emit::f_instruction_of<T>() + e_instruction__RETURN_BOOLEAN - e_instruction__BOOLEAN) << v_value;
} else {
a_emit << t_emit::f_instruction_of<T>() << a_emit.v_stack << v_value;
}
a_emit.f_push();
a_emit.f_at(this);
return t_operand();
return {};
}

}
Expand Down
10 changes: 5 additions & 5 deletions include/xemmai/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ enum t_instruction
e_instruction__INTEGER,
e_instruction__FLOAT,
e_instruction__INSTANCE,
e_instruction__RETURN_N,
e_instruction__RETURN_B,
e_instruction__RETURN_I,
e_instruction__RETURN_F,
e_instruction__RETURN_L,
e_instruction__RETURN_NUL,
e_instruction__RETURN_BOOLEAN,
e_instruction__RETURN_INTEGER,
e_instruction__RETURN_FLOAT,
e_instruction__RETURN_INSTANCE,
e_instruction__RETURN_V,
e_instruction__RETURN_T,
e_instruction__CALL,
Expand Down
64 changes: 32 additions & 32 deletions src/ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ t_operand t_lambda::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_c
}
(a_emit << a_emit.v_stack << code).f_push();
if (a_clear) a_emit.f_pop();
return t_operand();
return {};
}

void t_if::f_flow(t_flow& a_flow)
Expand Down Expand Up @@ -225,7 +225,7 @@ t_operand t_if::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_clear
a_emit.f_join(v_junction);
a_emit.f_target(label1);
a_emit.f_merge(v_junction);
return t_operand();
return {};
}

void t_while::f_flow(t_flow& a_flow)
Expand Down Expand Up @@ -269,7 +269,7 @@ t_operand t_while::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_cl
if (!a_clear) a_emit.f_emit_null();
a_emit.f_target(break0);
a_emit.f_merge(v_junction_exit);
return t_operand();
return {};
}

void t_for::f_flow(t_flow& a_flow)
Expand Down Expand Up @@ -326,7 +326,7 @@ t_operand t_for::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_clea
if (!a_clear) a_emit.f_emit_null();
a_emit.f_target(break0);
a_emit.f_merge(v_junction_exit);
return t_operand();
return {};
}

void t_break::f_flow(t_flow& a_flow)
Expand All @@ -347,7 +347,7 @@ t_operand t_break::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_cl
if (!a_clear) a_emit.f_push();
a_emit.f_join(*a_emit.v_targets->v_break_junction);
a_emit << e_instruction__JUMP << *a_emit.v_targets->v_break;
return t_operand();
return {};
}

void t_continue::f_flow(t_flow& a_flow)
Expand All @@ -359,7 +359,7 @@ t_operand t_continue::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a
{
if (!a_clear) a_emit.f_push();
a_emit << e_instruction__JUMP << *a_emit.v_targets->v_continue;
return t_operand();
return {};
}

void t_return::f_flow(t_flow& a_flow)
Expand All @@ -385,7 +385,7 @@ t_operand t_return::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_c
}
a_emit.v_stack = stack;
if (!a_clear) a_emit.f_push();
return t_operand();
return {};
}

void t_try::f_flow(t_flow& a_flow)
Expand Down Expand Up @@ -509,7 +509,7 @@ t_operand t_try::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_clea
join(return0, targets0->v_return, targets0->v_return_junction, targets0->v_return_stack);
if (step) a_emit.f_target(*step);
a_emit.v_targets = targets0;
return t_operand();
return {};
}

void t_throw::f_flow(t_flow& a_flow)
Expand All @@ -525,7 +525,7 @@ t_operand t_throw::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_cl
a_emit << e_instruction__THROW << a_emit.v_stack;
if (!a_clear) a_emit.f_push();
a_emit.f_at(this);
return t_operand();
return {};
}

void t_object_get::f_flow(t_flow& a_flow)
Expand All @@ -540,7 +540,7 @@ t_operand t_object_get::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool
a_emit << e_instruction__OBJECT_GET << a_emit.v_stack - 1 << v_key << 0;
a_emit.f_at(this);
if (a_clear) a_emit.f_pop();
return t_operand();
return {};
}

void t_object_get::f_method(t_emit& a_emit)
Expand Down Expand Up @@ -568,7 +568,7 @@ t_operand t_object_get_indirect::f_emit(t_emit& a_emit, bool a_tail, bool a_oper
a_emit.f_pop();
a_emit.f_at(this);
if (a_clear) a_emit.f_pop();
return t_operand();
return {};
}

void t_object_put::f_flow(t_flow& a_flow)
Expand All @@ -586,7 +586,7 @@ t_operand t_object_put::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool
a_emit.f_pop();
a_emit.f_at(this);
if (a_clear) a_emit.f_pop();
return t_operand();
return {};
}

void t_object_put_indirect::f_flow(t_flow& a_flow)
Expand All @@ -606,7 +606,7 @@ t_operand t_object_put_indirect::f_emit(t_emit& a_emit, bool a_tail, bool a_oper
a_emit.f_pop().f_pop();
a_emit.f_at(this);
if (a_clear) a_emit.f_pop();
return t_operand();
return {};
}

void t_object_has::f_flow(t_flow& a_flow)
Expand All @@ -621,7 +621,7 @@ t_operand t_object_has::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool
a_emit << e_instruction__OBJECT_HAS << a_emit.v_stack - 1 << v_key << 0;
a_emit.f_at(this);
if (a_clear) a_emit.f_pop();
return t_operand();
return {};
}

void t_object_has_indirect::f_flow(t_flow& a_flow)
Expand All @@ -639,7 +639,7 @@ t_operand t_object_has_indirect::f_emit(t_emit& a_emit, bool a_tail, bool a_oper
a_emit.f_pop();
a_emit.f_at(this);
if (a_clear) a_emit.f_pop();
return t_operand();
return {};
}

void t_symbol_get::f_flow(t_flow& a_flow)
Expand All @@ -666,7 +666,7 @@ t_operand t_symbol_get::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool
a_emit.f_push();
a_emit.f_at(this);
if (a_clear) a_emit.f_pop();
return t_operand();
return {};
}
if (v_variable->v_shared) {
size_t instruction = e_instruction__SCOPE_GET0 + (v_resolved < 3 ? v_resolved : 3);
Expand All @@ -691,7 +691,7 @@ t_operand t_symbol_get::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool
a_emit.f_push();
a_emit.f_at(this);
if (a_clear) a_emit.f_pop();
return t_operand();
return {};
}

void t_scope_put::f_flow(t_flow& a_flow)
Expand Down Expand Up @@ -736,7 +736,7 @@ t_operand t_scope_put::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool
a_emit.f_pop();
}
a_emit.f_at(this);
return t_operand();
return {};
}
v_value->f_emit(a_emit, false, false);
a_emit.f_emit_safe_point(this);
Expand All @@ -753,12 +753,12 @@ t_operand t_scope_put::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool
a_emit << v_variable.v_index;
a_emit.f_at(this);
if (a_clear) a_emit.f_pop();
return t_operand();
return {};
}

t_operand t_self::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_clear)
{
if (a_clear) return t_operand();
if (a_clear) return {};
if (a_tail) a_emit.f_emit_safe_point(this);
if (v_outer > 0) {
a_emit << static_cast<t_instruction>(e_instruction__SCOPE_GET0 + (v_outer < 3 ? v_outer : 3)) << a_emit.v_stack;
Expand All @@ -769,7 +769,7 @@ t_operand t_self::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_cle
}
a_emit.f_push();
a_emit.f_at(this);
return t_operand();
return {};
}

void t_class::f_flow(t_flow& a_flow)
Expand All @@ -784,7 +784,7 @@ t_operand t_class::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_cl
a_emit << e_instruction__CLASS << a_emit.v_stack - 1;
a_emit.f_at(this);
if (a_clear) a_emit.f_pop();
return t_operand();
return {};
}

void t_super::f_flow(t_flow& a_flow)
Expand All @@ -799,21 +799,21 @@ t_operand t_super::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_cl
a_emit << e_instruction__SUPER << a_emit.v_stack - 1;
a_emit.f_at(this);
if (a_clear) a_emit.f_pop();
return t_operand();
return {};
}

t_operand t_null::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_clear)
{
if (a_operand) return nullptr;
if (a_clear) return t_operand();
if (a_clear) return {};
if (a_tail) {
a_emit.f_emit_safe_point(this);
(a_emit << e_instruction__RETURN_N).f_push();
(a_emit << e_instruction__RETURN_NUL).f_push();
} else {
a_emit.f_emit_null();
}
a_emit.f_at(this);
return t_operand();
return {};
}

void t_unary::f_flow(t_flow& a_flow)
Expand Down Expand Up @@ -875,7 +875,7 @@ t_operand t_unary::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_cl
a_emit.f_push();
a_emit.f_at(this);
if (a_clear) a_emit.f_pop();
return t_operand();
return {};
}

void t_binary::f_flow(t_flow& a_flow)
Expand Down Expand Up @@ -1090,7 +1090,7 @@ t_operand t_binary::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_c
a_emit.f_push();
a_emit.f_at(this);
if (a_clear) a_emit.f_pop();
return t_operand();
return {};
}

void t_call::f_flow(t_flow& a_flow)
Expand Down Expand Up @@ -1131,7 +1131,7 @@ t_operand t_call::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_cle
a_emit.f_push();
a_emit.f_at(this);
if (a_clear) a_emit.f_pop();
return t_operand();
return {};
}

void t_get_at::f_flow(t_flow& a_flow)
Expand All @@ -1152,7 +1152,7 @@ t_operand t_get_at::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_c
a_emit.f_pop().f_pop();
a_emit.f_at(this);
if (a_clear) a_emit.f_pop();
return t_operand();
return {};
}

void t_get_at::f_bind(t_emit& a_emit)
Expand Down Expand Up @@ -1184,7 +1184,7 @@ t_operand t_set_at::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_c
a_emit.f_pop().f_pop().f_pop();
a_emit.f_at(this);
if (a_clear) a_emit.f_pop();
return t_operand();
return {};
}

}
Expand All @@ -1209,7 +1209,7 @@ t_object* t_emit::operator()(ast::t_scope& a_scope)
<< e_instruction__STACK_GET << v_stack << 0
<< e_instruction__SCOPE_PUT0 << v_stack << 0;
ast::f_emit_block_without_value(*this, a_scope.v_block);
*this << e_instruction__RETURN_N;
*this << e_instruction__RETURN_NUL;
f_resolve();
if (v_safe_points) {
t_code::t_variable self;
Expand Down
20 changes: 10 additions & 10 deletions src/code.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ size_t t_code::f_loop(t_context* a_context, const void*** a_labels)
&&label__INTEGER,
&&label__FLOAT,
&&label__INSTANCE,
&&label__RETURN_N,
&&label__RETURN_B,
&&label__RETURN_I,
&&label__RETURN_F,
&&label__RETURN_L,
&&label__RETURN_NUL,
&&label__RETURN_BOOLEAN,
&&label__RETURN_INTEGER,
&&label__RETURN_FLOAT,
&&label__RETURN_INSTANCE,
&&label__RETURN_V,
&&label__RETURN_T,
&&label__CALL,
Expand Down Expand Up @@ -493,22 +493,22 @@ size_t t_code::f_loop(t_context* a_context)
stack[0] = value;
}
XEMMAI__CODE__BREAK
XEMMAI__CODE__CASE(RETURN_N)
XEMMAI__CODE__CASE(RETURN_NUL)
a_context->f_return(nullptr);
return -1;
XEMMAI__CODE__CASE(RETURN_B)
XEMMAI__CODE__CASE(RETURN_BOOLEAN)
a_context->f_return(reinterpret_cast<intptr_t>(*++pc) != 0);
return -1;
XEMMAI__CODE__CASE(RETURN_I)
XEMMAI__CODE__CASE(RETURN_INTEGER)
a_context->f_return(reinterpret_cast<intptr_t>(*++pc));
return -1;
XEMMAI__CODE__CASE(RETURN_F)
XEMMAI__CODE__CASE(RETURN_FLOAT)
{
XEMMAI__CODE__FLOAT(v0, v1)
a_context->f_return(v0);
}
return -1;
XEMMAI__CODE__CASE(RETURN_L)
XEMMAI__CODE__CASE(RETURN_INSTANCE)
a_context->f_return(*static_cast<const t_pvalue*>(*++pc));
return -1;
XEMMAI__CODE__CASE(RETURN_V)
Expand Down
Loading

0 comments on commit 1dce95e

Please sign in to comment.