Skip to content

Commit

Permalink
Merge pull request #155 from PedroABeneveli/master
Browse files Browse the repository at this point in the history
Grupo 6 - Monad para o Interpretador
  • Loading branch information
rbonifacio authored Jul 1, 2024
2 parents 4539c50 + 5e07115 commit 24ceb1c
Show file tree
Hide file tree
Showing 11 changed files with 347 additions and 343 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
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
414 changes: 208 additions & 206 deletions src/main/scala/br/unb/cic/oberon/interpreter/Interpreter.scala

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions src/main/scala/br/unb/cic/oberon/repl/OberonEngine.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import java.util.Collections
import scala.io.Source
import scala.jdk.CollectionConverters._
import scala.util.matching.Regex
import cats.implicits._

class OberonEngine extends ScriptEngine {
object Format extends Enumeration {
Expand Down Expand Up @@ -178,7 +179,7 @@ class OberonEngine extends ScriptEngine {
)
val coreModule = CoreTransformer.reduceOberonModule(module)

env = interpreter.runInterpreter(coreModule)
env = interpreter.run(coreModule)
null
}

Expand All @@ -189,11 +190,11 @@ class OberonEngine extends ScriptEngine {
val command = ScalaParser.parserREPL(statement)
command match {
case v: REPLVarDeclaration =>
env = v.declarations.foldLeft(env)((acc, b) => interpreter.declareVariable(acc, b))
env = v.declarations.traverse(b => for {_ <- interpreter.declareVariable(b)} yield ()).runS(env).value
case c: REPLConstant =>
env = interpreter.declareConstant(env, c.constants)
env = interpreter.declareConstant(c.constants).runS(env).value
case u: REPLUserTypeDeclaration =>
env = interpreter.declareUserDefinedType(env, u.userTypes)
env = interpreter.declareUserDefinedType(u.userTypes).runS(env).value
case s: REPLStatement =>
s.stmt match {
case AssignmentStmt(des, exp) =>
Expand All @@ -205,15 +206,15 @@ class OberonEngine extends ScriptEngine {
case VarAssignment(name) =>
put(name, exp)
}
case s: Statement => env = interpreter.executeStatement(env, CoreTransformer.reduceToCoreStatement(s))
case s: Statement => env = interpreter.executeStatement(CoreTransformer.reduceToCoreStatement(s)).runS(env).value
}
case e: REPLExpression => return expressionValue(e.exp)
}
null
}

private def expressionValue(exp: Expression): Any = {
val (e, result) = interpreter.evalExpression(env, exp)
val (e, result) = interpreter.evalExpression(exp).run(env).value
env = e
result match {
case v: Value => return v.value
Expand All @@ -229,7 +230,7 @@ class OberonEngine extends ScriptEngine {
case b: Boolean => BoolValue(b)
case e: Exception => StringValue(e.getMessage)
case e: Expression => {
val (env1, exp) = interpreter.evalExpression(env, e)
val (env1, exp) = interpreter.evalExpression(e).run(env).value
env = env1
exp
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ArithmeticTestSuite extends AnyFunSuite {

val interpreter = new Interpreter()
val env = new Environment[Expression]()
val (_, res) = interpreter.evalExpression(env, m)
val res = interpreter.evalExpression(m).runA(env).value
assert(expected == res)

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class EvalExpressionVisitorTest extends AnyFunSuite {
val bTrue = BoolValue(true)
val bFalse = BoolValue(false)

val (_, exp1) = interpreter.evalExpression(env, val10)
val exp1 = interpreter.evalExpression(val10).runA(env).value
assert(exp1 == val10)

val (_, exp2) = interpreter.evalExpression(env, bTrue)
val exp2 = interpreter.evalExpression(bTrue).runA(env).value
assert(exp2 == bTrue)

val (_, exp3) = interpreter.evalExpression(env, bFalse)
val exp3 = interpreter.evalExpression(bFalse).runA(env).value
assert(exp3 == bFalse)
}

Expand All @@ -32,7 +32,7 @@ class EvalExpressionVisitorTest extends AnyFunSuite {

val exp = AddExpression(val10, MultExpression(val20, val30))

val (_, exp1) = interpreter.evalExpression(env, exp)
val exp1 = interpreter.evalExpression(exp).runA(env).value
assert(exp1 == IntValue(610))
}

Expand All @@ -45,7 +45,7 @@ class EvalExpressionVisitorTest extends AnyFunSuite {

val exp = SubExpression(val20, DivExpression(val30, val10))

val (_, exp1) = interpreter.evalExpression(env, exp)
val exp1 = interpreter.evalExpression(exp).runA(env).value
assert(exp1 == IntValue(17))
}

Expand All @@ -56,29 +56,29 @@ class EvalExpressionVisitorTest extends AnyFunSuite {
val valFalse = BoolValue(false)
val exp = AndExpression(valTrue, AndExpression(valTrue, OrExpression(valTrue, valFalse)))

val (_, exp1) = interpreter.evalExpression(env, exp)
val exp1 = interpreter.evalExpression(exp).runA(env).value
assert(exp1 == valTrue)
}

test("Test eval on global variables") {
val interpreter = new Interpreter()
var env = new Environment[Expression]()
env = interpreter.setGlobalVariable(env, "x", IntValue(30))
env = interpreter.setGlobalVariable("x", IntValue(30)).runS(env).value

val exp = AddExpression(IntValue(10), VarExpression("x"))

val (_, exp1) = interpreter.evalExpression(env, exp)
val exp1 = interpreter.evalExpression(exp).runA(env).value
assert(exp1 == IntValue(40))
}

test("Test eval on local (stack) and global variables") {
val interpreter = new Interpreter()
var env = new Environment[Expression]()
env = interpreter.setGlobalVariable(env, "x", IntValue(30))
env = interpreter.setLocalVariable(env, "y", IntValue(10))
env = interpreter.setGlobalVariable("x", IntValue(30)).runS(env).value
env = interpreter.setLocalVariable("y", IntValue(10)).runS(env).value

val exp = AddExpression(VarExpression("x"), VarExpression("y"))
val (_, exp1) = interpreter.evalExpression(env, exp)
val exp1 = interpreter.evalExpression(exp).runA(env).value
assert(exp1 == IntValue(40))
}

Expand Down
Loading

0 comments on commit 24ceb1c

Please sign in to comment.