File: gen_gl.spl

package info (click to toggle)
spl 1.0~pre6-3.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,360 kB
  • sloc: ansic: 16,614; yacc: 3,182; sh: 299; makefile: 167; xml: 156
file content (257 lines) | stat: -rw-r--r-- 5,702 bytes parent folder | download | duplicates (3)
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

load "file";
load "array";

var gl_h = file_read(parameters.srcfile);
gl_h =~ s/,\s*\n/, /g;

write(<:>
	: // mod_gl.c: Auto-generated by gen_gl.spl
	:
	: #include "GL/gl.h"
	:
	: #include "spl.h"
	: #include "compat.h"
	:
	: /**
	:  * OpenGL Module
	:  *
	:  * This module provides an interface to the OpenGL API.
	:  */
</>);

function void_to_spl(t)
{
	return [ retexpr: "0" ];
}

function ANYFLOAT_to_spl(t)
{
	return [ callprefix: "$t ret = ", retexpr: "SPL_NEW_FLOAT(ret)" ];
}

var GLfloat_to_spl = ANYFLOAT_to_spl;
var GLclampf_to_spl = ANYFLOAT_to_spl;
var GLdouble_to_spl = ANYFLOAT_to_spl;
var GLclampd_to_spl = ANYFLOAT_to_spl;

function ANYINT_to_spl(t)
{
	return [ callprefix: "$t ret = ", retexpr: "SPL_NEW_INT(ret)" ];
}

var GLenum_to_spl = ANYINT_to_spl;
var GLsizei_to_spl = ANYINT_to_spl;
var GLbitfield_to_spl = ANYINT_to_spl;

var GLbyte_to_spl = ANYINT_to_spl;
var GLshort_to_spl = ANYINT_to_spl;
var GLint_to_spl = ANYINT_to_spl;

var GLubyte_to_spl = ANYINT_to_spl;
var GLushort_to_spl = ANYINT_to_spl;
var GLuint_to_spl = ANYINT_to_spl;

function spl_to_ANYFLOAT(n, t)
{
	return [ getargs: "\t$t arg$n = spl_clib_get_float(task);\n" ];
}

var spl_to_GLfloat = spl_to_ANYFLOAT;
var spl_to_GLclampf = spl_to_ANYFLOAT;
var spl_to_GLdouble = spl_to_ANYFLOAT;
var spl_to_GLclampd = spl_to_ANYFLOAT;

function spl_to_GLboolean(n, t)
{
	return [ getargs: "\t$t arg$n = spl_clib_get_int(task) ? GL_TRUE : GL_FALSE;\n" ];
}

function spl_to_ANYINT(n, t)
{
	return [ getargs: "\t$t arg$n = spl_clib_get_int(task);\n" ];
}

var spl_to_GLenum = spl_to_ANYINT;
var spl_to_GLsizei = spl_to_ANYINT;
var spl_to_GLbitfield = spl_to_ANYINT;

var spl_to_GLbyte = spl_to_ANYINT;
var spl_to_GLshort = spl_to_ANYINT;
var spl_to_GLint = spl_to_ANYINT;

var spl_to_GLubyte = spl_to_ANYINT;
var spl_to_GLushort = spl_to_ANYINT;
var spl_to_GLuint = spl_to_ANYINT;

var fdata;
var skipped_handlers;

foreach[] fdecl (gl_h =~ /^GLAPI.*?\)/Amg)
{
	fdecl =~ s/\s+/ /g;
	fdecl =~ s/\s*([()])\s*/$1/g;
	fdecl =~ s/\s+(\**)\s*[^\s,()[\]]+(\[\d+\]|)([,)])/$1$2$3/g;
	fdecl =~ s/(GL|)API(ENTRY|)\s+//g;

	if (fdecl =~ /\d.v/ or fdecl =~ /MESA/)
		continue;

	var cdecl = fdecl;
	fdecl =~ s/(const)\s+/$1_/g;
	fdecl =~ s/\[(\d+)\]/_a$1/g;
	fdecl =~ s/\s+\*/_p/g;

	fdecl =~ s/^(\S+)\s+(\S+)\((.*)\)/$3/;
	var types = [ $1 ], name = $2;

	while (fdecl =~ s/([^\s,]+)//)
		if ($1 ~!= "void")
			push types, $1;

	var tid = array_join(types, ":");
	if (not declared fdata[tid])
		fdata[tid] = [
			types: types,
			cdecl: cdecl,
			flist: []
		];

	push fdata[tid].flist, name;
}

