Skip to content

Commit

Permalink
Compiled macros rather than interpreted in the evaluator
Browse files Browse the repository at this point in the history
  • Loading branch information
prozacchiwawa committed Aug 9, 2023
1 parent 5289f93 commit f183d5b
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 504 deletions.
26 changes: 21 additions & 5 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,13 @@ fn fe_opt(
})
}

pub fn compile_pre_forms(
pub fn compile_from_compileform(
allocator: &mut Allocator,
runner: Rc<dyn TRunProgram>,
opts: Rc<dyn CompilerOpts>,
pre_forms: &[Rc<SExp>],
p0: CompileForm,
symbol_table: &mut HashMap<String, String>,
) -> Result<SExp, CompileErr> {
// Resolve includes, convert program source to lexemes
let p0 = frontend(opts.clone(), pre_forms)?;

let p1 = if opts.frontend_opt() {
// Front end optimization
fe_opt(allocator, runner.clone(), opts.clone(), p0)?
Expand Down Expand Up @@ -197,6 +194,25 @@ pub fn compile_pre_forms(
codegen(allocator, runner, opts, &p2, symbol_table)
}

pub fn compile_pre_forms(
allocator: &mut Allocator,
runner: Rc<dyn TRunProgram>,
opts: Rc<dyn CompilerOpts>,
pre_forms: &[Rc<SExp>],
symbol_table: &mut HashMap<String, String>,
) -> Result<SExp, CompileErr> {
// Resolve includes, convert program source to lexemes
let p0 = frontend(opts.clone(), pre_forms)?;

compile_from_compileform(
allocator,
runner,
opts,
p0,
symbol_table
)
}

pub fn compile_file(
allocator: &mut Allocator,
runner: Rc<dyn TRunProgram>,
Expand Down
59 changes: 2 additions & 57 deletions src/compiler/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use clvm_rs::allocator::Allocator;
use crate::classic::clvm::__type_compatibility__::{bi_one, bi_zero};
use crate::classic::clvm_tools::stages::stage_0::TRunProgram;

use crate::compiler::clvm::{run, PrimOverride};
use crate::compiler::clvm::run;
use crate::compiler::codegen::codegen;
use crate::compiler::compiler::is_at_capture;
use crate::compiler::comptypes::{
Expand Down Expand Up @@ -135,61 +135,6 @@ pub struct Evaluator {
ignore_exn: bool,
}

fn compile_to_run_err(e: CompileErr) -> RunFailure {
match e {
CompileErr(l, e) => RunFailure::RunErr(l, e),
}
}

impl PrimOverride for Evaluator {
fn try_handle(
&self,
head: Rc<SExp>,
_context: Rc<SExp>,
tail: Rc<SExp>,
) -> Result<Option<Rc<SExp>>, RunFailure> {
let have_args: Vec<Rc<BodyForm>> = if let Some(args_list) = tail.proper_list() {
args_list
.iter()
.map(|e| Rc::new(BodyForm::Quoted(e.clone())))
.collect()
} else {
return Ok(None);
};

if let SExp::Atom(hl, head_atom) = head.borrow() {
let mut call_args = vec![Rc::new(BodyForm::Value(SExp::Atom(
hl.clone(),
head_atom.clone(),
)))];
call_args.append(&mut have_args.clone());
// Primitives can't have tails.
let call_form = Rc::new(BodyForm::Call(head.loc(), call_args, None));

for x in self.extensions.iter() {
if let Some(res) = x
.try_eval(
self,
Rc::new(SExp::Nil(head.loc())),
&HashMap::new(),
&head.loc(),
head_atom,
&have_args,
call_form.clone(),
)
.map_err(compile_to_run_err)?
{
return dequote(head.loc(), res)
.map_err(compile_to_run_err)
.map(Some);
}
}
}

Ok(None)
}
}

fn select_helper(bindings: &[HelperForm], name: &[u8]) -> Option<HelperForm> {
for b in bindings.iter() {
if b.name() == name {
Expand Down Expand Up @@ -1383,7 +1328,7 @@ impl<'info> Evaluator {
self.prims.clone(),
prim,
args,
Some(self),
None,
Some(PRIM_RUN_LIMIT),
)
.map_err(|e| match e {
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,9 @@ fn make_let_bindings(
) -> Result<Vec<Rc<Binding>>, CompileErr> {
let err = Err(CompileErr(
body.loc(),
"Bad binding tail ".to_string() + &body.to_string(),
format!("Bad binding tail {body:?}")
));
eprintln!("make_let_bindings {body}");
match body.borrow() {
SExp::Nil(_) => Ok(vec![]),
SExp::Cons(_, head, tl) => head
Expand All @@ -271,7 +272,10 @@ fn make_let_bindings(
result.append(&mut rest_bindings);
Ok(result)
}
_ => err.clone(),
_ => {
eprintln!("crap {body:?}");
err.clone()
}
})
.unwrap_or_else(|| err.clone()),
_ => err,
Expand Down
Loading

0 comments on commit f183d5b

Please sign in to comment.