aboutsummaryrefslogtreecommitdiff
path: root/doc/type-system.tex
blob: e1497beb3b28d4fb745caddc8698bc665d346f70 (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
131
\part{Type system}

An Elna program consists of one or more source files, called \textbf{modules}.

Each module can declare \textbf{types}, \textbf{global variables} and
\textbf{procedures}, used by this module or exported to be used by other
modules.

\begin{grammar}
<type> = <array-type>
	\alt{} <pointer-type>
	\alt{} <record-type>
	\alt{} <enumeration-type>
	\alt{} <procedure-type>
	\alt{} <identifier>.
\end{grammar}

\chapter{Primitive types}

\begin{itemize}
	\item Pointer
	\item Word
	\item Int
	\item Bool
	\item String
	\item Char
\end{itemize}

\chapter{Pointer types}

\begin{grammar}
<pointer-type> = `^' <type>.
\end{grammar}

\begin{lstlisting}[caption=Example]
var
  x: Int;
  y: ^Int;
begin
  y := @x;
  y^ := 0
end.
\end{lstlisting}

\chapter{Static array}

\begin{grammar}
<array-type> = `[' <expression> `]' <type>.
\end{grammar}

\begin{lstlisting}[caption=Example]
var
  array: [3]Int := [1, 2, 3];
begin
  array[1] := array[2]
end.
\end{lstlisting}

\chapter{Procedure types}

\begin{grammar}
<identifiers> = <identifier> \{`,' <identifier>\}.

<parameter> = <identifiers> `:\@' <type>.

<procedure-heading> = `(' [<parameter> \{`;' <parameter>\}] `)' <return-declaration>.

<procedure-body> = <constant-part> <variable-part> <statement-part> `end'.

<procedure-declaration> = `proc' <identifier-definition> <procedure-heading> \\
	(<procedure-body> | `extern').

<return-declaration> = [`:\@' `!\@' | `:\@' type].

<procedure-type> = `proc' <procedure-heading>.
\end{grammar}

\begin{lstlisting}[caption=Example]
var
  a: proc(Int) -> Int;

proc f(x: Int) -> Int;
end;

begin
  a := f;
  a(0)
end.
\end{lstlisting}

\chapter{Records}

\begin{grammar}
<field> = <identifiers> `:\@' <type>.

<record-type> = `record' [`(' <identifier> `)'] [<field> \{`;' <field>\}] `end'.
\end{grammar}

\begin{lstlisting}[caption=Example]
type
  T = record
    x: Int
  end;
  U = record(T)
    y: Int;
    z: Int
  end;

var
  u: U;
begin
  u := U(0, 1, 2);
  u.x := 3
end.
\end{lstlisting}

\chapter{Enumerations}

\begin{grammar}
<enumeration-type> = `(' <identifiers> `)'.
\end{grammar}

\begin{lstlisting}[caption=Example]
type
  E = (one, two, three);
var
  e: E;
begin
  e := E.one
end.
\end{lstlisting}