xo-expression xo-reader: integer comparisons !=, <, >

This commit is contained in:
Roland Conybeare 2025-07-27 14:32:31 -04:00
commit b811e34090
6 changed files with 101 additions and 1 deletions

View file

@ -34,6 +34,12 @@ namespace xo {
/** create apply-expression to compare two 64-bit integers **/
static rp<Apply> make_cmp_ne_i64(const rp<Expression> & lhs,
const rp<Expression> & rhs);
/** create apply-expression for less-than comparison of two 64-bit integers **/
static rp<Apply> make_cmp_lt_i64(const rp<Expression> & lhs,
const rp<Expression> & rhs);
/** create apply-expression for greater-than comparison of two 64-bit integers **/
static rp<Apply> make_cmp_gt_i64(const rp<Expression> & lhs,
const rp<Expression> & rhs);
/** create apply-expression to add two 64-bit integers **/
static rp<Apply> make_add2_i64(const rp<Expression> & lhs,

View file

@ -204,6 +204,10 @@ namespace xo {
static rp<PrimitiveType> make_cmp_eq2_i64();
/** ne2_i64: compare two 64-bit integers for inequality **/
static rp<PrimitiveType> make_cmp_ne2_i64();
/** lt2_i64: compare two 64-bit integers for lessthan **/
static rp<PrimitiveType> make_cmp_lt2_i64();
/** gt2_i64: compare two 64-bit integers for greaterthan **/
static rp<PrimitiveType> make_cmp_gt2_i64();
};
/** builtin primitives :: i64 x i64 -> i64 **/

View file

@ -48,6 +48,22 @@ namespace xo {
{lhs, rhs});
}
rp<Apply>
Apply::make_cmp_lt_i64(const rp<Expression> & lhs,
const rp<Expression> & rhs)
{
return Apply::make(Primitive_cmp_i64::make_cmp_lt2_i64(),
{lhs, rhs});
}
rp<Apply>
Apply::make_cmp_gt_i64(const rp<Expression> & lhs,
const rp<Expression> & rhs)
{
return Apply::make(Primitive_cmp_i64::make_cmp_gt2_i64(),
{lhs, rhs});
}
// ----- integer arithmetic -----
rp<Apply>

View file

@ -22,6 +22,16 @@ extern "C" {
return x != y;
}
bool
cmp_lt2_i64(std::int64_t x, std::int64_t y) {
return x < y;
}
bool
cmp_gt2_i64(std::int64_t x, std::int64_t y) {
return x > y;
}
std::int64_t
add2_i64(std::int64_t x, std::int64_t y) {
return x + y;
@ -93,6 +103,34 @@ namespace xo {
return s_retval;
}
auto
Primitive_cmp_i64::make_cmp_lt2_i64() -> rp<PrimitiveType>
{
static rp<PrimitiveType> s_retval;
if (!s_retval)
s_retval = Primitive::make("cmp_lt2_i64",
&cmp_lt2_i64,
true /*explicit_symbol_def*/,
llvmintrinsic::i_slt);
return s_retval;
}
auto
Primitive_cmp_i64::make_cmp_gt2_i64() -> rp<PrimitiveType>
{
static rp<PrimitiveType> s_retval;
if (!s_retval)
s_retval = Primitive::make("cmp_gt2_i64",
&cmp_gt2_i64,
true /*explicit_symbol_def*/,
llvmintrinsic::i_sgt);
return s_retval;
}
/* TODO: remaining integer arithmetic */
auto

View file

@ -418,6 +418,9 @@ namespace xo {
case tokentype::tk_leftangle:
case tokentype::tk_rightangle:
this->on_operator_token(tk, p_psm);
return;
case tokentype::tk_dot:
assert(false);
return;

View file

@ -182,12 +182,41 @@ namespace xo {
}
case optype::op_equal:
return Apply::make_cmp_eq_i64(lhs_, rhs_);
if (lhs_->valuetype()->is_i64() && rhs_->valuetype()->is_i64()) {
return Apply::make_cmp_eq_i64(lhs_, rhs_);
} else {
this->apply_type_error(c_self_name,
op_type_, lhs_, rhs_, p_psm);
return nullptr;
}
break;
case optype::op_less:
// TODO: floating-point less-than
if (lhs_->valuetype()->is_i64() && rhs_->valuetype()->is_i64()) {
return Apply::make_cmp_lt_i64(lhs_, rhs_);
} else {
this->apply_type_error(c_self_name,
op_type_, lhs_, rhs_, p_psm);
return nullptr;
}
break;
case optype::op_less_equal:
case optype::op_not_equal:
assert(false);
case optype::op_great:
if (lhs_->valuetype()->is_i64() && rhs_->valuetype()->is_i64()) {
return Apply::make_cmp_gt_i64(lhs_, rhs_);
} else {
this->apply_type_error(c_self_name,
op_type_, lhs_, rhs_, p_psm);
return nullptr;
}
break;
case optype::op_great_equal:
assert(false);
@ -532,6 +561,10 @@ namespace xo {
return optype::op_equal;
case tokentype::tk_cmpne:
return optype::op_not_equal;
case tokentype::tk_leftangle:
return optype::op_less;
case tokentype::tk_rightangle:
return optype::op_great;
default:
assert(false);
return optype::invalid;