xo-reader xo-expression xo-tokenizer xo-jit: comparison + apply

This commit is contained in:
Roland Conybeare 2025-07-23 23:19:16 -05:00
commit 93b2daab6c
28 changed files with 720 additions and 171 deletions

View file

@ -30,6 +30,22 @@ namespace xo {
return new Apply(fn_retval_type, fn, argv);
}
rp<Apply>
Apply::make_cmp_eq_i64(const rp<Expression> & lhs,
const rp<Expression> & rhs)
{
return Apply::make(Primitive_cmp_i64::make_cmp_eq2_i64(),
{lhs, rhs});
}
rp<Apply>
Apply::make_cmp_ne_i64(const rp<Expression> & lhs,
const rp<Expression> & rhs)
{
return Apply::make(Primitive_cmp_i64::make_cmp_ne2_i64(),
{lhs, rhs});
}
rp<Apply>
Apply::make_add2_f64(const rp<Expression> & lhs,
const rp<Expression> & rhs)

View file

@ -28,17 +28,14 @@ namespace xo {
std::string lhs_name,
rp<Expression> rhs)
: Expression(exprtype::define, rhs_valuetype),
lhs_name_{std::move(lhs_name)},
lhs_var_{Variable::make(lhs_name, rhs_valuetype)},
rhs_{std::move(rhs)}
{
this->free_var_set_ = this->calc_free_variables();
}
rp<Variable>
DefineExpr::lhs_variable() const
{
return Variable::make(lhs_name(), valuetype());
}
const std::string &
DefineExpr::lhs_name() const { return lhs_var_->name(); }
std::set<std::string>
DefineExpr::calc_free_variables() const
@ -58,7 +55,7 @@ namespace xo {
void
DefineExpr::display(std::ostream & os) const {
os << "<Define"
<< xtag("name", lhs_name_)
<< xtag("name", lhs_var_->name())
<< xtag("rhs", rhs_)
<< ">";
} /*display*/
@ -67,30 +64,8 @@ namespace xo {
DefineExpr::pretty_print(const ppindentinfo & ppii) const
{
return ppii.pps()->pretty_struct(ppii, "Define",
refrtag("name", lhs_name_),
refrtag("name", lhs_var_->name()),
refrtag("rhs", rhs_));
#ifdef OBSOLETE
ppstate * pps = ppii.pps();
if (ppii.upto()) {
if (!pps->print_upto("<Define"))
return false;
if (!pps->print_upto_tag("name", lhs_name_))
return false;
if (!pps->print_upto_tag("rhs", rhs_))
return false;
return true;
} else {
pps->write("<Define");
pps->newline_pretty_tag(ppii.ci1(), "name", lhs_name_);
pps->newline_pretty_tag(ppii.ci1(), "rhs", rhs_);
pps->write(">");
return false;
}
#endif
}
// ----- DefineExprAccess -----
@ -117,6 +92,12 @@ namespace xo {
nullptr /*rhs*/);
}
void
DefineExprAccess::assign_lhs_name(const std::string & x)
{
this->lhs_var_->assign_name(x);
}
void
DefineExprAccess::assign_rhs(const rp<Expression> & x)
{
@ -125,6 +106,10 @@ namespace xo {
this->rhs_ = x;
if (x) {
if (lhs_var_ && !lhs_var_->valuetype()) {
this->lhs_var_->assign_valuetype(x->valuetype());
}
this->assign_valuetype(x->valuetype());
}

View file

@ -97,7 +97,7 @@ namespace xo {
LocalEnv::upsert_local(bp<Variable> target) {
for (auto & var : this->argv_) {
if (var->name() == target->name()) {
/* replace existing variable. May change its type */
/* replace existing variable. This may change its type */
var = target.promote();
return;
}

View file

@ -3,6 +3,16 @@
#include "Primitive.hpp"
extern "C" {
bool
cmp_eq2_i64(std::int64_t x, std::int64_t y) {
return x == y;
}
bool
cmp_ne2_i64(std::int64_t x, std::int64_t y) {
return x != y;
}
double
add2_f64(double x, double y) {
return x + y;
@ -26,6 +36,34 @@ extern "C" {
namespace xo {
namespace ast {
auto
Primitive_cmp_i64::make_cmp_eq2_i64() -> rp<PrimitiveType>
{
static rp<PrimitiveType> s_retval;
if (!s_retval)
s_retval = Primitive::make("cmp_eq2_i64",
&cmp_eq2_i64,
true /*explicit_symbol_def*/,
llvmintrinsic::i_eq);
return s_retval;
}
auto
Primitive_cmp_i64::make_cmp_ne2_i64() -> rp<PrimitiveType>
{
static rp<PrimitiveType> s_retval;
if (!s_retval)
s_retval = Primitive::make("cmp_ne2_i64",
&cmp_ne2_i64,
true /*explicit_symbol_def*/,
llvmintrinsic::i_ne);
return s_retval;
}
auto
Primitive_f64::make_add2_f64() -> rp<PrimitiveType>
{

View file

@ -49,47 +49,6 @@ namespace xo {
rtag("type", print::quot(this->valuetype()
? this->valuetype()->short_name()
: "nullptr")));
#ifdef OBSOLETE
ppstate * pps = ppii.pps();
if (ppii.upto()) {
if (!pps->print_upto("<Variable"))
return false;
if (!pps->print_upto_tag("name", name_))
return false;
if (this->valuetype()) {
if (!pps->print_upto_tag("type", this->valuetype()->short_name()))
return false;
} else {
if (!pps->print_upto_tag("type", "nullptr"))
return false;
}
pps->write(">");
return true;
} else {
pps->write("<Variable");
pps->newline_pretty_tag(ppii.ci1(), "name", name_);
/* use tag instead of rtag for type,
* since not guaranteed to print machine-readably
*/
if (this->valuetype()) {
pps->newline_indent(ppii.ci1());
pps->pretty(xtag("type", this->valuetype()->short_name()));
} else {
pps->newline_pretty_tag(ppii.ci1(), "type", "nullptr");
}
pps->write(">");
return false;
}
#endif
}
} /*namespace ast*/
} /*namespace xo*/