Skip to content
Snippets Groups Projects
Commit e2c59a06 authored by Ali Sinan Köksal's avatar Ali Sinan Köksal
Browse files

Component for CP, write program into file and read from it, initial structure...

Component for CP, write program into file and read from it, initial structure for transforming calls

parent fa847883
No related branches found
No related tags found
No related merge requests found
package funcheck
import scala.tools.nsc._
import scala.tools.nsc.plugins._
class CPComponent(val global: Global, val pluginInstance: FunCheckPlugin)
extends PluginComponent
with CodeExtraction
with Serialization
with CallTransformation
{
import global._
// This is how it works from 2.8 on..
override val runsRightAfter: Option[String] = None
override val runsAfter: List[String] = List("refchecks")
val phaseName = "constraint-programming"
/** this is initialized when the Funcheck phase starts*/
var fresh: scala.tools.nsc.util.FreshNameCreator = null
def newPhase(prev: Phase) = new CPPhase(prev)
class CPPhase(prev: Phase) extends StdPhase(prev) {
def apply(unit: CompilationUnit): Unit = {
//global ref to freshName creator
fresh = unit.fresh
val prog: purescala.Definitions.Program = extractCode(unit)
val fileName = writeProgram(prog)
println("Program extracted and written into: " + fileName)
transformCalls(unit)
println("Finished transformation")
/*
try {
val recovered = readProgram(fileName)
println
println("Recovered: " + recovered)
} catch {
case e => e.printStackTrace()
}
*/
}
}
}
package funcheck
import scala.tools.nsc.transform.TypingTransformers
trait CallTransformation
extends TypingTransformers
with CodeExtraction
{
import global._
private lazy val funcheckPackage = definitions.getModule("funcheck")
private lazy val cpDefinitionsModule = definitions.getModule("funcheck.CP")
def transformCalls(unit: CompilationUnit) : Unit =
unit.body = new CallTransformer(unit).transform(unit.body)
class CallTransformer(unit: CompilationUnit) extends TypingTransformer(unit) {
override def transform(tree: Tree) : Tree = {
tree match {
case a @ Apply(TypeApply(Select(s: Select, n), _), rhs @ List(predicate: Function)) if (cpDefinitionsModule == s.symbol && n.toString == "choose") => {
println("I'm inside a choose call!")
val Function(funValDefs, funBody) = predicate
val fd = extractPredicate(unit, funValDefs, funBody)
println("Here is the extracted FunDef:")
println(fd)
super.transform(a)
}
case _ => super.transform(tree)
}
}
}
}
package funcheck
import java.io.{FileInputStream,FileOutputStream,ObjectInputStream,ObjectOutputStream}
import purescala.Definitions.Program
trait Serialization {
val fileSuffix = ".serialized"
val dirName = "serialized"
def programFileName(prog : Program) : String = {
prog.mainObject.id.toString
}
def writeProgram(prog : Program) : String = {
val directory = new java.io.File(dirName)
directory.mkdir()
val file = java.io.File.createTempFile(programFileName(prog), fileSuffix, directory)
val fos = new FileOutputStream(file)
val oos = new ObjectOutputStream(fos)
oos.writeObject(prog)
oos.flush()
fos.close()
file.getAbsolutePath()
}
def readProgram(filename : String) : Program = {
val fis = new FileInputStream(filename)
val ois = new ObjectInputStream(fis)
val recovered : Program = ois.readObject().asInstanceOf[Program]
fis.close()
recovered
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment