From e52ce5dea8be9795fe14a4bb780e35ed14bed17f Mon Sep 17 00:00:00 2001 From: Francis Russell Date: Wed, 12 Sep 2012 22:53:02 +0100 Subject: [PATCH] Add addition, subtraction and component access operators. --- examples/integrals_pos.ofl | 2 +- src/ParsedOFL.hs | 3 +++ src/Parsing.hs | 13 ++++++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/examples/integrals_pos.ofl b/examples/integrals_pos.ofl index 6214ce2..8a5beac 100644 --- a/examples/integrals_pos.ofl +++ b/examples/integrals_pos.ofl @@ -6,7 +6,7 @@ Integer order SpatialIndex i # Computation -rmat[i, alpha, beta] = inner(bra[alpha], ket[beta] * pos[i]^order) +rmat[i, alpha, beta] = inner(bra[alpha], ket[beta] * component(r, i)^order) # Implementation specific target ONETEP diff --git a/src/ParsedOFL.hs b/src/ParsedOFL.hs index fe41140..745a85d 100644 --- a/src/ParsedOFL.hs +++ b/src/ParsedOFL.hs @@ -15,7 +15,10 @@ data Expression = IndexedIdentifier String [String] | Sum Expression String | Multiply Expression Expression | Divide Expression Expression | + Add Expression Expression | + Sub Expression Expression | Power Expression Expression | + Component Expression String | Derivative Expression String deriving Show data Assignment = Assign String [String] Expression deriving Show diff --git a/src/Parsing.hs b/src/Parsing.hs index 372a39c..7e63b0e 100644 --- a/src/Parsing.hs +++ b/src/Parsing.hs @@ -10,7 +10,9 @@ import Text.Parsec.Combinator oflIndexTypes = ["FunctionIndex", "SpinIndex", "SpatialIndex"] oflOperandTypes = ["Real", "Function", "Integer"] -oflKeywords = oflIndexTypes ++ oflOperandTypes ++ ["*", "-", "^", "laplacian", "inner", "sum", "derivative"] +oflKeywords = oflIndexTypes ++ + oflOperandTypes ++ + ["^", "+", "-", "*", "/", "laplacian", "inner", "sum", "derivative", "component"] oflDef = emptyDef{ commentStart = "" , commentEnd = "" @@ -36,7 +38,9 @@ TokenParser { reserved = oflReserved oflOperatorTable = [[Infix (do { oflSymbol "^"; return (\x y -> Power x y) }) AssocLeft], [Prefix (do { oflSymbol "-"; return (\x -> Negate x)})], [Infix (do { oflSymbol "*"; return (\x y -> Multiply x y) }) AssocLeft - ,Infix (do { oflSymbol "/"; return (\x y -> Divide x y) }) AssocLeft] + ,Infix (do { oflSymbol "/"; return (\x y -> Divide x y) }) AssocLeft], + [Infix (do { oflSymbol "+"; return (\x y -> Add x y) }) AssocLeft + ,Infix (do { oflSymbol "-"; return (\x y -> Sub x y) }) AssocLeft] ] parseType = foldl1 (<|>) [do { oflReserved $ show t; return t} | t <- [Real, Function, Integer]] @@ -73,6 +77,7 @@ parseTerm = do { oflReserved "laplacian"; operand <- oflParens parseExpression; do { oflReserved "inner"; inner <- oflParens parseInner; return inner} <|> do { oflReserved "sum"; sum <- oflParens parseSum; return sum} <|> do { oflReserved "derivative"; derivative <- oflParens parseDerivative; return derivative} <|> + do { oflReserved "component"; component <- oflParens parseComponent; return component} <|> do { float <- oflFloat; return $ ConstReal float } <|> do { integer <- oflInteger; return $ ConstInteger integer } <|> oflParens parseExpression <|> @@ -84,10 +89,12 @@ parseInner = do { e1 <- parseExpression; oflSymbol ","; e2 <- parseExpression; r parseSum = do { e1 <- parseExpression; oflSymbol ","; index <- parseIdentifier; return $ Sum e1 index} "sum" parseDerivative = do { e1 <- parseExpression; oflSymbol ","; index <- parseIdentifier; return $ Derivative e1 index} "derivative" + +parseComponent = do { e1 <- parseExpression; oflSymbol ","; index <- parseIdentifier; return $ Component e1 index} "component access" parseIdentifierAccess = do { identifier <- parseIdentifier; indices <- option [] (oflBrackets $ oflCommaSep parseIdentifier); - return $ IndexedIdentifier identifier indices + return $ IndexedIdentifier identifier indices } "indexed identifier" parseOFL :: Parsec String OFL OFL -- 2.47.3