added comments

This commit is contained in:
2025-09-07 18:54:55 +01:00
parent c323e84a00
commit cd527903c2
4 changed files with 18 additions and 4 deletions

View File

@@ -52,6 +52,8 @@ data Environment = Environment { label :: Int
handleSymbol :: Handle -> ZorthExpr -> StateT Environment IO ()
handleSymbol _ (ZorthASTComment _) = return ()
handleSymbol h (ZorthASTInteger i) =
do
liftIO $ hPutStrLn h $ " push "<>show i
@@ -87,7 +89,7 @@ handleSymbol h (ZorthASTWord "/") = do
\ pop rax\n\
\ cqo\n\
\ idiv rbx\n\
\ imul rax, rbx\n"
\ push rax\n"
return ()
handleSymbol h (ZorthASTWord "ret") = do

View File

@@ -12,6 +12,7 @@ data ZorthExpr = ZorthASTInteger Int
| ZorthASTWordDecl (String,ZorthAST)
| ZorthASTIfElse (ZorthAST,ZorthAST)
| ZorthASTWhile (ZorthAST,ZorthAST)
| ZorthASTComment String
deriving Show
word1 :: Parser String
@@ -47,7 +48,9 @@ pZorthInteger = do
pZorthWord :: Parser ZorthExpr
pZorthWord = do
skipNonsenseSymbols
ZorthASTWord <$> word1
w <- word1
eof <|> (nonsenseSymbol >> return ())
return $ ZorthASTWord w
pZorthWordDecl :: Parser ZorthExpr
pZorthWordDecl = do
@@ -56,7 +59,6 @@ pZorthWordDecl = do
xs <- manyTill pZorthExpr (do { ZorthASTWord ";" <- pZorthWord; return () })
return $ ZorthASTWordDecl (name,xs)
pZorthIfElse :: Parser ZorthExpr
pZorthIfElse = do
ZorthASTWord "if" <- pZorthWord
@@ -71,8 +73,14 @@ pZorthWhile = do
body <- manyTill pZorthExpr (do { ZorthASTWord "repeat" <- pZorthWord; return () })
return $ ZorthASTWhile (condition,body)
pZorthComment :: Parser ZorthExpr
pZorthComment = do
ZorthASTWord "(" <- pZorthWord
body <- manyTill get (do { ZorthASTWord ")" <- pZorthWord; return () })
return $ ZorthASTComment body
pZorthExpr :: Parser ZorthExpr
pZorthExpr = pZorthWhile <|> pZorthIfElse <|> pZorthWordDecl <++ pZorthInteger <++ pZorthWord
pZorthExpr = pZorthComment <|> pZorthWhile <|> pZorthIfElse <|> pZorthWordDecl <++ pZorthInteger <++ pZorthWord
pZorth :: Parser ZorthAST
pZorth = some pZorthExpr

View File

@@ -1,3 +1,5 @@
( 69420 - a variation on FizzBuzz... but with 69 and 420 )
: over swap dup rot rot ;
: mod over over / * - ;

View File

@@ -1,3 +1,5 @@
( iterative fibonacci sequence )
: fib
0 1
begin