-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchx.tex
65 lines (46 loc) · 2.19 KB
/
chx.tex
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
\chapter{The CHM Language Grammar}
This is the grammar for the CHM language. The meaning of all constructs define here is defined in chapter \ref{chap:compilerCHM} (\emph{CHM Compiler}).
\section{CHM headers}
Polymorphic functions and structures are constructed using special annotations (called CHM headers) following grammar:
\begin{lstlisting}
chm_header = '<' type_decls '>'
| '<' type_decls ':' constraints '>'
| '<' constraints '>'
type_decls = type_decl
| type_decls ',' IDENT
constraints = constraint
| constraints ',' constraint
constraint = new_type
| predicate
new_type = IDENT '~' type
predicate = CLASS '<' type '>'
type = DECLARATOR
| DECLARATOR ':' '<' types '>'
types = DECLARATOR
| types ',' DECLARATOR
\end{lstlisting}
This is a simplified version of a piece of the actual grammar used in the implementation.
\begin{itemize}
\item `IDENT' is a new identifier
\item `CLASS' is the name of a type class
\item `DECLARATOR' is a declarator from the C language (a type)
\end{itemize}
\section{Classes and Their Instances}
The other addition CHM brings to C are classes. They are defined (simplified version) as follows:
\begin{lstlisting}
class = chm_head 'class' IDENT '{' ext_decls '}'
| chm_head 'class' IDENT < typevar > '{' ext_decls '}'
instance = 'instance' IDENT '<' type '>'
'{' ext_decls '}'
chm_head 'instance' IDENT '<' type '>'
'{' ext_decls '}'
\end{lstlisting}
\lstinline{ext_decls} are declarations and definitions which would be legal in the global scope. However, it is not allowed to put declarations in instances, and all their definitions have to be first declared in the matching classes.
\section{Polymorphic Functions and Structures}
Polymorphic Functions and Structures are defined as regular C constructs preceded by \emph{CHM headers}:
\begin{lstlisting}
CHM_function_definition = chm_header function_definition
CHM_function_declaration = chm_header function_declaration
CHM_struct_definition = chm_header struct_definition
\end{lstlisting}
We omit \textbf{unions} as a lot of grammars (including ours) consider them syntactically equivalent to structs.