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-499] Add documentation on cli arguments #226

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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ Other features are described in the following documentation files:
- 📗 Pattern matching: [Pattern matching](docs/pattern-matching.md)
- 📗 Native numbers and operations: [Native numbers](docs/native-numbers.md)
- 📗 Builtin definitions: [Builtin definitions](docs/builtin-defs.md)
- 📗 CLI arguments: [CLI arguments](docs/cli-arguments.md)
- 📙 Duplications and superpositions: [Dups and sups](docs/dups-and-sups.md)
- 📙 Scopeless lambdas: [Using scopeless lambdas](docs/using-scopeless-lambdas.md)
- 📙 Tagged lambdas and applications: [Automatic vectorization with tagged lambdas](docs/automatic-vectorization-with-tagged-lambdas.md)
Expand Down
25 changes: 25 additions & 0 deletions docs/cli-arguments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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:

main = (λx1 λx2 λx3 (MainBody x1 x2 x3) arg1 arg2 arg3)
```

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)

// Can't receive CLI arguments
main = λx λy λz (MainBody x y z)
```
10 changes: 10 additions & 0 deletions src/term/transform/apply_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ impl Display for ArgError {
}

impl Ctx<'_> {
/// Applies the arguments to the program being run by applying them to the main function.
///
/// Example:
/// ```hvm
/// main x1 x2 x3 = (MainBody x1 x2 x3)
/// ```
/// Calling with `hvml run <file> arg1 arg2 arg3`, it becomes:
/// ```hvm
/// main = (λx1 λx2 λx3 (MainBody x1 x2 x3) arg1 arg2 arg3)
/// ```
pub fn apply_args(&mut self, args: Option<Vec<Term>>) -> Result<(), Info> {
self.info.start_pass();

Expand Down
Loading