Skip to content
Snippets Groups Projects
Commit e782ef6f authored by Etienne Kneuss's avatar Etienne Kneuss Committed by Philippe Suter
Browse files

Provide all classes to CompilationUnit

parent 984286c4
No related branches found
No related tags found
No related merge requests found
...@@ -43,8 +43,11 @@ object CodeGeneration { ...@@ -43,8 +43,11 @@ object CodeGeneration {
case Int32Type | BooleanType => case Int32Type | BooleanType =>
ch << IRETURN ch << IRETURN
case UnitType | TupleType(_) | SetType(_) | MapType(_, _) | AbstractClassType(_) | CaseClassType(_) =>
ch << ARETURN
case other => case other =>
throw CompilationException("Unsupported return type : " + other) throw CompilationException("Unsupported return type : " + other.getClass)
} }
ch.freeze ch.freeze
......
...@@ -14,14 +14,14 @@ import cafebabe.Flags._ ...@@ -14,14 +14,14 @@ import cafebabe.Flags._
import CodeGeneration._ import CodeGeneration._
class CompilationUnit(val program: Program, val mainClass: ClassFile, implicit val env: CompilationEnvironment) { class CompilationUnit(val program: Program, val classes: Seq[ClassFile], implicit val env: CompilationEnvironment) {
val mainClassName = defToJVMName(program, program.mainObject)
val loader = new CafebabeClassLoader val loader = new CafebabeClassLoader
loader.register(mainClass) classes.foreach(loader.register(_))
def writeClassFiles() { def writeClassFiles() {
mainClass.writeToFile(mainClassName + ".class") for (cl <- classes) {
cl.writeToFile(cl.className + ".class")
}
} }
private var _nextExprId = 0 private var _nextExprId = 0
...@@ -41,8 +41,12 @@ class CompilationUnit(val program: Program, val mainClass: ClassFile, implicit v ...@@ -41,8 +41,12 @@ class CompilationUnit(val program: Program, val mainClass: ClassFile, implicit v
case b: java.lang.Boolean => case b: java.lang.Boolean =>
BooleanLiteral(b.booleanValue) BooleanLiteral(b.booleanValue)
case cc: runtime.CaseClass =>
println("YAY")
throw CompilationException("YAY Unsupported return value : " + e)
case _ => case _ =>
throw CompilationException("Unsupported return value : " + e) throw CompilationException("MEH Unsupported return value : " + e.getClass)
} }
def compileExpression(e: Expr, args: Seq[Identifier]): CompiledExpression = { def compileExpression(e: Expr, args: Seq[Identifier]): CompiledExpression = {
...@@ -83,7 +87,7 @@ class CompilationUnit(val program: Program, val mainClass: ClassFile, implicit v ...@@ -83,7 +87,7 @@ class CompilationUnit(val program: Program, val mainClass: ClassFile, implicit v
case Int32Type | BooleanType => case Int32Type | BooleanType =>
ch << IRETURN ch << IRETURN
case UnitType | TupleType(_) | SetType(_) | MapType(_, _) => case UnitType | TupleType(_) | SetType(_) | MapType(_, _) | AbstractClassType(_) | CaseClassType(_) =>
ch << ARETURN ch << ARETURN
case other => case other =>
...@@ -102,13 +106,21 @@ object CompilationUnit { ...@@ -102,13 +106,21 @@ object CompilationUnit {
def compileProgram(p: Program): Option[CompilationUnit] = { def compileProgram(p: Program): Option[CompilationUnit] = {
implicit val env = CompilationEnvironment.fromProgram(p) implicit val env = CompilationEnvironment.fromProgram(p)
var classes = Seq[ClassFile]()
for((parent,children) <- p.algebraicDataTypes) { for((parent,children) <- p.algebraicDataTypes) {
val acf = compileAbstractClassDef(p, parent) val acf = compileAbstractClassDef(p, parent)
val ccfs = children.map(c => compileCaseClassDef(p, c)) val ccfs = children.map(c => compileCaseClassDef(p, c))
classes = classes :+ acf
classes = classes ++ ccfs
} }
val mainClassName = defToJVMName(p, p.mainObject) val mainClassName = defToJVMName(p, p.mainObject)
val cf = new ClassFile(mainClassName, None) val cf = new ClassFile(mainClassName, None)
classes = classes :+ cf
cf.addDefaultConstructor cf.addDefaultConstructor
cf.setFlags(( cf.setFlags((
...@@ -136,6 +148,6 @@ object CompilationUnit { ...@@ -136,6 +148,6 @@ object CompilationUnit {
compileFunDef(funDef, m.codeHandler) compileFunDef(funDef, m.codeHandler)
} }
Some(new CompilationUnit(p, cf, env)) Some(new CompilationUnit(p, classes, env))
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment