Revert bs-a and bs-b patch.
[mpsl.git] / test.mpsl
blob8420e169bf11cccd7d7e7e6de385c948181443d8
1 /* test.mpsl */
3 /* program written in mpsl
4    (Minimum Profit Scripting Language) */
6 print("This is a test.\n");
8 /* hash */
9 days = {
10         "lunes"         => "monday",
11         "martes"        => "tuesday",
12         "miercoles"     => "wednesday",
13         "jueves"        => "thursday",
14         "viernes"       => "friday"
15         };
17 print("English for lunes is ", days.lunes, "\n");
18 print("English for martes is ", days['martes'], "\n");
19 day="miercoles";
20 print("English for ", day, " is ", days[day], "\n");
22 /* multiple level structure */
23 window = {};
24 window.ids = {};
25 wid = { 'x' => 640, 'y' => 400 };
26 window.ids['main'] = wid;
28 print("x: ", window['ids']['main']['x'], "\n");
29 print("x: ", window.ids.main.x, "\n");
31 dump(window);
33 content = glob("*.*");
34 print("Current directory: ", join(content, ", "), "\n");
35 print("Number of files: ", size(content), "\n");
37 foreach(a, content)
39         print("Value for a: ", a, "\n");
42 a = "hey!";
43 if(a eq "hey!")
45         local a;
47         a = "wow!";
48         print("Value for a: ", a, "\n");
50 print("Value for a: ", a, "\n");
52 if(a eq "hey!")
54         print("Variable scope is fine\n");
56 else
58         print("Something weird happened to variable 'a'\n");
61 math = {};
63 sub math.sum(a, b) { return(a + b); }
65 print(math.sum(5, 4), "\n");
67 _sum = math.sum;
68 print(_sum(6, 7), "\n");
70 ary = [ 0, 1, 2, 3 ];
71 print("size: ", size(ary), "\n");
72 ary[100] = 100;
73 print("size: ", size(ary), "\n");
75 a = 10;
76 b = 20;
78 /* ternary operator */
79 c = a > b && a || b;
80 print("c: ", c, "\n");
82 c = a < b && a || b;
83 print("c: ", c, "\n");
85 c = 100;
86 c %= 49;
87 print("c: ", c, "\n");
89 a = sub { print("I'm working for the world go round!\n"); };
90 a();
92 sum = sub (a, b) { return(a + b); };
93 print("sum 5 + 3: ", sum(5, 3), "\n");
94 print("sum 25 + 33: ", sum(25, 33), "\n");
96 /* greatest common divisor (Euclid's algorithm) */
97 sub gcd(m, n)
99         while (m > 0)
100         {
101                 if(n > m)
102                 {
103                         local t = m;
104                         m = n;
105                         n = t;
106                 }
108                 m -= n;
109         }
111         return(n);
114 print("gcd(100, 50) == ", gcd(100, 50), "\n");
115 print("gcd(65536, 16384) == ", gcd(65536, 16384), "\n");
116 print("gcd(123456, 654321) == ", gcd(123456, 654321), "\n");
118 sub avg(a, b) { a += b; a /= 2; }
120 print("avg(100, 50) == ", avg(100, 50), "\n");
121 print("avg(65536, 16384) == ", avg(65536, 16384), "\n");
122 print("avg(123456, 654321) == ", avg(123456, 654321), "\n");
124 on_exit_subs = [];
125 push(on_exit_subs, sub { print("Close files\n"); } );
126 push(on_exit_subs, sub { print("Clean up memory\n"); } );
127 push(on_exit_subs, sub { print("Other stuff\n"); } );
129 /* now exiting */
130 foreach(f, on_exit_subs) f();
132 math_ops = [
133         sub (a, b) { print(a, " + ", b, ": ", a + b, "\n"); },
134         sub (a, b) { print(a, " - ", b, ": ", a - b, "\n"); },
135         sub (a, b) { print(a, " * ", b, ": ", a * b, "\n"); },
136         sub (a, b) { print(a, " / ", b, ": ", a / b, "\n"); }
139 foreach(f, math_ops) f(10, 20);
141 print("cmp('sunday','monday'): ", cmp('sunday','monday'), "\n");
142 print("cmp('friday','monday'): ", cmp('friday','monday'), "\n");
143 print("cmp('sunday','sunday'): ", cmp('sunday','sunday'), "\n");
145 l = keys(days);
146 print("\nUnsorted:\n", join(l, "\n"), "\n");
148 l = sort(keys(days));
149 print("\nSorted:\n", join(l, "\n"),"\n");
151 l = sort(keys(days), sub (a, b) { cmp(a, b); });
152 print("\nSorted, with explicit callback function:\n", join(l, "\n"),"\n");
154 l = sort(keys(days), sub (a, b) { cmp(b, a); });
155 print("\nReverse-sorted:\n", join(l, "\n"),"\n");
157 l = sort([ 1, 2, 10, 20, 100, 200 ]);
158 print("\nASCII-sorted:\n", join(l, "\n"),"\n");
160 l = sort([ 1, 2, 10, 20, 100, 200 ], sub (a, b) { a - b; });
161 print("\nNumeric-sorted:\n", join(l, "\n"),"\n");
163 print("\nFile tests:\n");
165 f = open("test.txt", "w");
166 ls = [ "Strings\n", "to be written\n", "to a file.\n" ];
168 if(f != NULL)
170         local l;
172         foreach(l, ls)
173                 write(f, l);
174         close(f);
176 else
177         print("Error writing to test.txt\n");
179 f = open("test.txt", "r");
181 if(f != NULL)
183         local l;
185         foreach(l, ls)
186         {
187                 if(l eq read(f))
188                         print("Read OK\n");
189                 else
190                         print("Read failed.\n");
191         }
193         close(f);
195 else
196         print("Error reading from test.txt\n");
198 unlink("test.txt");
200 sub chop(s) { sregex(s, "/\n$/", NULL); }
202 eol = "\n";
203 sub chop2(s) { sregex(s, "/" ~ eol ~ "$/", NULL); }
205 if(chop("text with eol\n") eq "text with eol")
206         print("Sregex works!\n");
208 if(chop2("text with eol\n") eq "text with eol")
209         print("Sregex works!\n");
211 source = "#!/usr/bin/env mpsl\n\n/* I'm a mpsl script\n\tand I *feel* good! */ /* look! */\n\nexit;\n";
212 print(source);
214 /* taken from http://aspn.activestate.com/ASPN/Cookbook/Rx/Recipe/59811 */
215 r = "|/\*[^*]*\*+([^/*][^*]*\*+)*/|";
217 sub substr(s, o, l) { local w=splice(s, NULL, o, l); return(w[1]); }
219 v = regex(source, r);
220 dump(v);
221 /* get last matching coords */
222 c = regex();
224 v = regex(source, r, c[0] + c[1]);
225 dump(v);
226 /* get last matching coords */
227 c = regex();
229 v = regex(source, r, c[0] + c[1]);
230 dump(v);
232 /* assignations as expressions */
233 print("\nAssignations as expressions: cat file\n");
234 if(f = open("config.h", "r"))
236         /* again assignations as expressions */
237         while(v = read(f))
238                 print(v);
240         close(f);
243 print("\nAssignations as expressions: looped regex matching\n");
244 c = 0;
246 while(v = regex(source, r, c))
248         print(v, "\n");
249         c = regex();
250         c = c[0] + c[1];
253 print("Various encodings\n");
255 foreach(enc, [ "UTF-8", NULL, "ISO-8859-1", "INVALID" ])
257         print("Encoding ", enc, ": ", encoding(enc), "\n");
261 ret = load("test2.mpsl");
262 print("test2.mpsl returned ", ret, "\n");
263 INC = [ "mpsl" ];
264 ret = load("test2.mpsl");
265 print("test2.mpsl returned ", ret, "\n");
268 sub dumper(v, i)
270         local ret = "";
271         local ind = "";
272         local n;
274         /* create the indentation string */
275         foreach(n, [ 0 .. i ])
276                 ind = ind ~ "  ";
278         if(is_exec(v))
279                 ret = "sub { 'DUMMY' }";
280         else
281         if (is_hash(v))
282         {
283                 local w = [];
285                 ret = "{\n";
287                 foreach(n, keys(v))
288                         push(w, dumper(n, i + 1) ~ " => " ~ dumper(v[n], i + 1));
290                 ret = ret ~ join(w, ",\n") ~ "\n" ~ ind ~ "}";
291         }
292         else
293         if (is_array(v))
294         {
295                 local w = [];
297                 ret = "[\n";
299                 foreach(n, v)
300                         push(w, dumper(n, i + 1));
302                 ret = ret ~ join(w, ",\n") ~ "\n" ~ ind ~ "]";
303         }
304         else
305         if(regex("/^[0-9\.]+$/", v))
306                 ret = v;
307         else
308                 ret = "'" ~ v ~ "'";
310         return(ind ~ ret);
313 foreach(v, [ "hola", 100, 5 + 4, 4.6, [ 1, 2, 3],
314         { 'a' => 1, 'b' => { 'bb' => 11, 'cc' => 22 } }, sub { 1; } ])
315         print("v = ", dumper(v), "\n");
317 print("More regex tests (matching words)\n");
319 v = "This 'is' a word, 'other5-w1th_1234' numb3r5\nseveral:'punctuation;marks!!!'s_func(100,23);";
320 print("Test phrase: ", v, "\n");
322 r = "/\w+/";
323 c = 0;
325 print("Words:\n");
326 while(w = regex(v, r, c))
328         print(w, "\n");
329         c = regex();
330         c = c[0] + c[1];
333 r = "/'[^']*'/";
334 c = 0;
336 print("Quotes:\n");
337 while(w = regex(v, r, c))
339         print(w, "\n");
340         c = regex();
341         c = c[0] + c[1];
344 print("Hexadecimal and octal numbers:\n");
346 v = 0xff;
348 if(v == 255)
349         print("Hexadecimal numbers work!!!\n");
350 else
351         print("Hexadecimal numbers DON'T work!!!\n");
353 v = 0177;
355 if(v == 127)
356         print("Octal numbers work!!!\n");
357 else
358         print("Octal numbers DON'T work!!!\n");
361 s = "This is a %s and a %d and another %s and a %f";
362 dict = { "%s" => "STRING", "%d" => "INTEGER", "%f" => "FLOAT" };
363 print(sregex(s, "/%\w/g", sub (a) { dict[a]; } ), "\n");
365 /* eval as compilable code */
366 v = eval("1 + 2;");
367 print("v is ", v, " (must be 3)\n");
369 /* eval as an executable value */
370 v = eval(sub { 3 + 4; });
371 print("v is ", v, " (must be 7)\n");
373 ERROR = NULL;
374 eval("func(");
375 print("Error is ", ERROR, " (it's ok)\n");
377 eval("error('hey!');");
378 print("Error is ", ERROR, " (it's ok)\n");
380 eval("eval(\"error('you!');\");");
381 print("Error is ", ERROR, " (it's ok)\n");
383 local n = 1;
385 foreach(n, [ 1, 2, 3 ]) {
386         print("Iterator: ", n, "\n");
389 print("n should still be 1: ", n, "\n");