+34
-39
@@ -10,8 +10,8 @@ module Gyehoek.Wasm
|
||||
, Expr
|
||||
-- ** quasiquoters
|
||||
, expr
|
||||
, Gyehoek.Sexp.sx
|
||||
, Gyehoek.Sexp.sxs
|
||||
, S.sx
|
||||
, S.sxs
|
||||
-- * GenMod effect
|
||||
, GenMod
|
||||
, runGenMod
|
||||
@@ -29,10 +29,6 @@ module Gyehoek.Wasm
|
||||
)
|
||||
where
|
||||
|
||||
import Language.SexpGrammar
|
||||
( SexpIso(..), (>>>) )
|
||||
import Language.SexpGrammar qualified as Sexp
|
||||
import Language.SexpGrammar.Generic
|
||||
import Data.List (List)
|
||||
import GHC.Generics (Generic)
|
||||
import Data.Text (Text)
|
||||
@@ -43,16 +39,16 @@ import Effectful.State.Dynamic
|
||||
import Control.Lens
|
||||
import Data.Vector.Strict (Vector)
|
||||
import qualified Data.Vector.Strict as V
|
||||
import Language.Sexp.Located
|
||||
import qualified Gyehoek.Sexp
|
||||
import GHC.IsList (IsList(..))
|
||||
import Language.Haskell.TH.Quote (QuasiQuoter)
|
||||
import Data.Data (Data)
|
||||
import Gyehoek.Sexp (sx)
|
||||
import Gyehoek.Sexp qualified as S
|
||||
import Gyehoek.Sexp (Datum, sx, (>>>))
|
||||
import Data.Foldable (traverse_)
|
||||
import Data.Coerce (coerce)
|
||||
|
||||
|
||||
newtype Module = MkModule { inner :: Vector Sexp }
|
||||
newtype Module = MkModule { inner :: Vector Datum }
|
||||
deriving (Show, Generic)
|
||||
deriving newtype (Semigroup, Monoid)
|
||||
|
||||
@@ -65,7 +61,7 @@ instance IsList Expr where
|
||||
fromList = MkExpr . V.fromList
|
||||
toList = V.toList . view #inner
|
||||
|
||||
newtype Instr = MkInstr { inner :: Sexp }
|
||||
newtype Instr = MkInstr { inner :: Datum }
|
||||
deriving (Show, Generic, Data, Eq)
|
||||
|
||||
newtype Idx = MkIdx { inner :: Natural }
|
||||
@@ -102,38 +98,38 @@ instance Monoid GenModState where
|
||||
}
|
||||
|
||||
data GenMod :: Effect where
|
||||
DefineFunction :: Sexp -> GenMod m Idx
|
||||
DefineType :: Sexp -> GenMod m Idx
|
||||
DefineGlobal :: Sexp -> GenMod m Idx
|
||||
Emit :: Sexp -> GenMod m ()
|
||||
DefineFunction :: Datum -> GenMod m Idx
|
||||
DefineType :: Datum -> GenMod m Idx
|
||||
DefineGlobal :: Datum -> GenMod m Idx
|
||||
Emit :: Datum -> GenMod m ()
|
||||
|
||||
type instance DispatchOf GenMod = Dynamic
|
||||
|
||||
defineFunction :: GenMod :> es => Sexp -> Eff es Idx
|
||||
defineFunction :: GenMod :> es => Datum -> Eff es Idx
|
||||
defineFunction = send . DefineFunction
|
||||
|
||||
defineFunctions :: GenMod :> es => List Sexp -> Eff es (List Idx)
|
||||
defineFunctions :: GenMod :> es => List Datum -> Eff es (List Idx)
|
||||
defineFunctions = traverse (send . DefineFunction)
|
||||
|
||||
defineType :: GenMod :> es => Sexp -> Eff es Idx
|
||||
defineType :: GenMod :> es => Datum -> Eff es Idx
|
||||
defineType = send . DefineType
|
||||
|
||||
defineTypes :: GenMod :> es => List Sexp -> Eff es (List Idx)
|
||||
defineTypes :: GenMod :> es => List Datum -> Eff es (List Idx)
|
||||
defineTypes = traverse (send . DefineType)
|
||||
|
||||
defineGlobal :: GenMod :> es => Sexp -> Eff es Idx
|
||||
defineGlobal :: GenMod :> es => Datum -> Eff es Idx
|
||||
defineGlobal = send . DefineGlobal
|
||||
|
||||
defineGlobals :: GenMod :> es => List Sexp -> Eff es (List Idx)
|
||||
defineGlobals :: GenMod :> es => List Datum -> Eff es (List Idx)
|
||||
defineGlobals = traverse (send . DefineGlobal)
|
||||
|
||||
emit :: GenMod :> es => List Sexp -> Eff es ()
|
||||
emit :: GenMod :> es => List Datum -> Eff es ()
|
||||
emit = traverse_ (send . Emit)
|
||||
|
||||
appendAndIncrement
|
||||
:: State GenModState :> es
|
||||
=> LensLike' ((,) Natural) GenModState Natural
|
||||
-> Sexp
|
||||
-> Datum
|
||||
-> Eff es Idx
|
||||
appendAndIncrement l s =
|
||||
state \st -> st
|
||||
@@ -154,39 +150,38 @@ execGenMod :: Eff (GenMod : es) a -> Eff es Module
|
||||
execGenMod = fmap snd . runGenMod
|
||||
|
||||
renderModule :: Module -> Text
|
||||
renderModule (MkModule ss) = Gyehoek.Sexp.format [sx|
|
||||
renderModule (MkModule ss) = S.encodeWith' S.datumIso [sx|
|
||||
(module ##{ss})
|
||||
|]
|
||||
|
||||
|
||||
-- SexpIso instances
|
||||
-- DatumIso instances
|
||||
|
||||
instance SexpIso Idx where
|
||||
sexpIso = with \idx ->
|
||||
Sexp.integer >>> Sexp.partialOsi f g
|
||||
instance S.DatumIso Idx where
|
||||
datumIso = S.with \idx ->
|
||||
S.integer >>> S.partialOsi f g
|
||||
>>> idx
|
||||
where
|
||||
f n | n < 0 = Left $ Sexp.unexpected "negative"
|
||||
<> Sexp.expected "natural"
|
||||
f n | n < 0 = Left $ S.unexpected "negative"
|
||||
<> S.expected "natural"
|
||||
| otherwise = Right $ fromIntegral n
|
||||
g = fromIntegral
|
||||
|
||||
instance SexpIso Instr where
|
||||
sexpIso = with id
|
||||
instance S.DatumIso Instr where
|
||||
datumIso = S.with S.id
|
||||
|
||||
instance Gyehoek.Sexp.SpliceSexp Expr where
|
||||
spliceSexp = toListOf $ #inner . each . #inner
|
||||
instance S.DataIso Expr where
|
||||
dataIso = S.dataIso @(Vector Instr) >>> S.iso coerce coerce
|
||||
|
||||
|
||||
-- quasiquoters
|
||||
|
||||
expr :: QuasiQuoter
|
||||
expr = Gyehoek.Sexp.makeSxs
|
||||
[||MkExpr . V.fromList . (each . #inner %~ Gyehoek.Sexp.stripLocation)
|
||||
. fmap (Gyehoek.Sexp.fromSexp @Instr) ||]
|
||||
expr = S.makeSxs
|
||||
[|| MkExpr . V.fromList . fmap (S.fromDatumUnsafe $ S.datumIso @Instr) ||]
|
||||
|
||||
wat :: QuasiQuoter
|
||||
wat = Gyehoek.Sexp.makeSx [|| id ||]
|
||||
wat = S.makeSx [|| id ||]
|
||||
|
||||
wats :: QuasiQuoter
|
||||
wats = Gyehoek.Sexp.makeSxs [|| id ||]
|
||||
wats = S.makeSxs [|| id ||]
|
||||
|
||||
Reference in New Issue
Block a user