From 4fd4ef20ab1bebef95cf4da92e9b83b98f7e2fa0 Mon Sep 17 00:00:00 2001 From: bunny Date: Sat, 6 Sep 2025 02:53:54 +0100 Subject: [PATCH] added cabal build system --- CHANGELOG.md | 5 +++++ Main.hs | 9 --------- Compiler.hs => app/Compiler.hs | 11 ++++++----- app/Main.hs | 14 ++++++++++++++ Monpar.hs => app/Monpar.hs | 5 ++++- Parser.hs => app/Parser.hs | 14 ++++++++++++-- build.sh | 2 +- zorth.cabal | 20 ++++++++++++++++++++ 8 files changed, 62 insertions(+), 18 deletions(-) create mode 100644 CHANGELOG.md delete mode 100644 Main.hs rename Compiler.hs => app/Compiler.hs (92%) create mode 100644 app/Main.hs rename Monpar.hs => app/Monpar.hs (92%) rename Parser.hs => app/Parser.hs (75%) mode change 100644 => 100755 build.sh create mode 100644 zorth.cabal diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..9bcfa98 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# Revision history for zorth + +## 0.0.6.9 -- YYYY-mm-dd + +* First version. Released on an unsuspecting world. diff --git a/Main.hs b/Main.hs deleted file mode 100644 index f9d6c80..0000000 --- a/Main.hs +++ /dev/null @@ -1,9 +0,0 @@ -module Main where - -import Parser -import Compiler - -main :: IO () -main = do - contents <- getContents - compileZorth $ parseZorth contents \ No newline at end of file diff --git a/Compiler.hs b/app/Compiler.hs similarity index 92% rename from Compiler.hs rename to app/Compiler.hs index 3f29695..00f13ff 100644 --- a/Compiler.hs +++ b/app/Compiler.hs @@ -3,6 +3,7 @@ module Compiler where import Parser import Text.Printf (printf) import Control.Monad (void) +import System.Process import System.IO handleSymbol :: Handle -> ZorthExpr -> IO () @@ -87,8 +88,8 @@ forthPrelude h = do \ ret\n\ \_start:\n" -compileZorth :: ZorthAST -> IO () -compileZorth [] = return () -compileZorth xs = do - forthPrelude stdout - foldr ((>>) . handleSymbol stdout) (return ()) xs +compileZorth :: Handle -> ZorthAST -> IO () +compileZorth h [] = return () +compileZorth h xs = do + forthPrelude h + foldr ((>>) . handleSymbol h) (return ()) xs \ No newline at end of file diff --git a/app/Main.hs b/app/Main.hs new file mode 100644 index 0000000..3e18c82 --- /dev/null +++ b/app/Main.hs @@ -0,0 +1,14 @@ +module Main where + +import Parser +import Compiler +import System.Process +import System.IO + +main :: IO () +main = do + contents <- getContents + withFile "out.asm" WriteMode (\f -> compileZorth f $ parseZorth contents) + readProcess "nasm" ["-f", "elf64", "out.asm", "-o", "out.o"] "" + readProcess "ld" ["out.o", "-o", "a.out"] "" + return () \ No newline at end of file diff --git a/Monpar.hs b/app/Monpar.hs similarity index 92% rename from Monpar.hs rename to app/Monpar.hs index 0a40c86..beb7614 100644 --- a/Monpar.hs +++ b/app/Monpar.hs @@ -75,4 +75,7 @@ word = neWord <|> return "" neWord = do x <- sat (\x -> x /= '\n' && x /= ' ') xs <- word - return $ x:xs \ No newline at end of file + return $ x:xs + +manyTill :: Parser a -> Parser b -> Parser [a] +manyTill p q = (q >> return []) <++ do { x <- p; xs <- manyTill p q; return $ x:xs } \ No newline at end of file diff --git a/Parser.hs b/app/Parser.hs similarity index 75% rename from Parser.hs rename to app/Parser.hs index 5ab639d..59a4ecd 100644 --- a/Parser.hs +++ b/app/Parser.hs @@ -9,7 +9,7 @@ type ZorthAST = [ZorthExpr] data ZorthExpr = ZorthASTInteger Int | ZorthASTWord String - | ZorthASTWordDecl [ZorthExpr] + | ZorthASTWordDecl (String,ZorthAST) deriving Show word1 :: Parser String @@ -48,8 +48,18 @@ pZorthWord = do skipNonsenseSymbols ZorthASTWord <$> word1 +pZorthWordDecl :: Parser ZorthExpr +pZorthWordDecl = do + ZorthASTWord ":" <- pZorthWord + ZorthASTWord name <- pZorthWord + xs <- manyTill pZorthExpr (do { ZorthASTWord ";" <- pZorthWord; return () }) + return $ ZorthASTWordDecl (name,xs) + +pZorthExpr :: Parser ZorthExpr +pZorthExpr = pZorthWordDecl <++ pZorthInteger <++ pZorthWord + pZorth :: Parser ZorthAST -pZorth = some (pZorthInteger <++ pZorthWord) +pZorth = some pZorthExpr parseZorth :: String -> ZorthAST parseZorth = fst . head . runParser pZorth diff --git a/build.sh b/build.sh old mode 100644 new mode 100755 index cd0abdf..00b977e --- a/build.sh +++ b/build.sh @@ -1 +1 @@ -ghc Main.hs -o zorth && ./zorth > out.asm && nasm -f elf64 out.asm -o out.o && ld out.o -o a.out && ./a.out \ No newline at end of file +cabal build && dist-newstyle/build/x86_64-linux/ghc-9.6.7/zorth-0.0.6.9/x/zorth/build/zorth/zorth && ./a.out \ No newline at end of file diff --git a/zorth.cabal b/zorth.cabal new file mode 100644 index 0000000..7ef50aa --- /dev/null +++ b/zorth.cabal @@ -0,0 +1,20 @@ +cabal-version: 3.0 +name: zorth +version: 0.0.6.9 +license: BSD-3-Clause +license-file: LICENSE +author: bunny +maintainer: bunnyanonn@proton.me +category: Development +build-type: Simple +extra-doc-files: CHANGELOG.md +common warnings + ghc-options: -Wall + +executable zorth + import: warnings + main-is: Main.hs + build-depends: base ^>=4.18.3.0, + process ^>=1.6.26.1 + hs-source-dirs: app + default-language: Haskell2010