-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
enum Expression { | ||
case Literal(value: Double) | ||
case Addition(left: Expression, right: Expression) | ||
case Subtraction(left: Expression, right: Expression) | ||
case Multiplication(left: Expression, right: Expression) | ||
case Division(left: Expression, right: Expression) | ||
|
||
def +(that: Expression): Expression = | ||
Addition(this, that) | ||
|
||
def -(that: Expression): Expression = | ||
Subtraction(this, that) | ||
|
||
def *(that: Expression): Expression = | ||
Multiplication(this, that) | ||
|
||
def /(that: Expression): Expression = | ||
Division(this, that) | ||
|
||
def eval: Double = | ||
this match { | ||
case Literal(value) => value | ||
case Addition(left, right) => left.eval + right.eval | ||
case Subtraction(left, right) => left.eval - right.eval | ||
case Multiplication(left, right) => left.eval * right.eval | ||
case Division(left, right) => left.eval / right.eval | ||
} | ||
} | ||
object Expression { | ||
def apply(value: Double): Expression = | ||
Literal(value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters