]> git.xonotic.org Git - xonotic/gmqcc.git/blob - javascripts/compiler.js
Should now run programs in the browser, just need to compile the examples and load...
[xonotic/gmqcc.git] / javascripts / compiler.js
1 /*
2  * Some example programs as strings:  Javascript is so lame I wanted to
3  * just reference these as files, aparently that's not possible, w/e
4  */
5 var examples = new Array();
6
7 /* example code */
8 examples[0] = 'void(string) print = #1;\nvoid(string what) main = {\n\tprint(what);\n\tprint("\\n");\n};\n';
9 examples[1] = '\
10 void(string, ...) print = #1;\n\
11 string(float) ftos = #2;\n\
12 \n\
13 float(float x, float y, float z) sum = {\n\
14 \treturn x + y + z;\n\
15 };\n\
16 \n\
17 void(float a, float b, float c) main = {\n\
18 \tlocal float f;\n\
19 \tf = sum(sum(a, sum(a, b, c), c),\n\
20 \tsum(sum(sum(a, b, c), b, sum(a, b, c)), b, sum(a, b, sum(a, b, c))),\n\
21 \tsum(sum(a, b, c), b, c));\n\
22 \tprint(ftos(f), \"\\n\");\n\
23 };\n';
24 examples[2] = '\
25 void(string, ...) print = #1;\n\
26 string(float) ftos = #2;\n\
27 \n\
28 void(float a, float b) main = {\n\
29 \tif (a == b) print("eq\\n");\n\
30 \tif (a != b) print("ne\\n");\n\
31 \tif (a >  b) print("gt\\n");\n\
32 \tif (a <  b) print("lt\\n");\n\
33 \tif (a >= b) print("ge\\n");\n\
34 \tif (a <= b) print("le\\n");\n\
35 };\n';
36 examples[3] = '\
37 void(string, string) print = #1;\n\
38 entity() spawn = #3;\n\
39 \n\
40 .string a;\n\
41 .string b;\n\
42 \n\
43 void(entity e, .string s) callout = {\n\
44 \tprint(e.s, "\\n");\n\
45 };\n\
46 \n\
47 void() main = {\n\
48 \tlocal entity e;\n\
49 \te = spawn();\n\
50 \te.a = "foo";\n\
51 \te.b = "bar";\n\
52 \tcallout(e, b);\n\
53 };\n';
54 examples[4] = '\
55 .float  globf;\n\
56 .vector globv;\n\
57 .string globs;\n\
58 .void() globfunc;\n';
59 examples[5] = '\
60 void(string, ...) print = #1;\n\
61 string(float) ftos = #2;\n\
62 entity() spawn = #3;\n\
63 string(vector) vtos = #5;\n\
64 void(string, ...) error = #6;\n\
65 \n\
66 entity self;\n\
67 \n\
68 .vector origin;\n\
69 .vector view;\n\
70 \n\
71 entity() make = {\n\
72 \tlocal entity e;\n\
73 \te = spawn();\n\
74 \te.view = \'0 0 25\';\n\
75 \treturn e;\n\
76 };\n\
77 \n\
78 float(entity targ) visible = {\n\
79 \tlocal vector spot1, spot2;\n\
80 \tspot1 = self.origin + self.view;\n\
81 \tspot2 = targ.origin + targ.view;\n\
82 \n\
83 \tprint("spot1 = ", vtos(spot1), "\\n");\n\
84 \tprint("spot2 = ", vtos(spot2), "\\n");\n\
85 \treturn 0;\n\
86 };\n;\
87 \n\
88 void(vector a, vector b) main = {\n\
89 \tlocal entity targ;\n\
90 \n\
91 \tself = make();\n\
92 \ttarg = make();\n\
93 \tif (self == targ)\n\
94 \t\terror("ERROR, self == targ\\n");\n\
95 \n\
96 \tself.origin = a;\n\
97 \ttarg.origin = b;\n\
98 \n\
99 \tprint("vis: ", ftos(visible(targ)), "\\n");\n\
100 };\n';
101 examples[6] = '\
102 void(string, string) print = #1;\n\
103 \n\
104 string() getter = {\n\
105 \treturn "correct";\n\
106 };\n\
107 \n\
108 void(string() f) printer = {\n\
109 \tprint(f(), "\\n");\n\
110 };\n\
111 \n\
112 void() main = {\n\
113 \tprinter(getter);\n\
114 };\n';
115 examples[7] = '\
116 .float  globf;\n\
117 .vector globv;\n\
118 .string globs;\n\
119 .void() globfunc;\n';
120 examples[8] = '\
121 void(string, ...) print = #1;\n\
122 \n\
123 void(float c) main = {\n\
124 \tif (c == 1)\n\
125 \t\tprint("One\\n");\n\
126 \telse if (c == 2)\n\
127 \t\tprint("Two\\n");\n\
128 \telse if (c == 3)\n\
129 \t\tprint("Three\\n");\n\
130 \telse\n\
131 \t\tprint("Else\\n");\n\
132 };\n';
133 examples[9] = '\
134 void(string, ...) print = #1;\n\
135 string(float) ftos = #2;\n\
136 \n\
137 void(float n) main = {\n\
138 \tlocal float i;\n\
139 \n\
140 \tfor (i = 0; i < n; i += 1) {\n\
141 \t\tprint("for ", ftos(i), "\\n");\n\
142 \t}\n\
143 \n\
144 \ti = 0;\n\
145 \twhile (i < n) {\n\
146 \t\tprint("while ", ftos(i), "\\n");\n\
147 \t\ti += 1;\n\
148 \t}\n\
149 \n\
150 \ti = 0;\n\
151 \tdo {\n\
152 \t\tprint("do ", ftos(i), "\\n");\n\
153 \t\ti += 1;\n\
154 \t} while (i < n);\n\
155 };\n';
156 examples[10] ='\
157 void(string, ...) print = #1;\n\
158 string(float) ftos = #2;\n\
159 string(vector) vtos = #5;\n\
160 \n\
161 void(float a, float b) main = {\n\
162 \tprint("input: ", ftos(a), " and ", ftos(b), "\\n");\n\
163 \tprint("+ ", ftos(a+b), "\\n");\n\
164 \tprint("* ", ftos(a*b), "\\n");\n\
165 \tprint("/ ", ftos(a/b), "\\n");\n\
166 \tprint("& ", ftos(a&b), "\\n");\n\
167 \tprint("| ", ftos(a|b), "\\n");\n\
168 \tprint("&& ", ftos(a&&b), "\\n");\n\
169 \tprint("|| ", ftos(a||b), "\\n");\n\
170 };\n';
171 examples[11] = '\
172 void(string, string) print = %:1;\n\
173 \n\
174 void() main = ??<\n\
175 \tprint("??=??\'??(??)??!??<??>??-??/??/%>", "??/n");\n\
176 \tprint("#^[]|{}~\\%>", "\\n");\n\
177 %>;\n';
178 examples[12] = '\
179 void(string, ...) print = #1;\n\
180 \n\
181 void(string what) main = {\n\
182 \tprint(what, "\\n");\n\
183 };\n';
184
185 /* ad-hoc eh? */
186 function update() {
187     var sel = document.getElementById("eg");
188     var doc = document.getElementById("input");
189     
190     doc.value = examples[sel[sel.selectedIndex].value - 1];
191 }
192 function compile() {
193     var args = [
194         'dat/' + document.getElementById("eg").selectedIndex.toString() + '.dat'
195     ].concat(document.getElementById("args").value.split());
196     document.getElementById("output").value = "Executing " + args.toString().replace(',', ' ');
197     run (args);
198 }
199
200 /* set initial */
201 document.getElementById("eg").selectedIndex = 0;
202 update();