Skip to content

Commit

Permalink
Merge pull request #41 from Chia-Network/20230809-compiled-macros
Browse files Browse the repository at this point in the history
20230809 compiled macros
  • Loading branch information
prozacchiwawa authored Sep 27, 2023
2 parents 3ac012a + e8aee71 commit dbe7641
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 550 deletions.
35 changes: 23 additions & 12 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,17 @@ fn fe_opt(
})
}

pub fn compile_pre_forms(
context: &mut BasicCompileContext,
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 mut wrapper = CompileContextWrapper::new(allocator, runner, symbol_table);
let p1 = if opts.frontend_opt() {
// Front end optimization
fe_opt(context, opts.clone(), p0)?
fe_opt(&mut wrapper.context, opts.clone(), p0)?
} else {
p0
};
Expand All @@ -202,7 +202,20 @@ pub fn compile_pre_forms(
};

// generate code from AST, optionally with optimization
codegen(context, opts, &p2)
codegen(&mut wrapper.context, opts, &p2)
}

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(
Expand All @@ -213,8 +226,7 @@ pub fn compile_file(
symbol_table: &mut HashMap<String, String>,
) -> Result<SExp, CompileErr> {
let pre_forms = parse_sexp(Srcloc::start(&opts.filename()), content.bytes())?;
let mut context_wrapper = CompileContextWrapper::new(allocator, runner, symbol_table);
compile_pre_forms(&mut context_wrapper.context, opts, &pre_forms)
compile_pre_forms(allocator, runner, opts, &pre_forms, symbol_table)
}

pub fn run_optimizer(
Expand Down Expand Up @@ -376,8 +388,7 @@ impl CompilerOpts for DefaultCompilerOpts {
symbol_table: &mut HashMap<String, String>,
) -> Result<SExp, CompileErr> {
let me = Rc::new(self.clone());
let mut context_wrapper = CompileContextWrapper::new(allocator, runner, symbol_table);
compile_pre_forms(&mut context_wrapper.context, me, &[sexp])
compile_pre_forms(allocator, runner, me, &[sexp], symbol_table)
}
}

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, truthy, PrimOverride};
use crate::compiler::clvm::{run, truthy};
use crate::compiler::codegen::{codegen, hoist_assign_form};
use crate::compiler::compiler::is_at_capture;
use crate::compiler::comptypes::{
Expand Down Expand Up @@ -142,61 +142,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 @@ -1732,7 +1677,7 @@ impl<'info> Evaluator {
self.prims.clone(),
prim,
args,
Some(self),
None,
Some(PRIM_RUN_LIMIT),
)
.map_err(|e| match e {
Expand Down
10 changes: 5 additions & 5 deletions src/compiler/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,7 @@ fn make_let_bindings(
opts: Rc<dyn CompilerOpts>,
body: Rc<SExp>,
) -> Result<Vec<Rc<Binding>>, CompileErr> {
let err = Err(CompileErr(
body.loc(),
"Bad binding tail ".to_string() + &body.to_string(),
));
let err = Err(CompileErr(body.loc(), format!("Bad binding tail {body:?}")));
let do_atomize = if opts.dialect().strict {
|a: &SExp| -> SExp { a.atomize() }
} else {
Expand All @@ -283,7 +280,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 dbe7641

Please sign in to comment.