aboutsummaryrefslogtreecommitdiff
path: root/README.org
blob: 8dd015a60618cf16b29522e15223fe8822c3a4d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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