Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
fmt + clippy + typecheck tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prozacchiwawa committed Jul 25, 2023
1 parent 8d0ebbd commit 44efb75
Show file tree
Hide file tree
Showing 13 changed files with 354 additions and 243 deletions.
94 changes: 45 additions & 49 deletions src/compiler/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::compiler::inline::{replace_in_inline, synthesize_args};
use crate::compiler::lambda::lambda_codegen;
use crate::compiler::prims::{primapply, primcons, primquote};
use crate::compiler::runtypes::RunFailure;
use crate::compiler::sexp::{decode_string, enlist, SExp};
use crate::compiler::sexp::{decode_string, SExp};
use crate::compiler::srcloc::Srcloc;
use crate::compiler::StartOfCodegenOptimization;
use crate::compiler::{BasicCompileContext, CompileContextWrapper};
Expand Down Expand Up @@ -65,7 +65,7 @@ fn cons_bodyform(loc: Srcloc, left: Rc<BodyForm>, right: Rc<BodyForm>) -> BodyFo
left,
right,
],
None
None,
)
}

Expand Down Expand Up @@ -396,27 +396,22 @@ fn generate_args_code(
if list.is_empty() && tail.is_none() {
Ok(Rc::new(SExp::Nil(l)))
} else {
let mut compiled_args: Rc<SExp> =
if let Some(t) = tail.as_ref() {
generate_expr_code(context, opts.clone(), compiler, t.clone())?.1
} else {
Rc::new(SExp::Nil(l.clone()))
};
let mut compiled_args: Rc<SExp> = if let Some(t) = tail.as_ref() {
generate_expr_code(context, opts.clone(), compiler, t.clone())?.1
} else {
Rc::new(SExp::Nil(l.clone()))
};

for hd in list.iter().rev() {
let generated =
generate_expr_code(context, opts.clone(), compiler, hd.clone())?.1;
let generated = generate_expr_code(context, opts.clone(), compiler, hd.clone())?.1;
if with_primcons {
compiled_args = Rc::new(primcons(
generated.loc(),
generated.clone(),
compiled_args
));
compiled_args =
Rc::new(primcons(generated.loc(), generated.clone(), compiled_args));
} else {
compiled_args = Rc::new(SExp::Cons(
generated.loc(),
generated.clone(),
compiled_args
compiled_args,
));
}
}
Expand Down Expand Up @@ -466,7 +461,7 @@ fn compile_call(
opts: Rc<dyn CompilerOpts>,
compiler: &PrimaryCodegen,
list: &[Rc<BodyForm>],
tail: Option<Rc<BodyForm>>
tail: Option<Rc<BodyForm>>,
) -> Result<CompiledCode, CompileErr> {
let arg_string_list: Vec<Vec<u8>> = list
.iter()
Expand Down Expand Up @@ -495,28 +490,28 @@ fn compile_call(
process_macro_call(context, opts.clone(), compiler, l, tl, Rc::new(code))
}

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

Callable::CallDefun(l, lookup) => {
generate_args_code(context, opts.clone(), compiler, l.clone(), &tl, tail, true).and_then(
|args| {
process_defun_call(
opts.clone(),
compiler,
l.clone(),
args,
Rc::new(lookup),
)
},
)
generate_args_code(context, opts.clone(), compiler, l.clone(), &tl, tail, true)
.and_then(|args| {
process_defun_call(opts.clone(), compiler, l.clone(), args, Rc::new(lookup))
})
}

Callable::CallPrim(l, p) => generate_args_code(context, opts, compiler, l.clone(), &tl, None, false)
.map(|args| {
CompiledCode(l.clone(), Rc::new(SExp::Cons(l, Rc::new(p), args)))
}),
Callable::CallPrim(l, p) => {
generate_args_code(context, opts, compiler, l.clone(), &tl, None, false)
.map(|args| CompiledCode(l.clone(), Rc::new(SExp::Cons(l, Rc::new(p), args))))
}

Callable::EnvPath => {
if tl.len() == 1 {
Expand Down Expand Up @@ -1114,7 +1109,7 @@ pub fn hoist_body_let_binding(
"@*env*".as_bytes().to_vec(),
))),
],
None
None,
)
});

Expand All @@ -1139,17 +1134,19 @@ pub fn hoist_body_let_binding(
new_call_list.push(new_arg);
vres.append(&mut new_helper.clone());
}
let new_tail =
if let Some(t) = tail.as_ref() {
let (new_helper, new_tail_elt) =
hoist_body_let_binding(outer_context.clone(), args.clone(), t.clone())?;
vres.append(&mut new_helper.clone());
Some(new_tail_elt)
} else {
None
};
let new_tail = if let Some(t) = tail.as_ref() {
let (new_helper, new_tail_elt) =
hoist_body_let_binding(outer_context.clone(), args.clone(), t.clone())?;
vres.append(&mut new_helper.clone());
Some(new_tail_elt)
} else {
None
};

