summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreliaslfox <>2017-09-13 09:54:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2017-09-13 09:54:00 (GMT)
commit312ab9aaef2ee1f6082825c71e403f1c17e06a21 (patch)
treeef54ab485637faef23fa188aa8cc3ec4da5110d1
parent0b92197186fd7595c7c6d404ba65cb53d4903452 (diff)
version 0.1.0.20.1.0.2
-rw-r--r--README.md80
-rw-r--r--language-elm.cabal4
-rw-r--r--src/Elm.hs80
3 files changed, 53 insertions, 111 deletions
diff --git a/README.md b/README.md
index cb1bebc..2bb3608 100644
--- a/README.md
+++ b/README.md
@@ -10,83 +10,3 @@ stack install language-elm
## Documentation
[Documentation](https://hackage.haskell.org/package/language-elm)
-<!--
-Import the libraries
-```haskell
-import Elm.Decleration
-import Elm.Expression
-import Elm.Import
-import Elm.Program
-import Elm.Type
-```
-
-Declare a program type
-```haskell
-program : Program
-program =
- Program
- "Tuple"
- (Select
- [ Item "first"
- , Item "second"
- , Item "mapFirst"
- , Item "mapSecond"
- ])
- []
- [ Dec
- "first"
- (TApp [TTuple2 (tvar "a1") (tvar "a2"), tvar "a1"])
- [Tuple2 (var "x") (Under)]
- (var "x")
- , Dec
- "second"
- (TApp [TTuple2 (tvar "a1") (tvar "a2"), tvar "a2"])
- [Tuple2 (Under) (var "y")]
- (var "y")
- , Dec
- "mapFirst"
- (TApp
- [ TApp [tvar "a", tvar "b"]
- , TTuple2 (tvar "a") (tvar "a2")
- , TTuple2 (tvar "b") (tvar "a2")
- ])
- [var "func", Tuple2 (var "x") (var "y")]
- (Tuple2 (App "func" [var "x"]) (var "y"))
- , Dec
- "mapSecond"
- (TApp
- [ TApp [tvar "a", tvar "b"]
- , TTuple2 (tvar "a1") (tvar "a")
- , TTuple2 (tvar "a1") (tvar "b")
- ])
- [var "func", Tuple2 (var "x") (var "y")]
- (Tuple2 (var "x") (App "func" [var "y"]))
- ]
-```
-
-Then render the program
-
-```haskell
-output :: String
-output = renderProgram program
-```
-
-Which results in the following output
-
-```elm
-module Tuple exposing (first, second, mapFirst, mapSecond)
-
-first :: ((a1, a2)) -> a1
-first (x, _) = x
-
-second :: ((a1, a2)) -> a2
-second (_, y) = y
-
-mapFirst :: (a -> b) -> ((a, a2)) -> ((b, a2))
-mapFirst func (x, y) = (func x, y)
-
-mapSecond :: (a -> b) -> ((a1, a)) -> ((a1, b))
-mapSecond func (x, y) = (x, func y)
-```
-
--->
diff --git a/language-elm.cabal b/language-elm.cabal
index 86f8f48..2a9c076 100644
--- a/language-elm.cabal
+++ b/language-elm.cabal
@@ -1,5 +1,5 @@
name: language-elm
-version: 0.1.0.1
+version: 0.1.0.2
synopsis: Generate elm code
description: Generate elm code from an ast
homepage: https://github.com/eliaslfox/language-elm#readme
@@ -31,7 +31,7 @@ test-suite language-elm-test
build-depends: base
, language-elm
, pretty >= 1.1.3 && < 1.2
- , hspec >= 2.4.4 && < 2.5
+ , hspec >= 2.4.3 && < 2.5
, mtl >= 2.2.1 && < 2.3
, protolude >= 0.2 && < 0.3
ghc-options: -threaded -rtsopts -with-rtsopts=-N
diff --git a/src/Elm.hs b/src/Elm.hs
index 19270e3..36c841a 100644
--- a/src/Elm.hs
+++ b/src/Elm.hs
@@ -2,7 +2,6 @@
{-# OPTIONS_GHC -Wall -Werror #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE Trustworthy #-}
-
module Elm
(
-- * Expressions
@@ -51,19 +50,25 @@ module Elm
-- * Generation
, renderModule
, render
+ , genStr
)
where
-import Prelude (error)
-import Protolude hiding (bool, list)
+-- Please note that doctest is very specefic about spacing
+-- so chaning spacing in the examples may break tests
+import Prelude (error)
+import Protolude hiding (bool, list)
+import Control.Monad.Writer
import Data.String
+import Elm.Classes
import qualified Elm.Decleration
import qualified Elm.Expression
-import Elm.GenError (GenError (WarningList))
+import Elm.GenError (GenError (WarningList))
import qualified Elm.Import
import qualified Elm.Program
-import qualified Elm.Type (TypeDec (..))
+import qualified Elm.Type (TypeDec (..))
+import qualified Text.PrettyPrint
type Expr = Elm.Expression.Expr
type Type = Elm.Type.TypeDec
@@ -99,7 +104,7 @@ var = Elm.Expression.Var
-- | Function application
--
--- >>> render (app [var "a", var "b", var "c"])
+-- >>> genStr (app [var "a", var "b", var "c"])
-- "a b c"
app :: [Expr] -> Expr
app = Elm.Expression.App
@@ -110,30 +115,36 @@ list = Elm.Expression.List
-- | Apply an operator to two sub expressions
--
--- >>> render (Op "+" (Int 5) (Int 6))
+-- >>> genStr (op "+" (int 5) (int 6))
-- "5 + 6"
op :: String -> Expr -> Expr -> Expr
op = Elm.Expression.Op
-- | A let...in block
--
--- >>> render (let_ (var "a") [(var "a", Int 5)])
--- "let
--- a = 5
--- in
--- a"
+-- >>> putStrLn $ genStr (let_ (var "a") [(var "a", int 5)])
+-- let
+-- a = 5
+-- in
+-- a
let_ :: Expr -> [(Expr, Expr)] -> Expr
let_ = Elm.Expression.Let
-- | A case...of block
--
--- >>> render (case_ (var "m") [(App [var "Just", var "x"], var "x"), (var "Nothing", var "default")]
--- "case m of
--- Just x ->
--- x
+-- >>> :{
+-- putStrLn $ genStr
+-- (case_ (var "m")
+-- [ (app [var "Just", var "x"], var "x")
+-- , (var "Nothing", var "default")
+-- ])
+-- :}
+-- case m of
+-- Just x ->
+-- x
-- <BLANKLINE>
--- Nothing ->
--- default"
+-- Nothing ->
+-- default
case_ :: Expr -> [(Expr, Expr)] -> Expr
case_ = Elm.Expression.Case
@@ -147,10 +158,10 @@ parens = Elm.Expression.Parens
-- | A type or type variable
--
--- >>> render (tvar "Nothing")
+-- >>> genStr (tvar "Nothing")
-- "Nothing"
--
--- >>> render (tvar "a")
+-- >>> genStr (tvar "a")
-- "a"
--
tvar :: String -> Type
@@ -158,7 +169,7 @@ tvar name = Elm.Type.Params name []
-- | A type with a single paramater
--
--- >>> render (tparam "Just" (tvar "a"))
+-- >>> genStr (tparam "Just" (tvar "a"))
-- "Just a"
--
tparam :: String -> Type -> Type
@@ -166,7 +177,7 @@ tparam name type_ = Elm.Type.Params name [type_]
-- | A type with multiple paramaters
--
--- >>> render (tparams "Result" [tvar "String", tvar "Int"])
+-- >>> genStr (tparams "Result" [tvar "String", tvar "Int"])
-- "Result String Int"
--
tparams :: String -> [Type] -> Type
@@ -174,7 +185,7 @@ tparams = Elm.Type.Params
-- | A zero item tuple type
--
--- >>> render tunit
+-- >>> genStr tunit
-- "()"
--
tunit :: Type
@@ -182,16 +193,16 @@ tunit = Elm.Type.TTuple []
-- | A multiple item tuple
--
--- >>> render (ttuple [tvar "a", tvar "b"])
--- "( a, b )"
+-- >>> genStr (ttuple [tvar "a", tvar "b"])
+-- "(a, b)"
--
ttuple :: [Type] -> Type
ttuple = Elm.Type.TTuple
-- | Type application
--
--- >>> render (tapp [tvar "a", tvar "b", tvar "c"])
--- "[a, b, c]"
+-- >>> genStr (tapp [tvar "a", tvar "b", tvar "c"])
+-- "a -> b -> c"
--
tapp :: [Type] -> Type
tapp = Elm.Type.TApp
@@ -199,7 +210,7 @@ tapp = Elm.Type.TApp
-- | A record type
--
--- >>> render (trecord [("a", tvar "Int"), ("b", tvar "String")]
+-- >>> genStr (trecord [("a", tvar "Int"), ("b", tvar "String")])
-- "{ a : Int, b : String }"
--
trecord :: [(String, Type)] -> Type
@@ -207,7 +218,7 @@ trecord = Elm.Type.TRecord Nothing
-- | A paramaterized record type
--
--- >>> render (trecord "a" [("b", tvar "Int")])
+-- >>> genStr (trecordParam "a" [("b", tvar "Int")])
-- "{ a | b : Int }"
--
trecordParam :: String -> [(String, Type)] -> Type
@@ -293,3 +304,14 @@ render module' =
str
else
error . show $ err
+
+
+genStr :: (Generate a) => a -> String
+genStr g =
+ let
+ (str, err) = runWriter . generate $ g
+ in
+ if err == WarningList [] then
+ Text.PrettyPrint.render str
+ else
+ error . show $ err