-
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.
* Implements JuvixTree evaluator * Adds JuvixTree evaluation tests * Adds the `juvix dev tree eval` command * Depends on #2587 * Depends on #2583
- Loading branch information
Showing
17 changed files
with
813 additions
and
300 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
module Commands.Dev.Tree where | ||
|
||
import Commands.Base | ||
import Commands.Dev.Tree.Eval as Eval | ||
import Commands.Dev.Tree.FromAsm as FromAsm | ||
import Commands.Dev.Tree.Options | ||
import Commands.Dev.Tree.Read as Read | ||
|
||
runCommand :: forall r. (Members '[Embed IO, App, TaggedLock] r) => TreeCommand -> Sem r () | ||
runCommand = \case | ||
Eval opts -> Eval.runCommand opts | ||
Read opts -> Read.runCommand opts | ||
FromAsm 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,17 @@ | ||
module Commands.Dev.Tree.Eval where | ||
|
||
import Commands.Base | ||
import Commands.Dev.Tree.Eval.Options | ||
import Juvix.Compiler.Tree.Translation.FromSource qualified as Tree | ||
import TreeEvaluator | ||
|
||
runCommand :: forall r. (Members '[Embed IO, App] r) => TreeEvalOptions -> Sem r () | ||
runCommand opts = do | ||
afile :: Path Abs File <- fromAppPathFile file | ||
s <- readFile (toFilePath afile) | ||
case Tree.runParser (toFilePath afile) s of | ||
Left err -> exitJuvixError (JuvixError err) | ||
Right tab -> evalTree tab | ||
where | ||
file :: AppPath File | ||
file = opts ^. treeEvalInputFile |
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.Tree.Eval.Options where | ||
|
||
import CommonOptions | ||
|
||
newtype TreeEvalOptions = TreeEvalOptions | ||
{ _treeEvalInputFile :: AppPath File | ||
} | ||
deriving stock (Data) | ||
|
||
makeLenses ''TreeEvalOptions | ||
|
||
parseTreeEvalOptions :: Parser TreeEvalOptions | ||
parseTreeEvalOptions = do | ||
_treeEvalInputFile <- parseInputFile FileExtJuvixTree | ||
pure TreeEvalOptions {..} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module TreeEvaluator where | ||
|
||
import App | ||
import CommonOptions | ||
import Juvix.Compiler.Tree.Data.InfoTable qualified as Tree | ||
import Juvix.Compiler.Tree.Error qualified as Tree | ||
import Juvix.Compiler.Tree.Evaluator qualified as Tree | ||
import Juvix.Compiler.Tree.Language.Value qualified as Tree | ||
import Juvix.Compiler.Tree.Pretty qualified as Tree | ||
|
||
evalTree :: forall r. (Members '[Embed IO, App] r) => Tree.InfoTable -> Sem r () | ||
evalTree tab = | ||
case tab ^. Tree.infoMainFunction of | ||
Just sym -> do | ||
r <- doEval tab (Tree.lookupFunInfo tab sym) | ||
case r of | ||
Left err -> | ||
exitJuvixError (JuvixError err) | ||
Right Tree.ValVoid -> | ||
return () | ||
Right val -> do | ||
renderStdOut (Tree.ppOutDefault tab val) | ||
putStrLn "" | ||
Nothing -> | ||
exitMsg (ExitFailure 1) "no 'main' function" | ||
where | ||
doEval :: | ||
Tree.InfoTable -> | ||
Tree.FunctionInfo -> | ||
Sem r (Either Tree.TreeError Tree.Value) | ||
doEval tab' funInfo = | ||
embed $ Tree.catchEvalErrorIO (Tree.hEvalIO stdin stdout tab' funInfo) |
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,63 +1,12 @@ | ||
module Juvix.Compiler.Asm.Interpreter.Base | ||
( module Juvix.Compiler.Asm.Interpreter.Base, | ||
( module Juvix.Compiler.Tree.Language.Value, | ||
module Juvix.Compiler.Asm.Language, | ||
Val, | ||
) | ||
where | ||
|
||
import Juvix.Compiler.Asm.Language | ||
import Juvix.Compiler.Tree.Language.Value hiding (Value) | ||
import Juvix.Compiler.Tree.Language.Value qualified as Tree | ||
|
||
{- | ||
The following types of values may be stored in the heap or an activation | ||
frame. | ||
- Integer (arbitrary precision) | ||
- Boolean | ||
- String | ||
- Constructor data | ||
- Closure | ||
-} | ||
|
||
data Val | ||
= ValInteger Integer | ||
| ValBool Bool | ||
| ValString Text | ||
| ValUnit | ||
| ValVoid | ||
| ValConstr Constr | ||
| ValClosure Closure | ||
deriving stock (Eq) | ||
|
||
data Constr = Constr | ||
{ _constrTag :: Tag, | ||
_constrArgs :: [Val] | ||
} | ||
deriving stock (Eq) | ||
|
||
data Closure = Closure | ||
{ _closureSymbol :: Symbol, | ||
_closureArgs :: [Val] | ||
} | ||
deriving stock (Eq) | ||
|
||
makeLenses ''Constr | ||
makeLenses ''Closure | ||
|
||
instance HasAtomicity Constr where | ||
atomicity Constr {..} | ||
| null _constrArgs = Atom | ||
| otherwise = Aggregate appFixity | ||
|
||
instance HasAtomicity Closure where | ||
atomicity Closure {..} | ||
| null _closureArgs = Atom | ||
| otherwise = Aggregate appFixity | ||
|
||
instance HasAtomicity Val where | ||
atomicity = \case | ||
ValInteger {} -> Atom | ||
ValBool {} -> Atom | ||
ValString {} -> Atom | ||
ValUnit -> Atom | ||
ValVoid -> Atom | ||
ValConstr c -> atomicity c | ||
ValClosure cl -> atomicity cl | ||
type Val = Tree.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
Oops, something went wrong.