xo-jit: handle variable refs + lambda defs

This commit is contained in:
Roland Conybeare 2024-06-13 18:00:16 -04:00
commit 731b91889c
3 changed files with 132 additions and 1 deletions

View file

@ -4,6 +4,8 @@
#include "xo/expression/Constant.hpp"
#include "xo/expression/Primitive.hpp"
#include "xo/expression/Apply.hpp"
#include "xo/expression/Lambda.hpp"
#include "xo/expression/Variable.hpp"
#include <iostream>
int
@ -12,6 +14,8 @@ main() {
using xo::ast::make_constant;
using xo::ast::make_primitive;
using xo::ast::make_apply;
using xo::ast::make_var;
using xo::ast::make_lambda;
using xo::xtag;
using std::cerr;
using std::endl;
@ -75,6 +79,35 @@ main() {
<< endl;
}
}
{
/* (lambda (x) (sin (cos x))) */
auto sin = make_primitive("sin", ::sin);
auto cos = make_primitive("cos", ::cos);
auto x_var = make_var("x");
auto call1 = make_apply(cos, x_var); /* (cos x) */
auto call2 = make_apply(sin, call1); /* (sin (cos x)) */
/* (define (lm_1 x) (sin (cos x))) */
auto lambda = make_lambda("lm_1",
{"x"},
call2);
auto llvm_ircode = jit->codegen(lambda);
if (llvm_ircode) {
/* note: llvm:errs() is 'raw stderr stream' */
cerr << "ex1 llvm_ircode:" << endl;
llvm_ircode->print(llvm::errs());
cerr << endl;
} else {
cerr << "ex1: code generation failed"
<< xtag("expr", lambda)
<< endl;
}
}
}
/** end ex1.cpp **/