quox/exe/Main.idr

167 lines
4.0 KiB
Idris
Raw Permalink Normal View History

2022-04-27 14:04:03 -04:00
module Main
2021-07-07 07:11:39 -04:00
2023-10-20 11:42:01 -04:00
import Quox.Syntax as Q
import Quox.Definition as Q
import Quox.Untyped.Syntax as U
2023-11-27 01:39:17 -05:00
import Quox.Parser
2023-10-20 11:42:01 -04:00
import Quox.Untyped.Erase
2023-10-24 17:52:19 -04:00
import Quox.Untyped.Scheme
2023-11-27 01:39:17 -05:00
import Quox.Pretty
2024-04-04 12:13:45 -04:00
import Quox.Log
2023-10-20 11:42:01 -04:00
import Options
2024-04-04 12:10:53 -04:00
import Output
import Error
import CompileMonad
2021-07-20 16:05:19 -04:00
2023-10-20 11:42:01 -04:00
import System
import System.File
2023-11-27 01:39:17 -05:00
import Data.IORef
2023-03-31 13:31:29 -04:00
import Control.Eff
2021-07-20 16:05:19 -04:00
2023-11-27 01:39:17 -05:00
%default total
2023-10-20 11:42:01 -04:00
%hide Doc.(>>=)
%hide Core.(>>=)
%hide FromParser.Error
%hide Erase.Error
%hide Lexer.Error
%hide Parser.Error
private
2023-11-27 01:39:17 -05:00
Step : Type -> Type -> Type
2024-04-04 12:10:53 -04:00
Step a b = OpenFile -> a -> Eff Compile b
2023-11-05 09:47:52 -05:00
2023-10-20 11:42:01 -04:00
private
step : ConsoleChannel -> Phase -> OutFile -> Step a b -> a -> Eff CompileStop b
step console phase file act x = do
opts <- askAt OPTS
2024-04-04 12:10:53 -04:00
res <- withOutFile console file fromError $ \h => lift $ act h x
2023-11-27 01:39:17 -05:00
when (opts.until == Just phase) stopHere
pure res
2024-04-04 12:10:53 -04:00
where
fromError : String -> FileError -> Eff CompileStop c
fromError file err = throw $ WriteError file err
2023-11-27 01:39:17 -05:00
private covering
parse : Step String PFile
parse h file = do
Just ast <- loadFile noLoc file
| Nothing => pure []
outputStr h $ show ast
pure ast
private covering
check : Step PFile (List Q.NDefinition)
check h decls =
map concat $ for decls $ \decl => do
defs <- liftFromParser $ fromPTopLevel decl
outputDocs h $ traverse (\(x, d) => prettyDef x d) defs
pure defs
private covering
erase : Step (List Q.NDefinition) (List U.NDefinition)
erase h defList =
for defList $ \(x, def) => do
def <- liftErase defs $ eraseDef defs x def
outputDoc h $ U.prettyDef x def
pure (x, def)
where defs = SortedMap.fromList defList
private covering
2024-04-04 12:10:53 -04:00
scheme : Step (List U.NDefinition) (List Sexp, List Id)
2023-11-27 01:39:17 -05:00
scheme h defs = do
sexps' <- for defs $ \(x, d) => do
(msexp, mains) <- liftScheme $ defToScheme x d
2024-04-04 12:10:53 -04:00
outputDoc h $ case msexp of
Just s => prettySexp s
Nothing => pure $ hsep [";;", prettyName x, "erased"]
2023-11-27 01:39:17 -05:00
pure (msexp, mains)
2024-04-04 12:10:53 -04:00
pure $ bimap catMaybes concat $ unzip sexps'
2023-11-27 01:39:17 -05:00
private covering
2024-04-04 12:10:53 -04:00
output : Step (List Sexp, List Id) ()
output h (sexps, mains) = do
main <- case mains of
[m] => pure m
[] => throw NoMain
_ => throw $ MultipleMains mains
2023-11-27 01:39:17 -05:00
lift $ outputDocs h $ do
res <- traverse prettySexp sexps
runner <- makeRunMain main
pure $ text Scheme.prelude :: res ++ [runner]
2023-10-20 11:42:01 -04:00
2023-11-27 01:39:17 -05:00
private covering
processFile : String -> Eff Compile ()
processFile file = withEarlyStop $ pipeline !(askAt OPTS) file where
pipeline : Options -> String -> Eff CompileStop ()
pipeline opts =
step CErr Parse opts.dump.parse Main.parse >=>
step CErr Check opts.dump.check Main.check >=>
step CErr Erase opts.dump.erase Main.erase >=>
step CErr Scheme opts.dump.scheme Main.scheme >=>
step COut End opts.outFile Main.output
2023-11-27 01:39:17 -05:00
export covering
2023-03-31 13:31:29 -04:00
main : IO ()
main = do
2023-10-20 11:42:01 -04:00
(_, opts, files) <- options
case !(runCompile opts !newState $ traverse_ processFile files) of
Right () => pure ()
2023-11-27 01:39:17 -05:00
Left e => dieError opts e
2023-10-20 11:42:01 -04:00
2023-03-31 13:31:29 -04:00
-----------------------------------
2023-05-14 13:58:46 -04:00
{-
2021-07-20 16:05:19 -04:00
2022-04-11 08:09:37 -04:00
private
2022-04-11 15:58:33 -04:00
text : PrettyOpts -> List String
2022-04-11 08:09:37 -04:00
text _ =
["",
#" ___ ___ _____ __ __"#,
#"/ _ `/ // / _ \\ \ /"#,
#"\_, /\_,_/\___/_\_\"#,
#" /_/"#,
""]
2022-03-06 19:19:26 -05:00
2023-11-27 01:39:17 -05:00
-- ["",
-- #" __ _ _ _ _____ __"#,
-- #"/ _` | || / _ \ \ /"#,
-- #"\__, |\_,_\___/_\_\"#,
-- #" |_|"#,
-- ""]
2022-04-11 08:09:37 -04:00
private
2022-04-11 15:58:33 -04:00
qtuwu : PrettyOpts -> List String
2022-04-11 08:09:37 -04:00
qtuwu opts =
if opts.unicode then
[#" ___,-´⎠ "#,
#"(·`──´ ◡ -´⎠"#,
2023-02-26 04:58:47 -05:00
#" \/\/──´⎞/`──´ "#,
#" ⎜⎟───,-₎ ⎞ "#,
#" ⎝⎠ (‾‾) ⎟ "#,
2022-04-11 08:09:37 -04:00
#" (‾‾‾) ⎟ "#]
else
[#" ___,-´/ "#,
#"(.`--´ u -´/"#,
#" \/\/--´|/`--´ "#,
2023-02-26 04:58:47 -05:00
#" ||---,-, \ "#,
#" `´ (--) | "#,
2022-04-11 08:09:37 -04:00
#" (---) | "#]
private
2022-04-11 15:58:33 -04:00
join1 : PrettyOpts -> String -> String -> String
2022-04-11 08:09:37 -04:00
join1 opts l r =
if opts.color then
" " <+> show (colored Green l) <+> " " <+> show (colored Magenta r)
else
" " <+> l <+> " " <+> r
export
2022-04-11 15:58:33 -04:00
banner : PrettyOpts -> String
2022-04-11 08:09:37 -04:00
banner opts = unlines $ zipWith (join1 opts) (qtuwu opts) (text opts)
2023-05-14 13:58:46 -04:00
-}