Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/bower_components/
/node_modules/
/.pulp-cache/
/output/
/.psci*
/src/.webpack.js
node_modules/
.stack-work/
*.hs.swo
npm-debug.log
2 changes: 2 additions & 0 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
13 changes: 13 additions & 0 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -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)
46 changes: 46 additions & 0 deletions edioma.cabal
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions src/Edioma.hs
Original file line number Diff line number Diff line change
@@ -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"
33 changes: 33 additions & 0 deletions src/Lib.hs
Original file line number Diff line number Diff line change
@@ -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)
66 changes: 66 additions & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions test/EdiomaSpec.hs
Original file line number Diff line number Diff line change
@@ -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)
]
1 change: 1 addition & 0 deletions test/Spec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}