/imp-language

Simple imperative language

Primary LanguageGoMIT LicenseMIT

IMP - Project

Go

Simple imperative language

About

Implementation of simple compiler with the following functionalities:

  1. 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))
  1. Type checker :
  • Checks the types of the given Programm, Statement,...
  • For example: 1 -> int
  1. 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

  1. Build: go build main.go
  2. Run: ./imp-project 'x := 1+2*3;' (First parameter is Program to compile)
  3. Run tests: go test -v

References