Skip to content

Commit

Permalink
Remove static arguments compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
imaqtkatt committed Feb 29, 2024
1 parent 25c51b6 commit 5870f0d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 44 deletions.
25 changes: 9 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,12 @@ pub fn create_host(book: Arc<Book>, labels: Arc<Labels>, compile_opts: CompileOp
pub fn check_book(book: &mut Book) -> Result<(), Info> {
// TODO: Do the checks without having to do full compilation
// TODO: Shouldn't the check mode show warnings?
compile_book(book, None, CompileOpts::light())?;
compile_book(book, CompileOpts::light())?;
Ok(())
}

pub fn compile_book(
book: &mut Book,
args: Option<Vec<Term>>,
opts: CompileOpts,
) -> Result<CompileResult, Info> {
let warns = desugar_book(book, args, opts)?;
pub fn compile_book(book: &mut Book, opts: CompileOpts) -> Result<CompileResult, Info> {
let warns = desugar_book(book, opts)?;
let (nets, labels) = book_to_nets(book);

let mut core_book = nets_to_hvmc(nets)?;
Expand All @@ -117,17 +113,11 @@ pub fn compile_book(
Ok(CompileResult { core_book, labels, warns })
}

pub fn desugar_book(
book: &mut Book,
args: Option<Vec<Term>>,
opts: CompileOpts,
) -> Result<Vec<Warning>, Info> {
pub fn desugar_book(book: &mut Book, opts: CompileOpts) -> Result<Vec<Warning>, Info> {
let mut ctx = Ctx::new(book);

ctx.check_shared_names();
ctx.set_entrypoint(if let Some(args) = &args { args.len() } else { 0 });

ctx.book.apply_args(args)?;
ctx.set_entrypoint();

ctx.book.encode_adts(opts.adt_encoding);
ctx.book.encode_builtins();
Expand Down Expand Up @@ -197,7 +187,10 @@ pub fn run_book(
compile_opts: CompileOpts,
args: Option<Vec<Term>>,
) -> Result<(Term, RunInfo), Info> {
let CompileResult { core_book, labels, warns } = compile_book(&mut book, args, compile_opts)?;
let mut ctx = Ctx::new(&mut book);
ctx.set_entrypoint();
ctx.book.apply_args(args)?;
let CompileResult { core_book, labels, warns } = compile_book(&mut book, compile_opts)?;

// Turn the book into an Arc so that we can use it for logging, debugging, etc.
// from anywhere else in the program
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Info> {
}

let mut book = load_book(&path)?;
let compiled = compile_book(&mut book, None, opts)?;
let compiled = compile_book(&mut book, opts)?;
println!("{}", compiled.display_with_warns(warning_opts)?);
}
Mode::Desugar { path, comp_opts, lazy_mode } => {
Expand All @@ -209,7 +209,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Info> {
}
let mut book = load_book(&path)?;
// TODO: Shouldn't the desugar have `warn_opts` too? maybe WarningOpts::allow_all() by default
let _warns = desugar_book(&mut book, None, opts)?;
let _warns = desugar_book(&mut book, opts)?;
println!("{}", book);
}
Mode::Run {
Expand Down
27 changes: 11 additions & 16 deletions src/term/check/set_entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub enum EntryErr {
NotFound(Name),
Multiple(Vec<Name>),
MultipleRules,
Arguments(usize, usize),
}

impl Display for EntryErr {
Expand All @@ -23,21 +22,23 @@ impl Display for EntryErr {
write!(f, "File has '{}', '{}' and '{}' definitions.", fnd[0], fnd[1], fnd[2])
}
EntryErr::MultipleRules => write!(f, "Main definition can't have more than one rule."),
EntryErr::Arguments(expected, got) => {
write!(f, "Main definition expects {expected} arguments, got {got}.")
}
}
}
}

impl Ctx<'_> {
pub fn set_entrypoint(&mut self, given_arguments: usize) {
pub fn set_entrypoint(&mut self) {
// already setted

Check warning on line 31 in src/term/check/set_entrypoint.rs

View workflow job for this annotation

GitHub Actions / cspell

Unknown word (setted)
if self.book.entrypoint.is_some() {
return;
}

let mut entrypoint = None;

let (custom, main, hvm1_main) = self.book.get_possible_entry_points();
match (custom, main, hvm1_main) {
(Some(entry), None, None) | (None, Some(entry), None) | (None, None, Some(entry)) => {
match validate_entry_point(entry, given_arguments) {
match validate_entry_point(entry) {
Ok(name) => entrypoint = Some(name),
Err(err) => self.info.error(err),
}
Expand All @@ -46,7 +47,7 @@ impl Ctx<'_> {
(Some(a), Some(b), None) | (None, Some(a), Some(b)) | (Some(a), None, Some(b)) => {
self.info.error(EntryErr::Multiple(vec![a.name.clone(), b.name.clone()]));

match validate_entry_point(a, given_arguments) {
match validate_entry_point(a) {
Ok(name) => entrypoint = Some(name),
Err(err) => self.info.error(err),
}
Expand All @@ -55,7 +56,7 @@ impl Ctx<'_> {
(Some(a), Some(b), Some(c)) => {
self.info.error(EntryErr::Multiple(vec![a.name.clone(), b.name.clone(), c.name.clone()]));

match validate_entry_point(a, given_arguments) {
match validate_entry_point(a) {
Ok(name) => entrypoint = Some(name),
Err(err) => self.info.error(err),
}
Expand All @@ -70,14 +71,8 @@ impl Ctx<'_> {
}
}

fn validate_entry_point(entry: &Definition, given_arguments: usize) -> Result<Name, EntryErr> {
if entry.rules.len() > 1 {
Err(EntryErr::MultipleRules)
} else if entry.rules[0].pats.len() != given_arguments {
Err(EntryErr::Arguments(entry.rules[0].pats.len(), given_arguments))
} else {
Ok(entry.name.clone())
}
fn validate_entry_point(entry: &Definition) -> Result<Name, EntryErr> {
if entry.rules.len() > 1 { Err(EntryErr::MultipleRules) } else { Ok(entry.name.clone()) }
}

impl Book {
Expand Down
14 changes: 10 additions & 4 deletions src/term/transform/apply_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ use crate::term::{Book, Pattern, Term};

impl Book {
pub fn apply_args(&mut self, args: Option<Vec<Term>>) -> Result<(), String> {
if let Some(main) = &self.entrypoint
if let Some(entrypoint) = &self.entrypoint
&& let Some(args) = args
{
let main_def = &mut self.defs[main];
let main_def = &mut self.defs[entrypoint];
let expected = main_def.rules[0].pats.len();
let got = args.len();

if !main_def.rules[0].pats.iter().all(|pat| matches!(pat, Pattern::Var(Some(..)))) {
return Err("Main definition should contain only var patterns.".into());
return Err("Definition '{entrypoint}' should contain only var patterns.".into());
}

if expected != got {
return Err(format!("Expected {expected} arguments, got {got}."));
}

main_def.convert_match_def_to_term();
let main_body = &mut self.defs[main].rule_mut().body;
let main_body = &mut self.defs[entrypoint].rule_mut().body;

*main_body = Term::call(main_body.clone(), args);
}
Expand Down
12 changes: 6 additions & 6 deletions tests/golden_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ fn compile_term() {
fn compile_file_o_all() {
run_golden_test_dir(function_name!(), &|code, path| {
let mut book = do_parse_book(code, path)?;
let compiled = compile_book(&mut book, None, CompileOpts::heavy())?;
let compiled = compile_book(&mut book, CompileOpts::heavy())?;
Ok(format!("{:?}", compiled))
})
}
#[test]
fn compile_file() {
run_golden_test_dir(function_name!(), &|code, path| {
let mut book = do_parse_book(code, path)?;
let compiled = compile_book(&mut book, None, CompileOpts::light())?;
let compiled = compile_book(&mut book, CompileOpts::light())?;
Ok(format!("{:?}", compiled))
})
}
Expand Down Expand Up @@ -189,7 +189,7 @@ fn simplify_matches() {
let mut book = do_parse_book(code, path)?;
let mut ctx = Ctx::new(&mut book);
ctx.check_shared_names();
ctx.set_entrypoint(0);
ctx.set_entrypoint();
ctx.book.encode_adts(AdtEncoding::TaggedScott);
ctx.book.encode_builtins();
ctx.book.resolve_ctrs_in_pats();
Expand Down Expand Up @@ -227,7 +227,7 @@ fn encode_pattern_match() {
let mut book = do_parse_book(code, path)?;
let mut ctx = Ctx::new(&mut book);
ctx.check_shared_names();
ctx.set_entrypoint(0);
ctx.set_entrypoint();
ctx.book.encode_adts(adt_encoding);
ctx.book.encode_builtins();
ctx.book.resolve_ctrs_in_pats();
Expand Down Expand Up @@ -258,7 +258,7 @@ fn encode_pattern_match() {
fn desugar_file() {
run_golden_test_dir(function_name!(), &|code, path| {
let mut book = do_parse_book(code, path)?;
desugar_book(&mut book, None, CompileOpts::light())?;
desugar_book(&mut book, CompileOpts::light())?;
Ok(book.to_string())
})
}
Expand Down Expand Up @@ -289,7 +289,7 @@ fn compile_entrypoint() {
run_golden_test_dir(function_name!(), &|code, path| {
let mut book = do_parse_book(code, path)?;
book.entrypoint = Some(Name::from("foo"));
let compiled = compile_book(&mut book, None, CompileOpts::light())?;
let compiled = compile_book(&mut book, CompileOpts::light())?;
Ok(format!("{:?}", compiled))
})
}
Expand Down
3 changes: 3 additions & 0 deletions tests/golden_tests/compile_file/add_args.hvm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add x y = (+ x y)

main x y = (add x y)
7 changes: 7 additions & 0 deletions tests/snapshots/compile_file__add_args.hvm.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/compile_file/add_args.hvm
---
@add = (<+ a b> (a b))
@main = (a (b c))
& @add ~ (a (b c))

0 comments on commit 5870f0d

Please sign in to comment.