summaryrefslogtreecommitdiff
path: root/DSL.hs
diff options
context:
space:
mode:
Diffstat (limited to 'DSL.hs')
-rw-r--r--DSL.hs29
1 files changed, 21 insertions, 8 deletions
diff --git a/DSL.hs b/DSL.hs
index 8e275ba..104ee18 100644
--- a/DSL.hs
+++ b/DSL.hs
@@ -47,7 +47,11 @@ data StackModifier = StackModifier { smName :: String
, smFunc :: Stack -> IO Stack
}
-type Program = [Operation]
+data Block
+ = BLinear [Operation]
+ deriving (Show)
+
+type Program = [Block]
data Machine = Machine { ok :: Bool
, stack :: Stack
@@ -142,13 +146,17 @@ operationP = firstOf [ pushDataP
, intrinsicP
]
-programP :: DSLParser Program
-programP = do
- () <$ optional wsP
- ops <- operationP `sepBy1` wsP
- () <$ optional wsP
+linearP :: DSLParser Block
+linearP = do
+ ops <- optional wsP *> operationP `sepBy1` wsP <* optional wsP
eof
- return ops
+ return $ BLinear ops
+
+blockP :: DSLParser Block
+blockP = firstOf [linearP]
+
+programP :: DSLParser Program
+programP = phrase $ mult1 blockP
stringToProgram :: String -> Maybe Program
stringToProgram str = do
@@ -296,8 +304,13 @@ applyOperation m (OpIntrinsic i) = applyIntrinsic i m
applyLinear :: Machine -> [Operation] -> IO Machine
applyLinear = foldlM applyOperation
+evalBlocks :: [Block] -> Machine -> IO Machine
+evalBlocks _ m@Machine{ ok=False } = return m
+evalBlocks [] m = return m
+evalBlocks (BLinear b:bs) m = applyLinear m b >>= evalBlocks bs
+
interpret :: Program -> IO ()
-interpret p = applyLinear newMachine p >> return ()
+interpret p = evalBlocks p newMachine >> return ()
interpretFromString :: String -> IO ()
interpretFromString = maybe err interpret . stringToProgram