diff --git a/.gitignore b/.gitignore index 4d159c2..a7c8b0d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,4 @@ -/bower_components/ -/node_modules/ -/.pulp-cache/ -/output/ -/.psci* -/src/.webpack.js +node_modules/ +.stack-work/ +*.hs.swo +npm-debug.log diff --git a/Setup.hs b/Setup.hs new file mode 100644 index 0000000..9a994af --- /dev/null +++ b/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/app/Main.hs b/app/Main.hs new file mode 100644 index 0000000..62e6fac --- /dev/null +++ b/app/Main.hs @@ -0,0 +1,13 @@ +module Main where + +import System.Environment + +import Lib (parse) + +readExpr :: String -> String +readExpr input = parse input + +main :: IO () +main = do + (expr:_) <- getArgs + putStrLn (readExpr expr) diff --git a/edioma.cabal b/edioma.cabal new file mode 100644 index 0000000..b4bc0eb --- /dev/null +++ b/edioma.cabal @@ -0,0 +1,46 @@ +name: edioma +version: 0.1.0.0 +synopsis: Initial project template from stack +description: Please see README.md +homepage: https://github.com/ProjetoDora/edioma#readme +license: BSD3 +license-file: LICENSE +author: Author name here +maintainer: example@example.com +copyright: 2016 Author name here +category: Web +build-type: Simple +-- extra-source-files: +cabal-version: >=1.10 + +library + hs-source-dirs: src + exposed-modules: Lib + , Edioma + build-depends: base >= 4.7 && < 5 + , parsec + default-language: Haskell2010 + +executable edioma-exe + hs-source-dirs: app + main-is: Main.hs + ghc-options: -threaded -rtsopts -with-rtsopts=-N + build-depends: base + , edioma + default-language: Haskell2010 + +test-suite edioma-test + type: exitcode-stdio-1.0 + hs-source-dirs: test + main-is: Spec.hs + build-depends: base + , edioma + , text + , hspec > 2 && < 3 + , QuickCheck >= 2.8 && < 2.9 + ghc-options: -threaded -rtsopts -with-rtsopts=-N + default-language: Haskell2010 + +source-repository head + type: git + location: https://github.com/ProjetoDora/edioma diff --git a/src/Edioma.hs b/src/Edioma.hs new file mode 100644 index 0000000..0fe8a3e --- /dev/null +++ b/src/Edioma.hs @@ -0,0 +1,20 @@ +module Edioma (Expr(..), eval) where + +data Expr + = Soma Expr Expr + | Mult Expr Expr + | Menos Expr Expr + | Lit Int + deriving (Show, Read, Eq) + +eval :: Expr -> Expr +eval (Lit a) = Lit a +eval (Soma a b) = case (eval a, eval b) of + (Lit a', Lit b') -> Lit (a' + b') + _ -> error "Cannot happen" +eval (Mult a b) = case (eval a, eval b) of + (Lit a', Lit b') -> Lit (a' * b') + _ -> error "Cannot happen" +eval (Menos a b) = case (eval a, eval b) of + (Lit a', Lit b') -> Lit (a' - b') + _ -> error "Cannot happen" diff --git a/src/Lib.hs b/src/Lib.hs new file mode 100644 index 0000000..84b4d95 --- /dev/null +++ b/src/Lib.hs @@ -0,0 +1,33 @@ +module Lib (Lib.parse) where + +import Edioma (Expr(..), eval) +import Text.ParserCombinators.Parsec +import Data.Char (digitToInt) + +{- parse :: String -> Either -} +parse s = case Text.ParserCombinators.Parsec.parse parseExpr "expression" s of + Left err -> "No match: " ++ show err + Right x -> show (eval x) + +parseExpr :: Parser Expr +parseExpr = parseOp + <|> parseLit + +operators = oneOf "+-*" + +parseOp :: Parser Expr +parseOp = do + char '(' + x <- parseExpr + op <- operators + y <- parseExpr + char ')' + return $ case op of + '+' -> Soma x y + '-' -> Menos x y + '*' -> Mult x y + +parseLit :: Parser Expr +parseLit = do + x <- digit + return $ Lit (digitToInt x) diff --git a/stack.yaml b/stack.yaml new file mode 100644 index 0000000..fd93e18 --- /dev/null +++ b/stack.yaml @@ -0,0 +1,66 @@ +# This file was automatically generated by 'stack init' +# +# Some commonly used options have been documented as comments in this file. +# For advanced use and comprehensive documentation of the format, please see: +# http://docs.haskellstack.org/en/stable/yaml_configuration/ + +# Resolver to choose a 'specific' stackage snapshot or a compiler version. +# A snapshot resolver dictates the compiler version and the set of packages +# to be used for project dependencies. For example: +# +# resolver: lts-3.5 +# resolver: nightly-2015-09-21 +# resolver: ghc-7.10.2 +# resolver: ghcjs-0.1.0_ghc-7.10.2 +# resolver: +# name: custom-snapshot +# location: "./custom-snapshot.yaml" +resolver: lts-6.13 + +# User packages to be built. +# Various formats can be used as shown in the example below. +# +# packages: +# - some-directory +# - https://example.com/foo/bar/baz-0.0.2.tar.gz +# - location: +# git: https://github.com/commercialhaskell/stack.git +# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a +# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a +# extra-dep: true +# subdirs: +# - auto-update +# - wai +# +# A package marked 'extra-dep: true' will only be built if demanded by a +# non-dependency (i.e. a user package), and its test suites and benchmarks +# will not be run. This is useful for tweaking upstream packages. +packages: +- '.' +# Dependency packages to be pulled from upstream that are not in the resolver +# (e.g., acme-missiles-0.3) +extra-deps: [] + +# Override default flag values for local packages and extra-deps +flags: {} + +# Extra package databases containing global packages +extra-package-dbs: [] + +# Control whether we use the GHC we find on the path +# system-ghc: true +# +# Require a specific version of stack, using version ranges +# require-stack-version: -any # Default +# require-stack-version: ">=1.1" +# +# Override the architecture used by stack, especially useful on Windows +# arch: i386 +# arch: x86_64 +# +# Extra directories used by stack for building +# extra-include-dirs: [/path/to/dir] +# extra-lib-dirs: [/path/to/dir] +# +# Allow a newer minor version of GHC than the snapshot specifies +# compiler-check: newer-minor \ No newline at end of file diff --git a/test/EdiomaSpec.hs b/test/EdiomaSpec.hs new file mode 100644 index 0000000..ce7ec4c --- /dev/null +++ b/test/EdiomaSpec.hs @@ -0,0 +1,31 @@ +module EdiomaSpec where + +import Test.Hspec +import Test.QuickCheck + +import Edioma + +spec :: Spec +spec = describe "eval" $ do + let isLit (Lit _) = True + isLit _ = False + + it "does not change literals" $ property $ \x + -> isLit x ==> eval x `shouldBe` x + + it "evaluates adition for literals" $ property $ \x y + -> eval (Soma (Lit x) (Lit y)) `shouldBe` Lit (x + y) + + it "evaluates subtraction for literals" $ property $ \x y + -> eval (Menos (Lit x) (Lit y)) `shouldBe` Lit (x - y) + + it "evaluates multiplication for literals" $ property $ \x y + -> eval (Mult (Lit x) (Lit y)) `shouldBe` Lit (x * y) + +instance Arbitrary Expr where + arbitrary = frequency + [ (1, Soma <$> arbitrary <*> arbitrary) + , (1, Mult <$> arbitrary <*> arbitrary) + , (1, Menos <$> arbitrary <*> arbitrary) + , (10, Lit <$> arbitrary) + ] diff --git a/test/Spec.hs b/test/Spec.hs new file mode 100644 index 0000000..a824f8c --- /dev/null +++ b/test/Spec.hs @@ -0,0 +1 @@ +{-# OPTIONS_GHC -F -pgmF hspec-discover #-}