summaryrefslogtreecommitdiff
path: root/doc/language.tex
blob: 36ee877370db6fa5462a322a6cff3df4372224f9 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
\part{Language}

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.

Each procedure can get some input and produce an output as a result of
executing a \textbf{statement block}, a list, where each \textbf{statement}
is executed in the order it appears in the block.

\chapter{Vocabulary}

A language is an infinite set of sentences, namely the sentences well formed
according to its syntax. In Elna, these sentences are called compilation units.
Each unit is a finite sequence of \textit{tokens} from a finite vocabulary.
The vocabulary of Elna consists of identifiers, reserved words, numbers, characters,
strings, operators, delimiters, and comments. They are called \textit{tokens}
and are composed of sequences of characters.

The following lexical rules must be observed when composing tokens. Blanks and
line breaks must not occur within tokens (except in comments and strings). They
are ignored unless they are essential to separate two consecutive tokens.
Capital and lower-case letters are considered as being distinct.

\section{Identifiers}

\textit{Identifiers} are sequences of letters, digits and underscores. The first
character must be a letter or an underscore.

\begin{grammar}
<identifier> = <letter> \{<letter> | <decimal-digit>\}.
\end{grammar}

Examples:

\begin{itemize}
		\item \verb|x|
		\item \verb|TypeName|
		\item \verb|procedure_name|
\end{itemize}

\section{Numbers}

Numbers are signed or unsigned integers, or real numbers. Integers may be
preceded by a prefix and followed by a suffix. The prefixes \verb|0x| and
\verb|0X| indicate hexadecimal representation, \verb|0b| and \verb|0B|
indicate binary representation. Unsigned integers have the suffix \verb|u|,
signed integers have no suffix.

A \textit{real number} always contains a decimal point. Optionally it may
also contain a decimal scale factor. The letters \verb|e| or \verb|E| is
pronounced as `times ten to the power of'.

\begin{grammar}
<integer-literal> = `0' | <counting-digit> \{<decimal-digit>\}.

<word-literal> = <integer-literal> `u'
	\alt{} `0' (`X' | `x') <hex-digit> \{<hex-digit>\}
	\alt{} `0' (`B' | `b') <binary-digit> \{<binary-digit>\}.

<real-literal> = <integer-literal> `.\@' <decimal-digit> \{<decimal-digit>\}
	\alt{} <integer-literal>\} `e' [`+' | `-'] <decimal-digit> \{<decimal-digit>\}.
\end{grammar}

Examples:

\begin{itemize}
		\item 2016
		\item 1987u
		\item 0xff
		\item 0b101
		\item 0.5
		\item 4.567e8
\end{itemize}

\section{Strings and characters}

Single \textit{characters} are enclosed in single quotation marks
(\textquotesingle).\@ \textit{Strings} are sequences of characters enclosed in
double quotation marks (\textquotedbl). The number of characters in a string is
called the \textit{the length} of the string.

\begin{grammar}
<escaped-character> = `\\' \\
	(`n' | `t' | `f' | `r' | `v' | `\\' | `\textquotesingle' | `\textquotedbl' | `0').

<hex-character> = `\\x' <hex-digit> \{<hex-digit>\}.

<character> = <printable-character> | <escaped-character> | <hex-character>.

<character-literal> = `\textquotesingle' <character> `\textquotesingle'.

<string-literal> = `\textquotedbl' \{<character>\} `\textquotedbl'.
\end{grammar}

Alternatively, a single character may be represented by a
\textit{escape sequence} (see~\ref{table:escape}), a character combination
beginning with a backslash (\textbackslash).

\begin{table}[ht]
\centering
\begin{tabular}{r l}
	\textbf{Sequence} & \textbf{Meaning} \\
	\toprule
	\verb|\n| & Newline \\
	\midrule
	\verb|\t| & Horizontal tab \\
	\midrule
	\verb|\f| & Form feed \\
	\midrule
	\verb|\r| & Carriage return \\
	\midrule
	\verb|\v| & Vertical tab \\
	\midrule
	\verb|\\| & Backslash \\
	\midrule
	\verb|\'| & Single quote \\
	\midrule
	\verb|\"| & Double quote \\
	\midrule
	\verb|\0| & Null character \\
	\midrule
	\verb|\xh…| & Arbitrary hexadecimal value, where \verb|n| is a hexadecimal digit \\
	\bottomrule