foreach[] ftype (fdata)
{
	var funcname = "handler__" ~ array_join(ftype.types, "__");

	function ctype(typename) {
		typename =~ s/_p$/*/;
		typename =~ s/_p\*$/**/;
		typename =~ s/_a(\d+)$/[$1]/;
		typename =~ s/^(const)_/$1 /;
		return typename;
	}

	var snippets = [
		getargs: "",
		callprefix: "",
		postcall: "",
		retexpr: ""
	];
	var snippet_functions;
	var skip_this = 0;

	foreach i (ftype.types)
		if (i == 0) {
			var t = "${ftype.types[i]}_to_spl";
			push t, ftype.types[i];
			push snippet_functions, t;
		} else {
			var t = "spl_to_${ftype.types[i]}";
			push t, i;
			push t, ftype.types[i];
			push snippet_functions, t;
		}

	foreach[] f (snippet_functions)
	{
		if (not defined [/][f]) {
			if (not skip_this)
				write("\n// SKIP: $funcname\n");
			write("// Not found: $f\n");
			skip_this = 1;
			continue;
		}
			
		foreach i ([/][f](@f))
			snippets[i] ~= $@[i];
	}

	if (skip_this)
		skipped_handlers[funcname] = 1;
	else
		write(<:>
			:
			: static struct spl_node *$funcname(struct spl_task *task<?spl
					if(snippets.getargs =~ /^\s*$/)?> UNUSED<?spl
					?>, void *data)
			: {
			:	${ctype(ftype.types[0])}(*funcp)(<?spl
					foreach i (ftype.types) {
						if (i == 0) continue;
						if (i != 1) ?>, <?spl
						?>${ctype(ftype.types[i])}<?spl
					}?>) = data;
				$snippets.getargs
			:	${snippets.callprefix}funcp(<?spl
					foreach i (ftype.types) {
						if (i == 0) continue;
						if (i != 1) ?>, <?spl
						?>arg$i<?spl
					}?>);
				$snippets.postcall
			:	return $snippets.retexpr;
			: }
		</>);
}

write(<:>
	:
	: /**
	:  * The following OpenGL functions are wrapped by this module:
	:  *
	<?spl
		var funclist;
		foreach[] ftype (fdata)
		foreach[] f (ftype.flist) {
			var funcname = "handler__${array_join(ftype.types, "__")}";
			if (not defined skipped_handlers[funcname])
				push funclist, ftype.cdecl =~ s/\S+\(/$f\(/R;
		}
		array_sort_by_values(funclist, function(a,b) {
			a =~ /\s(?P<_a>[^(]+)\(/I;
			b =~ /\s(?P<_b>[^(]+)\(/I;
			return _a ~> _b;
		});
	?>
	<spl:foreach var="[]f" list="funclist">
	:  *	$f;
	</spl:foreach>
	:  */
	: // manual function_list;
	:
	: struct spl_node *handler_glEnum(struct spl_task *task, void *data UNUSED)
	: {
	:	int ret;
	:	char *arg = spl_clib_get_string(task);
	<spl:foreach var="[]e" list="(gl_h =~ /^#define\s+(GL_[A-Z0-9_]+)\s/ANmg)">
	:	if (!strcmp(arg, "${e[1]}"))
	:		ret = ${e[1]};
	:	else
	</spl:foreach>
	:		return 0;
	:	return SPL_NEW_INT(ret);
	: }
	:
	: void SPL_ABI(spl_mod_gl_init)(struct spl_vm *vm, struct spl_module *mod UNUSED, int restore UNUSED)
	: {
	:	spl_clib_reg(vm, "glEnum", handler_glEnum, 0);
	<spl:foreach var="[]ftype" list="fdata">
	  <spl:foreach var="[]f" list="ftype.flist">
	    <?spl var funcname = "handler__${array_join(ftype.types, "__")}"; ?>
	    <spl:if code="defined skipped_handlers[funcname]">
	://	spl_clib_reg(vm, "$f", $funcname, $f);
	    </spl:if>
	    <spl:else>
	:	spl_clib_reg(vm, "$f", $funcname, $f);
	    </spl:else>
	  </spl:foreach>
	</spl:foreach>
	: }
	:
	:void SPL_ABI(spl_mod_gl_done)(struct spl_vm *vm UNUSED, struct spl_module *mod UNUSED)
	:{
	:	return;
	:}
</>);