Due: Wednesday, November 29, 11:59pm Pacific Time
This homework is the next step in our Trefoil journey. You will implement an interpreter for version 3 of Trefoil, which extends version 2 to support functions and a few other features.
You should start by making a copy of your hw5 solution and then making the following changes:
trefoil2.ml
to trefoil3.ml
.open Trefoil2lib
open Trefoil3lib
trefoil2test.ml
to trefoil3test.ml
.open Trefoil2lib
open Trefoil3lib
src/dune
file with the updated file
from the HW6 starter
code.newtests.ml
file into your trefoil3test.ml
file. These
are additional provided tests for the new features.The Trefoil v3 language is an extension of Trefoil v2. We only describe the differences.
There is one new kind of binding, a function binding. We discussed it informally in lecture. For a formal description, see the Trefoil v3 language spec.
In order to support function bindings, the dynamic environment maps names to entries, which can be either a variable entry or a function entry, as discussed in lecture.
let
expressions to support binding multiple bindings as discussed in lecture.cond
expression for multiway branching as discussed in lecture.Extend your previous homework solution to implement the changes described in the Trefoil v3 language spec. Write tests that cover all the normal and error case behavior for all new language features.
We highly recommend keeping your tests from HW5 around to help you find bugs. You do not need to resubmit tests that you submitted for HW5, but you can if you want to.
After your Trefoil v3 interpreter is working, write a trefoil program of your
choosing that is at least 10 lines long and uses at least 2 new features from
v3. Put this in a file called hw6.trefoil
and turn it in.
Write a paragraph describing an idea for a final project. Both this task and the final project are optional. You can change your mind about whether to do the final project at any point.
Some ideas:
With all of these ideas, it would be easy to go overboard and propose a project that is too big. Try to propose something that you could do in a week while you are also busy with your other end-of-quarter tasks.
Describe your idea in one paragraph or so. Put your description in a file called proposal.txt
and turn it in.
Submitting a project proposal will earn you a small number of bonus points.
You can do anything in whatever order you want, but here is one suggested path.
Update the dynamic environment to support "entries":
type dynamic_env
at the top of interpreter.ml
to introduce a type
called env_entry
that is defined simultaneously with dynamic_env
.string_of_dynenv_entry
from the HW5 starter code so that it works with your new env_entry
type. This function is
called by trefoil3.ml
and your code will not build without it.Variable
case in interpret_expression
to take entries into account. VarBinding
case of interpret_binding
to take entries into account.Extend let
.
let
to support multiple variables.expr_of_pst
case for let
to support multiple variables and return your updated AST node.interpret_expression
case for let
to support multiple variables
according to the semantics in the spec.let
that you copied from HW5 so that they compile with the new AST node."multi var let"
, "no var let"
, and "let swap"
pass.let
that compares the output of expr_of_pst
to a
manually constructed AST. Be sure to exercise multiple variables in your test.let
expression that tries to define the same variable twice
within a single let
expression. The test should check that an abstract syntax error is thrown.let
.Implement cond
.
cond
.expr_of_pst
to parse cond
expressions.cond
PST. You can use
a locally defined recursive helper function, or you can use a higher order function
from the standard library.cond
and ensure it passes.interpret_expression
that implements the semantics of cond
.cond
AST. "basic cond"
, "empty cond"
, and "cond parsing malformed"
pass.cond
.Implement function bindings.
binding_of_pst
to parse function bindings.interpret_binding
that implements the semantics of function bindings.Implement function calls.
expr_of_pst
to parse function callsinterpret_expression
that implements the semantics of function calls."basic function"
"lexical scope"
"pow"
"car_cdr_countdown"
"sum_countdown"
"sum_cond"
Write some Trefoil.
Brainstorm project ideas.
Submit these files to Gradescope:
ast.ml
interpreter.ml
trefoil3test.ml
hw6.trefoil
proposal.txt
if you want to propose a project