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

External Lab - Mauricio Molina #11

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
1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.5.0")
3 changes: 3 additions & 0 deletions src/main/scala/calculator/ir/AST.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ sealed abstract class Expr extends AST

case class Num(n: Int) extends Expr
case class Plus(left: Expr, right: Expr) extends Expr
case class Minus(left: Expr, right: Expr) extends Expr
case class Mult(left: Expr, right: Expr) extends Expr
case class Div(left: Expr, right: Expr) extends Expr
3 changes: 3 additions & 0 deletions src/main/scala/calculator/ir/sugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ package object ir {
// take the right operand and returns the appropriate Expr
implicit class ExprBuilder(val left: Expr) {
def |+|(right: Expr) = Plus(left, right)
def |-| (right: Expr) = Minus(left, right)
def |*| (right: Expr) = Mult(left, right)
def |/| (right: Expr) = Div(left, right)
}
}
5 changes: 5 additions & 0 deletions src/main/scala/calculator/parser/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ object CalcParser extends JavaTokenParsers with PackratParsers {
// expressions
lazy val expr: PackratParser[Expr] =
( expr~"+"~fact ^^ {case l~"+"~r ⇒ l |+| r}
| expr~"-"~fact ^^ {case l~"-"~r ⇒ l |-| r}
| expr~"*"~fact ^^ {case l~"*"~r ⇒ l |*| r}
| expr~"/"~fact ^^ {case l~"/"~r ⇒ l |/| r}
| fact )



// factors
lazy val fact: PackratParser[Expr] =
number
Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/calculator/semantics/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ package object semantics {
def eval(ast: AST): Int = ast match {
case Num(i) ⇒ i
case Plus(left, right) ⇒ eval(left) + eval(right)
case Minus(left, right) ⇒ eval(left) - eval(right)
case Mult(left, right) ⇒ eval(left) * eval(right)
case Div(left, right) ⇒ eval(left) / eval(right)
}
}
36 changes: 36 additions & 0 deletions src/test/scala/calculator/parser/ParserTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,41 @@ class CalcParserTests extends FunSpec with LangParseMatchers[AST] {
program("1 + 2 + 100") should parseAs ( (1 |+| 2) |+| 100 )
}

}

describe("Subtraction") {

it("can subtract two numbers") {
program("1-1") should parseAs ( 1 |-| 1 )
}

it("can be chained (and is left-associative)") {
program("1 - 2 - 100") should parseAs ( (1 |-| 2) |-| 100 )
}

}

describe("Multiplication") {

it("can multiply two numbers") {
program("1*1") should parseAs ( 1 |*| 1 )
}

it("can be chained (and is left-associative)") {
program("1 * 2 * 100") should parseAs ( (1 |*| 2) |*| 100 )
}

}

describe("Division") {

it("can add divide numbers") {
program("1/1") should parseAs ( 1 |/| 1 )
}

it("can be chained (and is left-associative)") {
program("1 / 2 / 100") should parseAs ( (1 |/| 2) |/| 100 )
}

}
}
45 changes: 45 additions & 0 deletions src/test/scala/calculator/semantics/SemanticsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,50 @@ class NumSemanticsTests extends FunSpec
}

}
describe("Subtraction") {

it("can subtract two numbers") {
program("1-1") should compute (0)
}

it("can be chained (and is left-associative)") {
program("100 - 2 - 1") should compute (97)
}

it("can handle negative numbers") {
program("1 - -1") should compute (2)
}

}

describe("Multiplication") {

it("can add multiply numbers") {
program("3*5") should compute (15)
}

it("can be chained (and is left-associative)") {
program("1 * 3 * 150") should compute (450)
}

it("can handle negative numbers") {
program("6 * -1") should compute (-6)
}

}
describe("Division") {

it("can add divide numbers") {
program("100/10") should compute (10)
}

it("can be chained (and is left-associative)") {
program("100/10/2") should compute (5)
}

it("can handle negative numbers") {
program("60/-5") should compute (-12)
}

}
}