Skip to content

CrocodielRussian/Compiler

Repository files navigation

OCaml risc-v License logo

Oberon Compiler

Description

This project is simple compiler of programmer language Oberon. It do the parse of Oberon code and build AST of that. After, It translte that AST in RISC-V AST. In finnally, it generate RISC-V code for execute of programm.

(back to top)

Getting started

To run building application, and execute it commands:

Clone this repo

git clone https://github.com/CrocodielRussian/Compiler

Download dependencies

RISC-V AS and LD, GDB Multiarch, qemu-user

sudo apt install --yes binutils-riscv64-linux-gnu gdb-multiarch qemu-user

Dune and Opam

opam pin add ./ -n
opam install . --deps-only --with-test --with-doc

Build

dune build

Create source bin/main.clang and write some Oberon code

def main() {
	var a := read_int();
	var b := read_int();
	print_int(a + b);
	return 0;
}

Execution

dune exec compiler -- bin/main.clang lang/main.s --compile
make run

Tests

dune test

(back to top)

Code examples

Summary of two numbers

def main() {
	var a := read_int();
	var b := read_int();
	print_int(a + b);
	return 0;
}

Summary of 1 to n numbers

def nsum(n) {
	if n <= 0 then 
		return 0;
	endif
	return n + nsum(n - 1);
}

def main() {
	var n := read_int();
	print_int(nsum(n));
	return 0;
}

Factorial of n

def factorial(n) {
	var acc:=1;
	while n>1 do
		acc:=acc*n;
		n:=n-1;
	done
	return acc;
}

def main() {
	var n := read_int();
	print_int(factorial(n));
	return 0;
}

Fibonacci of n

def fibonachi(n) {
	var a:=0; var b:=1;
	while n>1 do
		b:=a+b;
		a:=b-a;
		n:=n-1;
	done
	return b;
}

def main() {
	var n := read_int();
	print_int(fibonachi(n));
	return 0;
}

(back to top)

Oberon grammar

Expressions

<add-operation> = + | -
<mult-operation> = * | / | %
<integer> = [0-9]+
<indetifier> = [a-zA-Z_]+
<unary-expression> = +<unit-expression> | -<unit-expression> | !<unit-expression>
<unit-expression> = <unary> | (<expression>) | <integer> | <indetifier> | <func-call>
<func-call> = <indetifier>(<expression-list>)
<mult-expression> = <unary-expression> | <unary-expression><mult-operation><mult-expression>
<math-expression> = <mult-expression> | <mult-expression><add-operation><math-expression>
<compare-expression> = <math-expression> | <math-expression><compare-operation><compare-expression>
<bool-operation> = && | ||
<bool-expression> = <compare-expression> | <compare-expression><bool-operation><bool-expression>
<assign-operation> = := | += | -= | *= | /=
<assign-expression> = <indetifier><assign-operation><expression>
<expression> = <bool-expression> | <assign-expression>
<expression-list> = e | <expression>,<expression-list>

Statements

<break-statement> = break
<return-statement> = return <expression>;
<if-statement> = if <expression> then <statement-list> else <statement-list> endif
<while-statement> = while <expression> do <statement-list> done
<expression-statement> = <expression>;
<var-init-statement> = var <indetifier> := <expression>;
<statement> = <var-init-statement> | <expression-statement> | <while-statement> | <if-statement> | <return-statement> | <break-statement>
<statement-list> = e | <statement><statement-list>

Structures

<func-define-struct> = def <indetifier>(var-list){<statement-list>}
<struct> = <func-define-struct>
<struct-list> = e | <struct-list>

Program

<program> = <struct-list>

(back to top)

Authors

(back to top)

License

Distributed under the MIT License. See LICENSE for more information.

(back to top)

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •