mirror of
https://github.com/anoma/juvix.git
synced 2024-12-17 11:51:42 +03:00
2c8a364143
- 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
111 lines
4.0 KiB
Haskell
111 lines
4.0 KiB
Haskell
module Commands.Format where
|
|
|
|
import Commands.Base
|
|
import Commands.Format.Options
|
|
import Data.Text qualified as Text
|
|
import Juvix.Formatter
|
|
import Juvix.Prelude.Pretty
|
|
|
|
data FormatNoEditRenderMode
|
|
= ReformattedFile (NonEmpty AnsiText)
|
|
| InputPath (Path Abs File)
|
|
| Silent
|
|
|
|
data FormatRenderMode
|
|
= EditInPlace FormattedFileInfo
|
|
| NoEdit FormatNoEditRenderMode
|
|
|
|
data FormatTarget
|
|
= TargetFile (Path Abs File)
|
|
| TargetProject (Path Abs Dir)
|
|
| TargetStdin
|
|
|
|
isTargetProject :: FormatTarget -> Bool
|
|
isTargetProject = \case
|
|
TargetProject {} -> True
|
|
_ -> False
|
|
|
|
targetFromOptions :: Members '[Embed IO, App] r => FormatOptions -> Sem r FormatTarget
|
|
targetFromOptions opts = do
|
|
globalOpts <- askGlobalOptions
|
|
let isStdin = globalOpts ^. globalStdin
|
|
f <- mapM filePathToAbs (opts ^. formatInput)
|
|
pkgDir <- askPkgDir
|
|
case f of
|
|
Just (Left p) -> return (TargetFile p)
|
|
Just Right {} -> return (TargetProject pkgDir)
|
|
Nothing -> do
|
|
isPackageGlobal <- askPackageGlobal
|
|
if
|
|
| isStdin -> return TargetStdin
|
|
| not (isPackageGlobal) -> return (TargetProject pkgDir)
|
|
| otherwise -> do
|
|
printFailureExit $
|
|
Text.unlines
|
|
[ "juvix format error: either 'JUVIX_FILE_OR_PROJECT' or '--stdin' option must be specified",
|
|
"Use the --help option to display more usage information."
|
|
]
|
|
|
|
runCommand :: forall r. Members '[Embed IO, App, Resource, Files] r => FormatOptions -> Sem r ()
|
|
runCommand opts = do
|
|
target <- targetFromOptions opts
|
|
let newSyntax = NewSyntax (opts ^. formatNewSyntax)
|
|
runOutputSem (renderFormattedOutput target opts) $ runScopeFileApp $ do
|
|
res <- runReader newSyntax $ case target of
|
|
TargetFile p -> format p
|
|
TargetProject p -> formatProject p
|
|
TargetStdin -> formatStdin
|
|
|
|
let exitFail :: IO a
|
|
exitFail = exitWith (ExitFailure 1)
|
|
case res of
|
|
FormatResultFail -> embed exitFail
|
|
FormatResultNotFormatted ->
|
|
{- use exit code 1 for
|
|
* unformatted files when using --check
|
|
* when running the formatter on a Juvix project
|
|
-}
|
|
when (opts ^. formatCheck || isTargetProject target) (embed exitFail)
|
|
FormatResultOK -> pure ()
|
|
|
|
renderModeFromOptions :: FormatTarget -> FormatOptions -> FormattedFileInfo -> FormatRenderMode
|
|
renderModeFromOptions target opts formattedInfo
|
|
| opts ^. formatInPlace = whenContentsModified (EditInPlace formattedInfo)
|
|
| opts ^. formatCheck = NoEdit Silent
|
|
| otherwise = case target of
|
|
TargetFile {} -> NoEdit (ReformattedFile (formattedInfo ^. formattedFileInfoContentsAnsi))
|
|
TargetProject {} -> whenContentsModified (NoEdit (InputPath (formattedInfo ^. formattedFileInfoPath)))
|
|
TargetStdin -> NoEdit (ReformattedFile (formattedInfo ^. formattedFileInfoContentsAnsi))
|
|
where
|
|
whenContentsModified :: FormatRenderMode -> FormatRenderMode
|
|
whenContentsModified res
|
|
| formattedInfo ^. formattedFileInfoContentsModified = res
|
|
| otherwise = NoEdit Silent
|
|
|
|
renderFormattedOutput :: forall r. Members '[Embed IO, App, Resource, Files] r => FormatTarget -> FormatOptions -> FormattedFileInfo -> Sem r ()
|
|
renderFormattedOutput target opts fInfo = do
|
|
let renderMode = renderModeFromOptions target opts fInfo
|
|
outputResult renderMode
|
|
where
|
|
outputResult :: FormatRenderMode -> Sem r ()
|
|
outputResult = \case
|
|
EditInPlace i@FormattedFileInfo {..} ->
|
|
runTempFileIO
|
|
. restoreFileOnError _formattedFileInfoPath
|
|
$ writeFile' _formattedFileInfoPath (i ^. formattedFileInfoContentsText)
|
|
NoEdit m -> case m of
|
|
ReformattedFile ts -> forM_ ts renderStdOut
|
|
InputPath p -> say (pack (toFilePath p))
|
|
Silent -> return ()
|
|
|
|
runScopeFileApp :: Member App r => Sem (ScopeEff ': r) a -> Sem r a
|
|
runScopeFileApp = interpret $ \case
|
|
ScopeFile p -> do
|
|
let appFile =
|
|
AppPath
|
|
{ _pathPath = mkPrepath (toFilePath p),
|
|
_pathIsInput = False
|
|
}
|
|
runPipeline appFile upToScoping
|
|
ScopeStdin -> runPipelineNoFile upToScoping
|