Skip to content

Commit

Permalink
code generator and tests updated
Browse files Browse the repository at this point in the history
  • Loading branch information
ikuraj committed Jun 13, 2012
1 parent 996f960 commit fcd9be4
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 32 deletions.
Expand Up @@ -6,6 +6,7 @@ import ch.epfl.insynth.combinator.AbsDeclaration
import ch.epfl.insynth.reconstruction.trees._
import ch.epfl.insynth.print._
import ch.epfl.scala.{ trees => Scala }
import ch.epfl.insynth.combinator.NormalDeclaration

/**
* class that converts an intermediate tree into a list of output elements (elements
Expand Down Expand Up @@ -78,14 +79,31 @@ object CodeGenerator extends (Node => List[CodeGenOutput]) {
val appIdentifier = transform(params.head.head, App).head

// check what the declaration says about the application
// TODO refactor - similar to the method construction
if (decl.isField) {
assert(params.size == 2)
// TODO refactor - similar to the method construction
// go through all the receiver objects
(List[Document]() /: transform(params(1).toList, App)) {
(listOfDocumentsToReturn, objectDoc) =>
listOfDocumentsToReturn :+ group(objectDoc :: "." :: appIdentifier)
}
// if the field needs this keyword
val needsThis = decl.hasThis
(List[Document]() /: params(1)) {
(listDocsReceivers, receiver) => {
// get documents for the receiver objects (or empty if none is needed)
val documentsForThis = {
if (needsThis)
receiver match {
case Identifier(_, NormalDeclaration(receiverDecl)) if receiverDecl.isThis =>
List(empty)
case _ => transform(receiver, App) map { (_:Document) :: "." }
}
else transform(receiver, App) map { (_:Document) :: "." }
}
// go through all the receiver objects and add to the list
(listDocsReceivers /: documentsForThis) {
(listDocsTransformedReceiver, receiverDoc) => {
listDocsTransformedReceiver :+ group(receiverDoc :: appIdentifier)
}
}
}
}
} else

if (!decl.isMethod)
Expand All @@ -101,21 +119,38 @@ object CodeGenerator extends (Node => List[CodeGenOutput]) {
firstTermFunctionTransform
else // if (decl.isMethod)
{
// get info about parameters
val paramsInfo = decl.scalaType match {
case Scala.Method(_, params, _) => params
case _ => throw new RuntimeException("Declared method but scala type is not")
}
// go through all the receiver objects
(List[Document]() /: transform(params(1).toList, App)) {
(listOfDocumentsToReturn, objectDoc) => {
// go through all combinations of parameters documents
(listOfDocumentsToReturn /:
getParamsCombinations(params.drop(2), paramsInfo)) {
(list, paramsDoc) => list :+
group(objectDoc :: "." :: appIdentifier :: paramsDoc)
}
}
}
// if the method needs this keyword
val needsThis = decl.hasThis
(List[Document]() /: params(1)) {
(listDocsReceivers, receiver) => {
// get documents for the receiver objects (or empty if none is needed)
val documentsForThis = {
if (!needsThis)
receiver match {
case Identifier(_, NormalDeclaration(receiverDecl)) if receiverDecl.isThis =>
List(empty)
case _ => transform(receiver, App) map { (_:Document) :: "." }
}
else transform(receiver, App) map { (_:Document) :: "." }
}
// go through all the receiver objects and add to the list
(listDocsReceivers /: documentsForThis) {
(listDocsTransformedReceiver, receiverDoc) => {
// go through all combinations of parameters documents
(listDocsTransformedReceiver /: getParamsCombinations(params.drop(2), paramsInfo)) {
// and add them to the list
(listDocsTransformedParameters, paramsDoc) => listDocsTransformedParameters :+
group(receiverDoc :: appIdentifier :: paramsDoc)
}
}
}
}
}
}
}
// function that is created as an argument or anything else
Expand Down
Expand Up @@ -8,15 +8,17 @@ import ch.epfl.insynth.reconstruction.Reconstructor
object ReconstructorTest {

def main(args: Array[String]): Unit = {
for (tree <-
val tests =
Array(
TreeExample.buildSimpleTree, TreeExample.buildComplexTree,
TreeExample.buildTreeAbsApplication, TreeExample.buildTreeArrowType,
/*TreeExample.buildTreeCycles, */TreeExample.buildTreeOverlapParameterTypeWithReturnType,
TreeExample.buildTreeSKombinator, TreeExample.buildTreeWithCurryingFunctions,
TreeExample.buildTreeWithVariousFunctions
// TreeExample.buildSimpleTree, TreeExample.buildComplexTree,
// TreeExample.buildTreeAbsApplication, TreeExample.buildTreeArrowType,
// /*TreeExample.buildTreeCycles, */TreeExample.buildTreeOverlapParameterTypeWithReturnType,
// TreeExample.buildTreeSKombinator, TreeExample.buildTreeWithCurryingFunctions,
// TreeExample.buildTreeWithVariousFunctions
TreeExample.buildTreeWithoutThis
)
)

