1
1
mirror of https://github.com/anoma/juvix.git synced 2025-01-05 14:34:03 +03:00
juvix/app/Commands/Format/Options.hs
Jan Mas Rovira 2c8a364143
New syntax for function definitions (#2243)
- Closes #2060
- Closes #2189


- This pr adds support for the syntax described in #2189. It does not
drop support for the old syntax.

It is possible to automatically translate juvix files to the new syntax
by using the formatter with the `--new-function-syntax` flag. E.g.

```
juvix format --in-place --new-function-syntax
```
# Syntax changes
Type signatures follow this pattern:
```
f (a1 : Expr) .. (an : Expr) : Expr
```
where each `ai` is a non-empty list of symbols. Braces are used instead
of parentheses when the argument is implicit.

Then, we have these variants:
1. Simple body. After the signature we have `:= Expr;`.
2. Clauses. The function signature is followed by a non-empty sequence
of clauses. Each clause has the form:
```
| atomPat .. atomPat := Expr
```
# Mutual recursion
Now identifiers **do not need to be defined before they are used**,
making it possible to define mutually recursive functions/types without
any special syntax.
There are some exceptions to this. We cannot forward reference a symbol
`f` in some statement `s` if between `s` and the definition of `f` there
is one of the following statements:
1. Local module
2. Import statement
3. Open statement

I think it should be possible to drop the restriction for local modules
and import statements
2023-07-10 19:57:55 +02:00

43 lines
1.2 KiB
Haskell

module Commands.Format.Options where
import CommonOptions
data FormatOptions = FormatOptions
{ _formatInput :: Maybe (Prepath FileOrDir),
_formatCheck :: Bool,
_formatNewSyntax :: Bool,
_formatInPlace :: Bool
}
deriving stock (Data)
makeLenses ''FormatOptions
parseInputJuvixFileOrDir :: Parser (Prepath FileOrDir)
parseInputJuvixFileOrDir =
strArgument
( metavar "JUVIX_FILE_OR_PROJECT"
<> help "Path to a .juvix file or to a directory containing a Juvix project."
<> completer juvixCompleter
<> action "directory"
)
parseFormat :: Parser FormatOptions
parseFormat = do
_formatInput <- optional parseInputJuvixFileOrDir
_formatCheck <-
switch
( long "check"
<> help "Do not print reformatted sources or unformatted file paths to standard output."
)
_formatNewSyntax <-
switch
( long "new-function-syntax"
<> help "Format the file using the new function syntax."
)
_formatInPlace <-
switch
( long "in-place"
<> help "Do not print reformatted sources to standard output. Overwrite the target's contents with the formatted version if the formatted version differs from the original content."
)
pure FormatOptions {..}