Project implementing a compiler in OCaml for CSC 312 at Grinnell college.
Implemented by Reilly Noonan Grant
This project is the begining of a compiler written in OCaml. It is being created primarily for educational purposes.
Currently, this project implements a parser, and lexer for support for a interpreter of a programing language based on OCaml.
Ensure that OCaml is installed
and ensure that #use "topfind";; #camlp4o;; #thread;; #require "core.top";; #require "core.syntax";; are in you .ocamlinit file
You can build the project with make
After making the project, you can run it by using compiler.native on a formated file. Examples of such files are provided in the tests directory with the extension .arith. Example test files
1 + 2
3
( + 1 2 )
where test.arith is the input, and test.out is the expected output, and test.parse.out is the expected output when the parse flag is enabled.
You can also run tests using make test, or running the run_test.sh script
We currently support the -parse flag, which will display the result of parsing the code in lisp syntax, the -step flag which will evaluate the code incrementally until a value is reached, and the -typecheck flag which evaluates the type of the program.
This project currently supports evaluation of a language with the following grammar
e ::= n | f | (e) | e1 + e2 | e1 - e2 | e1 * e2 | e1 /e2 .. | true | false | e1 <= e2 |e1 < e2|e1 = e2||e1 > e2 |e1 >= e2 .. | if e1 then e2 else e3 | NaN | e1 and e2 | e1 or e2 .. | let x : t = e1 in e2 | fun (x:t1) :t2 = e .. | fix f (y :t1) : t2 = e .. | e1 e2 |() | (e1,e2,..,en) | fst e | snd e| nth n e .. |[] : t | e1 :: e2 | hd e | tl e | empty e .. | ref e | e1 := e2 | !e | e1 ; e2 | while e1 do e2 end
t ::= int | bool | t1 -> t2 | () | t1 * t2* .. *tn |
Build on 03/16/18 First:
- Replaced Pairs with Tuples, a more general version
- Added the "nth" unary operator
- Updated types to reflect this
- Implemented above features
- Do not support nested variables with the same name
- Does not currently support currying
Build on 03/16/18 First:
- Updated Grammar in readme
- Added state: --Ref creates a ref cell, which can be derefrenced with .. ! to get the value assigned -- := assigns a ref cell to have a new value of the same type -- ; allows for sequences of expression, by evaluating the first .. expression, then evaluating the second and returning it -- Added while loops
- Updated tests to support new features
- Implemented above features
- Do not support nested variables with the same name
- Does not currently support currying
Build on 03/15/18:
- Implemented Typechecking
- Added Pairs, Lists, and the Unit type
- Added fst, snd for getting the first and second element of a pair
- Added hd and tl for getting the first element of a list, and getting .. all but the first element of the list
- Added empty for checking a list to see if it is empty
- Implemented above features
- Do not support nested variables with the same name
- Does not currently support currying
Build on 02/25/18:
- Added flag to the compiler that allows incremental evaluation
- Implemented above feature
- Changed evaluation to use the step by step evalution
- Do not support nested variables with the same name
Build on 02/23/18:
- Added Let binding,
- Added non recursive functions
- Added recursive functions
- Added support for entering NaN directly to in a source file
- Added support for entering Booleans directly to in a source file
- Added support for boolean operators and and or
- Added support for numerical comparisons other than <=
- Added above features
- Implemented the subst function to facilitate let bindings and functions
- Added new tests to support new features
- Do not support nested variables with the same name
Build on 02/20/18:
- Switched to an infix notation
- Changed implementation to use a lexer and .. parser generator
- Switched to infix notation
- No known bugs
Build on 02/07/18:
- Added support for floats, and NaN
- Added support for floats, and NaN
- No known bugs
Build on 02/06/18:
- Created a basic Command line interface.
- Takes arguments at the command line and returns them
- Given the -length flag returns the lengths of each argument
- Added ability to lex, parse and evaluate expressions given in file .. format
- Support for addition, multiplication, subtraction and division
- Support for booleans
- Support for the less than or equal operation
- Support for if expressions
- Catches type errors, and identifies the expression at fault
- Complete code overhaul, based partially on .. https://github.com/psosera/csc312-example-compiler
- No known bugs
Build on 01/29/18:
- Created a basic Command line interface.
- Takes arguments at the command line and returns them
- Given the -length flag returns the lengths of each argument
- First Build
- No known bugs