packages feed

ParserFunction 0.0.3 → 0.0.4

raw patch · 18 files changed

+76/−69 lines, 18 filesdep ~basebinary-added

Dependency ranges changed: base

Files

+ .DS_Store view

binary file changed (absent → 6148 bytes)

+ ._.DS_Store view

binary file changed (absent → 82 bytes)

+ ._LICENSE view

binary file changed (absent → 167 bytes)

+ ._ParserFunction.cabal view

binary file changed (absent → 167 bytes)

+ ._Setup.hs view

binary file changed (absent → 197 bytes)

+ ._Text view

binary file changed (absent → 197 bytes)

LICENSE view
@@ -1,4 +1,4 @@-Copyright (c) 2008 Enzo Haussecker+Copyright (c) 2011 Enzo Haussecker  Permission is hereby granted, free of charge, to any person obtaining this work and associated documentation files (the "Work"), to deal in the Work without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Work, and to permit persons to whom the Work is furnished to do so, subject to the following conditions: 
ParserFunction.cabal view
@@ -1,18 +1,23 @@-Name:            ParserFunction-Version:         0.0.3-License:         BSD3-License-file:    LICENSE-Author:          Enzo Haussecker-Synopsis:        An algorithm for parsing Expressions.-Description:     The centerpiece of this package is a function called "expressionToDouble", which parses an expression (in the form of a string) and returns a Double. Examples of this function can be found by viewing the source code for this module.-Build-Depends:   base, parsec, containers-stability:       Stable-Category:        Parsing-build-type:      Simple-cabal-version:   >= 1.2-tested-with:     GHC ==6.8.2--Exposed-modules: -	Text.ParserCombinators.Parsec.ParserFunction+name:          ParserFunction+version:       0.0.4+cabal-version: >= 1.6+license:       BSD3+license-file:  LICENSE+author:        Enzo Haussecker+maintainer:    ehaussecker@gmail.com+category:      Parsing+synopsis:      Utilities for parsing and evaluating string-expressions.+description:+    ParserFunction provides utilities for parsing and evaluating string-expressions.+    The centerpiece of this module is a function called @evalStrExpr@,+    which evaluates a string-expression using a variable lookup map.+    Example: @evalStrExpr@ \"exp(x)\" [(\'x\',1)] gives 2.718281828459045.+    More examples can be found by viewing the source code for this module.+    This module also exports all supporting functionality. +build-type: Simple +Library+  exposed-modules:+    Text.ParserCombinators.Parsec.ParserFunction+  build-depends: base < 6, parsec, containers
+ Text/.DS_Store view

binary file changed (absent → 6148 bytes)

+ Text/._.DS_Store view

binary file changed (absent → 82 bytes)

+ Text/._ParserCombinators view

binary file changed (absent → 197 bytes)

+ Text/ParserCombinators/.DS_Store view

binary file changed (absent → 6148 bytes)

+ Text/ParserCombinators/._.DS_Store view

binary file changed (absent → 82 bytes)

+ Text/ParserCombinators/._Parsec view

binary file changed (absent → 197 bytes)

+ Text/ParserCombinators/Parsec/.DS_Store view

binary file changed (absent → 6148 bytes)

+ Text/ParserCombinators/Parsec/._.DS_Store view

binary file changed (absent → 82 bytes)

+ Text/ParserCombinators/Parsec/._ParserFunction.hs view

binary file changed (absent → 171 bytes)

