From cdf8bba16e4502adc1a18e5cd588210ed7bec5a3 Mon Sep 17 00:00:00 2001 From: imaqtkatt Date: Fri, 8 Mar 2024 10:16:23 -0300 Subject: [PATCH 1/2] Remove argument count restriction --- src/diagnostics.rs | 12 ++++++------ src/term/transform/apply_args.rs | 24 +++++++----------------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/diagnostics.rs b/src/diagnostics.rs index 777cf4fd3..3c60825e5 100644 --- a/src/diagnostics.rs +++ b/src/diagnostics.rs @@ -5,7 +5,7 @@ use crate::term::{ }, display::DisplayFn, transform::{ - apply_args::ArgError, encode_pattern_matching::MatchErr, resolve_refs::ReferencedMainErr, + apply_args::PatternArgError, encode_pattern_matching::MatchErr, resolve_refs::ReferencedMainErr, simplify_ref_to_ref::CyclicDefErr, }, Name, @@ -119,7 +119,7 @@ pub enum Error { EntryPoint(EntryErr), TopLevel(TopLevelErr), Custom(String), - ArgError(ArgError), + PatternArgError(PatternArgError), RepeatedBind(RepeatedBindWarn), } @@ -140,7 +140,7 @@ impl Error { Error::EntryPoint(err) => write!(f, "{err}"), Error::TopLevel(err) => write!(f, "{err}"), Error::Custom(err) => write!(f, "{err}"), - Error::ArgError(err) => write!(f, "{err}"), + Error::PatternArgError(err) => write!(f, "{err}"), Error::RepeatedBind(err) => write!(f, "{err}"), }) } @@ -188,9 +188,9 @@ impl From for Error { } } -impl From for Error { - fn from(value: ArgError) -> Self { - Self::ArgError(value) +impl From for Error { + fn from(value: PatternArgError) -> Self { + Self::PatternArgError(value) } } diff --git a/src/term/transform/apply_args.rs b/src/term/transform/apply_args.rs index 943fd09d8..077dd1bad 100644 --- a/src/term/transform/apply_args.rs +++ b/src/term/transform/apply_args.rs @@ -6,17 +6,11 @@ use crate::{ }; #[derive(Clone, Debug)] -pub enum ArgError { - PatternArgError, - ArityArgError { expected: usize, got: usize }, -} +pub struct PatternArgError(Pattern); -impl Display for ArgError { +impl Display for PatternArgError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - ArgError::PatternArgError => write!(f, ""), - ArgError::ArityArgError { expected, got } => write!(f, "Expected {expected} arguments, got {got}."), - } + write!(f, "Expected a variable pattern, found '{}'.", self.0) } } @@ -37,17 +31,13 @@ impl Ctx<'_> { if let Some(entrypoint) = &self.book.entrypoint { let main_def = &mut self.book.defs[entrypoint]; - if !main_def.rules[0].pats.iter().all(|pat| matches!(pat, Pattern::Var(Some(..)))) { - self.info.def_error(entrypoint.clone(), ArgError::PatternArgError); + for pat in &main_def.rules[0].pats { + if !matches!(pat, Pattern::Var(Some(..))) { + self.info.def_error(entrypoint.clone(), PatternArgError(pat.clone())); + } } if let Some(args) = args { - let expected = main_def.rules[0].pats.len(); - let got = args.len(); - if expected != got { - self.info.error(ArgError::ArityArgError { expected, got }); - } - main_def.convert_match_def_to_term(); let main_body = &mut self.book.defs[entrypoint].rule_mut().body; From 09492296f99795ffa9cb9c582247b6d51f805022 Mon Sep 17 00:00:00 2001 From: imaqtkatt Date: Fri, 8 Mar 2024 11:19:03 -0300 Subject: [PATCH 2/2] Update cli arguments doc --- docs/cli-arguments.md | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/docs/cli-arguments.md b/docs/cli-arguments.md index b592de5d1..e786b0b12 100644 --- a/docs/cli-arguments.md +++ b/docs/cli-arguments.md @@ -1,25 +1,33 @@ # CLI arguments It's possible to pass arguments to a program executed with `hvml run`: + ```sh hvml run [Arguments in expression form]... ``` + It accepts any expression that would also be valid inside an hvm-lang function. Arguments are passed to programs by applying them to the entrypoint function: + ```js main x1 x2 x3 = (MainBody x1 x2 x3) -// Calling with `hvml run arg1 arg2 arg3`, it becomes: +// Calling with `hvml run arg1 arg2 arg3 argN`, it becomes: -main = (λx1 λx2 λx3 (MainBody x1 x2 x3) arg1 arg2 arg3) +main = (λx1 λx2 λx3 (MainBody x1 x2 x3) arg1 arg2 arg3 argN) ``` -The entrypoint function must receive exactly the number of arguments specified in the left-hand side of its definition. -``` -// Must pass exactly 3 arguments when running -main x y z = (MainBody x y z) +There are no restrictions on the number of arguments passed to the program. + +```rust +// Can receive 2 CLI arguments +main x y = (+ x y) // Can't receive CLI arguments -main = λx λy λz (MainBody x y z) -``` \ No newline at end of file +main = λx λy (+ x y) + +// Calling with just one argument +hvml run 5 +λa (+ a 5) +```