Skip to content

Commit

Permalink
Standardize on ByteString everywhere instead of String/Text/ByteStrin…
Browse files Browse the repository at this point in the history
…g mishmash
  • Loading branch information
dougalm committed Jan 3, 2025
1 parent 5a5ddeb commit abaa2fd
Show file tree
Hide file tree
Showing 16 changed files with 323 additions and 323 deletions.
7 changes: 5 additions & 2 deletions dex.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ library
, Builder
-- , CheapReduction
-- , CheckType
, ToLLVM
, ConcreteSyntax
-- , Core
-- , DPS
Expand All @@ -49,6 +50,7 @@ library
, Inference
-- , Inline
, Lexing
, LLVMFFI
, MonadUtil
, MTL1
, Name
Expand All @@ -62,6 +64,7 @@ library
, SourceIdTraversal
, TopLevel
-- , Transpose
, Types.LLVM
, Types.Simple
, Types.Complicated
, Types.Primitives
Expand Down Expand Up @@ -90,8 +93,6 @@ library
-- Parsing
, megaparsec
, parser-combinators
-- Text output
, text
-- Portable system utilities
, directory
, filepath
Expand All @@ -109,6 +110,7 @@ library
, http-types
, wai
, warp
, word8
if flag(debug)
cpp-options: -DDEX_DEBUG
default-language: Haskell2010
Expand Down Expand Up @@ -152,6 +154,7 @@ library
, NamedFieldPuns
, NamedWildCards
, NumericUnderscores
, OverloadedRecordDot
, PatternGuards
, PolyKinds
, PostfixOperators
Expand Down
54 changes: 21 additions & 33 deletions src/dex.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@
import Control.Monad
import Control.Monad.State.Strict
import Options.Applicative hiding (Success, Failure)
import Text.PrettyPrint.ANSI.Leijen (text, hardline)
import System.Posix.Terminal (queryTerminal)
import System.Posix.IO (stdOutput)

import Data.String (fromString)
import Data.List
import qualified Data.ByteString.Char8 as BS
import qualified System.Console.ANSI as ANSI
import System.Console.ANSI hiding (Color)

import Types.Source
import TopLevel
import ConcreteSyntax (parseSourceBlocks)
import PPrint
import Util (readFileText)
import Util (readFileText, BString)


data EvalMode = ReplMode
| ScriptMode FilePath
| WebMode FilePath
| GenerateHTML FilePath FilePath
| ClearCache
| Doit -- for ad-hoc haskell code

data CmdOpts = CmdOpts EvalMode EvalConfig

Expand All @@ -36,16 +39,16 @@ runMode (CmdOpts evalMode cfg) = case evalMode of
void $ runTopperM cfg stdOutLogger env do
blocks <- parseSourceBlocks <$> readFileText fname
forM_ blocks \block -> do
liftIO $ putStr $ pprint block
liftIO $ BS.putStr $ pprint block
evalSourceBlockRepl block
_ -> error "not implemented"
Doit -> error "This is an entry point for running ad-hoc Haskell code."

stdOutLogger :: Outputs -> IO ()
stdOutLogger (Outputs outs) = do
isatty <- queryTerminal stdOutput
forM_ outs \out -> do
when (outputPrintFilter out) do
putStr $ printOutput isatty out
BS.putStr $ printOutput isatty out

outputPrintFilter :: Output -> Bool
outputPrintFilter = \case
Expand All @@ -63,8 +66,7 @@ parseOpts :: ParserInfo CmdOpts
parseOpts = simpleInfo $ CmdOpts <$> parseMode <*> parseEvalOpts

helpOption :: String -> String -> Mod f a
helpOption optionName options =
helpDoc (Just (text optionName <> hardline <> text options))
helpOption optionName options = help $ optionName <> "\n" <> options

parseMode :: Parser EvalMode
parseMode = subparser $
Expand All @@ -73,6 +75,7 @@ parseMode = subparser $
<> command "generate-html" (simpleInfo (GenerateHTML <$> sourceFileInfo <*> destFileInfo))
<> command "clean" (simpleInfo (pure ClearCache))
<> command "script" (simpleInfo (ScriptMode <$> sourceFileInfo))
<> command "doit" (simpleInfo (pure Doit))
where
sourceFileInfo = argument str (metavar "FILE" <> help "Source program")
destFileInfo = argument str (metavar "OUTFILE" <> help "Output path")
Expand All @@ -86,48 +89,33 @@ enumOption :: String -> String -> a -> [(String, a)] -> Parser a
enumOption optName prettyOptName defaultVal options = option
(optionList options)
(long optName <> value defaultVal <>
helpOption prettyOptName (intercalate " | " $ fst <$> options))
helpOption prettyOptName (fromString (intercalate " | " $ fst <$> options)))

parseEvalOpts :: Parser EvalConfig
parseEvalOpts = EvalConfig
<$> (option pathOption $ long "lib-path" <> value [LibBuiltinPath]
<> metavar "PATH" <> help "Library path")
<*> optional (strOption $ long "prelude" <> metavar "FILE" <> help "Prelude file")
parseEvalOpts = EvalConfig [LibBuiltinPath]
<$> optional (strOption $ long "prelude" <> metavar "FILE" <> help "Prelude file")
<*> flag NoOptimize Optimize (short 'O' <> help "Optimize generated code")
<*> enumOption "print" "Print backend" PrintCodegen printBackends
where
printBackends = [ ("haskell", PrintHaskell)
, ("dex" , PrintCodegen) ]