Text/ParserCombinators/Parsec/ParserFunction.hs view
@@ -1,25 +1,20 @@ ---   ParserFunction ---   by Enzo Haussecker ----   The centerpiece of this module is a function called: expressionToDouble, ----   which parses an expression (in the form of a string) and returns a Double.-----   You must declare all variables in the expression! +---   The centerpiece of this module is a function called: evalStrExpr, +---   which evaluates a string-expression using a variable lookup map. ----   Examples are as fallows.+---   Examples of evalStrExpr are as fallows. ----   > expressionToDouble "5 - 2" []+---   > evalStrExpr "5 - 2" [] ---   3.0------   > expressionToDouble "x^2 + y" [('x',2),('y',3)]+---   > evalStrExpr "x^2 + y" [('x',2),('y',3)] ---   7.0-----   > expressionToDouble "cos(x)" [('x',pi)]+---   > evalStrExpr "cos(x)" [('x',pi)] ---   -1.0  module Text.ParserCombinators.Parsec.ParserFunction-    (expressionToDouble) where+    (Expr,evalStrExpr,strToExpr,buildExpr,expressionTable,factor,variables,number,evaluate) where  import Text.ParserCombinators.Parsec.Expr  import Text.ParserCombinators.Parsec@@ -28,6 +23,7 @@ import Data.List (isInfixOf) import Data.Char (toLower) +-- The Expr data type provides a basis for ordering mathematical operations. data Expr = Num Double    | Var Char      | Sub Expr Expr           | Div Expr Expr | Pow Expr Expr | Log Expr           | Abs Expr      | Sqrt Expr     | Cbrt Expr@@ -41,14 +37,18 @@           | Sec Expr      | Csc Expr      | Cot Expr           | Mul Expr Expr | Add Expr Expr | Exp Expr deriving (Show, Eq, Ord) -expressionToDouble :: String -> [(Char,Double)] -> Double-expressionToDouble s m = eval (M.fromAscList $ caseMap m) (fromMaybe failing $ stringToExpr s)+--- evalStrExpr evaluates a string-expression s using the variable lookup map m. +evalStrExpr :: String -> [(Char,Double)] -> Double+evalStrExpr s m = evaluate (M.fromAscList $ caseMap m) (fromMaybe failing $ strToExpr s)     where          caseMap x = fmap (\ (a, b) -> ([toLower a], b)) x         failing   = error "Parser error in expression" -stringToExpr :: String -> Maybe Expr-stringToExpr xs = if any (==True) (symbols failingSymbols xs)+--- strToExpr parses a string-expression and returns an Expr expression, +--- or nothing if the string is malformed. A failing example is "2+-2". +--- The correct format should be "2+(-2)".+strToExpr :: String -> Maybe Expr+strToExpr xs = if any (==True) (symbols failingSymbols xs)                   then Nothing                   else either (const Nothing) (Just) (parse buildExpr "" handleString)     where@@ -59,6 +59,7 @@                           "/^","/*","//","/+","/-","+^","+*","+/","++","+-",                           "-^","-*","-/","-+","--"] +--- buildExpr is the fundamental parsing function. buildExpr :: Parser Expr buildExpr = buildExpressionParser expressionTable factor @@ -106,44 +107,45 @@             fe = toInteger . fromEnum             ch2num = (subtract $ fe '0') . fe -eval :: M.Map String Double -> Expr -> Double-eval m expr =+--- evaluate takes a map and expression to produce a numerical value.+evaluate :: M.Map String Double -> Expr -> Double+evaluate m expr =     case expr of          (Num d)           -> d         (Var c)         -> fromMaybe (failing c) (M.lookup [c] m)-        (Add expr1 expr2) -> (eval m expr1) +  (eval m expr2)-        (Sub expr1 expr2) -> (eval m expr1) -  (eval m expr2)-        (Mul expr1 expr2) -> (eval m expr1) *  (eval m expr2)-        (Div expr1 expr2) -> (eval m expr1) /  (eval m expr2)-        (Pow expr1 expr2) -> (eval m expr1) ** (eval m expr2)-        (Exp expr1)       -> exp (eval m expr1)-        (Sqrt expr1)      -> (eval m expr1) ** (0.5)-        (Cbrt expr1)      -> (eval m expr1) ** (1/3)-        (Log expr1)       -> log (eval m expr1)-        (Abs expr1)       -> abs (eval m expr1)-        (Sin expr1)       -> sin (eval m expr1) -        (Cos expr1)       -> cos (eval m expr1)-        (Tan expr1)       -> tan (eval m expr1)-        (Sec expr1)       -> 1/sin (eval m expr1) -        (Csc expr1)       -> 1/cos (eval m expr1)-        (Cot expr1)       -> 1/tan (eval m expr1)-        (Sinh expr1)      -> sinh (eval m expr1) -        (Cosh expr1)      -> cosh (eval m expr1)-        (Tanh expr1)      -> tanh (eval m expr1)-        (Sech expr1)      -> 1/sinh (eval m expr1) -        (Csch expr1)      -> 1/cosh (eval m expr1)-        (Coth expr1)      -> 1/tanh (eval m expr1)-        (ArcSin expr1)    -> asin (eval m expr1) -        (ArcCos expr1)    -> acos (eval m expr1)-        (ArcTan expr1)    -> atan (eval m expr1)-        (ArcSec expr1)    -> 1/asin (eval m expr1) -        (ArcCsc expr1)    -> 1/acos (eval m expr1)-        (ArcCot expr1)    -> 1/atan (eval m expr1)-        (ArcSinh expr1)   -> asinh (eval m expr1) -        (ArcCosh expr1)   -> acosh (eval m expr1)-        (ArcTanh expr1)   -> atanh (eval m expr1)-        (ArcSech expr1)   -> 1/asinh (eval m expr1) -        (ArcCsch expr1)   -> 1/acosh (eval m expr1)-        (ArcCoth expr1)   -> 1/atanh (eval m expr1)+        (Add expr1 expr2) -> (evaluate m expr1) +  (evaluate m expr2)+        (Sub expr1 expr2) -> (evaluate m expr1) -  (evaluate m expr2)+        (Mul expr1 expr2) -> (evaluate m expr1) *  (evaluate m expr2)+        (Div expr1 expr2) -> (evaluate m expr1) /  (evaluate m expr2)+        (Pow expr1 expr2) -> (evaluate m expr1) ** (evaluate m expr2)+        (Exp expr1)       -> exp (evaluate m expr1)+        (Sqrt expr1)      -> (evaluate m expr1) ** (0.5)+        (Cbrt expr1)      -> (evaluate m expr1) ** (1/3)+        (Log expr1)       -> log (evaluate m expr1)+        (Abs expr1)       -> abs (evaluate m expr1)+        (Sin expr1)       -> sin (evaluate m expr1) +        (Cos expr1)       -> cos (evaluate m expr1)+        (Tan expr1)       -> tan (evaluate m expr1)+        (Sec expr1)       -> 1/sin (evaluate m expr1) +        (Csc expr1)       -> 1/cos (evaluate m expr1)+        (Cot expr1)       -> 1/tan (evaluate m expr1)+        (Sinh expr1)      -> sinh (evaluate m expr1) +        (Cosh expr1)      -> cosh (evaluate m expr1)+        (Tanh expr1)      -> tanh (evaluate m expr1)+        (Sech expr1)      -> 1/sinh (evaluate m expr1) +        (Csch expr1)      -> 1/cosh (evaluate m expr1)+        (Coth expr1)      -> 1/tanh (evaluate m expr1)+        (ArcSin expr1)    -> asin (evaluate m expr1) +        (ArcCos expr1)    -> acos (evaluate m expr1)+        (ArcTan expr1)    -> atan (evaluate m expr1)+        (ArcSec expr1)    -> 1/asin (evaluate m expr1) +        (ArcCsc expr1)    -> 1/acos (evaluate m expr1)+        (ArcCot expr1)    -> 1/atan (evaluate m expr1)+        (ArcSinh expr1)   -> asinh (evaluate m expr1) +        (ArcCosh expr1)   -> acosh (evaluate m expr1)+        (ArcTanh expr1)   -> atanh (evaluate m expr1)+        (ArcSech expr1)   -> 1/asinh (evaluate m expr1) +        (ArcCsch expr1)   -> 1/acosh (evaluate m expr1)+        (ArcCoth expr1)   -> 1/atanh (evaluate m expr1)     where         failing x = error $ "M.lookup error in value for variable `" ++ [x] ++ "'"