\end{tabular}
\caption{Escape sequences}\label{table:escape}
\end{table}

Examples:

\begin{itemize}
		\item \verb|"String"|
		\item \verb|'c'|
		\item \verb|'\''|
		\item \verb|"\"multi\nline\nquoted\nstring\""|
\end{itemize}

\section{Operators and delimiters}

\textit{Operators} and \textit{delimiters} are the special characters, character
pairs, or reserved words listed below. These reserved words consist exclusively
of letters and cannot be used in the role of identifiers.

\begin{itemize}
	\item{}:=
	\item $@\quad\hat{}\quad\sim$
	\item $.\quad,\quad;\quad:\quad|$
	\item $<\quad>\quad>=\quad<=\quad<>\quad=$
	\item $+\quad-\quad*\quad/$
	\item $or\quad{}xor\quad\&$
	\item (\ and\ )
	\item \lbrack{} and \rbrack{}
	\item \{ and \}
	\item Pointer
	\item module
	\item import
	\item type
	\item const
	\item var
	\item begin
	\item end
	\item proc
	\item record
	\item while
	\item do
	\item case
	\item of
	\item if
	\item then
	\item elsif
	\item else
	\item cast
	\item return
	\item true
	\item false
	\item nil
\end{itemize}

\section{Comments}

\textit{Comments} may be inserted between any two tokens in a program. They
are arbitrary character sequences opened by the bracket \verb|(*| and closed
by \verb|*)|. Comments do not affect the meaning of a program.

\chapter{Expressions}

Expressions are constructs denoting rules of computation whereby constants and current values of
variables are combined to derive other values by the application of operators and function
procedures. Expressions consist of operands and operators. Parentheses may be used to express
specific associations of operators and operands.

\section{Literal constants}

\begin{grammar}
<literal> = <integer-literal> | <word-literal> | <real-literal>
	\alt{} <string-literal> | <character-literal>
	\alt{} `true' | `false' | `nil'.
\end{grammar}

Literal constants are

\begin{itemize}
	\item signed and unsigned integers,
	\item real numbers,
	\item booleans,
	\item characters,
	\item strings,
	\item and enumerations.
\end{itemize}

\section{Call expressions}

\subsection*{Procedure call}

\begin{grammar}
<procedure-call> = <designator> `(' [<expressions>] `)'.
\end{grammar}

If the designator object is a procedure followed by a (possibly empty) parameter list,
the designator implies an activation of the procedure and stands for the value resulting
from its execution. The (types of the) actual parameters must correspond to the formal
parameters as specified in the procedure's declaration.

\subsection*{Type cast}

\begin{grammar}
<cast> = `cast' `(' <expression> `:\@' <type> `)'.
\end{grammar}

The type of an object can be reinterpreted with a cast expression: \\
\verb|cast(object: Type)|.

\subsection*{Traits}

\begin{grammar}
<trait-identifier> = `#' <identifier>.

<trait> = <trait-identifier> `(' [<types>] `)'.
\end{grammar}

Traits allow to query some information about the types, like their size or
field offset or alignment. Calling a trait looks like a procedure call but
trait names start with a \verb|#| and their arguments are type expressions and
not value expressions.

Supported compiler traits:

\begin{itemize}
	\item \verb|#size(T)| queries type size.
	\item \verb|#align(T)| queries type alignment.
	\item \verb|#offset(T, F)| queries the offset of the field \verb|F| in the record \verb|T|.
\end{itemize}

\section{Object designators}

