Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
rbonifacio authored Jul 1, 2024
2 parents 85df21d + 0cda131 commit 6da8c42
Show file tree
Hide file tree
Showing 21 changed files with 1,122 additions and 934 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.0" % "test"
libraryDependencies += "org.scalatest" %% "scalatest-featurespec" % "3.2.0" % "test"
libraryDependencies += "org.typelevel" %% "paiges-core" % "0.3.0"

libraryDependencies += "org.typelevel" %% "cats-core" % "2.10.0"

libraryDependencies += "org.scala-graph" %% "graph-core" % "1.13.2"
libraryDependencies += "org.scala-graph" %% "graph-dot" % "1.13.0"
Expand Down
27 changes: 27 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
description = "An implementation of the Oberon language in Scala";

inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
};

outputs = { self, nixpkgs }:
let
build_for = system:

let
pkgs = import nixpkgs { inherit system; };
in

pkgs.stdenv.mkDerivation {
name = "hello";
src = self;
buildInputs = [
pkgs.oh-my-zsh
pkgs.zsh
pkgs.jdk11
(pkgs.sbt.override { jre = pkgs.jdk11; })
];
};
in {
packages.x86_64-linux.default = build_for "x86_64-linux";
packages.x86_64-darwin.default = build_for "x86_64-darwin";
};
}
19 changes: 19 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
let
nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-23.11";
pkgs = import nixpkgs { config = {}; overlays = []; };
in

pkgs.mkShellNoCC {
packages = with pkgs; [
cowsay
lolcat
sbt
metals
];

GREETING = "Hello, Nix!";

shellHook = ''
echo $GREETING | cowsay | lolcat
'';
}
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 @@ -58,7 +58,7 @@ object JVMCodeGenerator extends CodeGenerator[String] {

constants.map {
case (constant) =>
val (_, v) = interpreter.evalExpression(env, constant.exp)
val v = interpreter.evalExpression(constant.exp).runA(env).value

v match {
case IntValue(value) => {
Expand Down
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 6da8c42

Please sign in to comment.