Ok((vres, Rc::new(BodyForm::Call(l.clone(), new_call_list, new_tail))))
Ok((
vres,
Rc::new(BodyForm::Call(l.clone(), new_call_list, new_tail)),
))
}
BodyForm::Lambda(letdata) => {
let new_function_args = Rc::new(SExp::Cons(
Expand Down Expand Up @@ -1416,8 +1413,7 @@ fn finalize_env_(
}

if let Some(res) = c.inlines.get(v) {
let (arg_list, arg_tail) =
synthesize_args(res.args.clone());
let (arg_list, arg_tail) = synthesize_args(res.args.clone());
return replace_in_inline(
context,
opts.clone(),
Expand All @@ -1426,7 +1422,7 @@ fn finalize_env_(
res,
res.args.loc(),
&arg_list,
arg_tail
arg_tail,
)
.map(|x| x.1);
}
Expand Down
56 changes: 37 additions & 19 deletions src/compiler/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ pub fn make_operator1(l: &Srcloc, op: String, arg: Rc<BodyForm>) -> BodyForm {
Rc::new(BodyForm::Value(SExp::atom_from_string(l.clone(), &op))),
arg,
],
None
None,
)
}

Expand All @@ -312,7 +312,7 @@ pub fn make_operator2(l: &Srcloc, op: String, arg1: Rc<BodyForm>, arg2: Rc<BodyF
arg1,
arg2,
],
None
None,
)
}

Expand Down Expand Up @@ -545,7 +545,7 @@ fn synthesize_args(
synthesize_args(f.clone(), env)?,
synthesize_args(r.clone(), env)?,
],
None
None,
)))
}
}
Expand Down Expand Up @@ -663,7 +663,7 @@ fn choose_from_env_by_path(path_: Number, args_program: Rc<BodyForm>) -> Rc<Body
))),
result_form,
],
None
None,
));
}
}
Expand Down Expand Up @@ -764,7 +764,7 @@ struct CallSpec<'a> {
name: &'a [u8],
args: &'a [Rc<BodyForm>],
tail: Option<Rc<BodyForm>>,
original: Rc<BodyForm>
original: Rc<BodyForm>,
}

impl<'info> Evaluator {
Expand Down Expand Up @@ -996,7 +996,8 @@ impl<'info> Evaluator {
prog_args,
))))
} else if call.name == b"com" {
let use_body = self.make_com_module(&call.loc, prog_args, arguments_to_convert[0].to_sexp());
let use_body =
self.make_com_module(&call.loc, prog_args, arguments_to_convert[0].to_sexp());
let compiled = self.compile_code(allocator, false, use_body)?;
let compiled_borrowed: &SExp = compiled.borrow();
Ok(Rc::new(BodyForm::Quoted(compiled_borrowed.clone())))
Expand All @@ -1021,7 +1022,8 @@ impl<'info> Evaluator {
all_primitive = false;
}

converted_args = SExp::Cons(call.loc.clone(), shrunk.to_sexp(), Rc::new(converted_args));
converted_args =
SExp::Cons(call.loc.clone(), shrunk.to_sexp(), Rc::new(converted_args));
}

if all_primitive {
Expand All @@ -1034,7 +1036,11 @@ impl<'info> Evaluator {
Ok(res) => Ok(res),
Err(e) => {
if only_inline || self.ignore_exn {
Ok(Rc::new(BodyForm::Call(call.loc.clone(), target_vec.clone(), None)))
Ok(Rc::new(BodyForm::Call(
call.loc.clone(),
target_vec.clone(),
None,
)))
} else {
Err(e)
}
Expand All @@ -1057,7 +1063,8 @@ impl<'info> Evaluator {
only_inline,
)
} else {
let reformed = BodyForm::Call(call.loc.clone(), target_vec.clone(), call.tail.clone());
let reformed =
BodyForm::Call(call.loc.clone(), target_vec.clone(), call.tail.clone());
self.chase_apply(allocator, &mut visited, Rc::new(reformed))
}
} else {
Expand Down Expand Up @@ -1132,7 +1139,7 @@ impl<'info> Evaluator {
Rc::new(BodyForm::Call(
iftrue.loc(),
vec![apply_head.clone(), iftrue.clone(), env.clone()],
None
None,
)),
);

