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.
To run building application, and execute it commands:
git clone https://github.com/CrocodielRussian/Compilersudo apt install --yes binutils-riscv64-linux-gnu gdb-multiarch qemu-useropam pin add ./ -n
opam install . --deps-only --with-test --with-docdune builddef main() {
var a := read_int();
var b := read_int();
print_int(a + b);
return 0;
}
dune exec compiler -- bin/main.clang lang/main.s --compilemake rundune testdef main() {
var a := read_int();
var b := read_int();
print_int(a + b);
return 0;
}
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;
}
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;
}
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;
}
<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>
<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>
<func-define-struct> = def <indetifier>(var-list){<statement-list>}
<struct> = <func-define-struct>
<struct-list> = e | <struct-list>
<program> = <struct-list>
Distributed under the MIT License. See LICENSE for more information.
