~psmay/+junk/mskp-parser.dev

« back to all changes in this revision

Viewing changes to input3.txt

  • Committer: Peter S. May
  • Date: 2011-01-05 03:32:30 UTC
  • Revision ID: peter_s._may_httppsmay.com-20110105033230-ndck6z0u8a8wtv5g
* input3.txt
        New test file rehashes JavaScript examples from Wikipedia in mskp. It currently parses basically okay, but two tokens had to be changed, indicating that there's some debugging to be done in the lexer/parser.

* build.xml
        Now runs input3.txt as a specimen.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
// A test of some JavaScript-like syntax in mskp.
 
3
 
 
4
// From Wikipedia
 
5
 
 
6
// Convention `@ ...' is used to specify formal arguments in functions.
 
7
 
 
8
function factorial `@ n' {
 
9
        if [n == 0] {
 
10
                return 1;
 
11
        };
 
12
        else {
 
13
                return [n * (factorial [n - 1])];
 
14
        };
 
15
};
 
16
 
 
17
var name (prompt "What is your name?");
 
18
alert ["Welcome " + name];
 
19
 
 
20
// tag:{ a b; c d } -> tag( (a b) (c d) )
 
21
// Convention function(...) -> (function ...)
 
22
 
 
23
function add `@ i j' {
 
24
        // Note that there is no = sign for a var assignment
 
25
        var add_pri function:{ `@ x y';
 
26
                return [x + y];
 
27
        };
 
28
        return (add_pri i j);
 
29
};
 
30
 
 
31
function showclosure {
 
32
        var inc (makeinc 1);
 
33
        inc; //1
 
34
        inc; //2
 
35
        inc; //3
 
36
};
 
37
 
 
38
function makeinc `@ initialValue' {
 
39
        var count initialValue;
 
40
        return function:{
 
41
                // Convention (_++ A) is post-increment (A++)
 
42
                // FIXME ++ should be _++
 
43
                return [count ++];
 
44
        };
 
45
};
 
46
 
 
47
function unlimited_args {
 
48
        for `init {var i 0}; test [i < [arguments . length]]; incr (++ i)' {
 
49
                alert [arguments . [i]];
 
50
        };
 
51
}
 
52
unlimited_args 1 2 3;
 
53
 
 
54
 
 
55
/*
 
56
 * LCMCalculator example
 
57
 */
 
58
 
 
59
function LCMCalculator `@ x y' {
 
60
        function checkInt `@ x' {
 
61
                if [[x % 1] != 0] {
 
62
                        throw (new TypeError [x + " is not an integer"]);
 
63
                };
 
64
                return x;
 
65
        };
 
66
        [[this . a] = (checkInt x)];
 
67
        [[this . b] = (checkInt y)];
 
68
};
 
69
// = used in prefix style
 
70
// Convention %:{...} is object literal
 
71
// Convention =>(A B) is object pair
 
72
= [LCMCalculator . prototype] = %:{ =>{
 
73
        gcd function:{
 
74
                // Convention var `= name value; = name value'
 
75
                var `
 
76
                        = a ([Math . abs] [this . a])
 
77
                        = b ([Math . abs] [this . b])
 
78
                        = t
 
79
                ';
 
80
                if [a < b] {
 
81
                        [t = b]; [b = a]; [a = t];
 
82
                };
 
83
                while [b !== 0] {
 
84
                        [t = b];
 
85
                        [b = [a % b]];
 
86
                        [a = t];
 
87
                };
 
88
                [[this . "gcd"] = function:{ return a; }];
 
89
                return a;
 
90
        };
 
91
        "lcm" function:{
 
92
                // FIXME DIV should be /
 
93
                var lcm [[this . a] DIV ([this . gcd]) * [this . b]];
 
94
                [[this . lcm] = function:{ return lcm; }];
 
95
                return lcm;
 
96
        };
 
97
        toString function:{
 
98
                return ["LCMCalculator: a = " + [this . a] + ", b = " + this.b];
 
99
        };
 
100
}};
 
101
 
 
102
// Convention @(...) is array literal
 
103
// Thus @( @{ A B; C D } ) -> JS [[A,B],[C,D]]
 
104
 
 
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)
 
108
 
 
109
-chain:{
 
110
        @(@{25 55; 21 56; 22 58; 28 56});
 
111
        -mcall{
 
112
                map function:{ `@ pair';
 
113
                        return (new LCMCalculator [pair . 0] [pair . 1]);
 
114
                };
 
115
                sort function:{ `@ a b';
 
116
                        return [ ([a . lcm]) - ([b . lcm]) ];
 
117
                };
 
118
                forEach function:{ `@ obj';
 
119
                        print [obj + ", gcd = " + ([obj . gcd]) + ", lcm = " + ([obj . lcm])];
 
120
                };
 
121
        };
 
122
};