Expand All @@ -1142,7 +1149,7 @@ impl<'info> Evaluator {
Rc::new(BodyForm::Call(
iffalse.loc(),
vec![apply_head, iffalse.clone(), env],
None
None,
)),
);

Expand All @@ -1158,7 +1165,7 @@ impl<'info> Evaluator {
flatten_expression_to_names(surrogate_apply_true?.to_sexp()),
flatten_expression_to_names(surrogate_apply_false?.to_sexp()),
],
None
None,
));

return Ok(res);
Expand Down Expand Up @@ -1231,7 +1238,10 @@ impl<'info> Evaluator {
match helper {
Some(HelperForm::Defmacro(mac)) => {
if call.tail.is_some() {
todo!();
return Err(CompileErr(
call.loc.clone(),
"Macros cannot use runtime rest arguments".to_string(),
));
}
self.invoke_macro_expansion(
allocator,
Expand Down Expand Up @@ -1261,11 +1271,19 @@ impl<'info> Evaluator {
only_inline,
)?);
}
return Ok(Rc::new(BodyForm::Call(call.loc.clone(), call_vec, call.tail.clone())));
return Ok(Rc::new(BodyForm::Call(
call.loc.clone(),
call_vec,
call.tail.clone(),
)));
}

let argument_captures_untranslated =
build_argument_captures(&call.loc, arguments_to_convert, call.tail.clone(), defun.args.clone())?;
let argument_captures_untranslated = build_argument_captures(
&call.loc,
arguments_to_convert,
call.tail.clone(),
defun.args.clone(),
)?;

let mut argument_captures = HashMap::new();
// Do this to protect against misalignment
Expand Down Expand Up @@ -1503,7 +1521,7 @@ impl<'info> Evaluator {
parts.iter().skip(1).cloned().collect();

match head_expr.borrow() {
BodyForm::Value(SExp::Atom(call_loc, call_name)) => self.handle_invoke(
BodyForm::Value(SExp::Atom(_call_loc, call_name)) => self.handle_invoke(
allocator,
&mut visited,
&CallSpec {
Expand All @@ -1518,15 +1536,15 @@ impl<'info> Evaluator {
env,
only_inline,
),
BodyForm::Value(SExp::Integer(call_loc, call_int)) => self.handle_invoke(
BodyForm::Value(SExp::Integer(_call_loc, call_int)) => self.handle_invoke(
allocator,
&mut visited,
&CallSpec {
loc: l.clone(),
name: &u8_from_number(call_int.clone()),
args: parts,
original: body.clone(),
tail: None
tail: None,
},
prog_args,
&arguments_to_convert,
Expand Down
22 changes: 12 additions & 10 deletions src/compiler/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,18 @@ fn args_to_expression_list(
} else {
match body.borrow() {
SExp::Cons(_l, first, rest) => {
if let SExp::Atom(fl, fname) = first.borrow() {
if let SExp::Atom(_fl, fname) = first.borrow() {
if fname == b"&rest" {
// Rest is a list containing one item that becomes the
// tail.
let (mut args, no_tail) =
args_to_expression_list(opts, rest.clone())?;
let (args, no_tail) = args_to_expression_list(opts, rest.clone())?;

if no_tail.is_some() {
return Err(CompileErr(
rest.loc(),
format!("only one use of &rest is allowed"),
));
}

if args.len() != 1 {
return Err(CompileErr(
Expand All @@ -232,15 +238,11 @@ fn args_to_expression_list(
let mut result_list = Vec::new();
let f_compiled = compile_bodyform(opts.clone(), first.clone())?;
result_list.push(Rc::new(f_compiled));
let (mut args, mut tail) =
args_to_expression_list(opts, rest.clone())?;
let (mut args, tail) = args_to_expression_list(opts, rest.clone())?;
result_list.append(&mut args);
Ok((result_list, tail))
}
_ => Err(CompileErr(
body.loc(),
format!("Bad arg list tail {body}"),
)),
_ => Err(CompileErr(body.loc(), format!("Bad arg list tail {body}"))),
}
}
}
Expand Down Expand Up @@ -1054,7 +1056,7 @@ pub fn generate_type_helpers(ty: &ChiaType) -> Vec<HelperForm> {
))),
Rc::new(BodyForm::Value(SExp::Atom(m.loc.clone(), vec![b'S']))),
],
None
None,
)),
synthetic: Some(SyntheticType::NoInlinePreference),
ty: Some(funty),
Expand Down
Loading

0 comments on commit 44efb75

Please sign in to comment.