From 0e856ca15d8aa1499edc33c7302d4d97be404290 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 8 Nov 2026 09:49:14 +0300 Subject: publish --- README.org | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 README.org (limited to 'README.org') diff --git a/README.org b/README.org new file mode 100644 index 0000000..8dd015a --- /dev/null +++ b/README.org @@ -0,0 +1,130 @@ +#+title: Refinement of the CMiLan compiler +#+subtitle: Adding function support +#+date: 2025H1 + +* Introduction +:PROPERTIES: +:CUSTOM_ID: introduction +:UNNUMBERED: toc +:END: +/Milan/ is an educational procedural programming language. The standard +distribution of the /CMilan/ compiler supports the definition of only +one procedure, which is the entry point of the program. In this +laboratory work, the compiler is extended to implement declarations, +definitions, and function calls. + +* Refinement description +:PROPERTIES: +:CUSTOM_ID: refinement-description +:END: +All functions must be declared before the entry point is defined, and +defined after it. In the function declarations, you need to specify +the number of arguments and indicate whether it returns a single value +or does not return anything. + +Several functions can have the same name if they have different +numbers of arguments (function-overloading). You can declare local +variables in functions, which shadow global variables. The compiler +eliminates tail calls. + +Obviously, some changes had to be made to the virtual machine as well, +i.e. stack framing. + +* Documentation +:PROPERTIES: +:CUSTOM_ID: documentation +:END: +The exported report can be found at [[https://git.michalec.dev/files/uni/term-06/math-logic/lab-04/report.pdf][this link]]. + +* Showcase +:PROPERTIES: +:CUSTOM_ID: showcase +:END: +** Doubling a number +*Source code:* +#+include: "src/double.mil" src text + +*Machine commands:* +#+include: "build/double.ms" src text + +*Usage:* +#+begin_src text +> 10 +20 +#+end_src + +** GCD +*Source code:* +#+include: "src/gcd.mil" src text + +*Machine commands:* +#+include: "build/gcd.ms" src text + +*Usage:* +#+begin_src text +> 9 +> 6 +3 +#+end_src + +** Factorial (w/o tail-call elimination) +*Source code:* +#+include: "src/fac.mil" src text + +*Machine commands:* +#+include: "build/fac.ms" src text + +*Usage:* +#+begin_src text +> 31 +738197504 +#+end_src + +** Factorial (w/ tail-call elimination) +*Source code:* +#+include: "src/fac-tail.mil" src text + +*Machine commands:* +#+include: "build/fac-tail.ms" src text + +*Usage:* +#+begin_src text +> 31 +738197504 +#+end_src + +** Fibonacci (w/o tail-call elimination) +*Source code:* +#+include: "src/fib.mil" src text + +*Machine commands:* +#+include: "build/fib.ms" src text + +*Usage:* +#+begin_src text +> 20000 +Error: stack overflow +Code: + + 14 CALL 4 +VM error +#+end_src + +** Fibonacci (w/ tail-call elimination) +*Source code:* +#+include: "src/fib-tail.mil" src text + +*Machine commands:* +#+include: "build/fib-tail.ms" src text + +*Usage:* +#+begin_src text +> 1000000 +1884755131 +#+end_src + +* Unlicense +:PROPERTIES: +:CUSTOM_ID: unlicense +:END: +#+include: UNLICENSE verse -- cgit v1.3.1