1 {-# LANGUAGE TemplateHaskell #-}
2 {-# LANGUAGE TypeApplications #-}
3 {-# LANGUAGE TypeFamilies #-}
4 {-# OPTIONS_GHC -Wno-missing-signatures #-}
5 -- for Symantic.Parser's TemplateHaskell
6 {-# LANGUAGE ScopedTypeVariables #-}
7 {-# LANGUAGE RankNTypes #-}
8 {-# LANGUAGE UnboxedTuples #-}
9 {-# OPTIONS_GHC -Wno-unused-matches #-}
10 {-# OPTIONS_GHC -Wno-unused-local-binds #-}
11 module Brainfuck where
13 --import qualified Data.Text.Lazy as TL
14 import Control.Monad ((=<<))
15 import Criterion.Main (Benchmark, bench, bgroup, env, nf)
16 import Data.Function (($))
17 import Data.Semigroup (Semigroup(..))
18 import Data.String (String)
19 import Data.Text (Text)
20 import qualified Data.Attoparsec.ByteString as AP.ByteString
21 import qualified Data.Attoparsec.Text as AP.Text
22 import qualified Data.ByteString as BS
23 import qualified Data.ByteString.Lazy as BSL
24 import qualified Data.List as List
25 import qualified Data.Text.IO as Text
26 import qualified System.IO as IO
28 import qualified Symantic.Parser as SP
29 import qualified Parsers.Brainfuck.Attoparsec as AP.Brainfuck
30 import qualified Parsers.Brainfuck.Handrolled as HR.Brainfuck
31 import qualified Parsers.Brainfuck.SymanticParser as SP.Brainfuck
32 import Paths_symantic_parser
34 inputPath inputName = getDataFileName ("parsers/Parsers/Brainfuck/inputs/"<>inputName<>".bf")
35 benchBrainfuck inputName =
37 [ env (Text.readFile =<< inputPath inputName) $ \inp ->
39 [ bench "SymanticParser" $
40 nf $$(SP.runParser @Text SP.Brainfuck.grammar) inp
41 , bench "Attoparsec" $
42 nf (AP.Text.parse AP.Brainfuck.parser) inp
43 , bench "Handrolled" $
44 nf HR.Brainfuck.parser inp
48 [ env (IO.readFile =<< inputPath inputName) $ \inp ->
50 [ bench "SymanticParser" $
51 nf $$(SP.runParser @String SP.Brainfuck.grammar) inp
55 [ env (BS.readFile =<< inputPath inputName) $ \inp ->
57 [ bench "SymanticParser" $
58 nf $$(SP.runParser @BS.ByteString SP.Brainfuck.grammar) inp
59 , bench "Attoparsec" $
60 nf (AP.ByteString.parse AP.Brainfuck.parser) inp
61 , bench "Handrolled" $
62 nf HR.Brainfuck.parser inp
65 , bgroup "ByteStringLazy"
66 [ env (BSL.readFile =<< inputPath inputName) $ \inp ->
68 [ bench "SymanticParser" $
69 nf $$(SP.runParser @BSL.ByteString SP.Brainfuck.grammar) inp
74 benchmark :: Benchmark
75 benchmark = bgroup "Brainfuck" $ List.concat
76 [ benchBrainfuck "helloworld"
77 , benchBrainfuck "compiler"
78 , benchBrainfuck "hanoi"