Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding a new feature - MathEvaluation #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions oberon/src/main/scala/br/unb/cic/oberon/matheval/MathEval.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package br.unb.cic.oberon.matheval

import br.unb.cic.oberon._

class MathEval() extends Visitor {
type T = Integer

var res = 0

def getValue() = res

def visit(v : IntValue) {
res = res + v.value
}

def visit(v : BoolValue) {

if(v.value)
res = 1 //set 1 as True
else
res = 0 //set 0 as False
}

def visit(e : AddExp) {
res =
e.lhs.accept(this)
e.rhs.accept(this)
}

def visit(e : AndExp) {
e.lhs.accept(this)
e.rhs.accept(this)
res = res + e.lhs.value + e.rhs.value
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package br.unb.cic.oberon

import org.scalatest.FunSuite

import br.unb.cic.oberon.matheval._

class MathEvalTest extends FunSuite {

test("Evaluation of an Int10 is 10") {
val int10 = new IntValue(10)

val v = new MathEval()

int10.accept(v)

assert(10 == v.getValue())
}

test("Evaluation of an Add(Int(5), Int(6)) is 11") {
val int5 = new IntValue(5)
val int6 = new IntValue(6)

val add = new AddExp(int5, int6)

val v = new MathEval()

add.accept(v)

assert(11 == v.getValue())
}
}