added conditions and if else branching

This commit is contained in:
2025-09-07 01:20:16 +01:00
parent 545137ce76
commit f600c3b57c
2 changed files with 72 additions and 10 deletions

View File

@@ -10,6 +10,7 @@ type ZorthAST = [ZorthExpr]
data ZorthExpr = ZorthASTInteger Int
| ZorthASTWord String
| ZorthASTWordDecl (String,ZorthAST)
| ZorthASTIfElse (ZorthAST,ZorthAST)
deriving Show
word1 :: Parser String
@@ -55,8 +56,16 @@ pZorthWordDecl = do
xs <- manyTill pZorthExpr (do { ZorthASTWord ";" <- pZorthWord; return () })
return $ ZorthASTWordDecl (name,xs)
pZorthIfElse :: Parser ZorthExpr
pZorthIfElse = do
ZorthASTWord "if" <- pZorthWord
ifBranch <- manyTill pZorthExpr (do { ZorthASTWord "else" <- pZorthWord; return () })
elseBranch <- manyTill pZorthExpr (do { ZorthASTWord "fi" <- pZorthWord; return () })
return $ ZorthASTIfElse (ifBranch,elseBranch)
pZorthExpr :: Parser ZorthExpr
pZorthExpr = pZorthWordDecl <++ pZorthInteger <++ pZorthWord
pZorthExpr = pZorthIfElse <|> pZorthWordDecl <++ pZorthInteger <++ pZorthWord
pZorth :: Parser ZorthAST
pZorth = some pZorthExpr