rc #13
@@ -27,6 +27,7 @@ import Debug.Trace
|
|||||||
import Data.Map.Strict qualified as M
|
import Data.Map.Strict qualified as M
|
||||||
import Data.List (intersect)
|
import Data.List (intersect)
|
||||||
import GHC.Stack (HasCallStack)
|
import GHC.Stack (HasCallStack)
|
||||||
|
import Control.Lens
|
||||||
----------------------------------------------------------------------------------
|
----------------------------------------------------------------------------------
|
||||||
|
|
||||||
data Heap a = Heap [Addr] (Map Addr a)
|
data Heap a = Heap [Addr] (Map Addr a)
|
||||||
@@ -34,6 +35,21 @@ data Heap a = Heap [Addr] (Map Addr a)
|
|||||||
|
|
||||||
type Addr = Int
|
type Addr = Int
|
||||||
|
|
||||||
|
type instance Index (Heap a) = Addr
|
||||||
|
type instance IxValue (Heap a) = a
|
||||||
|
|
||||||
|
instance Ixed (Heap a) where
|
||||||
|
ix a k (Heap as m) = Heap as <$> M.alterF k' a m where
|
||||||
|
k' (Just v) = Just <$> k v
|
||||||
|
k' Nothing = pure Nothing
|
||||||
|
|
||||||
|
instance At (Heap a) where
|
||||||
|
at ma k (Heap as m) = Heap as <$> M.alterF k ma m
|
||||||
|
|
||||||
|
instance FoldableWithIndex Addr Heap where
|
||||||
|
ifoldr fi z (Heap _ m) = ifoldr fi z m
|
||||||
|
ifoldMap iam (Heap _ m) = ifoldMap iam m
|
||||||
|
|
||||||
instance Semigroup (Heap a) where
|
instance Semigroup (Heap a) where
|
||||||
Heap ua ma <> Heap ub mb = Heap u m
|
Heap ua ma <> Heap ub mb = Heap u m
|
||||||
where
|
where
|
||||||
@@ -54,7 +70,7 @@ instance Foldable Heap where
|
|||||||
length (Heap _ m) = M.size m
|
length (Heap _ m) = M.size m
|
||||||
|
|
||||||
instance Traversable Heap where
|
instance Traversable Heap where
|
||||||
traverse t (Heap u m) = Heap u <$> (traverse t m)
|
traverse t (Heap u m) = Heap u <$> traverse t m
|
||||||
|
|
||||||
----------------------------------------------------------------------------------
|
----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|||||||
35
src/GM.hs
35
src/GM.hs
@@ -32,6 +32,11 @@ import Text.PrettyPrint hiding ((<>))
|
|||||||
import Text.PrettyPrint.HughesPJ (maybeParens)
|
import Text.PrettyPrint.HughesPJ (maybeParens)
|
||||||
import Data.Foldable (traverse_)
|
import Data.Foldable (traverse_)
|
||||||
import System.IO (Handle, hPutStrLn)
|
import System.IO (Handle, hPutStrLn)
|
||||||
|
-- TODO: an actual output system
|
||||||
|
-- TODO: an actual output system
|
||||||
|
-- TODO: an actual output system
|
||||||
|
-- TODO: an actual output system
|
||||||
|
import System.IO.Unsafe (unsafePerformIO)
|
||||||
import Data.String (IsString)
|
import Data.String (IsString)
|
||||||
import Data.Heap
|
import Data.Heap
|
||||||
import Debug.Trace
|
import Debug.Trace
|
||||||
@@ -40,6 +45,9 @@ import Core2Core
|
|||||||
import Core
|
import Core
|
||||||
----------------------------------------------------------------------------------
|
----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
tag_Unit_unit :: Int
|
||||||
|
tag_Unit_unit = 0
|
||||||
|
|
||||||
tag_Bool_True :: Int
|
tag_Bool_True :: Int
|
||||||
tag_Bool_True = 1
|
tag_Bool_True = 1
|
||||||
|
|
||||||
@@ -99,6 +107,7 @@ data Instr = Unwind
|
|||||||
| Pack Tag Int -- Pack Tag Arity
|
| Pack Tag Int -- Pack Tag Arity
|
||||||
| CaseJump [(Tag, Code)]
|
| CaseJump [(Tag, Code)]
|
||||||
| Split Int
|
| Split Int
|
||||||
|
| Print
|
||||||
| Halt
|
| Halt
|
||||||
deriving (Show, Eq)
|
deriving (Show, Eq)
|
||||||
|
|
||||||
@@ -222,9 +231,33 @@ step st = case head (st ^. gmCode) of
|
|||||||
Split n -> splitI n
|
Split n -> splitI n
|
||||||
Pack t n -> packI t n
|
Pack t n -> packI t n
|
||||||
CaseJump as -> caseJumpI as
|
CaseJump as -> caseJumpI as
|
||||||
|
Print -> printI
|
||||||
Halt -> haltI
|
Halt -> haltI
|
||||||
where
|
where
|
||||||
|
|
||||||
|
printI :: GmState
|
||||||
|
printI = case hLookupUnsafe a h of
|
||||||
|
NNum n -> (evilTempPrinter `seq` st)
|
||||||
|
& gmCode .~ i
|
||||||
|
& gmStack .~ s
|
||||||
|
where
|
||||||
|
-- TODO: an actual output system
|
||||||
|
-- TODO: an actual output system
|
||||||
|
-- TODO: an actual output system
|
||||||
|
-- TODO: an actual output system
|
||||||
|
evilTempPrinter = unsafePerformIO (print n)
|
||||||
|
NConstr _ as -> st
|
||||||
|
& gmCode .~ i' ++ i
|
||||||
|
& gmStack .~ s'
|
||||||
|
where
|
||||||
|
i' = mconcat $ replicate n [Eval,Print]
|
||||||
|
n = length as
|
||||||
|
s' = as ++ s
|
||||||
|
where
|
||||||
|
h = st ^. gmHeap
|
||||||
|
(a:s) = st ^. gmStack
|
||||||
|
Print : i = st ^. gmCode
|
||||||
|
|
||||||
-- nuke the state
|
-- nuke the state
|
||||||
haltI :: GmState
|
haltI :: GmState
|
||||||
haltI = error "halt#"
|
haltI = error "halt#"
|
||||||
@@ -605,6 +638,8 @@ compiledPrims =
|
|||||||
, binop "/#" Div
|
, binop "/#" Div
|
||||||
, binop "==#" Equals
|
, binop "==#" Equals
|
||||||
, binop "<#" Lesser
|
, binop "<#" Lesser
|
||||||
|
, ("print#", 1, [ Push 0, Eval, Print, Pack tag_Unit_unit 0, Update 1, Pop 1
|
||||||
|
, Unwind])
|
||||||
]
|
]
|
||||||
where
|
where
|
||||||
unop k i = (k, 1, [Push 0, Eval, i, Update 1, Pop 1, Unwind])
|
unop k i = (k, 1, [Push 0, Eval, i, Update 1, Pop 1, Unwind])
|
||||||
|
|||||||
Reference in New Issue
Block a user