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

JuvixTree tests #2587

Merged
merged 4 commits into from
Jan 25, 2024
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
2 changes: 2 additions & 0 deletions app/Commands/Dev/Tree.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module Commands.Dev.Tree where

import Commands.Base
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
Read opts -> Read.runCommand opts
FromAsm opts -> FromAsm.runCommand opts
23 changes: 23 additions & 0 deletions app/Commands/Dev/Tree/FromAsm.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Commands.Dev.Tree.FromAsm where

import Commands.Base
import Commands.Dev.Tree.FromAsm.Options
import Juvix.Compiler.Asm.Translation.FromSource qualified as Asm
import Juvix.Compiler.Tree.Data.InfoTable qualified as Tree
import Juvix.Compiler.Tree.Error (TreeError)
import Juvix.Compiler.Tree.Pretty qualified as Tree
import Juvix.Compiler.Tree.Translation.FromAsm qualified as Tree

runCommand :: forall r. (Members '[Embed IO, App] r) => TreeFromAsmOptions -> Sem r ()
runCommand opts = do
afile :: Path Abs File <- fromAppPathFile file
s <- readFile (toFilePath afile)
case Asm.runParser (toFilePath afile) s of
Left err -> exitJuvixError (JuvixError err)
Right tab -> do
r :: Either JuvixError Tree.InfoTable <- runError $ mapError (JuvixError @TreeError) $ Tree.fromAsm tab
tab' <- getRight r
renderStdOut (Tree.ppOutDefault tab' tab')
where
file :: AppPath File
file = opts ^. treeFromAsmInputFile
15 changes: 15 additions & 0 deletions app/Commands/Dev/Tree/FromAsm/Options.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Commands.Dev.Tree.FromAsm.Options where

import CommonOptions

newtype TreeFromAsmOptions = TreeFromAsmOptions
{ _treeFromAsmInputFile :: AppPath File
}
deriving stock (Data)

makeLenses ''TreeFromAsmOptions

parseTreeFromAsmOptions :: Parser TreeFromAsmOptions
parseTreeFromAsmOptions = do
_treeFromAsmInputFile <- parseInputFile FileExtJuvixAsm
pure TreeFromAsmOptions {..}
16 changes: 14 additions & 2 deletions app/Commands/Dev/Tree/Options.hs
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
module Commands.Dev.Tree.Options where

import Commands.Dev.Tree.FromAsm.Options
import Commands.Dev.Tree.Read.Options
import CommonOptions

newtype TreeCommand
data TreeCommand
= Read TreeReadOptions
| FromAsm TreeFromAsmOptions
deriving stock (Data)

parseTreeCommand :: Parser TreeCommand
parseTreeCommand =
hsubparser $
mconcat
[ commandRead
[ commandRead,
commandFromAsm
]
where
commandRead :: Mod CommandFields TreeCommand
commandRead = command "read" readInfo

commandFromAsm :: Mod CommandFields TreeCommand
commandFromAsm = command "from-asm" fromAsmInfo

readInfo :: ParserInfo TreeCommand
readInfo =
info
(Read <$> parseTreeReadOptions)
(progDesc "Parse a JuvixTree file and pretty print it")

fromAsmInfo :: ParserInfo TreeCommand
fromAsmInfo =
info
(FromAsm <$> parseTreeFromAsmOptions)
(progDesc "Convert a JuvixAsm file to JuvixTree and pretty print it")
10 changes: 10 additions & 0 deletions src/Juvix/Compiler/Asm/Extra/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,13 @@ getConstrSize rep argsNum = case rep of
MemRepTuple -> argsNum
MemRepUnit -> 0
MemRepUnpacked {} -> 0

getCommandInfo :: Command -> CommandInfo
getCommandInfo = \case
Instr CmdInstr {..} -> _cmdInstrInfo
Branch CmdBranch {..} -> _cmdBranchInfo
Case CmdCase {..} -> _cmdCaseInfo
Save CmdSave {..} -> _cmdSaveInfo

getCommandLocation :: Command -> Maybe Location
getCommandLocation = (^. commandInfoLocation) . getCommandInfo
31 changes: 18 additions & 13 deletions src/Juvix/Compiler/Asm/Translation/FromTree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,16 @@ genCode fi =
goExtendClosure :: Bool -> Tree.NodeExtendClosure -> Code'
goExtendClosure isTail Tree.NodeExtendClosure {..} =
snocReturn isTail $
DL.snoc
DL.append
(goArgs (toList _nodeExtendClosureArgs))
( mkInstr $
ExtendClosure $
InstrExtendClosure
{ _extendClosureArgsNum = length _nodeExtendClosureArgs
}
( DL.snoc
(go False _nodeExtendClosureFun)
( mkInstr $
ExtendClosure $
InstrExtendClosure
{ _extendClosureArgsNum = length _nodeExtendClosureArgs
}
)
)

goCall :: Bool -> Tree.NodeCall -> Code'
Expand All @@ -131,13 +134,15 @@ genCode fi =
Tree.CallClosure arg ->
DL.append
(goArgs _nodeCallArgs)
( DL.snoc (go False arg) $
mkInstr $
(if isTail then TailCall else Call) $
InstrCall
{ _callType = CallClosure,
_callArgsNum = length _nodeCallArgs
}
( DL.snoc
(go False arg)
( mkInstr $
(if isTail then TailCall else Call) $
InstrCall
{ _callType = CallClosure,
_callArgsNum = length _nodeCallArgs
}
)
)

goCallClosures :: Bool -> Tree.NodeCallClosures -> Code'
Expand Down
43 changes: 43 additions & 0 deletions src/Juvix/Compiler/Tree/Error.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Juvix.Compiler.Tree.Error where

import Juvix.Compiler.Tree.Language.Base
import Juvix.Data.PPOutput
import Text.Show

data TreeError = TreeError
{ _treeErrorLoc :: Maybe Location,
_treeErrorMsg :: Text
}

makeLenses ''TreeError

instance ToGenericError TreeError where
genericError :: (Member (Reader GenericOptions) r) => TreeError -> Sem r GenericError
genericError e = ask >>= generr
where
generr :: GenericOptions -> Sem r GenericError
generr _ =
return
GenericError
{ _genericErrorLoc = i,
_genericErrorMessage = ppOutput msg,
_genericErrorIntervals = [i]
}
where
i = getLoc e
msg = pretty (e ^. treeErrorMsg)

instance Pretty TreeError where
pretty (TreeError {..}) = pretty _treeErrorMsg

instance Show TreeError where
show (TreeError {..}) = fromText _treeErrorMsg

instance HasLoc TreeError where
getLoc (TreeError {..}) = fromMaybe defaultLoc _treeErrorLoc
where
defaultLoc :: Interval
defaultLoc = singletonInterval (mkInitialLoc sourcePath)

sourcePath :: Path Abs File
sourcePath = $(mkAbsFile "/<tree>")
4 changes: 2 additions & 2 deletions src/Juvix/Compiler/Tree/Keywords.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Juvix.Data.Keyword.All
kwSave,
kwSeq_,
kwShow,
kwStrConcat,
kwStrcat,
kwSub_,
kwTrace,
)
Expand All @@ -47,7 +47,7 @@ allKeywords =
kwLe_,
kwSeq_,
kwEq_,
kwStrConcat,
kwStrcat,
kwShow,
kwAtoi,
kwTrace,
Expand Down
8 changes: 4 additions & 4 deletions src/Juvix/Compiler/Tree/Pretty/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ instance PrettyCode TypeConstr where
let ctrname = fromJust (HashMap.lookup _typeConstrTag tagNames)
let cname = annotate (AnnKind KNameConstructor) (pretty ctrname)
args <- mapM ppCode _typeConstrFields
return $ iname <> kwColon <> cname <> encloseSep "(" ")" ", " args
return $ iname <> kwColon <> cname <> parens (hsep (punctuate comma args))

instance PrettyCode TypeFun where
ppCode :: (Member (Reader Options) r) => TypeFun -> Sem r (Doc Ann)
Expand All @@ -114,7 +114,7 @@ instance PrettyCode TypeFun where
ppLeftExpression funFixity (head _typeFunArgs)
| otherwise -> do
args <- mapM ppCode _typeFunArgs
return $ encloseSep "(" ")" ", " (toList args)
return $ parens $ hsep $ punctuate comma (toList args)
r <- ppRightExpression funFixity _typeFunTarget
return $ l <+> kwArrow <+> r

Expand Down Expand Up @@ -326,7 +326,7 @@ ppFunInfo ppCode' FunctionInfo {..} = do
return $
keyword Str.function
<+> annotate (AnnKind KNameFunction) (pretty (quoteFunName $ quoteName _functionName))
<> encloseSep lparen rparen ", " args
<> parens (hsep (punctuate comma args))
<+> colon
<+> targetty
<+> braces' c
Expand All @@ -338,7 +338,7 @@ ppFunSig FunctionInfo {..} = do
return $
keyword Str.function
<+> annotate (AnnKind KNameFunction) (pretty (quoteFunName $ quoteName _functionName))
<> encloseSep lparen rparen ", " argtys
<> parens (hsep (punctuate comma argtys))
<+> colon
<+> targetty
<> semi
Expand Down
Loading
Loading