summaryrefslogtreecommitdiff
path: root/DSL/Interpretation.hs
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-02-22 21:42:36 +1300
committerMatthew Hall <hallmatthew314@gmail.com>2023-02-22 21:42:36 +1300
commit0bde837174fcb9c17cb3adbf6bc3c7407cab10df (patch)
treeed5c1d5f6c0a7526e6c0808ebc02f19366ce4a03 /DSL/Interpretation.hs
parent076364eb60b008e3ea9503049b5f0199273811cb (diff)
Proctables are now maps
Diffstat (limited to 'DSL/Interpretation.hs')
-rw-r--r--DSL/Interpretation.hs11
1 files changed, 7 insertions, 4 deletions
diff --git a/DSL/Interpretation.hs b/DSL/Interpretation.hs
index e68b64f..37165c3 100644
--- a/DSL/Interpretation.hs
+++ b/DSL/Interpretation.hs
@@ -1,5 +1,6 @@
module DSL.Interpretation where
+import qualified Data.Map.Strict as M
import Data.Foldable (foldlM)
import DSL.Types
@@ -8,7 +9,7 @@ import DSL.Intrinsics
import DSL.StdLib (stdlib)
newMachine :: Machine
-newMachine = Machine { ok=True, stack=[], pTable=[] }
+newMachine = Machine { ok=True, stack=[], pTable=M.empty }
pushData :: Machine -> StackData -> Machine
pushData m@Machine{ stack=xs } x = m{ stack=x:xs }
@@ -47,7 +48,7 @@ applyIntrinsic i m = do
applyCall :: ProcName -> Machine -> IO Machine
applyCall _ m@Machine{ ok=False } = return m
-applyCall name m@Machine{ pTable=t } = case lookup name t of
+applyCall name m@Machine{ pTable=t } = case M.lookup name t of
Nothing -> hcf m $ "PROCCALL: undefined proc: " ++ name
Just bs -> evalBlocks bs m
@@ -92,6 +93,8 @@ evalBlocks (BLinear b:bs) m = applyLinear m b >>= evalBlocks bs
evalBlocks (BIf c t f:bs) m = applyIf c t f m >>= evalBlocks bs
evalBlocks (BWhile c b:bs) m = applyWhile c b m >>= evalBlocks bs
-interpret :: ([ProcSpec], Program) -> IO ()
-interpret (t, p) = evalBlocks p newMachine{ pTable=stdlib++t } >> return ()
+interpret :: (ProcTable, Program) -> IO ()
+interpret (t, p) = case mergeProcTables stdlib t of
+ Just t' -> () <$ evalBlocks p newMachine{ pTable=t' }
+ Nothing -> putStrLn "Failed to include stdlib, duplicate proc definition?"