Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
prozacchiwawa committed Feb 8, 2022
1 parent d8b8636 commit 6d319d0
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 113 deletions.
43 changes: 23 additions & 20 deletions src/compiler/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::compiler::compiler::run_optimizer;
use crate::compiler::comptypes::{
cons_of_string_map, foldM, join_vecs_to_string, list_to_cons, mapM, with_heading, Binding,
BodyForm, Callable, CompileErr, CompileForm, CompiledCode, CompilerOpts, DefunCall, HelperForm,
InlineFunction, PrimaryCodegen, LetFormKind
InlineFunction, LetFormKind, PrimaryCodegen,
};
use crate::compiler::debug::{build_swap_table_mut, relabel};
use crate::compiler::frontend::compile_bodyform;
Expand Down Expand Up @@ -346,9 +346,12 @@ pub fn process_macro_call(
let args_to_macro = list_to_cons(l.clone(), &converted_args);
build_swap_table_mut(&mut swap_table, &args_to_macro);

let arg_strs: Vec<String> =
args.iter().map(|x| x.to_sexp().to_string()).collect();
println!("process macro args {:?} code {}", arg_strs, code.to_string());
let arg_strs: Vec<String> = args.iter().map(|x| x.to_sexp().to_string()).collect();
println!(
"process macro args {:?} code {}",
arg_strs,
code.to_string()
);

run(
allocator,
Expand Down Expand Up @@ -488,17 +491,15 @@ fn compile_call(
Rc::new(code),
),

Callable::CallInline(l, inline) => {
replace_in_inline(
allocator,
runner.clone(),
opts.clone(),
compiler,
l.clone(),
&inline,
&tl,
)
},
Callable::CallInline(l, inline) => replace_in_inline(
allocator,
runner.clone(),
opts.clone(),
compiler,
l.clone(),
&inline,
&tl,
),

Callable::CallDefun(l, lookup) => {
generate_args_code(allocator, runner, opts.clone(), compiler, l.clone(), &tl)
Expand Down Expand Up @@ -923,7 +924,8 @@ fn process_helper_let_bindings(
match result[i].clone() {
HelperForm::Defun(l, name, inline, args, body) => {
let context = if (inline) { Some(args.clone()) } else { None };
let helper_result = hoist_body_let_binding(compiler, context, args.clone(), body.clone());
let helper_result =
hoist_body_let_binding(compiler, context, args.clone(), body.clone());
let hoisted_helpers = helper_result.0;
let hoisted_body = helper_result.1.clone();

Expand Down Expand Up @@ -1028,8 +1030,9 @@ fn finalize_env_(
c,
l.clone(),
res,
&synthesize_args(res.args.clone())
).map(|x| x.1.clone()),
&synthesize_args(res.args.clone()),
)
.map(|x| x.1.clone()),
None => {
/* Parentfns are functions in progress in the parent */
if !c.parentfns.get(v).is_none() {
Expand Down Expand Up @@ -1069,7 +1072,7 @@ fn finalize_env_(
.map(|r| Rc::new(SExp::Cons(l.clone(), h.clone(), r.clone())))
}),

_ => Ok(env.clone())
_ => Ok(env.clone()),
}
}

Expand All @@ -1088,7 +1091,7 @@ fn finalize_env(
l.clone(),
h.clone(),
),
_ => Ok(c.env.clone())
_ => Ok(c.env.clone()),
}
}

Expand Down
13 changes: 6 additions & 7 deletions src/compiler/comptypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl InlineFunction {
Rc::new(SExp::Cons(
self.body.loc(),
self.args.clone(),
self.body.to_sexp()
self.body.to_sexp(),
))
}
}
Expand Down Expand Up @@ -66,7 +66,7 @@ pub struct Binding {
#[derive(Clone, Debug)]
pub enum LetFormKind {
Parallel,
Sequential
Sequential,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -271,11 +271,10 @@ impl BodyForm {
bindings.iter().map(|x| x.to_sexp()).collect();
let bindings_cons = list_to_cons(loc.clone(), &translated_bindings);
let translated_body = body.to_sexp();
let marker =
match kind {
LetFormKind::Parallel => "let",
LetFormKind::Sequential => "let*"
};
let marker = match kind {
LetFormKind::Parallel => "let",
LetFormKind::Sequential => "let*",
};
Rc::new(SExp::Cons(
loc.clone(),
Rc::new(SExp::atom_from_string(loc.clone(), &marker.to_string())),
Expand Down
18 changes: 10 additions & 8 deletions src/compiler/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use std::rc::Rc;
use crate::classic::clvm::__type_compatibility__::bi_one;

use crate::compiler::comptypes::{
list_to_cons, Binding, BodyForm, CompileErr, CompileForm, CompilerOpts, HelperForm, ModAccum, LetFormKind,
list_to_cons, Binding, BodyForm, CompileErr, CompileForm, CompilerOpts, HelperForm,
LetFormKind, ModAccum,
};
use crate::compiler::preprocessor::preprocess;
use crate::compiler::rename::rename_children_compileform;
Expand Down Expand Up @@ -278,17 +279,18 @@ pub fn compile_bodyform(body: Rc<SExp>) -> Result<BodyForm, CompileErr> {

match tail.proper_list() {
Some(v) => {
if *atom_name == "let".as_bytes().to_vec() || *atom_name == "let*".as_bytes().to_vec() {
if *atom_name == "let".as_bytes().to_vec()
|| *atom_name == "let*".as_bytes().to_vec()
{
if v.len() != 2 {
return finish_err("let");
}

let kind =
if *atom_name == "let".as_bytes().to_vec() {
LetFormKind::Parallel
} else {
LetFormKind::Sequential
};
let kind = if *atom_name == "let".as_bytes().to_vec() {
LetFormKind::Parallel
} else {
LetFormKind::Sequential
};

let bindings = v[0].clone();
let body = v[1].clone();
Expand Down
125 changes: 71 additions & 54 deletions src/compiler/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ use crate::classic::clvm::__type_compatibility__::bi_one;
use crate::classic::clvm_tools::stages::stage_0::TRunProgram;

use crate::compiler::codegen::{
generate_expr_code,
get_callable,
get_call_name,
process_macro_call
generate_expr_code, get_call_name, get_callable, process_macro_call,
};
use crate::compiler::comptypes::{
BodyForm, CompileErr, CompiledCode, CompilerOpts, InlineFunction, PrimaryCodegen, Callable
BodyForm, Callable, CompileErr, CompiledCode, CompilerOpts, InlineFunction, PrimaryCodegen,
};
use crate::compiler::sexp::{decode_string, SExp};
use crate::compiler::srcloc::Srcloc;
Expand All @@ -26,11 +23,8 @@ fn apply_fn(loc: Srcloc, name: String, expr: Rc<BodyForm>) -> Rc<BodyForm> {
Rc::new(BodyForm::Call(
loc.clone(),
vec![
Rc::new(BodyForm::Value(SExp::atom_from_string(
loc.clone(),
&name,
))),
expr
Rc::new(BodyForm::Value(SExp::atom_from_string(loc.clone(), &name))),
expr,
],
))
}
Expand All @@ -39,7 +33,7 @@ fn at_form(loc: Srcloc, path: Number) -> Rc<BodyForm> {
apply_fn(
loc.clone(),
"@".to_string(),
Rc::new(BodyForm::Value(SExp::Integer(loc.clone(), path.clone())))
Rc::new(BodyForm::Value(SExp::Integer(loc.clone(), path.clone()))),
)
}

Expand Down Expand Up @@ -68,63 +62,92 @@ fn enlist_remaining_args(loc: Srcloc, arg_choice: usize, args: &Vec<Rc<BodyForm>
let i = args.len() - i_reverse - 1;
result_body = BodyForm::Call(
loc.clone(),
vec!(
Rc::new(BodyForm::Value(SExp::atom_from_string(loc.clone(), &"c".to_string()))),
vec![
Rc::new(BodyForm::Value(SExp::atom_from_string(
loc.clone(),
&"c".to_string(),
))),
args[i].clone(),
Rc::new(result_body)
)
Rc::new(result_body),
],
);
}

Rc::new(result_body)
}

fn pick_value_from_arg_element(match_args: Rc<SExp>, provided: Rc<BodyForm>, apply: &dyn Fn(Rc<BodyForm>) -> Rc<BodyForm>, name: Vec<u8>) -> Option<Rc<BodyForm>> {
fn pick_value_from_arg_element(
match_args: Rc<SExp>,
provided: Rc<BodyForm>,
apply: &dyn Fn(Rc<BodyForm>) -> Rc<BodyForm>,
name: Vec<u8>,
) -> Option<Rc<BodyForm>> {
match match_args.borrow() {
SExp::Cons(l, a, b) => {
let matched_a = pick_value_from_arg_element(a.clone(), provided.clone(), &|x| {
apply_fn(l.clone(), "f".to_string(), apply(x.clone()))
}, name.clone());
let matched_b = pick_value_from_arg_element(b.clone(), provided.clone(), &|x| {
apply_fn(l.clone(), "r".to_string(), apply(x.clone()))
}, name.clone());
let matched_a = pick_value_from_arg_element(
a.clone(),
provided.clone(),
&|x| apply_fn(l.clone(), "f".to_string(), apply(x.clone())),
name.clone(),
);
let matched_b = pick_value_from_arg_element(
b.clone(),
provided.clone(),
&|x| apply_fn(l.clone(), "r".to_string(), apply(x.clone())),
name.clone(),
);

let result = match (matched_a, matched_b) {
(Some(a), _) => Some(a),
(_, Some(b)) => Some(b),
_ => None
_ => None,
};

result
},
}
SExp::Atom(l, a) => {
if *a == name {
Some(apply(provided))
} else {
None
}
}
_ => None
_ => None,
}
}

fn arg_lookup(match_args: Rc<SExp>, arg_choice: usize, args: &Vec<Rc<BodyForm>>, name: Vec<u8>) -> Option<Rc<BodyForm>> {
fn arg_lookup(
match_args: Rc<SExp>,
arg_choice: usize,
args: &Vec<Rc<BodyForm>>,
name: Vec<u8>,
) -> Option<Rc<BodyForm>> {
match match_args.borrow() {
SExp::Cons(l, f, r) => {
match pick_value_from_arg_element(f.clone(), args[arg_choice].clone(), &|x| x.clone(), name.clone()) {
match pick_value_from_arg_element(
f.clone(),
args[arg_choice].clone(),
&|x| x.clone(),
name.clone(),
) {
Some(x) => Some(x),
None => arg_lookup(r.clone(), arg_choice + 1, args, name.clone())
None => arg_lookup(r.clone(), arg_choice + 1, args, name.clone()),
}
},
_ => pick_value_from_arg_element(match_args.clone(), enlist_remaining_args(match_args.loc(), arg_choice, args), &|x: Rc<BodyForm>| x.clone(), name)
}
_ => pick_value_from_arg_element(
match_args.clone(),
enlist_remaining_args(match_args.loc(), arg_choice, args),
&|x: Rc<BodyForm>| x.clone(),
name,
),
}
}

fn get_inline_callable(
opts: Rc<dyn CompilerOpts>,
compiler: &PrimaryCodegen,
loc: Srcloc,
list_head: Rc<BodyForm>
list_head: Rc<BodyForm>,
) -> Result<Callable, CompileErr> {
let list_head_borrowed: &BodyForm = list_head.borrow();
let name = get_call_name(loc.clone(), list_head_borrowed.clone())?;
Expand Down Expand Up @@ -154,17 +177,16 @@ fn replace_inline_body(
if i == 0 {
new_args.push(call_args[i].clone());
} else {
let replaced =
replace_inline_body(
allocator,
runner.clone(),
opts.clone(),
compiler,
call_args[i].loc(),
inline,
&args.clone(),
call_args[i].clone()
)?;
let replaced = replace_inline_body(
allocator,
runner.clone(),
opts.clone(),
compiler,
call_args[i].loc(),
inline,
&args.clone(),
call_args[i].clone(),
)?;
new_args.push(replaced);
}
}
Expand All @@ -187,20 +209,20 @@ fn replace_inline_body(
l.clone(),
&new_inline,
&pass_on_args,
new_inline.body.clone()
new_inline.body.clone(),
)
},
}
_ => {
let call = BodyForm::Call(l.clone(), new_args);
println!("passing on call: {}", call.to_sexp().to_string());
Ok(Rc::new(call))
}
}
},
}
BodyForm::Value(SExp::Atom(_, a)) => arg_lookup(inline.args.clone(), 0, args, a.clone())
.map(|x| Ok(x.clone()))
.unwrap_or_else(|| Ok(expr.clone())),
_ => Ok(expr.clone())
_ => Ok(expr.clone()),
}
}

Expand All @@ -223,14 +245,9 @@ pub fn replace_in_inline(
inline,
args,
inline.body.clone(),
).and_then(|x| {
)
.and_then(|x| {
println!("generate expr code {}", x.to_sexp().to_string());
generate_expr_code(
allocator,
runner,
opts,
compiler,
x.clone()
)
generate_expr_code(allocator, runner, opts, compiler, x.clone())
})
}
Loading

0 comments on commit 6d319d0

Please sign in to comment.