1 {-# LANGUAGE NoMonomorphismRestriction #-}
2 {-# LANGUAGE TemplateHaskell #-}
3 module Golden.Grammar where
7 import Data.String (String)
8 import Prelude (undefined)
9 import Text.Show (Show)
10 import qualified Prelude
11 import qualified Language.Haskell.TH as TH
13 import Symantic.Parser
14 import qualified Symantic.Parser.Staging as H
16 data Expr = Var String | Num Int | Add Expr Expr deriving Show
17 data Asgn = Asgn String Expr deriving Show
19 data BrainFuckOp = RightPointer | LeftPointer | Increment | Decrement | Output | Input | Loop [BrainFuckOp] deriving (Show, Eq)
22 cinput = m --try (string "aaa") <|> string "db" --(string "aab" <|> string "aac") --(char 'a' <|> char 'b') *> string "ab"
24 --m = match "ab" (lookAhead item) op empty
25 --op 'a' = item $> haskell "aaaaa"
26 --op 'b' = item $> haskell "bbbbb"
28 -- match :: Eq a => [Pure repr a] -> repr a -> (Pure repr a -> repr b) -> repr b -> repr b
29 bf = match [char '>'] item op empty
30 op (H.ValueCode '>' _) = string ">"
33 --defuncTest = haskell Just <$> (haskell (+) <$> (item $> haskell 1) <*> (item $> haskell 8))
35 -- manyTest = many (string "ab" $> (haskell 'c'))
37 --nfb = negLook (char 'a') <|> void (string "ab")
39 --skipManyInspect = skipMany (char 'a')
41 boom :: Applicable repr => repr ()
43 let foo = (-- newRegister_ unit (\r0 ->
44 let goo = (-- newRegister_ unit (\r1 ->
45 let hoo = {-get r0 <~> get r1 *>-} goo *> hoo in hoo
47 in goo) *> pure H.unit
50 haskell :: a -> TH.CodeQ a -> H.Haskell a
51 haskell e c = H.Haskell (H.ValueCode (H.Value e) c)
53 brainfuck :: Grammar repr => repr [BrainFuckOp]
54 brainfuck = whitespace *> bf
56 whitespace = skipMany (noneOf "<>+-[],.$")
57 lexeme p = p <* whitespace
58 -- match :: Eq a => [Pure repr a] -> repr a -> (Pure repr a -> repr b) -> repr b -> repr b
59 bf = many (lexeme (match ((\c -> haskell c [||c||]) Prelude.<$> "><+-.,[") (look item) op empty))
60 -- op :: Pure repr Char -> repr BrainFuckOp
61 op (H.Haskell (H.ValueCode (H.Value c) _)) = case c of
62 '>' -> item $> haskell RightPointer [||RightPointer||]
63 '<' -> item $> haskell LeftPointer [||LeftPointer||]
64 '+' -> item $> haskell Increment [||Increment||]
65 '-' -> item $> haskell Decrement [||Decrement||]
66 '.' -> item $> haskell Output [||Output||]
67 ',' -> item $> haskell Input [||Input||]
68 '[' -> between (lexeme item) (char ']') (haskell Loop [||Loop||] <$> bf)