Skip to content

Commit 6e6c795

Browse files
author
Kirill Saksin
committed
Initial commit (and most likely last one)
0 parents  commit 6e6c795

File tree

16 files changed

+541
-0
lines changed

16 files changed

+541
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.stack-work
2+
.history

LICENSE

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Copyright Author name here (c) 2017
2+
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above
12+
copyright notice, this list of conditions and the following
13+
disclaimer in the documentation and/or other materials provided
14+
with the distribution.
15+
16+
* Neither the name of Author name here nor the names of other
17+
contributors may be used to endorse or promote products derived
18+
from this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# calc
2+
3+
Experiments in Haskell: command line calculator.
4+
5+
## Usage
6+
7+
```bash
8+
$ stack build
9+
$ stack exec calc-exe
10+
```
11+
12+
### As library
13+
14+
```haskell
15+
import Term
16+
17+
computed = eval $ Sum (ValueF 10) (Pi)
18+
```
19+
20+
21+
```haskell
22+
import Term
23+
import Text.Parsec
24+
25+
parseCalculation text = fmap toExpr $ parse termsP "my calc" text
26+
execParsed parsed = fmap eval parsed
27+
```
28+

Setup.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

app/Main.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Main where
2+
3+
import CalcRepl
4+
5+
import Control.Monad
6+
import System.IO
7+
import System.Console.Repline
8+
import Control.Monad.Trans
9+
10+
main :: IO ()
11+
main = evalRepl "> " (\x -> liftIO $ replBody x) [] (Word $ const $ return []) (return ())

calc.cabal

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: calc
2+
version: 0.1.0.0
3+
-- synopsis:
4+
-- description:
5+
homepage: https://github.com/saksmt/experimental-hs-calc#readme
6+
license: BSD3
7+
license-file: LICENSE
8+
author: Kirill Saksin
9+
maintainer: [email protected]
10+
copyright: 2017 Kirill Saksin
11+
category: Console
12+
build-type: Simple
13+
extra-source-files: README.md
14+
cabal-version: >=1.10
15+
16+
library
17+
hs-source-dirs: src
18+
exposed-modules: Term, Expr, CalcRepl
19+
other-modules: Term.Def, Term.Parser, Term.ToExpr, Util.Predicate, Util.List
20+
build-depends: base >= 4.7 && < 5
21+
, parsec
22+
, parsec-numbers
23+
, derive
24+
default-language: Haskell2010
25+
26+
executable calc-exe
27+
hs-source-dirs: app
28+
main-is: Main.hs
29+
ghc-options: -threaded -rtsopts -with-rtsopts=-N
30+
build-depends: base
31+
, calc
32+
, repline
33+
, mtl
34+
default-language: Haskell2010
35+
36+
test-suite calc-test
37+
type: exitcode-stdio-1.0
38+
hs-source-dirs: test
39+
main-is: Spec.hs
40+
build-depends: base
41+
, calc
42+
ghc-options: -threaded -rtsopts -with-rtsopts=-N
43+
default-language: Haskell2010
44+
45+
source-repository head
46+
type: git
47+
location: https://github.com/saksmt/experimental-hs-calc

src/CalcRepl.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module CalcRepl where
2+
3+
import Term
4+
import Expr
5+
import Text.Parsec
6+
7+
import Control.Monad
8+
import System.IO
9+
10+
replBody raw = either ((hPutStrLn stderr) . show) print $ fmap (eval . toExpr) $ parse termsP "(interactive)" raw
11+

src/Expr.hs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{-# LANGUAGE TemplateHaskell #-}
2+
3+
module Expr(Expr(..), eval) where
4+
5+
import Data.List(findIndex)
6+
import Data.Fixed(mod')
7+
8+
data Expr = ValueF Double
9+
| Sum Expr Expr
10+
| Diff Expr Expr
11+
| Pow Expr Expr
12+
| Mul Expr Expr
13+
| Div Expr Expr
14+
| Neg Expr
15+
| Mod Expr Expr
16+
| Round Expr
17+
| Floor Expr
18+
| Abs Expr
19+
| Sin Expr
20+
| Cos Expr
21+
| Tan Expr
22+
| ASin Expr
23+
| ACos Expr
24+
| ATan Expr
25+
| Pi
26+
| Sqrt Expr
27+
| Log Expr Expr
28+
| Exp Expr
29+
| E
30+
| Ceil Expr deriving (Show)
31+
32+
eval :: Expr -> Double
33+
eval (ValueF v) = v
34+
eval (Sum a b) = eval a + eval b
35+
eval (Diff a b) = eval a - eval b
36+
eval (Pow a b) = eval a ** eval b
37+
eval (Mul a b) = eval a * eval b
38+
eval (Neg v) = - eval v
39+
eval (Mod a b) = eval a `mod'` eval b
40+
eval (Round v) = fromIntegral $ round $ eval v
41+
eval (Floor v) = fromIntegral $ floor $ eval v
42+
eval (Ceil v) = fromIntegral $ ceiling $ eval v
43+
eval (Div a b) = eval a / eval b
44+
eval (Abs v) = abs $ eval v
45+
eval (Sin v) = sin $ eval v
46+
eval (Cos v) = cos $ eval v
47+
eval (Tan v) = tan $ eval v
48+
eval (ASin v) = asin $ eval v
49+
eval (ACos v) = acos $ eval v
50+
eval (ATan v) = atan $ eval v
51+
eval (Pi) = pi
52+
eval (Sqrt v) = sqrt $ eval v
53+
eval (Log b e) = eval b `logBase` eval e
54+
eval (Exp v) = exp $ eval v
55+
eval (E) = exp 1
56+

src/Term.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Term
2+
( termsP
3+
, toExpr
4+
, Term
5+
) where
6+
7+
import Term.Parser(termsP)
8+
import Term.Def(Term)
9+
import Term.ToExpr(toExpr)
10+

src/Term/Def.hs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{-# LANGUAGE TemplateHaskell #-}
2+
3+
module Term.Def
4+
( Term(..)
5+
, isSumT
6+
, isDiffT
7+
, isPowT
8+
, isMulT
9+
, isDivT
10+
, isModT
11+
, isNegT
12+
, isPrioT
13+
, isRoundT
14+
, isFloorT
15+
, isCeilT
16+
, isAbsT
17+
, isSinT
18+
, isCosT
19+
, isTanT
20+
, isASinT
21+
, isACosT
22+
, isATanT
23+
, isPiT
24+
, isSqrtT
25+
, isLogT
26+
, isExpT
27+
, isET
28+
) where
29+
30+
import Data.DeriveTH
31+
import Data.Derive.Is
32+
33+
data Term = ValueT Double
34+
| SumT
35+
| DiffT
36+
| PowT
37+
| MulT
38+
| DivT
39+
| ModT
40+
| NegT Term
41+
| PrioT [Term]
42+
| RoundT [Term]
43+
| FloorT [Term]
44+
| AbsT [Term]
45+
| SinT [Term]
46+
| CosT [Term]
47+
| TanT [Term]
48+
| ASinT [Term]
49+
| ACosT [Term]
50+
| ATanT [Term]
51+
| PiT
52+
| SqrtT [Term]
53+
| LogT [Term] [Term]
54+
| ExpT [Term]
55+
| ET
56+
| CeilT [Term] deriving (Show)
57+
58+
$( derive makeIs ''Term )
59+

0 commit comments

Comments
 (0)