From cd527903c25d2b5e691ae9c6507209bbd9372ea4 Mon Sep 17 00:00:00 2001 From: bunny Date: Sun, 7 Sep 2025 18:54:55 +0100 Subject: [PATCH] added comments --- app/Compiler.hs | 4 +++- app/Parser.hs | 14 +++++++++++--- examples/69420.zorth | 2 ++ examples/fib.zorth | 2 ++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/Compiler.hs b/app/Compiler.hs index 65bbabd..e0f89e5 100644 --- a/app/Compiler.hs +++ b/app/Compiler.hs @@ -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 diff --git a/app/Parser.hs b/app/Parser.hs index 33fc461..a1e8502 100644 --- a/app/Parser.hs +++ b/app/Parser.hs @@ -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 diff --git a/examples/69420.zorth b/examples/69420.zorth index 999bb5b..cc53565 100644 --- a/examples/69420.zorth +++ b/examples/69420.zorth @@ -1,3 +1,5 @@ +( 69420 - a variation on FizzBuzz... but with 69 and 420 ) + : over swap dup rot rot ; : mod over over / * - ; diff --git a/examples/fib.zorth b/examples/fib.zorth index 84930f3..459fc6a 100644 --- a/examples/fib.zorth +++ b/examples/fib.zorth @@ -1,3 +1,5 @@ +( iterative fibonacci sequence ) + : fib 0 1 begin