From: Francis Russell Date: Fri, 14 Sep 2012 13:40:18 +0000 (+0100) Subject: Verify that LHS of assignment is an lvalue. X-Git-Url: https://git.unchartedbackwaters.co.uk/w/?a=commitdiff_plain;h=e3d4f0a3eaf19a9b51301db39f66a5ff83ad6357;p=francis%2Fofc.git Verify that LHS of assignment is an lvalue. --- diff --git a/src/ParsedOFL.hs b/src/ParsedOFL.hs index db234b5..4a46e29 100644 --- a/src/ParsedOFL.hs +++ b/src/ParsedOFL.hs @@ -87,7 +87,7 @@ getType ofl (IndexedIdentifier name _) = getValueType ofl name getType ofl (ConstReal _) = Real getType ofl (ConstInteger _) = Integer getType ofl (Negate e) = getType ofl e -getType ofl (Inner _ _) = Function +getType ofl (Inner _ _) = Real getType ofl (Laplacian _) = Function getType ofl (Sum e _) = getType ofl e getType ofl (Multiply a b) = promote (getType ofl a) (getType ofl b) @@ -101,25 +101,31 @@ getType ofl (Derivative e i) = getType ofl e emptyOFL :: OFL emptyOFL = OFL { assignments = [], symbols = Map.empty } +-- Validation +data ValidationError = Message String deriving Show +type ValidationResult = Either ValidationError () +validationSuccess = Right () +validationFailure = \x -> Left (Message x) + validateAssignment :: OFL -> Assignment -> ValidationResult validateAssignment ofl (Assign lhs rhs) = do { validateExpression ofl lhs; validateExpression ofl rhs; - return () + isLValue ofl lhs; + case (getType ofl lhs) == (getType ofl rhs) of + True -> validationSuccess + False -> validationFailure $ "Types of left and right-hand sides of assignment do not match" } --- Validation - -data ValidationError = Message String deriving Show -type ValidationResult = Either ValidationError () -validationSuccess = Right () -validationFailure = \x -> Left (Message x) +isLValue :: OFL -> Expression -> ValidationResult +isLValue ofl (IndexedIdentifier name indices) = validationSuccess +isLValue _ e = validationFailure $ "Expression " ++ show e ++ " is not an assignable value" indexExists :: OFL -> String -> ValidationResult indexExists ofl name = if (hasIndex ofl name) then validationSuccess else validationFailure $ "Unknown index " ++ name valueExists :: OFL -> String -> ValidationResult -valueExists ofl name = if (hasValue ofl name) then validationSuccess else validationFailure $ "Unknownm value " ++ name +valueExists ofl name = if (hasValue ofl name) then validationSuccess else validationFailure $ "Unknown value " ++ name isFunction :: OFL -> Expression -> ValidationResult isFunction ofl e = case (getType ofl e) of