Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sc-502] Allow number of CLI arguments to be different than declared in main #229

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions docs/cli-arguments.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
# CLI arguments

It's possible to pass arguments to a program executed with `hvml run`:

```sh
hvml run <Path to program> [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 <file> arg1 arg2 arg3`, it becomes:
// Calling with `hvml run <file> 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)
```
main = λx λy (+ x y)

// Calling with just one argument
hvml run <path> 5
λa (+ a 5)
```
12 changes: 6 additions & 6 deletions src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -119,7 +119,7 @@ pub enum Error {
EntryPoint(EntryErr),
TopLevel(TopLevelErr),
Custom(String),
ArgError(ArgError),
PatternArgError(PatternArgError),
RepeatedBind(RepeatedBindWarn),
}

Expand All @@ -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}"),
})
}
Expand Down Expand Up @@ -188,9 +188,9 @@ impl From<TopLevelErr> for Error {
}
}

impl From<ArgError> for Error {
fn from(value: ArgError) -> Self {
Self::ArgError(value)
impl From<PatternArgError> for Error {
fn from(value: PatternArgError) -> Self {
Self::PatternArgError(value)
}
}

Expand Down
24 changes: 7 additions & 17 deletions src/term/transform/apply_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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;

Expand Down
Loading