#+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