You should be able to do sbt run
to run an initial version of the calculator
interpreter. You should also be able to do sbt test
to run some auto-generated
tests of the initial parser and interpreter.
You should be able to do sbt eclipse
to generate a ScalaIDE project. Then, you
can import the project in the usual way. Once in ScalaIDE, you can run the
interpreter by opening the file src/main/scala/calculator/calc.scala
and
running it.
Running tests in ScalaIDE: The ScalaCheck testing library
doesn't seem to work well with Eclipse yet. You'll probably want to run the
tests outside of Eclipse, using sbt
.
Extend the code to implement the following grammar:
n โ ๐ต
e โ Expr ::= e + t | e - t | t
t โ Term ::= t * f | t / f | f
f โ Fact ::= n | ( e )
It's best to add features in the following order:
- subtraction
- multiplication
- division
- parenthetical expressions
When you add a new feature to a language, extend the implemention in the following order:
- Intermediate representation
- Data structures
- Sugar (if you want)
- Parser
- Tests
- Implementation
- Semantics
- Tests
- Implementation
If you finish (or just want to explore things more), here are some options.
Can you give better error-checking and error messages for the parser? For the read-eval-print loop? You could also write one or more semantic checks: a pass over the AST to check for errors, e.g., divide by literal 0.
You could add more features to the language, including
- comparisons, e.g.,
<
,>
,=
, etc. - constants, e.g., built-in names such as
ฯ
ore
- variables: assignment and use
- functions (either with or without recursion)