\begin{grammar}
<selector> = `[' <expression> `]' | `.\@' <identifier> | `^'.

<designator> = <reference> <selector> | <identifier>.

<reference> = <literal>
	\alt{} <designator>
	\alt{} <trait>
	\alt{} <cast>
	\alt{} <procedure-call>
	\alt{} `(' <expression> `)'.
\end{grammar}

With the exception of literal constants and call expressions, operands are denoted by
\textit{designators}. A designator consists of an identifier referring to the constant,
variable, or procedure to be designated. This identifier or any other expression may be
followed by selectors, if the designated object is an element of a structure.

Designators are addressable, meaning that it is possible to get the address of the
designated object or assign a value to it (if the designated object is mutable).

\subsection*{Subscript selector}

If \verb|A| designates an array or a string, then \verb|A[E]| denotes that element of
\verb|A| whose index is the current value of the expression \verb|E|. The type of
\verb|E| must be of type \verb|Word| or \verb|Int|. The first element has index 1,
the second 2 and so on.

\subsection*{Dereference selector}

If \verb|P| designates a pointer, \verb|P^| denotes the object which is
referenced by \verb|P|.

\subsection*{Field selector}

If \verb|R| designates a record, then \verb|R.f| denotes the field \verb|f| of
\verb|R|.

\subsection*{Variable designator}

If the designated object is a variable, then the designator refers to the
variable's current value.

If the designated object is a procedure, a designator without parameter list
refers to the address of the procedure. Procedure designators designate
immutable objects.

\subsection*{Selector evaluation order}

Selectors are evaluated from left to right.

For example the expression \verb|r^.field| includes 3 designators:

\begin{enumerate}
	\item Variable designator \verb|r|.
	\item \verb|r^| dereferences the pointer \verb|r| and refers
		to the object at the address in \verb|r|.
	\item \verb|r^.field| accesses the field \verb|field| in that object.
\end{enumerate}

\section{Unary expressions}

\begin{grammar}
<unary-operator> = `@' | `~' | `-'.

<factor> = <unary-operator> <factor> | <reference>.
\end{grammar}

Unary expressions are expressions with a prefix operator followed by one
operand. Unary operators in table~\ref{table:unary} associate from right
to left.

$@$ takes the address of its operand. The operand expression should be
addressable.

$\sim$ applied on a boolean acts as logical not. Applied on an integer --- as
bitwise not.

\begin{table}[ht]
\centering
\begin{tabularx}{0.7\textwidth}{%
	r
	l
	>{\centering\arraybackslash}X
}
	\textbf{Symbol} & \textbf{Symbol name} & \textbf{Description} \\
	\toprule
	$@$ & \textit{at sign} & Address \\
	\midrule
	$-$ & \textit{minus} & Sign inversion \\
	\midrule
	$+$ & \textit{plus} & Identity operation \\
	\midrule
	$\sim$ & \textit{tilde} & Negation \\
	\bottomrule
\end{tabularx}
\caption{Unary operators}\label{table:unary}
\end{table}

\section{Binary expressions}

\begin{grammar}
<term> = <factor> \{<multiplication-operator> <factor>\}.

<simple-expression> = <term> \{<addition-operator> <term>\}.

<comparand> = <simple-expression> \{<shift-operator> <simple-expression>\}.

<relation> = <comparand> \{<relation-operator> <comparand>\}.

<operand> = <relation> \{`&' <relation>\}.

<expression> = <operand> \{(`or' | `xor') <operand>\}.
\end{grammar}

The syntax of expressions distinguishes between several classes of
operators with different precedences (binding strengths). Operators of
the same precedence associate from left to right. For example, $x - y - z$
stands for $(x - y) - z$.

The available operators are listed in the table~\ref{table:binary}.
In some instances, several different operations are designated by
the same operator symbol. In these cases, the actual operation is
identified by the type of the operands.

