2
// A test of some JavaScript-like syntax in mskp.
6
// Convention `@ ...' is used to specify formal arguments in functions.
8
function factorial `@ n' {
13
return [n * (factorial [n - 1])];
17
var name (prompt "What is your name?");
18
alert ["Welcome " + name];
20
// tag:{ a b; c d } -> tag( (a b) (c d) )
21
// Convention function(...) -> (function ...)
23
function add `@ i j' {
24
// Note that there is no = sign for a var assignment
25
var add_pri function:{ `@ x y';
31
function showclosure {
38
function makeinc `@ initialValue' {
39
var count initialValue;
41
// Convention (_++ A) is post-increment (A++)
42
// FIXME ++ should be _++
47
function unlimited_args {
48
for `init {var i 0}; test [i < [arguments . length]]; incr (++ i)' {
49
alert [arguments . [i]];
56
* LCMCalculator example
59
function LCMCalculator `@ x y' {
60
function checkInt `@ x' {
62
throw (new TypeError [x + " is not an integer"]);
66
[[this . a] = (checkInt x)];
67
[[this . b] = (checkInt y)];
69
// = used in prefix style
70
// Convention %:{...} is object literal
71
// Convention =>(A B) is object pair
72
= [LCMCalculator . prototype] = %:{ =>{
74
// Convention var `= name value; = name value'
76
= a ([Math . abs] [this . a])
77
= b ([Math . abs] [this . b])
81
[t = b]; [b = a]; [a = t];
88
[[this . "gcd"] = function:{ return a; }];
92
// FIXME DIV should be /
93
var lcm [[this . a] DIV ([this . gcd]) * [this . b]];
94
[[this . lcm] = function:{ return lcm; }];
98
return ["LCMCalculator: a = " + [this . a] + ", b = " + this.b];
102
// Convention @(...) is array literal
103
// Thus @( @{ A B; C D } ) -> JS [[A,B],[C,D]]
105
// Convention -chain(A -mcall(F B B2 B3) -mcall(G C C2 C3))
106
// -> ([([A . F] B B2 B3) . G] C C2 C3)
107
// -> JS A.F(B,B2,B3).G(C,C2,C3)
110
@(@{25 55; 21 56; 22 58; 28 56});
112
map function:{ `@ pair';
113
return (new LCMCalculator [pair . 0] [pair . 1]);
115
sort function:{ `@ a b';
116
return [ ([a . lcm]) - ([b . lcm]) ];
118
forEach function:{ `@ obj';
119
print [obj + ", gcd = " + ([obj . gcd]) + ", lcm = " + ([obj . lcm])];