Skip to content

Commit

Permalink
Merge branch 'TheMasterGame0-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
rbonifacio committed Jul 1, 2024
2 parents ef997dc + af2214d commit 4539c50
Show file tree
Hide file tree
Showing 7 changed files with 700 additions and 593 deletions.
15 changes: 12 additions & 3 deletions src/main/scala/br/unb/cic/oberon/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import br.unb.cic.oberon.codegen.{
}
import br.unb.cic.oberon.interpreter._
import br.unb.cic.oberon.parser.Oberon2ScalaParser

import br.unb.cic.oberon.ir.ast._
import br.unb.cic.oberon.tc.TypeChecker
import br.unb.cic.oberon.environment.Environment

import br.unb.cic.oberon.repl.REPL
import org.rogach.scallop._
import org.rogach.scallop.exceptions
Expand Down Expand Up @@ -117,13 +121,18 @@ object Main extends App with Oberon2ScalaParser {
val content = Files.readString(conf.tc.inputPath.get.get)
val module = parseAbs(parse(oberonParser,content))

val visitor = new TypeChecker()
val errors = visitor.checkModule(module)

// Alterar a instanciação do TypeChecker para fazer o checkModule ser ´parte do construtor
// Dessa forma, os val visitor e errors passam a ser o mesmo.
val env = new Environment[Type]()
val visitor = new TypeChecker(env)
val errors = visitor.checkModule(module).runA(env).value.written

if (errors.isEmpty) {
println("The code is correctly typed")
} else {
println("Type errors detected:")
errors.filter(v => (v._2 != "None")).foreach(v => println(v))
errors.filter(v => (v != "None")).foreach(v => println(v))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import br.unb.cic.oberon.ir.ast.{
}
import br.unb.cic.oberon.ir.jimple._
import br.unb.cic.oberon.tc.{ExpressionTypeChecker, TypeChecker}
import br.unb.cic.oberon.environment.Environment

import scala.collection.mutable.ListBuffer

Expand All @@ -39,12 +40,13 @@ object JimpleCodeGenerator extends CodeGenerator[ClassDeclaration] {
}

def generateConstants(module: OberonModule): List[Field] = {
val visitor = new ExpressionTypeChecker(new TypeChecker())
val env = new Environment[Type]()
val visitor = new ExpressionTypeChecker(new TypeChecker(env), env)

module.constants.map(constant =>
Field(
modifiers = List(PublicModifer, StaticModifier, FinalModifier),
fieldType = jimpleType(visitor.checkExpression(constant.exp), module),
fieldType = jimpleType(visitor.checkExpression(constant.exp, visitor.env).runA(visitor.env).value.value, module),
name = constant.name
)
)
Expand Down
29 changes: 16 additions & 13 deletions src/main/scala/br/unb/cic/oberon/codegen/TACodeGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package br.unb.cic.oberon.codegen
import br.unb.cic.oberon.ir.ast.{Constant => ASTConstant, _}
import br.unb.cic.oberon.ir.tac._
import br.unb.cic.oberon.tc.{ExpressionTypeChecker, TypeChecker}
import br.unb.cic.oberon.environment.Environment

object TACodeGenerator extends CodeGenerator[List[TAC]] {

private var tc = new TypeChecker()
private var expVisitor = new ExpressionTypeChecker(tc)
var env = new Environment[Type]()
private var tc = new TypeChecker(env)
private var expVisitor = new ExpressionTypeChecker(tc, env)

private val typeByteSize: Map[Type, Int] =
Map(IntegerType -> 4,
Expand All @@ -29,7 +31,7 @@ object TACodeGenerator extends CodeGenerator[List[TAC]] {
val (t, insts1) = generateExpression(exp, insts)
designator match {
case VarAssignment(varName) =>
val v = Name(varName, expVisitor.checkExpression(exp).get)
val v = Name(varName, expVisitor.checkExpression(exp, expVisitor.env).runA(expVisitor.env).value.value.get)
insts1 :+ CopyOp(t, v, "")
case ArrayAssignment(array, index) =>
val (a, insts2) = generateExpression(array, insts1)
Expand Down Expand Up @@ -278,15 +280,15 @@ object TACodeGenerator extends CodeGenerator[List[TAC]] {
case NullValue =>
return (Constant("Null", NullType), insts)

case VarExpression(name) =>
return (Name(name, expVisitor.checkExpression(expr).get), insts)
case VarExpression(name) =>
return (Name(name, expVisitor.checkExpression(expr, tc.env).runA(tc.env).value.value.getOrElse(UndefinedType)), insts)

case AddExpression(left, right) =>
val (t, l, r, insts2) = generateBinaryExpression(
left,
right,
insts,
expVisitor.checkExpression(expr).get
expVisitor.checkExpression(expr, expVisitor.env).runA(expVisitor.env).value.value.get
)
return (t, insts2 :+ AddOp(l, r, t, ""))

Expand All @@ -295,7 +297,7 @@ object TACodeGenerator extends CodeGenerator[List[TAC]] {
left,
right,
insts,
expVisitor.checkExpression(expr).get
expVisitor.checkExpression(expr, expVisitor.env).runA(expVisitor.env).value.value.get
)
return (t, insts2 :+ SubOp(l, r, t, ""))

Expand All @@ -304,7 +306,7 @@ object TACodeGenerator extends CodeGenerator[List[TAC]] {
left,
right,
insts,
expVisitor.checkExpression(expr).get
expVisitor.checkExpression(expr, expVisitor.env).runA(expVisitor.env).value.value.get
)
return (t, insts2 :+ MulOp(l, r, t, ""))

Expand All @@ -313,7 +315,7 @@ object TACodeGenerator extends CodeGenerator[List[TAC]] {
left,
right,
insts,
expVisitor.checkExpression(expr).get
expVisitor.checkExpression(expr, expVisitor.env).runA(expVisitor.env).value.value.get
)

return (t, insts2 :+ DivOp(l, r, t, ""))
Expand Down Expand Up @@ -403,11 +405,11 @@ object TACodeGenerator extends CodeGenerator[List[TAC]] {
case ArraySubscript(array, index) =>
val (a, insts1) = generateExpression(array, insts)
val (i, insts2) = generateExpression(index, insts1)
val t = new Temporary(expVisitor.checkExpression(expr).get)
val t = new Temporary(expVisitor.checkExpression(expr, expVisitor.env).runA(expVisitor.env).value.value.getOrElse(NullType))
return (t, insts2 :+ ArrayGet(a, i, t, ""))
case PointerAccessExpression(name) =>
val p = Name(name, LocationType)
val t = new Temporary(expVisitor.checkExpression(expr).get)
val t = new Temporary(expVisitor.checkExpression(expr, expVisitor.env).runA(expVisitor.env).value.value.getOrElse(NullType))
return (t, insts :+ GetValue(p, t, ""))

case FieldAccessExpression(record, field) =>
Expand Down Expand Up @@ -515,8 +517,9 @@ object TACodeGenerator extends CodeGenerator[List[TAC]] {
}

def reset(): Unit = {
tc = new TypeChecker()
expVisitor = new ExpressionTypeChecker(tc)
var env = new Environment[Type]()
tc = new TypeChecker(env)
expVisitor = new ExpressionTypeChecker(tc, env)
Temporary.reset
LabelGenerator.reset
}
Expand Down
Loading

0 comments on commit 4539c50

Please sign in to comment.