printOutput :: Bool -> Output -> String
printOutput :: Bool -> Output -> BString
printOutput isatty out = case out of
Error _ -> addColor isatty Red $ addPrefix ">" $ pprint out
_ -> addPrefix (addColor isatty Cyan ">") $ pprint out

addPrefix :: String -> String -> String
addPrefix prefix s = unlines $ map prefixLine $ lines s
where prefixLine :: String -> String
addPrefix :: BString -> BString -> BString
addPrefix prefix s = BS.unlines $ map prefixLine $ BS.lines s
where prefixLine :: BString -> BString
prefixLine l = case l of "" -> prefix
_ -> prefix ++ " " ++ l
_ -> prefix <> " " <> l

addColor :: Bool -> ANSI.Color -> String -> String
addColor :: Bool -> ANSI.Color -> BString -> BString
addColor False _ s = s
addColor True c s =
setSGRCode [SetConsoleIntensity BoldIntensity, SetColor Foreground Vivid c]
++ s ++ setSGRCode [Reset]

pathOption :: ReadM [LibPath]
pathOption = splitPaths [] <$> str
where
splitPaths :: [LibPath] -> String -> [LibPath]
splitPaths revAcc = \case
[] -> reverse revAcc
s -> let (p,t) = break (==':') s in
splitPaths (parseLibPath p:revAcc) (dropWhile (==':') t)

parseLibPath = \case
"BUILTIN_LIBRARIES" -> LibBuiltinPath
path -> LibDirectory path
fromString (setSGRCode [SetConsoleIntensity BoldIntensity, SetColor Foreground Vivid c])
<> s <> fromString (setSGRCode [Reset])

main :: IO ()
main = execParser parseOpts >>= runMode
16 changes: 8 additions & 8 deletions src/lib/AbstractSyntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ import Control.Monad (forM, when)
import Data.Functor
import Data.Either
import Data.Maybe (catMaybes)
-- import Data.Set qualified as S
import Data.Text (Text)
import qualified Data.ByteString as BS

import ConcreteSyntax
import Err
Expand All @@ -78,7 +77,7 @@ parseBlock b = liftSyntaxM $ block b
liftSyntaxM :: Fallible m => SyntaxM a -> m a
liftSyntaxM cont = liftExcept cont

parseTopDeclRepl :: Text -> Maybe SourceBlock
parseTopDeclRepl :: BString -> Maybe SourceBlock
parseTopDeclRepl s = case sbContents b of
UnParseable True _ -> Nothing
_ -> case checkSourceBlockParses $ sbContents b of
Expand Down Expand Up @@ -370,7 +369,7 @@ singleArg = \case
((,) <$> withoutSrc <$> identifier "named argument" lhs <*> expr rhs)
g -> Left <$> expr g

identifier :: String -> GroupW -> SyntaxM SourceNameW
identifier :: BString -> GroupW -> SyntaxM SourceNameW
identifier ctx (WithSrcs sid _ g) = case g of
CLeaf (CIdentifier name) -> return $ WithSrc sid name
_ -> throw sid $ ExpectedIdentifier ctx
Expand Down Expand Up @@ -530,10 +529,11 @@ leaf sid = \case
CIdentifier name -> return $ fromSourceNameW $ WithSrc sid name
CNat word -> return $ UNatLit word
CInt int -> return $ UIntLit int
CString str -> do
xs <- return $ map (WithSrcE sid . charExpr) str
let toListVar = mkUVar sid "to_list"
return $ explicitApp toListVar [WithSrcE sid (UTabCon xs)]
CString str -> undefined
-- CString str -> do
-- xs <- return $ map (WithSrcE sid . charExpr) $ BS.unpack str
-- let toListVar = mkUVar sid "to_list"
-- return $ explicitApp toListVar [WithSrcE sid (UTabCon xs)]
CChar char -> return $ charExpr char
CFloat num -> return $ UFloatLit num
CHole -> return UHole
Expand Down
7 changes: 3 additions & 4 deletions src/lib/Actor.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ import Control.Monad
import Control.Monad.State.Strict
import Control.Monad.Reader
import Data.IORef
import Data.Text (Text)
import System.Directory (getModificationTime)
import GHC.Generics

import IncState
import MonadUtil
import Util (readFileText)
import Util (readFileText, BString)

-- === Actor implementation ===

Expand Down Expand Up @@ -186,12 +185,12 @@ launchClock intervalMicroseconds mailbox =

-- === File watcher ===

type SourceFileContents = Text
type SourceFileContents = BString
type FileWatcher = StateServer (Overwritable SourceFileContents)

data FileWatcherMsg =
ClockSignal_FW ()
| Subscribe_FW (SubscribeMsg (Overwritable Text))
| Subscribe_FW (SubscribeMsg (Overwritable BString))
deriving (Show)

launchFileWatcher :: MonadIO m => FilePath -> m FileWatcher
Expand Down
Loading

0 comments on commit abaa2fd

Please sign in to comment.