added while looping construct
This commit is contained in:
@@ -154,6 +154,26 @@ handleSymbol h (ZorthASTIfElse (ifBranch,elseBranch)) = do
|
|||||||
put $ Environment l'' state
|
put $ Environment l'' state
|
||||||
liftIO $ hPutStr h $ ".L"<>l2<>":\n"
|
liftIO $ hPutStr h $ ".L"<>l2<>":\n"
|
||||||
|
|
||||||
|
handleSymbol h (ZorthASTWhile (condition,body)) = do
|
||||||
|
(Environment l state) <- get
|
||||||
|
let bodyl = show $ l+1
|
||||||
|
let conditionl = show $ l+2
|
||||||
|
let restl = show $ l+3
|
||||||
|
put $ Environment (l+3) state
|
||||||
|
liftIO $ hPutStrLn h $ " jmp .L"<>conditionl
|
||||||
|
liftIO $ hPutStrLn h $ ".L"<>bodyl<>":"
|
||||||
|
compileZorthASTState h body
|
||||||
|
(Environment l' _) <- get
|
||||||
|
put $ Environment l' state
|
||||||
|
liftIO $ hPutStrLn h $ "\n.L"<>conditionl<>":"
|
||||||
|
compileZorthASTState h condition
|
||||||
|
(Environment l'' _) <- get
|
||||||
|
put $ Environment l'' state
|
||||||
|
liftIO $ hPutStrLn h $ " pop rax"
|
||||||
|
liftIO $ hPutStrLn h $ " cmp rax,0"
|
||||||
|
liftIO $ hPutStrLn h $ " jg .L"<>bodyl
|
||||||
|
liftIO $ hPutStrLn h $ ".L"<>restl<>":"
|
||||||
|
|
||||||
truthOperator :: Handle -> String -> IO ()
|
truthOperator :: Handle -> String -> IO ()
|
||||||
truthOperator h s =
|
truthOperator h s =
|
||||||
hPutStr h $
|
hPutStr h $
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ data ZorthExpr = ZorthASTInteger Int
|
|||||||
| ZorthASTWord String
|
| ZorthASTWord String
|
||||||
| ZorthASTWordDecl (String,ZorthAST)
|
| ZorthASTWordDecl (String,ZorthAST)
|
||||||
| ZorthASTIfElse (ZorthAST,ZorthAST)
|
| ZorthASTIfElse (ZorthAST,ZorthAST)
|
||||||
|
| ZorthASTWhile (ZorthAST,ZorthAST)
|
||||||
deriving Show
|
deriving Show
|
||||||
|
|
||||||
word1 :: Parser String
|
word1 :: Parser String
|
||||||
@@ -41,7 +42,6 @@ pZorthInteger :: Parser ZorthExpr
|
|||||||
pZorthInteger = do
|
pZorthInteger = do
|
||||||
skipNonsenseSymbols
|
skipNonsenseSymbols
|
||||||
i <- pZorthSignedInteger <|> pZorthUnsignedInteger
|
i <- pZorthSignedInteger <|> pZorthUnsignedInteger
|
||||||
eof <|> void nonsenseSymbol
|
|
||||||
return i
|
return i
|
||||||
|
|
||||||
pZorthWord :: Parser ZorthExpr
|
pZorthWord :: Parser ZorthExpr
|
||||||
@@ -64,8 +64,15 @@ pZorthIfElse = do
|
|||||||
elseBranch <- manyTill pZorthExpr (do { ZorthASTWord "fi" <- pZorthWord; return () })
|
elseBranch <- manyTill pZorthExpr (do { ZorthASTWord "fi" <- pZorthWord; return () })
|
||||||
return $ ZorthASTIfElse (ifBranch,elseBranch)
|
return $ ZorthASTIfElse (ifBranch,elseBranch)
|
||||||
|
|
||||||
|
pZorthWhile :: Parser ZorthExpr
|
||||||
|
pZorthWhile = do
|
||||||
|
ZorthASTWord "begin" <- pZorthWord
|
||||||
|
condition <- manyTill pZorthExpr (do { ZorthASTWord "while" <- pZorthWord; return () })
|
||||||
|
body <- manyTill pZorthExpr (do { ZorthASTWord "repeat" <- pZorthWord; return () })
|
||||||
|
return $ ZorthASTWhile (condition,body)
|
||||||
|
|
||||||
pZorthExpr :: Parser ZorthExpr
|
pZorthExpr :: Parser ZorthExpr
|
||||||
pZorthExpr = pZorthIfElse <|> pZorthWordDecl <++ pZorthInteger <++ pZorthWord
|
pZorthExpr = pZorthWhile <|> pZorthIfElse <|> pZorthWordDecl <++ pZorthInteger <++ pZorthWord
|
||||||
|
|
||||||
pZorth :: Parser ZorthAST
|
pZorth :: Parser ZorthAST
|
||||||
pZorth = some pZorthExpr
|
pZorth = some pZorthExpr
|
||||||
|
|||||||
Reference in New Issue
Block a user