This commit is contained in:
@@ -20,6 +20,7 @@ data VM = MkVM
|
|||||||
, code :: List Instr
|
, code :: List Instr
|
||||||
, registers :: HashMap Name Val
|
, registers :: HashMap Name Val
|
||||||
, stdout :: Text
|
, stdout :: Text
|
||||||
|
, result :: Maybe (List Val)
|
||||||
}
|
}
|
||||||
deriving (Show, Generic)
|
deriving (Show, Generic)
|
||||||
|
|
||||||
@@ -28,10 +29,9 @@ data Env = MkEnv
|
|||||||
}
|
}
|
||||||
deriving (Show, Generic)
|
deriving (Show, Generic)
|
||||||
|
|
||||||
step :: Env -> VM -> Either (List Val) VM
|
step :: Env -> VM -> VM
|
||||||
step e vm = case vm ^. #code of
|
step e vm = case vm ^. #code of
|
||||||
CallLabel "halt" xs :_ -> Left xs
|
i:is -> stepI e (vm & #code .~ is) i
|
||||||
i:is -> Right $ stepI e (vm & #code .~ is) i
|
|
||||||
_ -> error "halt never called"
|
_ -> error "halt never called"
|
||||||
|
|
||||||
stepI :: Env -> VM -> Instr -> VM
|
stepI :: Env -> VM -> Instr -> VM
|
||||||
@@ -51,6 +51,8 @@ stepI e vm (PopCont r) = case vm ^. #kstack of
|
|||||||
stepI e vm (CallReg r xs) = stepI e vm (CallLabel l xs)
|
stepI e vm (CallReg r xs) = stepI e vm (CallLabel l xs)
|
||||||
where l = vm ^?! #registers . at r . _Just . #ValLabel
|
where l = vm ^?! #registers . at r . _Just . #ValLabel
|
||||||
|
|
||||||
|
stepI e vm (CallLabel "halt" xs) = vm & #result ?~ xs
|
||||||
|
|
||||||
stepI e vm (CallLabel l xs) =
|
stepI e vm (CallLabel l xs) =
|
||||||
vm & #code .~ b.code
|
vm & #code .~ b.code
|
||||||
& #registers .~ H.fromList (b.params `zip` xs)
|
& #registers .~ H.fromList (b.params `zip` xs)
|
||||||
@@ -68,6 +70,7 @@ initialVM = MkVM
|
|||||||
, code = [CallLabel "main" []]
|
, code = [CallLabel "main" []]
|
||||||
, registers = mempty
|
, registers = mempty
|
||||||
, stdout = ""
|
, stdout = ""
|
||||||
|
, result = Nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
initialEnv :: Program -> Env
|
initialEnv :: Program -> Env
|
||||||
@@ -81,4 +84,6 @@ loop f a = case f a of
|
|||||||
Left b -> b
|
Left b -> b
|
||||||
|
|
||||||
eval :: Program -> List Val
|
eval :: Program -> List Val
|
||||||
eval p = loop (step $ initialEnv p) initialVM
|
eval p = initialVM & loop \vm -> case vm ^. #result of
|
||||||
|
Nothing -> Right $ step (initialEnv p) vm
|
||||||
|
Just rs -> Left rs
|
||||||
|
|||||||
Reference in New Issue
Block a user