This commit is contained in:
2026-05-15 21:27:30 -06:00
parent 6dda8c4268
commit 15e872779e
4 changed files with 44 additions and 18 deletions

View File

@@ -8,7 +8,10 @@
{-# OPTIONS_GHC -Wno-orphans -Wno-unused-matches -Wno-missing-signatures #-} {-# OPTIONS_GHC -Wno-orphans -Wno-unused-matches -Wno-missing-signatures #-}
{- HLINT ignore "Avoid lambda using `infix`" -} {- HLINT ignore "Avoid lambda using `infix`" -}
module Gyehoek.ANF.Syntax module Gyehoek.ANF.Syntax
(toANF, lower) ( Exp(..)
, toANF
, lower
)
where where
import Data.Text (Text) import Data.Text (Text)

View File

@@ -17,38 +17,56 @@ import System.OsPath (OsPath)
import System.FilePath ((-<.>)) import System.FilePath ((-<.>))
import Effectful.FileSystem import Effectful.FileSystem
import Effectful import Effectful
import Effectful.FileSystem.IO import Effectful.FileSystem.IO qualified as FS
import Gyehoek.GenSym (runGenSym) import Effectful.FileSystem.IO.ByteString qualified as FB
import Gyehoek.GenSym (runGenSym, GenSym)
import qualified Gyehoek.Sexp as Sexp import qualified Gyehoek.Sexp as Sexp
import Data.Text.Lens import Data.Text.Lens
import Data.List (List) import Data.List (List)
import qualified Gyehoek.Scheme.Syntax as Scm import qualified Gyehoek.Scheme.Syntax as Scm
import Effectful.Exception import Effectful.Exception
import qualified Gyehoek.QBE as QBE
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import System.IO (Handle)
main :: IO () main :: IO ()
main = do main = do
opts <- info (helper <*> parser) (fullDesc) opts <- execParser $ info (helper <*> parser) fullDesc
runEff . runFileSystem . driver $ opts runEff . runFileSystem . runGenSym . driver $ opts
hPutStr :: IOE :> es => Handle -> Text -> Eff es () hPutStr :: FileSystem :> es => Handle -> Text -> Eff es ()
hPutStr h = liftIO . TIO.hPutStr h hPutStr h = FB.hPutStr h . T.encodeUtf8
readFile :: IOE :> es => FilePath -> Eff es Text hGetContents :: FileSystem :> es => Handle -> Eff es Text
readFile = liftIO . TIO.readFile hGetContents h = T.decodeUtf8 <$> FB.hGetContents h
readFile :: FileSystem :> es => FilePath -> Eff es Text
readFile f = FS.withFile f FS.ReadMode hGetContents
readScm :: FileSystem :> es => FilePath -> Eff es (List Scm.Exp) readScm :: FileSystem :> es => FilePath -> Eff es (List Scm.Exp)
readScm f = (Sexp.parseSexps f <$> readFile f) >>= either error pure readScm f = (Sexp.parseSexps f <$> readFile f) >>= either error pure
driver :: FileSystem :> es => Options -> Eff es () toANF
driver = runGenSym . traverseOf_ (#sourceFiles . folded) \f -> :: (GenSym :> es, FileSystem :> es)
withFile f ReadMode \h_scm -> => FilePath -> List Scm.Exp -> Eff es (List ANF.Exp)
(Sexp.encode <$> ANF.toANF _) >>= \case toANF f exps = do
Left e -> hPutStr stderr (view packed e) anfs <- traverse ANF.toANF exps
Right s -> case traverse Sexp.encode anfs of
withFile (f -<.> "anf") WriteMode \h_anf -> do Left e -> hPutStr FS.stderr (view packed e)
hPutStr h_anf ";;; -*- mode:scheme -*-\n\n" Right ss -> do
hPutStr h_anf s let anf_file = f -<.> "anf"
FS.withFile anf_file FS.WriteMode \h_anf -> do
hPutStr h_anf ";;; -*- mode:scheme -*-\n\n"
hPutStr h_anf $ foldr (\x y -> x <> "\n\n" <> y) "" ss
hPutStr FS.stderr $ "wrote " <> T.pack anf_file
pure anfs
driver :: (GenSym :> es, FileSystem :> es) => Options -> Eff es ()
driver = runGenSym . traverseOf_ (#sourceFiles . folded) \f -> do
exps <- readScm f
anfs <- toANF f exps
pure ()

4
play/t.anf Normal file
View File

@@ -0,0 +1,4 @@
;;; -*- mode:scheme -*-
(let ((x0 (prim:write 4))) x0)

1
play/t.scm Normal file
View File

@@ -0,0 +1 @@
(prim:write 4)