]> git.unchartedbackwaters.co.uk Git - francis/ofc.git/commitdiff
Add addition, subtraction and component access operators.
authorFrancis Russell <francis@unchartedbackwaters.co.uk>
Wed, 12 Sep 2012 21:53:02 +0000 (22:53 +0100)
committerFrancis Russell <francis@unchartedbackwaters.co.uk>
Wed, 12 Sep 2012 21:53:02 +0000 (22:53 +0100)
examples/integrals_pos.ofl
src/ParsedOFL.hs
src/Parsing.hs

index 6214ce24ea9fdbfc3ff16bf50824db153b2ee42c..8a5beac30f7f9d3b5392a44f4880f073005b57c4 100644 (file)
@@ -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
index fe411407731050dfc4cd04ee30955ac23a6c4e51..745a85d680d079e557b4f5feedd40b69076824f7 100644 (file)
@@ -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
index 372a39c45c1828e8465c349f423798da6a982660..7e63b0e3406b9bef26c0c71c93d60e1c02ffe385 100644 (file)
@@ -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