aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/tests/QuasiExpr.hs36
-rw-r--r--tests/tests/QuasiQuote.hs9
2 files changed, 45 insertions, 0 deletions
diff --git a/tests/tests/QuasiExpr.hs b/tests/tests/QuasiExpr.hs
new file mode 100644
index 00000000..6fc00a72
--- /dev/null
+++ b/tests/tests/QuasiExpr.hs
@@ -0,0 +1,36 @@
+{-# LANGUAGE TemplateHaskell #-}
+
+-- Used by QuasiQuote. Example taken from the GHC documentation.
+module QuasiExpr where
+
+import Language.Haskell.TH
+import Language.Haskell.TH.Quote
+import Data.Typeable
+import Data.Generics
+
+data Expr = IntExpr Integer
+ | AntiIntExpr String
+ | BinopExpr BinOp Expr Expr
+ | AntiExpr String
+ deriving(Show, Typeable, Data)
+
+data BinOp = AddOp
+ | SubOp
+ | MulOp
+ | DivOp
+ deriving(Show, Typeable, Data)
+
+eval :: Expr -> Integer
+eval (IntExpr n) = n
+eval (BinopExpr op x y) = (opToFun op) (eval x) (eval y)
+ where
+ opToFun AddOp = (+)
+ opToFun SubOp = (-)
+ opToFun MulOp = (*)
+ opToFun DivOp = div
+
+expr = QuasiQuoter parseExprExp undefined
+
+-- cheating...
+parseExprExp :: String -> Q Exp
+parseExprExp _ = [| BinopExpr AddOp (IntExpr 1) (IntExpr 2) |]
diff --git a/tests/tests/QuasiQuote.hs b/tests/tests/QuasiQuote.hs
new file mode 100644
index 00000000..a1aa7256
--- /dev/null
+++ b/tests/tests/QuasiQuote.hs
@@ -0,0 +1,9 @@
+{-# LANGUAGE TemplateHaskell, QuasiQuotes #-}
+
+-- example taken from the GHC documentation
+module QuasiQuote where
+
+import QuasiExpr
+
+run :: IO ()
+run = do { print $ eval [$expr|1 + 2|] }