gzybola / latte-compiler

Implementation of a compiler for a simple imperative language Latte

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Latte compiler

₊̣̇.ෆ˟̑̑˚̑̑˟̑ෆ.₊̣̇.ෆ˟̑̑˚̑̑˟̑ෆ.₊̣̇.ෆ˟̑̑˚̑̑˟̑ෆ.₊̣̇.ෆ˟̑̑˚̑̑˟̑ෆ.₊̣̇..ෆ˟̑̑˚̑̑˟̑ෆ.₊̣̇.ෆ˟̑̑˚̑̑˟̑ෆ.₊̣̇.ෆ˟̑̑˚̑̑˟̑̑˟̑ෆ.₊̣̇.ෆ˟̑̑˚̑*̑˟̑
Implementation of a compiler for a simple imperative language Latte

Latte language

Latte is an imperative language, almost a subset of Java and can be easily translated to it.
A Latte program is a sequence of function definitions. A function definition has a value type, a name, an argument list, and a body consisting of statements.

Names in function definitions must be different.

One function must have the name main. Its value type must be int and its argument list empty:

int main () { ... } ;

All functions should return a value of their value type. Void functions without return statements are allowed.

Functions can call each other.
Full grammar can be found latte

Example: Hello world

// Hello world 

int main () {
  printString("hello world") ;
  return 0 ;
}

Example: factorial in two ways

  printInt(fact(7)) ;
  printInt(factr(7)) ;
  return 0 ;
}

// iterative
int fact (int n) {
  int i,r ;
  i = 1 ;
  r = 1 ;
  while (i < n+1) {
    r = r * i ;
    i++ ;
  }
  return r ;
}

// recursive
int factr (int n) {
  if (n < 2) 
    return 1 ;
  else 
    return (n * factr(n-1)) ; 
}

More valid examples in good directory.
To test frontend some examples of incorrect programmes bad
Full description of requirements: latte

Installation

Use the package manager cabal to install hex and discrimination-0.4.

cabal install hex
cabal install discrimination-0.4
make

Usage

latc_x86 foo/bar/baz.lat for a correct program baz.lat should create files baz.s (assembly) and executtable a.out in the directory foo/bar

latc_x86 foo/bar/baz.lat

For an accepted program, the compiler must output OK ("OK\n") and exit with code 0.
For a rejected program, the first line of stderr must be ERROR ("ERROR\n"). Further lines should contain appropriate error messages. The compiler must then exit with a non-zero exit code.

₊̣̇.ෆ˟̑̑˚̑̑˟̑ෆ.₊̣̇.ෆ˟̑̑˚̑̑˟̑ෆ.₊̣̇.ෆ˟̑̑˚̑̑˟̑ෆ.₊̣̇.ෆ˟̑̑˚̑̑˟̑ෆ.₊̣̇..ෆ˟̑̑˚̑̑˟̑ෆ.₊̣̇.ෆ˟̑̑˚̑̑˟̑ෆ.₊̣̇.ෆ˟̑̑˚̑̑˟̑̑˟̑ෆ.₊̣̇.ෆ˟̑̑˚̑*̑˟̑

About

Implementation of a compiler for a simple imperative language Latte


Languages

Language:Haskell 97.1%Language:Yacc 2.1%Language:C 0.6%Language:Makefile 0.2%