-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvary.js
195 lines (187 loc) · 6.67 KB
/
vary.js
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
//
// In C, it's difficult to have code that is variadic based on run time
// parameters. For instance you can't make C easily create a long as variable
// "a" in some cases and a byte as variable "a" in other cases based on some
// runtime variable's contents. You have to switch based on the type, often
// allocate memory and do a lot of extra work, and that makes everything
// complicated and time consuming to write.
//
// This code creates some macros that make it easier to write C code that
// varies by type.
//
// TODO VARY_*() macros need to handle general list more robustly
// TODO VARY_*() should be able to cast to best sizes in some circumstances
// TODO consider switching to ribosome of these kinds of scripts
// TODO VARY_*() macros should be rewritten in terms of the minimum subset of types they accept
//
var lib = require('./common.js');
function skip(t) {
t=t[1];
return (lib.dontcast.indexOf(t)!=-1)?true:false;
}
console.log(lib.prelude +
"// vary on a single element. unpacks x[i] into a C variable \n"+
"// called _x, of the correct c native type. then executes \n"+
"// your code (stmt). If it's a type that can't be unpacked, \n"+
"// the TAG of the type is set in failvar, you can handle it. \n\n"+
"// VARY_EL varies over one element: x[i]. \n"+
"#define VARY_EL(x,i,stmt,failvar) ({ \\");
var tmpls=[
"\tif(x->t=={{x0}}){/*cant vary {{x2}}*/ failvar={{x0}};}\\",
"\tif(x->t=={{x0}}){/*{{x2}}*/\\\n"+
"\t\t{{x3}} _x=AS_{{x1}}(x,i);\\\n"+
"\t\tstmt;}\\"
];
lib.each(lib.types,function(tx) {
if(skip(tx))tmpl=tmpls[0];
else tmpl=tmpls[1];
var a=[];
for(var i in tx)a["x"+i]=tx[i];
console.log(lib.exhaust(lib.projr(lib.repl,a),tmpl));
});
console.log("})");
console.log("#define VARY_EACH(x,stmt,failvar) ({ \\\n" +
"\tint _i=0,_xn=x->n,_xt=x->t; /*PF(\"VE\");DUMP(x);*/\\");
var tmpl="\tif(_xt=={{x0}}){/*cant vary {{x2}}*/ failvar={{x0}}; }\\";
lib.each(lib.types,function(tx) {
if(skip(tx)){
var a=[];
for(var i in tx)a["x"+i]=tx[i];
console.log(lib.exhaust(lib.projr(lib.repl,a),tmpl));
}
});
var tmpl="\tif(_xt=={{x0}}){/*{{x2}}*/ \\\n" +
"\t\t{{x3}} _x,_xtmp=0;\\\n" +
"\t\twhile (_i < _xn) { _x=AS_{{x1}}(x,_i); /* printf(\"%d {{5}}\\n\", _i, _x); */ stmt; _i++; }\\\n" +
"\t}\\";
lib.each(lib.types,function(tx) {
if(skip(tx))return;
var a=[];
for(var i in tx)a["x"+i]=tx[i];
console.log(lib.exhaust(lib.projr(lib.repl,a),tmpl));
});
console.log("})");
console.log("#define VARY_EACHLIST(x,stmt,failvar) ({ \\\n" +
"\tint _i=0,_xn=x->n,_xt=x->t; /*PF(\"VE\");DUMP(x);*/\\");
var tmpl="\tif(_xt=={{x0}}){/*cant vary {{x2}}*/ failvar={{x0}}; }\\";
lib.each(lib.types,function(tx) {
if(tx[1] != 'l' && skip(tx)){
var a=[];
for(var i in tx)a["x"+i]=tx[i];
console.log(lib.exhaust(lib.projr(lib.repl,a),tmpl));
}
});
var tmpl="\tif(_xt=={{x0}}){/*{{x2}}*/ \\\n" +
"\t\t{{x3}} _x,_xtmp=0;\\\n" +
"\t\twhile (_i < _xn) { _x=AS_{{x1}}(x,_i); /* printf(\"%d {{5}}\\n\", _i, _x); */ stmt; _i++; }\\\n" +
"\t}\\";
lib.each(lib.types,function(tx) {
if(tx[1] != 'l' && skip(tx))return;
var a=[];
for(var i in tx)a["x"+i]=tx[i];
console.log(lib.exhaust(lib.projr(lib.repl,a),tmpl));
});
console.log("})");
console.log("#define VARY_EACHBOTH(x,y,stmt,failvar) ({ \\\n" +
"\tint _i=0,_j=0,_xn=x->n,_yn=y->n,_xt=x->t,_yt=y->t;\\");
var tmpl="\tif(_xt=={{x0}}||_yt=={{x0}}){/*cant vary {{x2}}*/ failvar={{x0}}; }\\";
lib.each(lib.types,function(tx) {
if(tx[1] != 'l' && skip(tx)){
var a=[];
for(var i in tx)a["x"+i]=tx[i];
console.log(lib.exhaust(lib.projr(lib.repl,a),tmpl));
}
});
var tmpl="\tif(_xt=={{x0}}&&_yt=={{y0}}){/*{{x2}} x {{y2}}*/ \\\n" +
"\t\t{{x3}} _x,_xtmp=0; {{y3}} _y,_ytmp;\\\n" +
"\t\twhile (_i<_xn && _j<_yn) { _x=AS_{{x1}}(x,_i%_xn); _y=AS_{{y1}}(y,_j%_yn); stmt; \\\n"+
"\t\tif(!SCALAR(x)) {_i++;} _j++; }\\\n" +
"\t}\\";
lib.each(lib.types,function(tx) {
if(skip(tx))return;
lib.each(lib.types, function(ty) {
if(skip(ty))return;
var a=[];
for(var i in tx)a["x"+i]=tx[i];
for(var i in ty)a["y"+i]=ty[i];
console.log(lib.exhaust(lib.projr(lib.repl,a),tmpl));
});
});
console.log("})");
// identical to VARY_EACHBOTH, but allows x or y to be a list
console.log("#define VARY_EACHBOTHLIST(x,y,stmt,failvar) ({ \\\n" +
"\tint _i=0,_j=0,_xn=x->n,_yn=y->n,_xt=x->t,_yt=y->t;\\");
var tmpl="\tif(_xt=={{x0}}||_yt=={{x0}}){/*cant vary {{x2}}*/ failvar={{x0}}; }\\";
lib.each(lib.types,function(tx) {
if(tx[1] != 'l' && skip(tx)){
var a=[];
for(var i in tx)a["x"+i]=tx[i];
console.log(lib.exhaust(lib.projr(lib.repl,a),tmpl));
}
});
var tmpl="\tif(_xt=={{x0}}&&_yt=={{y0}}){/*{{x2}} x {{y2}}*/ \\\n" +
"\t\t{{x3}} _x,_xtmp=0; {{y3}} _y,_ytmp;\\\n" +
"\t\twhile (_i<_xn && _j<_yn) { _x=AS_{{x1}}(x,_i%_xn); _y=AS_{{y1}}(y,_j%_yn); stmt; \\\n"+
"\t\tif(!SCALAR(x)) {_i++;} _j++; }\\\n" +
"\t}\\";
lib.each(lib.types,function(tx) {
if(tx[1] != 'l' && skip(tx))return;
lib.each(lib.types, function(ty) {
if(skip(ty))return;
var a=[];
for(var i in tx)a["x"+i]=tx[i];
for(var i in ty)a["y"+i]=ty[i];
console.log(lib.exhaust(lib.projr(lib.repl,a),tmpl));
});
});
console.log("})");
console.log("#define VARY_EACHLEFT(x,y,stmt,failvar) ({ \\\n" +
"\tint _i=0,_j=0,_xn=x->n,_yn=y->n,_xt=x->t,_yt=y->t;\\");
var tmpl="\tif(_xt=={{x0}}||_yt=={{x0}}){/*cant vary {{x2}}*/ failvar={{x0}}; }\\";
lib.each(lib.types,function(tx) {
if(skip(tx)){
var a=[];
for(var i in tx)a["x"+i]=tx[i];
console.log(lib.exhaust(lib.projr(lib.repl,a),tmpl));
}
});
var tmpl="\tif(_xt=={{x0}}&&_yt=={{y0}}){/*{{x2}} x {{y2}}*/ \\\n" +
"\t\t{{x3}} _x;{{y3}} _y; _y=AS_{{x1}}(y,0);\\\n" +
"\t\twhile (_i < _xn) { _x=AS_{{x1}}(x,_i); stmt; _i++; }\\\n" +
"\t}\\";
lib.each(lib.types,function(tx) {
if(skip(tx))return;
lib.each(lib.types, function(ty) {
if(skip(ty))return;
var a=[];
for(var i in tx)a["x"+i]=tx[i];
for(var i in ty)a["y"+i]=ty[i];
console.log(lib.exhaust(lib.projr(lib.repl,a),tmpl));
});
});
console.log("})");
console.log("#define VARY_EACHRIGHT(x,y,stmt,failvar) ({ \\\n" +
"\tint _i=0,_j=0,_xn=x->n,_yn=y->n,_xt=x->t,_yt=y->t;\\");
var tmpl="\tif(_xt=={{x0}}||_yt=={{x0}}){/*cant vary {{x2}}*/ failvar={{x0}}; }\\";
lib.each(lib.types,function(tx) {
if(skip(tx)){
var a=[];
for(var i in tx)a["x"+i]=tx[i];
console.log(lib.exhaust(lib.projr(lib.repl,a),tmpl));
}
});
var tmpl="\tif(_xt=={{x0}}&&_yt=={{y0}}){/*{{x2}} x {{y2}}*/ \\\n" +
"\t\t{{x3}} _x;{{y3}} _y; _x=AS_{{x1}}(x,0);\\\n" +
"\t\twhile (_j < _yn) { _y=AS_{{y1}}(y,_j); stmt; _j++; }\\\n" +
"\t}\\";
lib.each(lib.types,function(tx) {
if(skip(tx))return;
lib.each(lib.types, function(ty) {
if(skip(ty))return;
var a=[];
for(var i in tx)a["x"+i]=tx[i];
for(var i in ty)a["y"+i]=ty[i];
console.log(lib.exhaust(lib.projr(lib.repl,a),tmpl));
});
});
console.log("})");