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 :: Handle -> ZorthExpr -> StateT Environment IO ()
handleSymbol _ (ZorthASTComment _) = return ()
handleSymbol h (ZorthASTInteger i) = handleSymbol h (ZorthASTInteger i) =
do do
liftIO $ hPutStrLn h $ " push "<>show i liftIO $ hPutStrLn h $ " push "<>show i
@@ -87,7 +89,7 @@ handleSymbol h (ZorthASTWord "/") = do
\ pop rax\n\ \ pop rax\n\
\ cqo\n\ \ cqo\n\
\ idiv rbx\n\ \ idiv rbx\n\
\ imul rax, rbx\n" \ push rax\n"
return () return ()
handleSymbol h (ZorthASTWord "ret") = do handleSymbol h (ZorthASTWord "ret") = do

View File

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

View File

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

View File

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