]> git.xonotic.org Git - xonotic/gmqcc.git/blob - doc/specification.tex
3d43e82012a195daadfae3ab77cd8bbe9b4b00ed
[xonotic/gmqcc.git] / doc / specification.tex
1 \documentclass[11pt]{article}
2 \makeindex
3 \usepackage{graphicx}    % needed for including graphics e.g. EPS, PS
4 \usepackage{listings}    % for C syntax highlighting
5 \usepackage{color}
6
7 \definecolor{dkgreen}{rgb}{0,0.6,0}
8 \definecolor{gray}{rgb}{0.5,0.5,0.5}
9 \definecolor{mauve}{rgb}{0.58,0,0.82}
10
11 % set listings colors
12 \lstset{ %
13   backgroundcolor=\color{white},  % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}
14   basicstyle=\footnotesize,       % the size of the fonts that are used for the code
15   breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
16   breaklines=true,                % sets automatic line breaking
17   captionpos=b,                   % sets the caption-position to bottom
18   commentstyle=\color{dkgreen},   % comment style
19   deletekeywords={...},           % if you want to delete keywords from the given language
20   escapeinside={\%*}{*)},         % if you want to add LaTeX within your code
21   keywordstyle=\color{blue},      % keyword style
22   language=C,                % the language of the code
23   morekeywords={entity,local},           % if you want to add more keywords to the set
24   numbers=left,                   % where to put the line-numbers; possible values are (none, left, right)
25   numbersep=5pt,                  % how far the line-numbers are from the code
26   numberstyle=\tiny\color{gray},  % the style that is used for the line-numbers
27   rulecolor=\color{black},        % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
28   showspaces=false,               % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
29   showstringspaces=false,         % underline spaces within strings only
30   showtabs=false,                 % show tabs within strings adding particular underscores
31   stepnumber=1,                   % the step between two line-numbers. If it's 1, each line will be numbered
32   stringstyle=\color{mauve},      % string literal style
33   tabsize=2,                      % sets default tabsize to 2 spaces
34   title=\lstname                  % show the filename of files included with \lstinputlisting; also try caption instead of title
35 }
36
37 \topmargin -1.5cm        % read Lamport p.163
38 \oddsidemargin -0.04cm   % read Lamport p.163
39 \evensidemargin -0.04cm
40
41 \textwidth 16.59cm
42 \textheight 21.94cm 
43 \parskip 7.2pt                         % spacing between paragraphs
44 \renewcommand{\baselinestretch}{1.5}    % 1.5 spacing between lines
45 \parindent 0pt                              % leading space for paragraphs
46 \title{The GMQCC Quake C Programming Language}
47 \author{Dale Weiler, Wolfgang Bullimer}
48
49 % subscript and superscript in text mode require
50 % strange uses of math expression scripting, which
51 % is unusable, these macros implement it using math
52 % expressions, and reimplement ^ and _ to work in
53 % text mode :-)
54 \makeatletter
55 \newcommand\textsubscript[1]{\@textsubscript{\selectfont#1}}
56 \def\@textsubscript#1{{\m@th\ensuremath{_{\mbox{\fontsize\sf@size\z@#1}}}}}
57 \newcommand\textbothscript[2]{%
58   \@textbothscript{\selectfont#1}{\selectfont#2}}
59 \def\@textbothscript#1#2{%
60   {\m@th\ensuremath{%
61     ^{\mbox{\fontsize\sf@size\z@#1}}%
62     _{\mbox{\fontsize\sf@size\z@#2}}}}}
63 \def\@super{^}\def\@sub{_}
64
65 \catcode`^\active\catcode`_\active
66 \def\@super@sub#1_#2{\textbothscript{#1}{#2}}
67 \def\@sub@super#1^#2{\textbothscript{#2}{#1}}
68 \def\@@super#1{\@ifnextchar_{\@super@sub{#1}}{\textsuperscript{#1}}}
69 \def\@@sub#1{\@ifnextchar^{\@sub@super{#1}}{\textsubscript{#1}}}
70 \def^{\let\@next\relax\ifmmode\@super\else\let\@next\@@super\fi\@next}
71 \def_{\let\@next\relax\ifmmode\@sub\else\let\@next\@@sub\fi\@next}
72 \makeatother
73
74 % set standard paper sizes
75 \setlength{\paperheight}{11in}
76 \setlength{\paperwidth}{8.5in}
77  
78 \begin{document}
79 \maketitle
80 \pagebreak
81 \tableofcontents
82 \pagebreak
83 \section{Introduction}
84 With the introduction of improvements of existing runtime environments, new features may be added to this specification.  Subclauses in the language warn implementors and programmers of usages which, though valid in themselfs, may conflict with future additions.
85
86 This specification is divided into three major subjects:
87 \begin{enumerate}
88         \item preliminary elements
89         \item the characteristics of environments that translate and execute QuakeC programs
90         \item the languages syntax, constraints, and semantics
91 \end{enumerate}
92
93 Examples are provided to illustrate possible forms of the constructions described, to better facilitate the reader.
94
95 \section{Scope}
96 This specification only specifies the form and establishes the interpretation of programs written in the GMQCC QuakeC programming language variant. It specifies:
97 \begin{enumerate}
98         \item The representation of Quake C programs;
99         \item The syntax and constraints of the Quake C language;
100         \item The semantic rules for interpreting Quake C programs;
101         \item The representation of input data to be processed by Quake C programs;
102         \item The restrictions and limits imposed by a conforming implementation of Quake C.
103 \end{enumerate}
104 This specification does not specify
105 \begin{enumerate}
106         \item The process in which Quake C programs are transformed for use by a data-processing system;
107         \item The process in which Quake C programs are invoked for use by a data-processing system;
108         \item The process in which input data is transformed for use by a Quake C program;
109         \item The process in which output data is transformed after being produces by a Quake C program;
110         \item The size of complexity involved with a Quake C program and its  data that will exceed the capacity of any specific data-processing system or the capacity of a particular processor;
111         \item The minimal requirements of a data-processing system which is capable of supporting a conforming implementation.
112 \end{enumerate}
113 These details are decided upon by the environment designer (usually the engine developer).  The details are as such subjected to implementation-defined behavior.
114
115 \section{Terms and definitions}
116 \subsection*{argument}
117 The expression in a comma-separated list bounded by parentheses in a function call expression, or a sequence of preprocessing tokens in a comma-separated list bounded by parentheses in a function-like macro invocation.
118 \subsection*{behavior}
119 The external appearance or action of an expression, statement, etc
120 \subsection*{implementation-defined behavior}
121 Unspecified behavior where each implementation decides how the choice is made.
122 \subsection*{unspecified behavior}
123 Use of an unspecified value, or other behavior where this specification provides two or more possibilities and imposes no further requirements on which is chosen in any instance (e.g. Thus any ambiguity is considered unspecified behavior).
124 \subsection*{constraint}
125 A restriction, either syntactically or semantically, by which the exposition of various language elements is to be interpreted.
126 \subsection*{implementation}
127 Particular set of software, running in a particular translation environment under particular control options, that performs translations of programs for, and supports execution of functions in, a particular execution environment.
128 \subsection*{object}
129 A region of data storage in the execution environment, the contents of which are capable of representing values.
130 \subsection*{parameter}
131 An object declared as part of a function declaration that acquires a value on entry to the function, or an identifier from the comma-separated list bounded by parentheses immediately following the macro name in a function-like macro definition.
132 \subsection*{value}
133 The precise meaning of the contents of an object when interpreted as having a specific type.
134 \subsection*{implementation-defined value}
135 An unspecified value where each implementation decides how the choice is made.
136 \subsection*{unspecified value}
137 A valid value of the relevant type where this specification imposes no requirements on which value is chosen in any instance.
138
139 \section{Conformance}
140 In this specification, "shall" is to be interpreted as a requirement on an implementation or on a program; conversely, "shall not" is to be interpreted as a prohibition on the implementation or on a program.
141
142 A conforming program is one that is acceptable to a conforming implementation.
143
144 An implementation shall be accompanied by a document that defines all implementation-defined characteristic and extensions.
145
146 \section{Conceptual models}
147 \subsection{Translation environment}
148 \subsubsection{Program structure}
149 A Quake C program need not all be translated at the same time.  The text of the program is kept in units called source files. All source files become concatenated, less any source lines skipped by any of the conditional inclusion preprocessing directives.  The final concatenation becomes the program structure.
150
151 \subsubsection{Translation phases}
152 \begin{enumerate}
153         \item Physical source file characters are mapped to the source character set (introducing new-line characters for end-of-line indicators) if necessary.  Trigraph and Digraph sequences are replaced by corresponding single-character internal representations.
154         \item The source file is decomposed into preprocessing tokens and sequences of white-space characters (including comments).
155         \item Preprocessing directives are executed and macro invocations expanded recursively.
156         \item Each escape sequence in character constants and string literals is converted to a member of the execution character set.
157         \item Adjacent character string literal tokens are concatenated.
158         \item White-space characters separating tokens are no longer sufficient. The resulting tokens are syntactically and semantically analyzed and translated.
159 \end{enumerate}
160
161 \subsection{Execution environment}
162 \section{Environmental considerations}
163 \subsection{Runtime considerations}
164 TODO
165 \pagebreak
166
167 \section{Notation}
168 \subsection{Document notation}
169 In the syntax notation used in this clause, syntactic categories (non-terminals) are indicated by italic type, and literal words and character set members (terminals) by bold type.  A colon (:) following a non-terminal introduces its definition.  Alternative definitions are listed on corresponding separate lines, except when prefaced by words "one of".  An optional symbol is indicated by a subscript "opt", so that\\
170 \{ expression _{opt} \}\\
171 indicates an optional expression closed in braces. When syntactic categories are refereed to in the main text, they are not italicized and words are separated by spaces instead of hyphens.
172 \subsection{Language notation}
173 TODO
174
175 \section{Concepts}
176 \subsection{Scopes of identifiers}
177 An identifier can denote an object; a function; a tag or member of an entity, or enumeration; a typedef name; a label name; a macro name; or a macro parameter.  The same identifier can denote different entities at different points in the program.  A member of an enumeration is called an enumeration constant.  Macro names and macro parameters are not considered further here, because prior to the semantic phase of program translation any occurrences of macro names in the source file are replaced by the preprocessing token sequences that constitute their macro definitions.
178
179 For each different entity that an identifier designates, the identifier is visible (i.e., can be used) only within a region of program text called its scope.  Different entities designated by the same identifier either have different scopes, or are in different name spaces.  There are four kinds of scopes: function, global, block and function prototype. (A function prototype is a declaration of a function that declares the types of its parameters.)
180
181 A label name is the only kind of identifier that has function scope.  It can be used (in a goto statement) anywhere in the function in which it appears, and is declared implicitly by it's syntactic appearance (prefixed by a : and a statement).
182
183 Every other identifier has scoped determined by the placement of its declaration (in a declarator or type specifier).  If the declarator or type specifier that declares the identifier appears outside of any block or list of parameters, the identifier has global scope, which terminates at the end of the program structure.  If the declarator or type specifier that declares the identifier appears inside a block or within the list of parameter declarations in a function definition, the identifier has block scope, which terminates at the end of the associated block. If the declarator or type specifier that declares the identifier appears within the list of parameter declarations in a function prototype (not part of a function definition), the identifier has function prototype scope, which terminates at the end of the function declarator.  If an identifier designates two different entities in the same name space, the scopes might overlap.  If so, the scope of one entity (the inner scope) will be a strict subset of the scope of the other entity (the outer scope).  Within the inner scope, the identifier designates the entity declared in the inner scope; the entity declared in the outer scope is hidden (and is not visible) within the inner scope.
184
185 Unless explicitly stated otherwise, where this specification uses the term "identifier" to refer to some entity (as opposed to the syntactic construct), it refers to the entity in the relevant name space whose declaration is visible at the point the identifier occurs.
186
187 Unless explicitly stated otherwise, where this specification uses the term "entity", is not to be ambiguous with the entity type, but considered a semantic construct.
188
189 Two identifiers have the same scope if and only if their scopes terminate at the same point.
190
191 Enumeration tags have scope that begins just after the appearance of the tag in a type specifier that declares the tag.  Each enumeration constant has scope that begins just after the appearance of its defining enumerator in an enumeration list.  Any other identifier has scope that begins just after the completion of its declarator.
192
193 \subsection{Name spaces of identifiers}
194 If more than one declaration of a particular identifier is visible at any point in the program structure, the syntactic context disambiguates uses that refer to different entities.  Thus, there are separate name spaces for various categories of identifiers, as follows:
195 \begin{enumerate}
196         \item label names (disambiguated by the syntax of the label declaration and use);
197         \item the tags of enumerations
198         \item all other identifiers, called ordinary identifiers (declared in ordinary declarators or as enumeration constants).
199 \end{enumerate}
200
201 \subsection{Types}
202 The meaning of a value stored in an object or returned by a function is determined by the type of the expression used to access it.  (An identifier declared to be an object is the simplest such expression; the type is specified in the declaration of the identifier.)  Types are partitioned into object types(types that fully describe object) and function types(types that describe functions).
203
204 An object declares as type bool is large enough to store the values 0 and 1.
205
206 An object declared as type char is large enough to store any member of the basic execution character set.  If a member of the basic execution character set is stored in a char object, its value is guaranteed to be nonnegative.  If any other character is stored in a char, the resulting value is implemented-defined.
207
208 An object declared as type string is large enough to store any length string-literal composed of any length chars, and as such follows the same rules as an object declared as type char.
209
210 The void type comprises an empty set of values; it is an incomplete type that cannot be completed.
211
212 The float and vector type .. TODO
213
214 An enumeration comprises a set of named integer constant values.  Each enumeration constitutes a different enumerated type.
215
216 \section{Lexical elements}
217 \subsection{Keywords}
218 \begin{tabular} { l l l l l }
219         for & do & while & if & else \\
220         local & return & const & switch & case \\
221         default & struct & union & break & continue \\
222         typedef & goto & namespace \\
223 \end{tabular}
224
225 \subsection{Identifiers}
226 \subsubsection*{identifier}
227 \begin{tabular} { l }
228         identifier-nondigit \\
229         identifier identifier-nondigit \\
230         identifier-digit \\
231 \end{tabular}
232
233 \subsubsection*{identifier-nondigit}
234 \begin{tabular} { l }
235         nondigit \\
236         other implementation-defined characters \\
237 \end{tabular}
238
239 \subsubsection*{nondigit}
240 \begin{tabular}{ c c c c c c c c c c c c c c c c c c c c }
241         \_ & a & b & c & d & e & f & g & h & i & j & k & l & m & n & o & p & q & r & s \\
242         t & u & v & w & y & z & A & B & C & D & E & F & G & H & I & J & K & L & M & N \\
243         O & P & Q & R & S & T & U & V & W & X & Y & Z \\
244 \end{tabular}
245
246 \subsubsection*{digit}
247 \begin{tabular}{c c c c c c c c c c c }
248         0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \\
249 \end{tabular}
250
251 \subsubsection*{hex-quad}
252 \begin{tabular} { l }
253         hexadecimal-digit hexadecimal-digit \\
254 \end{tabular}
255
256 \subsection{Constants}
257 \label{sec:constants}
258 \subsubsection*{constant}
259 \begin{tabular} { l }
260         integer-constant \\
261         floating-constant \\
262         enumeration-constant \\
263         character-constant \\
264         vector-constant \\
265 \end{tabular}
266
267 \subsubsection*{integer-constant}
268 \begin{tabular} { l }
269         decimal-constant \\
270         octal-constant \\
271         hexadecimal-constant \\
272 \end{tabular}
273
274 \subsubsection*{decimal-constant}
275 \begin{tabular} { l }
276         nonzero-digit \\
277         decimal-constant digit \\
278 \end{tabular}
279
280 \subsubsection*{octal-constant}
281 \begin{tabular} { l }
282         0 \\
283         octal-constant octal-digit
284 \end{tabular}
285
286 \subsubsection*{hexadecimal-constant}
287 \begin{tabular} { l }
288         hexadecimal-prefix hexadecimal-digit \\
289         hexadecimal-constant hexadecimal-digit \\
290 \end{tabular}
291
292 \subsubsection*{hexadecimal-prefix}
293 \begin{tabular} { c c }
294         0x & 0X \\
295 \end{tabular}
296
297 \subsubsection*{nonzero-digit}
298 \begin{tabular} { c c c c c c c c c }
299         1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \\
300 \end{tabular}
301
302 \subsubsection*{octal-digit}
303 \begin{tabular}{ c c c c c c c c c }
304         0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 \\
305 \end{tabular}
306
307 \subsubsection*{hexadecimal-digit}
308 \begin{tabular}{ c c c c c c c c c c c c c c c c }
309         0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & A & B & C & D & E & F \\
310 \end{tabular}
311
312 \subsubsection*{floating-constant}
313 \begin{tabular} { l }
314         decimal-floating-constant \\
315         hexadecimal-floating-constant \\
316 \end{tabular}
317
318 \subsection{String Literals}
319 \subsubsection*{string-literal}
320 \begin{tabular} { l }
321         " s-char-sequence _{opt} " \\
322         \_(" s-char-sequence _{opt} ") \\
323 \end{tabular}
324
325 \subsubsection*{s-char-sequence}
326 \begin{tabular} { l }
327         s-char \\
328         s-char-sequence s-char \\
329 \end{tabular}
330
331 \subsubsection*{s-char}
332 \begin{tabular} { l }
333         any member of the source character set except double-quote, backslash, or new-line characters \\
334         escape-sequence \\
335 \end{tabular}
336
337 \subsection{Header names}
338 \subsubsection*{header-name}
339 \begin{tabular} { l }
340         < h-char-sequence > \\
341         " q-char-sequence " \\
342 \end{tabular}
343
344 \subsubsection*{h-char-sequence}
345 \begin{tabular} { l }
346         h-char \\
347         h-char-sequence h-char \\
348 \end{tabular}
349
350 \subsubsection*{h-char}
351 \begin{tabular} { l }
352         any member of the source character set except the new-line and > character \\
353 \end{tabular}
354
355 \subsubsection*{q-char-sequence}
356 \begin{tabular} { l }
357         q-char \\
358         q-char-sequence q-char \\
359 \end{tabular}
360
361 \subsubsection*{q-char}
362 \begin{tabular} { l }
363         any member of the source character set except the new-line and " character \\
364 \end{tabular}
365
366 \pagebreak
367
368 \subsection{Comments}
369 Except within a character constant, a string literal, or a comment, the characters /* introduce a comment. The contents of such a comment are examined only to find the characters */ that terminate it. (Thus /* .. */ comments do not nest.)
370
371 Expect within a character constant, a string literal, or a comment, the character // introduces a comment that includes all characters up to, but not including, the next new-line character.  The contents of such a comment are examined only to find the terminating new-line character.
372
373 \begin{small}
374 EXAMPLE
375 \end{small}
376 \begin{lstlisting}[language=C]
377 "a//b"             // four-character string literal
378 #include "//e"     // undefined behavior
379 // */              // comment, not syntax error
380 a = b/**//c;       // same as a = b / c;
381 /*//*/ foo();      // same as foo();
382 \end{lstlisting}
383
384 \section{Expressions}
385 \subsection{Primary expressions}
386 \subsubsection*{Syntax}
387 \begin{tabular}{ l }
388         identifier \\
389         constant \\
390         string-literal \\
391         ( expression )
392 \end{tabular}
393 \subsubsection*{Semantics}
394 An identifier is a primary expression, provided it has been declares as a designating object (in which case it is an lvalue) or a function (in which case it is a function designator).
395
396 A constant is a primary expression, with type as detailed in \ref{sec:constants}.
397
398 \section{Constant Expressions}
399 \subsection{Description}
400 A constant expression can be evaluated during translation rather than runtime, and accordingly can be used in any place that a constant may be.
401 \subsection{Constraints}
402 Constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators, except when they're contained within a subexpression that is not evaluated.
403
404 Each constant expression shall evaluate to a constant that is in the range of representable values for it's designated type.
405 \subsection{Semantics}
406 An expression that evaluates to a constant is required in several contexts.  If a floating expression is evaluated in the translation environment, the arithmetic precision and range shall be at least as great as if they expression were being evaluated in the execution environment.
407
408 An integer constant expression shall have integral type, and shall only have operands that are integer constants, enumeration constants, character constants, and floating constants that are the immediate operands of casts.
409
410 The semantic rules for the evaluation of a constant expression are the same as for nonconstant expressions.
411
412 An implementation may accept other forms of constant expressions
413 \section{Declarations}
414 \subsection{Constraints}
415 All declarations shall declare at least a declarator (other than the parameters of a function), a tag, or members of an enumeration.
416
417 All declarations in the same scope that refer to the same object or function shall specify compatible types.
418 \subsection{Semantics}
419 A declaration specifies the interpretation and attributes of a set of identifiers.  A definition of an identifier is a declaration for that identifier that:
420 \begin{enumerate}
421         \item for an object, causes storage to be reversed for that object;
422         \item for a function, includes the function body;
423         \item for an enumeration constant or typedef name, is the (only) declaration of the identifier.
424 \end{enumerate}
425
426
427 \section{Statement and blocks}
428 A statement specifies a specific action to be preformed.  Execpt as indicated, statements are executed in sequence.
429
430 \section{Preprocessing directives}
431 TODO
432
433 \end{document}