Skip to content

Commit

Permalink
Finish initial compiler prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Nguyen committed Jul 14, 2024
0 parents commit ef4f8f5
Show file tree
Hide file tree
Showing 31 changed files with 1,771 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.stack-work/
*~
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Changelog for `bf-hs`

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to the
[Haskell Package Versioning Policy](https://pvp.haskell.org/).

## Unreleased

## 0.1.0.0 - YYYY-MM-DD
26 changes: 26 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright 2024 Author name here

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# bf-hs
2 changes: 2 additions & 0 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
68 changes: 68 additions & 0 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{-# LANGUAGE LambdaCase #-}

module Main (main) where

import Data.Foldable (Foldable (..))
import Data.List (unfoldr)
import Debug.Trace
import Language.BF (bf, bf2nasm, runParser)
import System.Environment (getArgs)
import System.IO (BufferMode (..), hSetBuffering, stdout)

printCode :: [String] -> IO ()
printCode f = do
putStrLn "```"
mapM_ putStrLn f
putStrLn "```\n"

getInput :: String -> IO String
getInput question = do
putStr question
getLine

data Args = Args
{ inputFile :: String
}
deriving (Show)

instance Semigroup Args where
(Args{inputFile = i1}) <> (Args{inputFile = i2}) =
Args
{ inputFile = if null i1 then i2 else i1
}

instance Monoid Args where
mempty = Args{inputFile = mempty}

lstrip :: (Eq a) => a -> [a] -> [a]
lstrip c = dropWhile (c ==)

parseArgs :: [String] -> Args
parseArgs args = fold $ unfoldr (parseArg . traceShowId) args
where
parseArg = \case
"-i" : name : xs -> pure (mempty{inputFile = lstrip ' ' name}, xs)
_ -> Nothing

main :: IO ()
main = do
hSetBuffering stdout NoBuffering

args <- parseArgs <$> getArgs
filename <-
if null $ inputFile args
then getInput "filename: "
else pure $ inputFile args

input <- readFile ("input/" <> filename <> ".b")
putStrLn "File Contents:"
printCode $ lines input

case runParser (bf2nasm bf) input of
Nothing -> putStrLn "welp that didnt work for some reason"
Just (prog, _s) -> do
putStrLn ("output/" <> filename <> ".asm")
-- printCode prog
writeFile ("output/" <> filename <> ".asm") $
unlines prog
putStrLn ""
69 changes: 69 additions & 0 deletions bf-hs.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
cabal-version: 2.2

-- This file has been generated from package.yaml by hpack version 0.36.0.
--
-- see: https://github.com/sol/hpack

name: bf-hs
version: 0.1.0.0
description: Please see the README on GitHub at <https://github.com/githubuser/bf-hs#readme>
homepage: https://github.com/githubuser/bf-hs#readme
bug-reports: https://github.com/githubuser/bf-hs/issues
author: Author name here
maintainer: [email protected]
copyright: 2024 Author name here
license: BSD-3-Clause
license-file: LICENSE
build-type: Simple
extra-source-files:
README.md
CHANGELOG.md

source-repository head
type: git
location: https://github.com/githubuser/bf-hs

library
exposed-modules:
Language.BF
other-modules:
Paths_bf_hs
autogen-modules:
Paths_bf_hs
hs-source-dirs:
src
default-extensions:
DeriveFunctor , LambdaCase , RankNTypes , MultiParamTypeClasses , FunctionalDependencies , FlexibleInstances , RecordWildCards , TupleSections , BangPatterns
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
build-depends:
base >=4.7 && <5
default-language: Haskell2010

executable bf-hs-exe
main-is: Main.hs
other-modules:
Paths_bf_hs
autogen-modules:
Paths_bf_hs
hs-source-dirs:
app
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, bf-hs
default-language: Haskell2010

test-suite bf-hs-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_bf_hs
autogen-modules:
Paths_bf_hs
hs-source-dirs:
test
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, bf-hs
default-language: Haskell2010
77 changes: 77 additions & 0 deletions input/fibonacci.b
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
[
Pulled from http://esoteric.sange.fi/brainfuck/bf-source/prog/fibonacci.txt
I just completed my first BrainFuck program, I think it's pretty good. It
generates the fibonacci number sequence, (for numbers under 100). Formats
quite nicely too. Here it is below with comments
]
+++++++++++ number of digits to output
> #1
+ initial number
>>>> #5
++++++++++++++++++++++++++++++++++++++++++++ (comma)
> #6
++++++++++++++++++++++++++++++++ (space)
<<<<<< #0
[
> #1
copy #1 to #7
[>>>>>>+>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]
<
divide #7 by 10 (begins in #7)
[
>
++++++++++ set the divisor #8
[
subtract from the dividend and divisor
-<-
if dividend reaches zero break out
copy dividend to #9
[>>+>+<<<-]>>>[<<<+>>>-]
set #10
+
if #9 clear #10
<[>[-]<[-]]
if #10 move remaining divisor to #11
>[<<[>>>+<<<-]>>[-]]
jump back to #8 (divisor possition)
<<
]
if #11 is empty (no remainder) increment the quotient #12
>>> #11
copy to #13
[>>+>+<<<-]>>>[<<<+>>>-]
set #14
+
if #13 clear #14
<[>[-]<[-]]
if #14 increment quotient
>[<<+>>[-]]
<<<<<<< #7
]
quotient is in #12 and remainder is in #11
>>>>> #12
if #12 output value plus offset to ascii 0
[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]
subtract #11 from 10
++++++++++ #12 is now 10
< #11
[->-<]
> #12
output #12 even if it's zero
++++++++++++++++++++++++++++++++++++++++++++++++.[-]
<<<<<<<<<<< #1
check for final number
copy #0 to #3
<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]
<- #3
if #3 output (comma) and (space)
[>>.>.<<<[-]]
<< #1
[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-
]
125 changes: 125 additions & 0 deletions input/hello_world.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
; GNU Assembler, Intel syntax, x86_64 Linux
SECTION .data
%define SYS_EXIT 1
%define SUCCESS 0
%define SYS_WRITE 4
%define STDOUT 1
%define SYS_READ 3
%define STDIN 0
SECTION .bss
array: resb 30000
SECTION .text
global _start
_start:
mov r12, array
cmp [r12], byte 0
je .LOOP_END_22
.LOOP_START_22:
sub [r12], byte 1
cmp [r12], byte 0
je .LOOP_END_24
.LOOP_START_24:
sub r12, byte 2
cmp [r12], byte 0
je .LOOP_END_27
.LOOP_START_27:
add [r12], byte 1
cmp [r12], byte 0
je .LOOP_END_29
.LOOP_START_29:
sub [r12], byte 3
add r12, byte 1
cmp [r12], byte 0
jne .LOOP_START_29
.LOOP_END_29:
sub [r12], byte 1
cmp [r12], byte 0
je .LOOP_END_36
.LOOP_START_36:
sub r12, byte 3
cmp [r12], byte 0
jne .LOOP_START_36
.LOOP_END_36:
cmp [r12], byte 0
jne .LOOP_START_27
.LOOP_END_27:
cmp [r12], byte 0
jne .LOOP_START_24
.LOOP_END_24:
add r12, byte 3
sub [r12], byte 1
cmp [r12], byte 0
jne .LOOP_START_22
.LOOP_END_22:
add r12, byte 1
sub [r12], byte 1
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, r12
mov rdx, 1
syscall
sub [r12], byte 3
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, r12
mov rdx, 1
syscall
add r12, byte 1
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, r12
mov rdx, 1
syscall
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, r12
mov rdx, 1
syscall
add r12, byte 1
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, r12
mov rdx, 1
syscall
sub r12, byte 4
sub [r12], byte 1
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, r12
mov rdx, 1
syscall
sub r12, byte 1
add [r12], byte 1
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, r12
mov rdx, 1
syscall
add r12, byte 5
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, r12
mov rdx, 1
syscall
add r12, byte 1
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, r12
mov rdx, 1
syscall
sub r12, byte 2
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, r12
mov rdx, 1
syscall
sub r12, byte 1
sub [r12], byte 1
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, r12
mov rdx, 1
syscall
mov rax, SYS_EXIT
mov rdi, SUCCESS
syscall
2 changes: 2 additions & 0 deletions input/hello_world.b
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
prints "hello world"
+[-[<<[+[--->]-[<<<]]]>>>-]>-.---.>..>.<<<<-.<+.>>>>>.>.<<.<-.
1 change: 1 addition & 0 deletions input/out.b
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
++[>++[>++[>+++++<-]<-]<-]>>>++.
Loading

0 comments on commit ef4f8f5

Please sign in to comment.