for (tree <- tests )
parametrizedTreeReconstruct(tree)
}

Expand Down
155 changes: 147 additions & 8 deletions ch.epfl.insynth.reconstruction/test/ch/epfl/test/TreeExample.scala
Expand Up @@ -12,6 +12,8 @@ object TreeExample {
val typeString = Const("String")
val typeBoolean = Const("Boolean")
val typeChar = Const("Char")
val typeFloat = Const("Float")
val typeDouble = Const("Double")
val typeUnit = Const("Unit")

val typeBottom = Const("$Bottom_Type_Just_For_Resolution$")
Expand Down Expand Up @@ -1095,29 +1097,166 @@ object TreeExample {
Map(
transform(typeInt) ->
ContainerNode(
Set(intNode, getIntNodeRec)
Set(intNode, getIntNode)
)
)
)

lazy val getIntNodeRec = SimpleNode(
fDeclaration,
// lazy val getIntNodeRec = SimpleNode(
// fDeclaration,
// Map(
// transform(typeInt) ->
// ContainerNode(
// Set(getIntNode)
// )
// )
// )


val query =
SimpleNode(
queryDeclaration,
Map( // for each parameter type - how can we resolve it
transform(typeInt) ->
ContainerNode(
Set(getIntNode)
)
)
)

query
}

/**
* Constructs a simple tree which has prints without "this" keyword
*/
def buildTreeWithoutThis = {
//************************************
// Goals
// find expression of type: String
// expression: query([this].m1(this.m2, this.f1, [this].f2))
//************************************

//************************************
// Scala types
//************************************
// class A { ... }
val objectA = Const("A")
// def m1(Char, Int, Float): String
val m1 = Method(objectA, List(List(typeChar, typeInt, typeFloat)), typeString)
// int field
val intField = Method(objectA, List(), typeInt)
// float field
val floatField = Method(objectA, List(), typeFloat)
// def m2(): Char
val m2 = Method(objectA, List(), typeChar)
// query: String → ⊥
val queryType = Function(typeString, typeBottom)

//************************************
// Declarations
//************************************
val objectADeclaration = new Declaration(
"this", // full name
transform(objectA), // inSynth type
objectA // scala type
)
objectADeclaration.setIsThis(true)

val m1Declaration = new Declaration(
"some.package.A.m1", // full name
transform(m1), // inSynth type
m1 // scala type
)
m1Declaration.setIsMethod(true)
m1Declaration.setIsThis(false)
m1Declaration.setHasThis(false)

val m2Declaration = new Declaration(
"some.package.A.m2", // full name
transform(m2), // inSynth type
m2 // scala type
)
m2Declaration.setIsMethod(true)
m2Declaration.setHasThis(false)

val intFieldDeclaration = Declaration(
"A.f1",
intField, intField
)
intFieldDeclaration.setIsField(true)
intFieldDeclaration.setHasThis(true)

val floatFieldDeclaration = Declaration(
"A.f2",
floatField, floatField
)
floatFieldDeclaration.setIsField(true)
floatFieldDeclaration.setHasThis(false)

// special query declaration
val queryDeclaration = new Declaration(
"special.name.for.query",
transform(queryType),
queryType
)
queryDeclaration.setIsQuery(true)

//************************************
// InSynth proof trees
//************************************

val thisNode = SimpleNode(
objectADeclaration,
Map()
)

val m1Node = SimpleNode(
m1Declaration,
Map(
transform(objectA) ->
ContainerNode(
Set( thisNode )
),
transform(typeInt) ->
ContainerNode(
Set(getIntNode)
Set(
SimpleNode(
intFieldDeclaration,
Map( transform(objectA) -> ContainerNode(Set( thisNode )))
)
)
),
transform(typeFloat) ->
ContainerNode(
Set(
SimpleNode(
floatFieldDeclaration,
Map( transform(objectA) -> ContainerNode(Set( thisNode )))
)
)
),
transform(typeChar) ->
ContainerNode(
Set(
SimpleNode(
m2Declaration,
Map( transform(objectA) -> ContainerNode(Set( thisNode )))
)
)
)
)
)
)


// goal:Bottom, type:B→⊥
// expression: query(new (this.m(), this.bla)):⊥
val query =
SimpleNode(
queryDeclaration,
Map( // for each parameter type - how can we resolve it
transform(typeInt) ->
transform(typeString) ->
ContainerNode(
Set(getIntNode)
Set(m1Node)
)
)
)
Expand Down

0 comments on commit fcd9be4

Please sign in to comment.