From 0042a1d999a06e9e1af623d9d143adb961fbe43e Mon Sep 17 00:00:00 2001 From: mewhenthe Date: Sun, 22 Jun 2025 13:41:02 +0200 Subject: [PATCH] Baller --- .gitignore | 4 ++++ main.hs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ test/test.bf | 3 +++ test/test1.bf | 2 ++ test/test2.bf | 1 + 5 files changed, 59 insertions(+) create mode 100644 .gitignore create mode 100644 main.hs create mode 100644 test/test.bf create mode 100644 test/test1.bf create mode 100644 test/test2.bf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..143c788 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +bf +bf.asm +bf.o +main diff --git a/main.hs b/main.hs new file mode 100644 index 0000000..915fc96 --- /dev/null +++ b/main.hs @@ -0,0 +1,49 @@ +import System.Environment +import System.IO + +template = "global _start\n\nSECTION .data\n\nSECTION .bss\ntape resb 255\nbuf resb 1\n\nSECTION .text\n_start:" +left_arrow = "\n\tsub rax, 1\n\tand rax, 0x00000000000000FF" +right_arrow = "\n\tadd rax, 1\n\tand rax, 0x00000000000000FF" +add_bf = "\n\tmov bl, [tape+rax]\n\tadd bl, 1\n\tmov [tape+rax], bl" +sub_bf = "\n\tmov bl, [tape+rax]\n\tsub bl, 1\n\tmov [tape+rax], bl" +dot = "\n\tpush rax\n\tpush qword [tape+rax]\n\tmov rax, 1\n\tmov rdi, 1,\n\tmov rsi, rsp\n\tmov rdx, 1\n\tsyscall\n\tpop rax\n\tpop rax" +comma = "\n\tpush rax\n\tmov rax, 0\n\tmov rdi, 0\n\tmov rsi, buf\n\tmov rdx, 1\n\tsyscall\n\tpop rax\n\tmov rbx, [buf]\n\tmov [tape+rax], bl" +end = "\n\n\n\tmov rdi, [tape+rax]\n\tmov rax, 60\n\tsyscall" + +opbrack = ":\n\tcmp byte [tape+rax], 0\n\tje close_" +closbrack = ":\n\tcmp byte [tape+rax], 0\n\tjne open_" + +append :: [Int] -> Int -> [Int] +append stack val = stack ++ [val] + +parse_other :: Char -> String +parse_other char + | char == '>' = right_arrow + | char == '<' = left_arrow + | char == '+' = add_bf + | char == '-' = sub_bf + | char == '.' = dot + | char == ',' = comma + | otherwise = "" + +parse_bracket :: Char -> Int -> String +parse_bracket char index + | char == '[' = "\nopen_" ++ show index ++ opbrack ++ show index + | char == ']' = "\nclose_" ++ show index ++ closbrack ++ show index + | otherwise = "" -- never + +parse :: String -> Int -> [Int] -> String -- the stack contains positions +parse file index brackstack + | index == (length file) = "" + | file!!index == '[' = parse_bracket (file!!index) index ++ (parse file (index+1) (append brackstack index)) + | file!!index == ']' = parse_bracket (file!!index) (last brackstack) ++ (parse file (index+1) (init brackstack)) + | otherwise = (parse_other $ file!!index) ++ parse file (index+1) brackstack + +main :: IO() +main = do + -- let file = "/home/coderlol/code/bfhaskell/test/test.bf" + args <- getArgs + print("Compiling " ++ (args!!0) ++ "...") + content <- readFile(args!!0) + let stuff = template ++ (parse content 0 []) ++ end + writeFile "./bf.asm" stuff \ No newline at end of file diff --git a/test/test.bf b/test/test.bf new file mode 100644 index 0000000..977d024 --- /dev/null +++ b/test/test.bf @@ -0,0 +1,3 @@ +>++++++++[<+++++++++>-]<.>++++[<+++++++>-]<+.+++++++..+++.>>++++++[<+++++++>-]<+ ++.------------.>++++++[<+++++++++>-]<+.<.+++.------.--------.>>>++++[<++++++++>- +]<+. \ No newline at end of file diff --git a/test/test1.bf b/test/test1.bf new file mode 100644 index 0000000..83bfbc4 --- /dev/null +++ b/test/test1.bf @@ -0,0 +1,2 @@ +++++++ 6 +[->++++++++++<]>+++++. 65 \ No newline at end of file diff --git a/test/test2.bf b/test/test2.bf new file mode 100644 index 0000000..eb8bdf4 --- /dev/null +++ b/test/test2.bf @@ -0,0 +1 @@ +,+++. \ No newline at end of file