-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR is a snapshot of the current work on the JuvixAsm -> Nockma translation. The compilation of Juvix programs to Nockma now works so we decided to raise this PR now to avoid it getting too large. ## Juvix -> Nockma compilation You can compile a frontend Juvix file to Nockma as follows: example.juvix ``` module example; import Stdlib.Prelude open; fib : Nat → Nat → Nat → Nat | zero x1 _ := x1 | (suc n) x1 x2 := fib n x2 (x1 + x2); fibonacci (n : Nat) : Nat := fib n 0 1; sumList (xs : List Nat) : Nat := for (acc := 0) (x in xs) acc + x; main : Nat := fibonacci 9 + sumList [1; 2; 3; 4]; ``` ``` $ juvix compile -t nockma example.juvix ``` This will generate a file `example.nockma` which can be run using the nockma evaluator: ``` $ juvix dev nockma eval example.nockma ``` Alternatively you can compile JuvixAsm to Nockma: ``` $ juvix dev asm compile -t nockma example.jva ``` ## Tests We compile an evaluate the JuvixAsm tests in https://github.com/anoma/juvix/blob/cb3659e08e552ee9ca40860077e39a4070cf3303/test/Nockma/Compile/Asm/Positive.hs We currently skip some because either: 1. They are too slow to run in the current evaluator (due to arithmetic operations using the unjetted nock code from the anoma nock stdlib). 2. They trace data types like lists and booleans which are represented differently by the asm interpreter and the nock interpreter 3. They operate on raw negative numbers, nock only supports raw natural numbers ## Next steps On top of this PR we will work on improving the evaluator so that we can enable the slow compilation tests. --------- Co-authored-by: Paul Cadman <[email protected]> Co-authored-by: Lukasz Czajka <[email protected]>
- Loading branch information
1 parent
5178979
commit 73364f4
Showing
64 changed files
with
2,212 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
module Commands.Dev.Nockma where | ||
|
||
import Commands.Base | ||
import Commands.Dev.Nockma.Eval as FromAsm | ||
import Commands.Dev.Nockma.Options | ||
import Commands.Dev.Nockma.Repl as Repl | ||
|
||
runCommand :: forall r. (Members '[Embed IO, App] r) => NockmaCommand -> Sem r () | ||
runCommand = \case | ||
NockmaRepl opts -> Repl.runCommand opts | ||
NockmaEval opts -> FromAsm.runCommand opts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Commands.Dev.Nockma.Eval where | ||
|
||
import Commands.Base hiding (Atom) | ||
import Commands.Dev.Nockma.Eval.Options | ||
import Juvix.Compiler.Nockma.Pretty | ||
import Juvix.Compiler.Nockma.Translation.FromAsm | ||
import Juvix.Compiler.Nockma.Translation.FromSource qualified as Nockma | ||
|
||
runCommand :: forall r. (Members '[Embed IO, App] r) => NockmaEvalOptions -> Sem r () | ||
runCommand opts = do | ||
afile <- fromAppPathFile file | ||
parsedTerm <- Nockma.parseTermFile (toFilePath afile) | ||
case parsedTerm of | ||
Left err -> exitJuvixError (JuvixError err) | ||
Right (TermCell c) -> do | ||
res <- runOutputSem @(Term Natural) (say . ppTrace) (evalCompiledNock' (c ^. cellLeft) (c ^. cellRight)) | ||
ret <- getReturn res | ||
putStrLn (ppPrint ret) | ||
Right TermAtom {} -> exitFailMsg "Expected nockma input to be a cell" | ||
where | ||
file :: AppPath File | ||
file = opts ^. nockmaEvalFile | ||
|
||
getReturn :: Term Natural -> Sem r (Term Natural) | ||
getReturn res = | ||
let valStack = getStack ValueStack res | ||
in case valStack of | ||
TermCell c -> return (c ^. cellLeft) | ||
TermAtom {} -> exitFailMsg "Program does not return a value" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Commands.Dev.Nockma.Eval.Options where | ||
|
||
import CommonOptions | ||
|
||
newtype NockmaEvalOptions = NockmaEvalOptions | ||
{ _nockmaEvalFile :: AppPath File | ||
} | ||
deriving stock (Data) | ||
|
||
makeLenses ''NockmaEvalOptions | ||
|
||
parseNockmaEvalOptions :: Parser NockmaEvalOptions | ||
parseNockmaEvalOptions = do | ||
_nockmaEvalFile <- parseInputFile FileExtNockma | ||
pure NockmaEvalOptions {..} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,36 @@ | ||
module Commands.Dev.Nockma.Options where | ||
|
||
import Commands.Dev.Nockma.Eval.Options | ||
import Commands.Dev.Nockma.Repl.Options | ||
import CommonOptions | ||
|
||
data NockmaCommand | ||
= NockmaRepl NockmaReplOptions | ||
| NockmaEval NockmaEvalOptions | ||
deriving stock (Data) | ||
|
||
parseNockmaCommand :: Parser NockmaCommand | ||
parseNockmaCommand = hsubparser commandRepl | ||
parseNockmaCommand = | ||
hsubparser $ | ||
mconcat | ||
[ commandRepl, | ||
commandFromAsm | ||
] | ||
where | ||
commandFromAsm :: Mod CommandFields NockmaCommand | ||
commandFromAsm = command "eval" fromAsmInfo | ||
where | ||
fromAsmInfo :: ParserInfo NockmaCommand | ||
fromAsmInfo = | ||
info | ||
(NockmaEval <$> parseNockmaEvalOptions) | ||
(progDesc "Evaluate a nockma file. The file should contain a single nockma cell: [subject formula]") | ||
|
||
commandRepl :: Mod CommandFields NockmaCommand | ||
commandRepl = command "repl" replInfo | ||
|
||
replInfo :: ParserInfo NockmaCommand | ||
replInfo = | ||
info | ||
(NockmaRepl <$> parseNockmaReplOptions) | ||
(progDesc "Run the nockma repl") | ||
where | ||
replInfo :: ParserInfo NockmaCommand | ||
replInfo = | ||
info | ||
(NockmaRepl <$> parseNockmaReplOptions) | ||
(progDesc "Run the nockma repl") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.