]> de.git.xonotic.org Git - xonotic/xonotic.wiki.git/blob - NewQC.md
rename before merge
[xonotic/xonotic.wiki.git] / NewQC.md
1 New QC Syntax
2 =============
3
4 {{\>toc}}
5
6 It is possible that at some point we decide to switch QC-compiler which requires some changes to the code.
7
8 **For more information see** http://dev.xonotic.org/projects/bocc
9
10 Clean syntax:
11 -------------
12
13 In fteqcc there are some ambiguities regarding fieldpointers, function pointers, and field-return-types etc.
14 A clean syntax is needed, the current implementation uses the following:
15
16 |definition|meaning|
17 |----------|-------|
18 |<code>float foo</code>|global variable|
19 |<code>float .foo</code>|entity field|
20 |<code>.float foo</code>|fieldpointer|
21 |<code>.float .foo</code>|entity field of type fieldpointer|
22 |<code>float foo(void)</code>|function|
23 |<code>float foo\*(void)</code>|function pointer|
24 |<code>.float foo(void)</code>|function returning a fieldpointer .float|
25 |<code>.float foo\*(void)</code>|function pointer, returning a fieldpointer .float|
26 |<code>float .foo(void)</code>|entity field of type function returning float|
27 |<code>.float .foo(void)</code>|entity field of type function returning fieldpointer|
28
29 Function definitions:
30 ---------------------
31
32 The old-style QC way of defining functions will not be supported, so
33
34     void(float x) something = { ... }
35
36 becomes
37
38     void something(float x) { ... }
39
40 which is the most common way to define functions in the xonotic code already anyway.
41
42 Constants:
43 ----------
44
45 From now on, the code
46
47     float x = 3
48
49 does what the first instinct tells you: it creates a global with the initial value 3. Contrary to old QC, where it created a constant.
50 To create a constant use:
51
52     const float x = 3
53
54 Extendable functions:
55 ---------------------
56
57 Since menuQC has some funny macro: ACCUMULATE\_FUNCTIONS, it seemed like a nice syntactical sugar to allow the following:
58
59     float myfunc() extendable
60     {
61         float mylocal = 3;
62     }
63
64     /* other code */
65
66     float myfunc()
67     {
68         mylocal += 5;
69         if (mylocal > 20)
70             return mylocal;
71     }
72
73     /* optionally: */
74     float myfunc() final
75     {
76         return 3;
77     }
78
79 Variadic parameters (do not use yet)
80 ------------------------------------
81
82 (This might get changed to be more flexible so do not rely on this syntax...)
83
84 Another "enhancement" is the possibility to have functions with variadic parameter lists. However, the only way to sanely access them (until pointers are allowed) is via a recursive way.
85 Here's an example that assumes float parameters and prints them one after the other:
86
87     void printfloats(float count, float first, ...)
88     {
89         if (count <= 0) // if there are no parameters, return
90             return;
91         if (count == 1) { // If there's one parameter, print it, plus a new-line
92             print(strcat(ftos(first), "\n"));
93             return;
94         }
95         // Otherwise we have multiple parameters left, so print the float, and add a comma
96         print(strcat(ftos(first), ", "));
97         myprint(count-1, ...);
98     }
99
100 So <code>myprint(4, 1, 2, 3, 4)</code> would print <code>"1, 2, 3, 4\\n"</code>