CMSC 330, Fall 2016
Organization of Programming Languages
Project 4 Part B – Small C Interpreter
Introduction
In project 4 Part A, you parsed SmallC code. In this part, you will write an interpreter for SmallC. Your interpreter can execute the SmallC code represented as an AST, which is generated by the parser in part A.
This is a new project. If you find any error in the description or in the test files, report it to the instructor. Make sure you check the piazza announcements and errata section of the project periodically.
Getting Started
Download the following archive file p4b.zip and extract its contents.
Along with files used to make direct submissions to the submit server (submit.jar, .submit, submit.rb), you will find the following project files:
- Public test input files
- Expected outputs for interpreter tests
- public_eval_assign-exp.out
- public_eval_assign1.out
- public_eval_define1.out
- public_eval_equal.out
- public_eval_exp1.out
- public_eval_exp2.out
- public_eval_greater.out
- public_eval_if-else-while.out
- public_eval_if.out
- public_eval_ifelse.out
- public_eval_less.out
- public_eval_main.out
- public_eval_nested-if.out
- public_eval_nested-ifelse.out
- public_eval_nested-while.out
- public_eval_print.out
- public_eval_test1.out
- public_eval_test2.out
- public_eval_test3.out
- public_eval_test4.out
- public_eval_while.out
- public_eval_test-assoc1.out
- Utility Code
- smallc.ml – your parser in project 4 part A.
- evaluate.ml – your interpreter
- eval.ml – uses your interpreter to execute smallC program. ( ocaml eval.ml main.c )
- Test Script – test_all.rb
- Testing instructions: README
SmallC Interpreter
Put your solution to this part in the to do section of file evalate.ml.
Your task is to write an interpreter for SmallC program, which in this case will be a function that evaluates a SmallC abstract syntax tree (AST).
What you will do: You will write a function eval that, given an environment and an AST, executes the SmallC function corresponding to that AST in the given environment. The type of eval is env -> ast ->env, where the first argument env is an environment, the second is the AST, and the result is an environment. The environment can be (string * value) list or (string, value_type) Hashtbl. It holds all defined variables and their values. You can use OCaml HashTbl module. When you implement your interpreter:
- Variables must be defined and initialized before they are used. Your interpreter throws exception when a variables is used before it is defined and initialized. You can do this by looking up the variable from your environment.
- A variable cannot be defined twice. Your interpreter throws exception if a variable is defined second time. You can do this by checking if a variable exists in your environment.
- Only printf statement produces output. “printf(x)” is equivalent to “printf(“%d\n”,x)” in C. Any SmallC code with no printf does not produce output.
Note that what you will implement for this part corresponds very closely to the operational semantics for OCaml-like programs give in lecture, so that may serve as a good reference (and this project may serve as a good way to understand that lecture better).
For example,
int main(){ int a; a = 10; int b; b = 1; int c; c = a + b; printf(c); }
output is
11