This commit is contained in:
crumbtoo
2023-11-10 14:34:28 -07:00
parent 1f7272d0f0
commit 0a9e4230ee
2 changed files with 24 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ import Data.Coerce
import Data.Pretty import Data.Pretty
import Data.List (intersperse) import Data.List (intersperse)
import Data.Function ((&)) import Data.Function ((&))
import Data.String
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
data Expr = Var Name data Expr = Var Name
@@ -44,6 +45,9 @@ data ScDef = ScDef Name [Name] Expr
newtype Program = Program [ScDef] newtype Program = Program [ScDef]
instance IsString Expr where
fromString = Var
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
instance Pretty Expr where instance Pretty Expr where

View File

@@ -90,8 +90,14 @@ instantiate (Let NonRec bs e) h g = instantiate e h' (g' ++ g)
let (h',a) = instantiate v h g let (h',a) = instantiate v h g
in (h',(k,a)) in (h',(k,a))
-- instantiate (Let Rec ((k:=v):bs) e) h g = instantiate e h' ((k,a):g) instantiate (Let Rec bs e) h g = instantiate e h' env
-- where (h',a) = instantiate v h g where
env = g' ++ g
(h', g') = mapAccumL instBinder h bs
instBinder :: TiHeap -> Binding -> (TiHeap, (Name, Addr))
instBinder h (k := v) =
let (h',a) = instantiate v h env
in (h',(k,a))
instantiate (Prim (IntP n)) h _ = alloc h (NNum n) instantiate (Prim (IntP n)) h _ = alloc h (NNum n)
@@ -149,10 +155,19 @@ doAdmin (TiState s d h g sts) = TiState s d h g (sts+1)
dbgProg :: Program -> IO () dbgProg :: Program -> IO ()
dbgProg p = prettyPrint `traverse_` eval (fromJust $ compile p) dbgProg p = prettyPrint `traverse_` eval (fromJust $ compile p)
testProg :: Program letrecExample :: Program
testProg = Program letrecExample = Program
-- [ ScDef "main" [] $ Prim IntAddP :$ Prim (IntP 2) :$ Prim (IntP 3) -- [ ScDef "main" [] $ Prim IntAddP :$ Prim (IntP 2) :$ Prim (IntP 3)
[ ScDef "main" [] $ Let NonRec ["x" := Prim (IntP 2)] (Var "x") [ ScDef "pair" ["x","y","f"] $ "f" :$ "x" :$ "y"
, ScDef "fst" ["p"] $ "p" :$ "K"
, ScDef "snd" ["p"] $ "p" :$ "K1"
, ScDef "f" ["x","y"] $
Let Rec
[ "a" := "pair" :$ "x" :$ "b"
, "b" := "pair" :$ "y" :$ "a"
]
("fst" :$ ("snd" :$ ("snd" :$ ("snd" :$ "a"))))
, ScDef "main" [] $ "f" :$ Prim (IntP 3) :$ Prim (IntP 4)
] ]
instance Pretty TiState where instance Pretty TiState where