3 /* program written in mpsl
4 (Minimum Profit Scripting Language) */
6 print("This is a test.\n");
11 "martes" => "tuesday",
12 "miercoles" => "wednesday",
13 "jueves" => "thursday",
17 print("English for lunes is ", days.lunes, "\n");
18 print("English for martes is ", days['martes'], "\n");
20 print("English for ", day, " is ", days[day], "\n");
22 /* multiple level structure */
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");
33 content = glob("*.*");
34 print("Current directory: ", join(content, ", "), "\n");
35 print("Number of files: ", size(content), "\n");
39 print("Value for a: ", a, "\n");
48 print("Value for a: ", a, "\n");
50 print("Value for a: ", a, "\n");
54 print("Variable scope is fine\n");
58 print("Something weird happened to variable 'a'\n");
63 sub math.sum(a, b) { return(a + b); }
65 print(math.sum(5, 4), "\n");
68 print(_sum(6, 7), "\n");
71 print("size: ", size(ary), "\n");
73 print("size: ", size(ary), "\n");
78 /* ternary operator */
80 print("c: ", c, "\n");
83 print("c: ", c, "\n");
87 print("c: ", c, "\n");
89 a = sub { print("I'm working for the world go round!\n"); };
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) */
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");
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"); } );
130 foreach(f, on_exit_subs) f();
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");
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" ];
177 print("Error writing to test.txt\n");
179 f = open("test.txt", "r");
190 print("Read failed.\n");
196 print("Error reading from test.txt\n");
200 sub chop(s) { sregex(s, "/\n$/", NULL); }
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";
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);
221 /* get last matching coords */
224 v = regex(source, r, c[0] + c[1]);
226 /* get last matching coords */
229 v = regex(source, r, c[0] + c[1]);
232 /* assignations as expressions */
233 print("\nAssignations as expressions: cat file\n");
234 if(f = open("config.h", "r"))
236 /* again assignations as expressions */
243 print("\nAssignations as expressions: looped regex matching\n");
246 while(v = regex(source, r, c))
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");
264 ret = load("test2.mpsl");
265 print("test2.mpsl returned ", ret, "\n");
274 /* create the indentation string */
275 foreach(n, [ 0 .. i ])
279 ret = "sub { 'DUMMY' }";
288 push(w, dumper(n, i + 1) ~ " => " ~ dumper(v[n], i + 1));
290 ret = ret ~ join(w, ",\n") ~ "\n" ~ ind ~ "}";
300 push(w, dumper(n, i + 1));
302 ret = ret ~ join(w, ",\n") ~ "\n" ~ ind ~ "]";
305 if(regex("/^[0-9\.]+$/", v))
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");
326 while(w = regex(v, r, c))
337 while(w = regex(v, r, c))
344 print("Hexadecimal and octal numbers:\n");
349 print("Hexadecimal numbers work!!!\n");
351 print("Hexadecimal numbers DON'T work!!!\n");
356 print("Octal numbers work!!!\n");
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 */
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");
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");
385 foreach(n, [ 1, 2, 3 ]) {
386 print("Iterator: ", n, "\n");
389 print("n should still be 1: ", n, "\n");