\begin{table}[ht]
\centering
\begin{tabularx}{\textwidth}{%
	c
	>{\centering\arraybackslash}X
	>{\raggedright\arraybackslash}X
}
	\textbf{Precedence} & \textbf{Operator} & \textbf{Description} \\
	\toprule
	1 & $* \quad / \quad \%$ & Multiplication, division and remainder. \\
	\midrule
	2 & $+ \quad -$ & Addition and subtraction. \\
	\midrule
	3 & $<< \quad >>$ & Left and right shifts. \\
	\midrule
	4 & $= \quad <> \quad > \quad < \quad <= \quad >=$ & Relational operators. \\
	\midrule
	5 & $\&$ & Logical conjuction. \\
	\midrule
	6 & $or \quad xor \quad$ & Logical disjunction operators. \\
	\bottomrule
\end{tabularx}
\caption{Operator precedence}\label{table:binary}
\end{table}

\subsection*{Logical and bitwise operators}

Meaning of the operators in table~\ref{table:logical} depends on the
operand type:

Applied on booleans they act as logical operators.

Applied on integers they perform the corresponding logical operation
bitwise, on each pair of bits of their operands.

$or$ and $\&$ are short-circuiting, if the evaluation of the left
side of the operation is enough to determine the final result,
the right side should not be evaluated.

\begin{table}[ht]
\centering
\begin{tabularx}{\textwidth}{%
	r
	l
	>{\centering\arraybackslash}X
	>{\centering\arraybackslash}X
}
	\textbf{Symbol} & \textbf{Description} &
		\textbf{$p \cdot q$ with $\cdot$ as logical operator stands for} &
		\textbf{As bitwise operator} \\
	\toprule
	$or$ & Inclusive disjunction & If $p$ then $true$, else $q$ & Bitwise or \\
	\midrule
	$xor$ & Exclusive disjunction & If $p$ then not $q$, else $q$ & Bitwise xor \\
	\midrule
	$\&$ & Conjuction & If $p$ then $q$, else $false$ & Bitwise and \\
	\bottomrule
\end{tabularx}
\caption{Logical operators}\label{table:logical}
\end{table}

\subsection*{Arithmetic operators}

Operators in table~\ref{table:arithmetic} apply to operands of numeric
types. Both operands must be of the same type, which is also the type of
the result.

Let $q = x / y$, and $r = x \% y$. Then quotient $q$ and remainder $r$ are
defined by the equation

\[
x = q * y + r \mid 0 \leq r < y
\]

Division on integer operands performs integer division.

\begin{table}[ht]
\centering
\begin{tabular}{r l}
	\textbf{Symbol} & \textbf{Result} \\
	\toprule
	$+$ & Sum \\
	\midrule
	$-$ & Difference \\
	\midrule
	$*$ & Product \\
	\midrule
	$/$ & Quotient \\
	\midrule
	$\%$ & Modulus \\
	\bottomrule
\end{tabular}
\caption{Arithmetic operators}\label{table:arithmetic}
\end{table}

\subsection*{Relations}

Relations in table~\ref{table:relation} are boolean. The ordering
relations $<$, $<=$, $>$, $>=$ apply to the numeric types. The
relations $=$ and $<>$ apply to all types.

\begin{table}[ht]
\centering
\begin{tabular}{r l}
	\textbf{Symbol} & \textbf{Result} \\
	\toprule
	$=$ & Equal \\
	\midrule
	$<>$ & Unequal \\
	\midrule
	$<$ & Less \\
	\midrule
	$<=$ & Less or equal \\
	\midrule
	$>$ & Greater \\
	\midrule
	$>=$ & Greater or equal \\
	\bottomrule
\end{tabular}
\caption{Relational operators}\label{table:relation}
\end{table}

\chapter{Statements}

\begin{grammar}
<statement> = <assignment> | <procedure-call> | <defer-statement>
    | <label-declaration> | <goto-statement> |
    | <while-statement> | <if-statement> | <case-statement>.
\end{grammar}

Statements denote actions. There are elementary and structured statements.
Elementary statements are not composed of any parts that are themselves
statements. They are the assignment and the procedure call. Structured
statements are composed of parts that are themselves statements. They
are used to express sequencing and conditional, selective, and
repetitive execution. A statement may also be empty, in which case it
denotes no action. The empty statement is included in order to relax
punctuation rules in statement sequences.

\section{Elementary statements}
\section{Conditional statements}
\section{Loop statements}