ParserFunction 0.0.4 → 0.0.5
raw patch · 16 files changed
+29/−24 lines, 16 files
Files
- .DS_Store binary
- ._.DS_Store binary
- ._LICENSE binary
- ._ParserFunction.cabal binary
- ._Setup.hs binary
- ._Text binary
- ParserFunction.cabal +7/−8
- Text/.DS_Store binary
- Text/._.DS_Store binary
- Text/._ParserCombinators binary
- Text/ParserCombinators/.DS_Store binary
- Text/ParserCombinators/._.DS_Store binary
- Text/ParserCombinators/._Parsec binary
- Text/ParserCombinators/Parsec/._.DS_Store binary
- Text/ParserCombinators/Parsec/._ParserFunction.hs binary
- Text/ParserCombinators/Parsec/ParserFunction.hs +22/−16
.DS_Store view
binary file changed (6148 → 6148 bytes)
._.DS_Store view
binary file changed (82 → 197 bytes)
._LICENSE view
binary file changed (167 → 244 bytes)
._ParserFunction.cabal view
binary file changed (167 → 167 bytes)
._Setup.hs view
binary file changed (197 → 197 bytes)
._Text view
binary file changed (197 → 197 bytes)
ParserFunction.cabal view
@@ -1,19 +1,18 @@ name: ParserFunction-version: 0.0.4+version: 0.0.5 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.+synopsis: Utilities for parsing and evaluating mathematical 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.+ ParserFunction provides utilities for parsing and evaluating mathematical expressions.+ The central parsing function in this package is @stringToExpr@, which parses an expression+ (as a string) and returns an expression tree of type Expr (or nothing if the string is malformed).+ Expressions can be evaluated using the function @evaluateExpression@. Examples of these+ functions can be found by viewing the source code for this package. build-type: Simple
Text/.DS_Store view
binary file changed (6148 → 6148 bytes)
Text/._.DS_Store view
binary file changed (82 → 197 bytes)
Text/._ParserCombinators view
binary file changed (197 → 197 bytes)
Text/ParserCombinators/.DS_Store view
binary file changed (6148 → 6148 bytes)
Text/ParserCombinators/._.DS_Store view
binary file changed (82 → 197 bytes)
Text/ParserCombinators/._Parsec view
binary file changed (197 → 197 bytes)
Text/ParserCombinators/Parsec/._.DS_Store view
binary file changed (82 → 197 bytes)
Text/ParserCombinators/Parsec/._ParserFunction.hs view
binary file changed (171 → 171 bytes)
Text/ParserCombinators/Parsec/ParserFunction.hs view
@@ -1,20 +1,28 @@ --- ParserFunction --- by Enzo Haussecker ---- The centerpiece of this module is a function called: evalStrExpr, ---- which evaluates a string-expression using a variable lookup map.+--- ParserFunction provides utilities for parsing and evaluating mathematical expressions. +--- The central parsing function in this package is stringToExpr, which parses an expression +--- (as a string) and returns an expression tree of type Expr (or nothing if the string is malformed). ---- Examples of evalStrExpr are as fallows.+--- Examples of stringToExpr are as fallows. ---- > evalStrExpr "5 - 2" []+--- > stringToExpr "cos(x^2)+4*(1+y)"+--- Just (Add (Cos (Pow (Var 'x') (Num 2.0))) (Mul (Num 4.0) (Add (Num 1.0) (Var 'y'))))++--- Expressions can be evaluated using the function evaluateExpression. Example: ++--- Examples of evaluateExpression are as fallows.++--- > evaluateExpression "5 - 2" [] --- 3.0---- > evalStrExpr "x^2 + y" [('x',2),('y',3)]+--- > evaluateExpression "x^2 + y" [('x',2),('y',3)] --- 7.0---- > evalStrExpr "cos(x)" [('x',pi)]+--- > evaluateExpression "cos(x)" [('x',pi)] --- -1.0 module Text.ParserCombinators.Parsec.ParserFunction- (Expr,evalStrExpr,strToExpr,buildExpr,expressionTable,factor,variables,number,evaluate) where+ (Expr,evaluateExpression,stringToExpr,buildExpr,expressionTable,factor,variables,number,evaluate) where import Text.ParserCombinators.Parsec.Expr import Text.ParserCombinators.Parsec@@ -37,18 +45,17 @@ | Sec Expr | Csc Expr | Cot Expr | Mul Expr Expr | Add Expr Expr | Exp Expr deriving (Show, Eq, Ord) ---- 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)+--- evaluateExpression evaluates a mathematical expression s using the variable map m. +evaluateExpression :: String -> [(Char,Double)] -> Double+evaluateExpression s m = evaluate (M.fromAscList $ caseMap m) (fromMaybe failing $ stringToExpr s) where caseMap x = fmap (\ (a, b) -> ([toLower a], b)) x failing = error "Parser error in expression" ---- 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)+--- stringToExpr parses an expression and returns an expression tree of type Expr, +--- (or nothing if the string is malformed).+stringToExpr :: String -> Maybe Expr+stringToExpr xs = if any (==True) (symbols failingSymbols xs) then Nothing else either (const Nothing) (Just) (parse buildExpr "" handleString) where@@ -59,7 +66,6 @@ "/^","/*","//","/+","/-","+^","+*","+/","++","+-", "-^","-*","-/","-+","--"] ---- buildExpr is the fundamental parsing function. buildExpr :: Parser Expr buildExpr = buildExpressionParser expressionTable factor