Skip to content

Instantly share code, notes, and snippets.

@zicklag
Created March 24, 2023 15:06
Show Gist options
  • Save zicklag/9531beef260f5ee9fdd25be38b81f222 to your computer and use it in GitHub Desktop.
Save zicklag/9531beef260f5ee9fdd25be38b81f222 to your computer and use it in GitHub Desktop.
HVM runtime constructor generation.
use hvm::{get_loc, load_ptr, readback, runtime, Core};
fn main() {
let script = std::env::args().nth(1).unwrap();
let script = std::fs::read_to_string(script).unwrap();
let script = hvm::language::syntax::read_file(&script).unwrap();
let mut runtime = {
let heap_size = 5 * hvm::CELLS_PER_MB;
let threads = 3;
let heap = hvm::new_heap(heap_size, threads);
let book = hvm::language::rulebook::gen_rulebook(&script);
let tids = hvm::new_tids(threads);
let mut prog = hvm::Program::new();
prog.add_book(&book);
prog.funs.insert(
prog.nams
.data
.iter()
.enumerate()
.find(|x| x.1 == &Some(String::from("Test.make_constructor")))
.map(|x| x.0 as u64)
.unwrap(),
hvm::runtime::Function::Compiled {
smap: [false; 2].into(),
visit: |_| false,
apply: |ctx| {
let Some(ctr_name) =
readback::as_string(ctx.heap, ctx.prog, &[ctx.tid], get_loc(ctx.term, 0))
else {
panic!("arg not a string");
};
let _args = get_loc(ctx.term, 1);
let Some(fn_id) = ctx
.prog
.nams
.data
.iter()
.enumerate()
.find(|x| x.1.as_ref() == Some(&ctr_name))
.map(|x| x.0 as u64) else {
panic!("unbound constructor name");
};
let ctr = runtime::alloc_closed_core(
ctx.heap,
ctx.prog,
ctx.tid,
&Core::Ctr {
func: fn_id,
args: vec![],
},
);
// runtime::link(ctx.heap, *ctx.host, load_arg(ctx.heap, ctx.term, 0));
runtime::link(ctx.heap, *ctx.host, load_ptr(ctx.heap, ctr));
// runtime::collect(
// ctx.heap,
// &ctx.prog.aris,
// ctx.tid,
// load_ptr(ctx.heap, get_loc(ctx.term, 0)),
// );
// runtime::free(ctx.heap, ctx.tid, get_loc(ctx.term, 0), 2);
true
},
},
);
hvm::runtime::Runtime {
heap,
prog,
book,
tids,
dbug: false,
}
};
let output = runtime.eval("(Main)");
println!("{}", output);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment