Skip to content

Jakubas/Compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

72 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to Build

You need ocamlbuild, menhir, and make installed to build the compiler.
To build the compiler use the command:
make

How to Test

The test script code is contained in eval_test.ml and parse_test.ml.
The test cases are in the subdirectories of the /test directory.


Running 'make tests' will execute all the regression tests against
the parser, evaluator, optimiser, and x86 codegen

Language Syntax

Cheat Sheet

Syntax Meaning
+ addition
- subtraction
* multiplication
/ division
<= lesser than or equal
>= greater than or equal
== equal
!= not equal
& logical and
| logical or
! logical not
*x dereferencing of identifier 'x'
x identifer 'x'
1 / 2 opcode application
x = *x + 1; 4 + 4 sequence of expressions
while (*i >= 0) { i = *i - 1 } while loop
if (*x == *y) {x = *x + 1} else {y = *y + 1} if else statement
val = 5 assignment of '5' to identifier 'val'
square(a;b) function application
6 constant of int
readInt() read int from input
printInt(3) print '3' to output
x = *x + 1 assign right side of assignment to identifier 'x'
final int x = 6; create a non-mutable 'x'
int i = 0; create a mutable 'i'

Extra Notes: < br />Semicolons after and before right brackets ('}') can be omitted or included. < br />So these three examples are parsed the same:

example1() {
  x = 0;
  while (*x <= 1) {
    x = 5;
  }
  5;
}

example2() {
  x = 0;
  while (*x <= 1) {
    x = 5;
  };
  5
}

example3() {
  x = 0;
  while (*x <= 1) {
    x = 5;
  };
  5;
}

Each syntax construct is documented in the following way:
Concrete Syntax: the syntax of the construct
Abstract Syntax: The corresponding syntax from the abstract syntax tree
Example(s): One or more code snippets of the syntax

Opcodes

The concrete syntax in this section is derived from the opcode type in the abstract syntax tree.

Concrete Syntax: +
Abstract Syntax: Plus
Example(s): 1 + 1

Concrete Syntax: -
Abstract Syntax: Minus
Example(s): 2 - 5

Concrete Syntax: *
Abstract Syntax: Times
Example(s): -1 * 5

Concrete Syntax: /
Abstract Syntax: Divide
Example(s): 6 / *y

Concrete Syntax: <=
Abstract Syntax: Leq
Example(s): *x <= 5

Concrete Syntax: >=
Abstract Syntax: Geq
Example(s): *y >= *k

Concrete Syntax: ==
Abstract Syntax: Equal
Example(s): 6 == 5

Concrete Syntax: !=
Abstract Syntax: Noteq
Example(s): 5 != *x

Concrete Syntax: &
Abstract Syntax: And
Example(s): *x >= 5 & *y <= 6

Concrete Syntax: |
Abstract Syntax: Or
Example(s): *x >= 2 | *y <= 3

Concrete Syntax: !e
Abstract Syntax: Not
Example(s): !*x == 5

Expressions

The concrete syntax in this section is derived from the expression type in the abstract syntax tree.
e is shorthand for expression
op is shorthand for opcode
i is shorthand for int
str is shorthand for string

Concrete Syntax: e;e
Abstract Syntax: Seq(e,e)
Example(s): x = *x + 1; *x + 6

Concrete Syntax: while (e) {e}
Abstract Syntax: While(e,e)
Example(s): while (*i >= 0) { i = *i - 1 }

Concrete Syntax: if (e) {e} else {e}
Abstract Syntax: If(e,e,e)
Example(s): if (*x >= *y) {x = *x + 1} else {y = *y + 1}

Concrete Syntax: e = e
Abstract Syntax: Asg(e,e)
Example(s): val = 6

Concrete Syntax: *e
Abstract Syntax: Deref(e)
Example(s): *x

Concrete Syntax: e op e
Abstract Syntax: Operator(op,e,e)
Example(s): 1 + 3; -6 * 5; *x >= -6

Concrete Syntax: op e
Abstract Syntax: Negate(op,e)
Example(s): !x; !m

Concrete Syntax: e(e)
Abstract Syntax: Application(e,e)
Example(s): square(a;b)

Concrete Syntax: 6
Abstract Syntax: Const(i)
Example(s): 5; 105; -542

Concrete Syntax: readInt()
Abstract Syntax: ReadInt
Example(s): readInt()

Concrete Syntax: printInt(e)
Abstract Syntax: Printint(e)
Example(s): printInt(5)

Concrete Syntax: x
Abstract Syntax: Identifier(string)
Example(s): cal; num;

Concrete Syntax: final int e = e;
Abstract Syntax: Let(str,e,e)
Example(s): final int x = 6 + 6;

Concrete Syntax: int e = e;
Abstract Syntax: New(str,e,e)
Example(s): int i = 0;

Fundef

Concrete Syntax: funName (stringArg1;stringArg2) {e}
Abstract Syntax: string * string list * expression
Example(s): add(a;b;c;d) { *a + *b + *c + *d }

Program

Concrete Syntax: fundef list
Abstract Syntax: fundef list
Example(s): main() { 1 } main2(c) { *c + -6 }

About

A compiler for my own language built on x86 instructions

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors