diff --git a/Test.scala b/Test.scala index 475902f071ef606441ff91afec957e7b500a9348..a1537f887a3d929b157f3b1667c8e71fba2f8831 100644 --- a/Test.scala +++ b/Test.scala @@ -8,4 +8,10 @@ object Test { require(i == j) i + j } ensuring(res => res == 2 * i) + + def booleans: Unit = { + var b : Boolean = true + b = false + + } } diff --git a/src/funcheck/Extractors.scala b/src/funcheck/Extractors.scala new file mode 100644 index 0000000000000000000000000000000000000000..9446295560bdab04015075e317075fdb9d0d519d --- /dev/null +++ b/src/funcheck/Extractors.scala @@ -0,0 +1,67 @@ +package funcheck + +import scala.tools.nsc._ + +/** Contains extractors to pull-out interesting parts of the Scala ASTs. */ +trait Extractors { + val global: Global + val pluginInstance: FunCheckPlugin + + import global._ + import global.definitions._ + + object StructuralExtractors { + object ScalaPredef { + /** Extracts method calls from scala.Predef. */ + def unapply(tree: Tree): Option[String] = tree match { + case Select(Select(This(scalaName),predefName),symName) + if("scala".equals(scalaName.toString) && "Predef".equals(predefName.toString)) => + Some(symName.toString) + case _ => None + } + } + + object EnsuredExpression { + /** Extracts the 'ensuring' contract from an expression. */ + def unapply(tree: Tree): Option[(Tree,Function)] = tree match { + case Apply( + Select( + Apply( + TypeApply( + ScalaPredef("any2Ensuring"), + TypeTree() :: Nil), + body :: Nil), + ensuringName), + (anonymousFun @ Function(ValDef(_, resultName, resultType, EmptyTree) :: Nil, + contractBody)) :: Nil) + if("ensuring".equals(ensuringName.toString)) => Some((body,anonymousFun)) + case _ => None + } + } + + object RequiredExpression { + /** Extracts the 'require' contract from an expression (only if it's the + * first call in the block). */ + def unapply(tree: Tree): Option[(Tree,Tree)] = tree match { + case Block(Apply(ScalaPredef("require"), contractBody :: Nil) :: Nil, body) => + Some((body,contractBody)) + case _ => None + } + } + } + + object ExpressionExtractors { + object ExBooleanLiteral { + /** Extracts the 'true' of 'false' constants. */ + def unapply(tree: Tree): Option[Boolean] = tree match { + case Literal(Constant(true)) => Some(true) + case Literal(Constant(false)) => Some(false) + case _ => None + } + } + } + + object TypeExtractors { + + } +}