IMP - Project
Simple imperative language
About
Implementation of simple compiler with the following functionalities:
- Parser :
- Parses a given Program, Statement, Expression,... to a proper AST that can be further processed
- For example:
1+0 -> plus(number(1), number(0))
- Type checker :
- Checks the types of the given Programm, Statement,...
- For example:
1 -> int
- Evaluator :
- Evaluates the given Expression
- For example:
1+0 -> 1
Syntax definition:
vars Variable names, start with lower-case letter
prog ::= block
block ::= "{" statement "}"
statement ::= statement ";" statement -- Command sequence
| vars ":=" exp -- Variable declaration
| vars "=" exp -- Variable assignment
| "while" exp block -- While
| "if" exp block "else" block -- If-then-else
| "print" exp -- Print
exp ::= 0 | 1 | -1 | ... -- Integers
| "true" | "false" -- Booleans
| exp "+" exp -- Addition
| exp "*" exp -- Multiplication
| exp "||" exp -- Disjunction
| exp "&&" exp -- Conjunction
| "!" exp -- Negation
| exp "==" exp -- Equality test
| exp "<" exp -- Lesser test
| "(" exp ")" -- Grouping of expressions
| vars -- Variables
Usage
- Build:
go build main.go
- Run:
./imp-project 'x := 1+2*3;'
(First parameter is Program to compile) - Run tests:
go test -v
References
- Based on: https://sulzmann.github.io/ModelBasedSW/imp.html (See lecture notes)
- Used Parser(Pratt Parser):