1 {-# LANGUAGE NoMonomorphismRestriction #-}
2 {-# LANGUAGE TemplateHaskell #-}
3 module Golden.Grammar where
5 import Control.Monad (Monad(..))
6 import Data.Either (Either(..))
8 import Data.Function (($), (.))
10 import Data.Semigroup (Semigroup(..))
11 import Data.String (String)
12 import Data.String (String, IsString(..))
13 import Language.Haskell.TH (TExpQ)
14 import Prelude (undefined)
15 import System.IO (IO, FilePath)
17 import Test.Tasty.Golden
18 import Text.Show (Show)
19 import qualified Data.ByteString.Lazy as BSL
20 import qualified Data.Text.Lazy as TL
21 import qualified Data.Text.Lazy.Encoding as TL
22 import qualified Prelude
24 import Symantic.Parser
25 import qualified Symantic.Parser.Staging as Hask
27 data Expr = Var String | Num Int | Add Expr Expr deriving Show
28 data Asgn = Asgn String Expr deriving Show
30 data BrainFuckOp = RightPointer | LeftPointer | Increment | Decrement | Output | Input | Loop [BrainFuckOp] deriving (Show, Eq)
33 cinput = m --try (string "aaa") <|> string "db" --(string "aab" <|> string "aac") --(char 'a' <|> char 'b') *> string "ab"
35 --m = match "ab" (lookAhead item) op empty
36 --op 'a' = item $> haskell "aaaaa"
37 --op 'b' = item $> haskell "bbbbb"
39 -- match :: Eq a => [Pure repr a] -> repr a -> (Pure repr a -> repr b) -> repr b -> repr b
40 bf = match [char '>'] item op empty
41 op (Hask.ValueCode '>' _) = string ">"
44 --defuncTest = haskell Just <$> (haskell (+) <$> (item $> haskell 1) <*> (item $> haskell 8))
46 -- manyTest = many (string "ab" $> (haskell 'c'))
48 --nfb = negLook (char 'a') <|> void (string "ab")
50 --skipManyInspect = skipMany (char 'a')
52 boom :: Applicable repr => repr ()
54 let foo = (-- newRegister_ unit (\r0 ->
55 let goo = (-- newRegister_ unit (\r1 ->
56 let hoo = {-get r0 <~> get r1 *>-} goo *> hoo in hoo
58 in goo) *> pure Hask.unit
61 haskell :: a -> TExpQ a -> Hask.Haskell a
62 haskell e c = Hask.Haskell (Hask.ValueCode (Hask.Value e) (Hask.Code c))
64 brainfuck :: Grammar repr => repr [BrainFuckOp]
65 brainfuck = whitespace *> bf
67 whitespace = skipMany (noneOf "<>+-[],.$")
68 lexeme p = p <* whitespace
69 -- match :: Eq a => [Pure repr a] -> repr a -> (Pure repr a -> repr b) -> repr b -> repr b
70 bf = many (lexeme (match ((\c -> haskell c [||c||]) Prelude.<$> "><+-.,[") (look item) op empty))
71 -- op :: Pure repr Char -> repr BrainFuckOp
72 op (Hask.Haskell (Hask.ValueCode (Hask.Value c) _)) = case c of
73 '>' -> item $> haskell RightPointer [||RightPointer||]
74 '<' -> item $> haskell LeftPointer [||LeftPointer||]
75 '+' -> item $> haskell Increment [||Increment||]
76 '-' -> item $> haskell Decrement [||Decrement||]
77 '.' -> item $> haskell Output [||Output||]
78 ',' -> item $> haskell Input [||Input||]
79 '[' -> between (lexeme item) (char ']') (haskell Loop [||Loop||] <$> bf)