ubcf/app/Main.hs

36 lines
1.3 KiB
Haskell
Raw Permalink Normal View History

2024-09-26 23:58:27 +02:00
-- ubcf - compiling brainfuck programs into ubc bytecode
-- Copyright (C) 2024 VegOwOtenks
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
--
-- contact me via vegowotenks at jossco dot de
module Main (main) where
import Lib
import Control.Monad (mapM_)
import Data.Binary (Word32, Word8, encode)
import Data.Bits
import qualified Data.ByteString.Lazy as BL
convertWord32 :: Word32 -> [Word8]
convertWord32 w = [fromIntegral (w), fromIntegral (w .>>. 8), fromIntegral (w .>>. 16), fromIntegral (w .>>. 24)]
main :: IO ()
main = do
s <- getContents
let tokens = tokenize s
case parseTokens tokens of
Left err -> putStr err
Right ops -> mapM_ (BL.putStr . encode) . (concatMap (convertWord32)) $ (compile ops)