Browse Source
Added type check visitor to validate types of identifiers in expressions. Started writing validation method on PRISM program class.
Added type check visitor to validate types of identifiers in expressions. Started writing validation method on PRISM program class.
Former-commit-id: 6416bea711
tempestpy_adaptions
dehnert
11 years ago
7 changed files with 378 additions and 18 deletions
-
24src/storage/expressions/Expression.cpp
-
23src/storage/expressions/Expression.h
-
15src/storage/expressions/SubstitutionVisitor.cpp
-
97src/storage/expressions/TypeCheckVisitor.cpp
-
50src/storage/expressions/TypeCheckVisitor.h
-
181src/storage/prism/Program.cpp
-
6src/storage/prism/Program.h
@ -0,0 +1,97 @@ |
|||
#include "src/storage/expressions/TypeCheckVisitor.h"
|
|||
#include "src/storage/expressions/Expressions.h"
|
|||
|
|||
#include "src/exceptions/ExceptionMacros.h"
|
|||
#include "src/exceptions/InvalidTypeException.h"
|
|||
|
|||
namespace storm { |
|||
namespace expressions { |
|||
template<typename MapType> |
|||
TypeCheckVisitor<MapType>::TypeCheckVisitor(MapType const& identifierToTypeMap) : identifierToTypeMap(identifierToTypeMap) { |
|||
// Intentionally left empty.
|
|||
} |
|||
|
|||
template<typename MapType> |
|||
void TypeCheckVisitor<MapType>::check(BaseExpression const* expression) { |
|||
expression->accept(this); |
|||
} |
|||
|
|||
template<typename MapType> |
|||
void TypeCheckVisitor<MapType>::visit(IfThenElseExpression const* expression) { |
|||
expression->getCondition()->accept(this); |
|||
expression->getThenExpression()->accept(this); |
|||
expression->getElseExpression()->accept(this); |
|||
} |
|||
|
|||
template<typename MapType> |
|||
void TypeCheckVisitor<MapType>::visit(BinaryBooleanFunctionExpression const* expression) { |
|||
expression->getFirstOperand()->accept(this); |
|||
expression->getSecondOperand()->accept(this); |
|||
} |
|||
|
|||
template<typename MapType> |
|||
void TypeCheckVisitor<MapType>::visit(BinaryNumericalFunctionExpression const* expression) { |
|||
expression->getFirstOperand()->accept(this); |
|||
expression->getSecondOperand()->accept(this); |
|||
} |
|||
|
|||
template<typename MapType> |
|||
void TypeCheckVisitor<MapType>::visit(BinaryRelationExpression const* expression) { |
|||
expression->getFirstOperand()->accept(this); |
|||
expression->getSecondOperand()->accept(this); |
|||
} |
|||
|
|||
template<typename MapType> |
|||
void TypeCheckVisitor<MapType>::visit(BooleanConstantExpression const* expression) { |
|||
auto identifierTypePair = this->identifierToTypeMap.find(expression->getConstantName()); |
|||
LOG_THROW(identifierTypePair != this->identifierToTypeMap.end(), storm::exceptions::InvalidArgumentException, "No type available for identifier '" << expression->getConstantName() << "'."); |
|||
LOG_THROW(identifierTypePair->second == ExpressionReturnType::Bool, storm::exceptions::InvalidTypeException, "Type mismatch for constant '" << expression->getConstantName() << "': expected bool, but found " << expression->getReturnType() << "."); |
|||
} |
|||
|
|||
template<typename MapType> |
|||
void TypeCheckVisitor<MapType>::visit(DoubleConstantExpression const* expression) { |
|||
auto identifierTypePair = this->identifierToTypeMap.find(expression->getConstantName()); |
|||
LOG_THROW(identifierTypePair != this->identifierToTypeMap.end(), storm::exceptions::InvalidArgumentException, "No type available for identifier '" << expression->getConstantName() << "'."); |
|||
LOG_THROW(identifierTypePair->second == ExpressionReturnType::Bool, storm::exceptions::InvalidTypeException, "Type mismatch for constant '" << expression->getConstantName() << "': expected double, but found " << expression->getReturnType() << "."); |
|||
} |
|||
|
|||
template<typename MapType> |
|||
void TypeCheckVisitor<MapType>::visit(IntegerConstantExpression const* expression) { |
|||
auto identifierTypePair = this->identifierToTypeMap.find(expression->getConstantName()); |
|||
LOG_THROW(identifierTypePair != this->identifierToTypeMap.end(), storm::exceptions::InvalidArgumentException, "No type available for identifier '" << expression->getConstantName() << "'."); |
|||
LOG_THROW(identifierTypePair->second == ExpressionReturnType::Bool, storm::exceptions::InvalidTypeException, "Type mismatch for constant '" << expression->getConstantName() << "': expected int, but found " << expression->getReturnType() << "."); |
|||
} |
|||
|
|||
template<typename MapType> |
|||
void TypeCheckVisitor<MapType>::visit(VariableExpression const* expression) { |
|||
auto identifierTypePair = this->identifierToTypeMap.find(expression->getVariableName()); |
|||
LOG_THROW(identifierTypePair != this->identifierToTypeMap.end(), storm::exceptions::InvalidArgumentException, "No type available for identifier '" << expression->getVariableName() << "'."); |
|||
LOG_THROW(identifierTypePair->second == ExpressionReturnType::Bool, storm::exceptions::InvalidTypeException, "Type mismatch for variable '" << expression->getVariableName() << "': expected " << identifierTypePair->first << ", but found " << expression->getReturnType() << "."); |
|||
} |
|||
|
|||
template<typename MapType> |
|||
void TypeCheckVisitor<MapType>::visit(UnaryBooleanFunctionExpression const* expression) { |
|||
expression->getOperand()->accept(this); |
|||
} |
|||
|
|||
template<typename MapType> |
|||
void TypeCheckVisitor<MapType>::visit(UnaryNumericalFunctionExpression const* expression) { |
|||
expression->getOperand()->accept(this); |
|||
} |
|||
|
|||
template<typename MapType> |
|||
void TypeCheckVisitor<MapType>::visit(BooleanLiteralExpression const* expression) { |
|||
// Intentionally left empty.
|
|||
} |
|||
|
|||
template<typename MapType> |
|||
void TypeCheckVisitor<MapType>::visit(IntegerLiteralExpression const* expression) { |
|||
// Intentionally left empty.
|
|||
} |
|||
|
|||
template<typename MapType> |
|||
void TypeCheckVisitor<MapType>::visit(DoubleLiteralExpression const* expression) { |
|||
// Intentionally left empty.
|
|||
} |
|||
} |
|||
} |
@ -0,0 +1,50 @@ |
|||
#ifndef STORM_STORAGE_EXPRESSIONS_TYPECHECKVISITOR_H_ |
|||
#define STORM_STORAGE_EXPRESSIONS_TYPECHECKVISITOR_H_ |
|||
|
|||
#include <stack> |
|||
|
|||
#include "src/storage/expressions/Expression.h" |
|||
#include "src/storage/expressions/ExpressionVisitor.h" |
|||
|
|||
namespace storm { |
|||
namespace expressions { |
|||
template<typename MapType> |
|||
class TypeCheckVisitor : public ExpressionVisitor { |
|||
public: |
|||
/*! |
|||
* Creates a new type check visitor that uses the given map to check the types of variables and constants. |
|||
* |
|||
* @param identifierToTypeMap A mapping from identifiers to expressions. |
|||
*/ |
|||
TypeCheckVisitor(MapType const& identifierToTypeMap); |
|||
|
|||
/*! |
|||
* Checks that the types of the identifiers in the given expression match the ones in the previously given |
|||
* map. |
|||
* |
|||
* @param expression The expression in which to check the types. |
|||
*/ |
|||
void check(BaseExpression const* expression); |
|||
|
|||
virtual void visit(IfThenElseExpression const* expression) override; |
|||
virtual void visit(BinaryBooleanFunctionExpression const* expression) override; |
|||
virtual void visit(BinaryNumericalFunctionExpression const* expression) override; |
|||
virtual void visit(BinaryRelationExpression const* expression) override; |
|||
virtual void visit(BooleanConstantExpression const* expression) override; |
|||
virtual void visit(DoubleConstantExpression const* expression) override; |
|||
virtual void visit(IntegerConstantExpression const* expression) override; |
|||
virtual void visit(VariableExpression const* expression) override; |
|||
virtual void visit(UnaryBooleanFunctionExpression const* expression) override; |
|||
virtual void visit(UnaryNumericalFunctionExpression const* expression) override; |
|||
virtual void visit(BooleanLiteralExpression const* expression) override; |
|||
virtual void visit(IntegerLiteralExpression const* expression) override; |
|||
virtual void visit(DoubleLiteralExpression const* expression) override; |
|||
|
|||
private: |
|||
// A mapping of identifier names to expressions with which they shall be replaced. |
|||
MapType const& identifierToTypeMap; |
|||
}; |
|||
} |
|||
} |
|||
|
|||
#endif /* STORM_STORAGE_EXPRESSIONS_TYPECHECKVISITOR_H_ */ |
Write
Preview
Loading…
Cancel
Save
Reference in new issue