An sml based project that parses, checks types and evaluates a limited-scope programming language.
Part of assignments for the course COL226: Programming Languages in second semester 2021, IIT Delhi.
The problem statements corresponding to these assignments can be found here: assignment2 and assignment3.
The compiler was built to lex, parse, type-check and evaluate a language very similar to sml. The scope was constrained to consider only integer, boolean and string operations.
- Supports integer, boolean and string data types
- Supports simple arithmetic (NEGATE, +, -, x) and boolean operations (AND, NOT, OR, XOR)
- Suports if...then...else...fi type conditional statements
- Supports let...in...end type declaration function
- Supports simple one argument functions
A compiler can roughly be divided into 3 main stages: Lexer/Parser, Type-Checker/Evaluator and Intermediate Representation generator. In this repository, the first two stages are built for a limited scope language of boolean and arithmetic operations.
In simple terms, the lexer effectively goes through the entire program and creates "tokens" (key words, variables, numbers etc.). The tokens can be of a set of different types and this set of tokes defines the syntax rules of the language. The lexer converts a program file into a sequence of tokens, which can be processed further by the parser.
The parser then processes this sequence of tokens to generate the parse tree and the abstract syntax tree. The next stage of Type-checker/Evaluator works using the abstract syntax tree provided by the parser.
The type checker verifies the type-based sytactic rules of the language. This limited scope language is strictly typed, with no implicit type casting enabled. So bool = 5
would throw an error in type checking.
After the type-checker's checks have passed, the program is evaluated. Using lookup tables to get variable values and other methods, the value of each statement of the program is evaluated!
Clone the repo, go into the src directory and then, make run
.
More detailed instructions can be found inside the src directory readme.