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

@ -28,6 +28,13 @@ namespace xo {
static rp<Apply> make(const rp<Expression> & fn,
const std::vector<rp<Expression>> & argv);
/** create apply-expression to compare two 64-bit integers **/
static rp<Apply> make_cmp_eq_i64(const rp<Expression> & lhs,
const rp<Expression> & rhs);
/** 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 to add two 64-bit floating-point numbers **/
static rp<Apply> make_add2_f64(const rp<Expression> & lhs,
const rp<Expression> & rhs);
@ -103,7 +110,7 @@ namespace xo {
virtual void display(std::ostream & os) const override;
virtual std::uint32_t pretty_print(const ppindentinfo & ppii) const override;
private:
protected:
Apply(TypeDescr apply_valuetype,
const rp<Expression> & fn,
const std::vector<rp<Expression>> & argv)
@ -111,7 +118,7 @@ namespace xo {
fn_{fn}, argv_(argv)
{}
private:
protected:
/** function to invoke **/
rp<Expression> fn_;
/** argument expressions, in l-to-r order **/
@ -152,6 +159,33 @@ namespace xo {
return Apply::make(fn, argv);
} /*make_apply*/
/** @class ApplyAccess
* @brief Apply with writeable members
*
* Convenient when scaffolding a parser,
* e.g. see xo-parser
**/
class ApplyAccess : public Apply {
public:
static rp<ApplyAccess> make_empty();
/** assign function being called to @p fn **/
void assign_fn(const rp<Expression>& fn);
/** assign expression for argument i, counting from 1.
* can use @p i = 0 as alternative to @ ref assign_fn
**/
void assign_arg(size_t i, const rp<Expression>& arg);
// inherited from GeneralizedExpression..
// void assign_valuetype(TypeDescr apply_valuetype);
private:
ApplyAccess(TypeDescr apply_valuetype,
const rp<Expression>& fn,
const std::vector<rp<Expression>>& argv)
: Apply(apply_valuetype, fn, argv) {}
};
} /*namespace ast*/
} /*namespace xo*/

View file

@ -21,6 +21,10 @@ namespace xo {
* is equivalent to
*
* (lambda (foo) body...)(rhsexpr)
*
* Promise:
* - memory location of @ref lhs_var_ is determined when parent DefineExpr
* constructed, and is stable across calls to @ref DefineExpr::assign_lhs_name
**/
class DefineExpr : public Expression {
public:
@ -32,10 +36,10 @@ namespace xo {
return bp<DefineExpr>::from(x);
}
const std::string & lhs_name() const { return lhs_name_; }
const std::string & lhs_name() const;
const rp<Expression> & rhs() const { return rhs_; }
rp<Variable> lhs_variable() const;
const rp<Variable>& lhs_variable() const { return lhs_var_; }
std::set<std::string> calc_free_variables() const;
@ -88,7 +92,7 @@ namespace xo {
protected:
/** symbol name for this definition **/
std::string lhs_name_;
rp<Variable> lhs_var_;
/** right-hand side of definition **/
rp<Expression> rhs_;
@ -108,10 +112,7 @@ namespace xo {
rp<Expression> rhs);
static rp<DefineExprAccess> make_empty();
void assign_lhs_name(const std::string & x) {
this->lhs_name_ = x;
}
void assign_lhs_name(const std::string & x);
void assign_rhs(const rp<Expression> & x);
private:

View file

@ -191,6 +191,19 @@ namespace xo {
return Primitive<FunctionPointer>::make(name, x, explicit_symbol_def, intrinsic);
}
/** builtin primitives :: i64 x i64 -> bool **/
class Primitive_cmp_i64 : public Primitive<bool (*)(std::int64_t, std::int64_t)> {
public:
using PrimitiveType = Primitive<bool (*)(std::int64_t, std::int64_t)>;
public:
/** eq2_i64: compare two 64-bit integers for equality **/
static rp<PrimitiveType> make_cmp_eq2_i64();
/** ne2_i64: compare two 64-bit integers for inequality **/
static rp<PrimitiveType> make_cmp_ne2_i64();
};
/** builtin primitives :: f64 x f64 -> f64 **/
class Primitive_f64 : public Primitive<double (*)(double, double)> {
public:
using PrimitiveType = Primitive<double (*)(double, double)>;

View file

@ -40,6 +40,8 @@ namespace xo {
return bp<Variable>::from(x);
}
void assign_name(const std::string & name) { name_ = name; }
const std::string & name() const { return name_; }
virtual std::set<std::string> get_free_variables() const override {
@ -76,8 +78,10 @@ namespace xo {
private:
/** variable name **/
std::string name_;
/** navigate environment via this path to find runtime memory
* location for this variable
/** Eventually: navigate environment via this path to find runtime memory
* location for this variable.
*
* Establish via @ref attach_envs
**/
binding_path path_;
}; /*Variable*/

View file

@ -62,6 +62,26 @@ namespace xo {
/** -> IRBuilder::CreateUdiv (divide 2 unsigned integers) **/
i_udiv,
/** -> IRBuilder::CreateICmpEQ (test integers for equality) **/
i_eq,
/** -> IRBuilder::CreateICmpNE (test integers for inequality) **/
i_ne,
/** -> IRBuilder::CreateICmpSGT (test signed integers for greater) **/
i_sgt,
/** -> IRBuilder::CreateICmpSGE (test signed integers for greater-or-equal) **/
i_sge,
/** -> IRBuilder::CreateICmpSLT (test signed integers for lesser) **/
i_slt,
/** -> IRBuilder::CreateCmpSLE (test signed integers for lesser-or-equal) **/
i_sle,
// TODO: unsigned comparisons
/** -> IRBuilder::CreateFadd (add 2 floating-point numbers) **/
fp_add,
@ -74,6 +94,8 @@ namespace xo {
/** -> IRBuilder::CreateFdiv (divide 2 floating-point numbers) **/
fp_div,
// TODO: floating-point comparisons
/**
* want to do whatever llvm IR @c llvm.sqrt.f64 and friends do.
* Not sure if that's an always-available function of something else
@ -107,6 +129,14 @@ namespace xo {
case llvmintrinsic::i_mul: return "i_mul";
case llvmintrinsic::i_sdiv: return "i_sdiv";
case llvmintrinsic::i_udiv: return "i_udiv";
case llvmintrinsic::i_eq: return "i_eq";
case llvmintrinsic::i_ne: return "i_ne";
case llvmintrinsic::i_sgt: return "i_sgt";
case llvmintrinsic::i_sge: return "i_sge";
case llvmintrinsic::i_slt: return "i_slt";
case llvmintrinsic::i_sle: return "i_sle";
case llvmintrinsic::fp_add: return "fp_add";
case llvmintrinsic::fp_sub: return "fp_sub";
case llvmintrinsic::fp_mul: return "fp_mul";

View file

@ -14,5 +14,19 @@ namespace xo {
return x.pretty_print(ppii);
}
};
template <>
struct ppdetail<xo::ast::LocalEnv*> {
static bool print_pretty(const ppindentinfo & ppii, const xo::ast::LocalEnv* x) {
if (x) {
return x->pretty_print(ppii);
} else {
ppii.pps()->write("<nullptr ");
ppii.pps()->write(reflect::type_name<xo::ast::LocalEnv>());
ppii.pps()->write(">");
return ppii.pps()->has_margin();
}
}
};
}
}