tweak lambda evaluator
[lambda.git] / code / sample.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html><head>
3 <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
4
5
6     <title>Top Down Operator Precedence</title>
7     <style>
8 th {background-color: thistle; border: black solid 1px; text-align: left;
9     padding-left: 10px; padding-right: 10px; vertical-align: top;}
10 td {background-color: white; border: black solid 1pt; padding-left: 10px;
11     padding-right: 10px; vertical-align: top;}
12 table {width: 90%; border: 0px; cellpadding: 0px;}
13     </style>
14 </head><body bgcolor="linen">
15 <h1>Top Down Operator Precedence</h1>
16
17 <p><a href="http://www.crockford.com/">Douglas Crockford</a></p>
18
19 <p>2007-08-05</p>
20
21 <h2>Demonstration</h2>
22 <p>The text that follows is the parse tree that the parser generated by
23   parsing itself. </p>
24 <textarea id="INPUT" style="border: 2px solid black; color: black; font-family: monospace; height: 3in; overflow: auto; padding: 0.5em; width: 100%;"></textarea>
25 <input id="PARSE" value="parse" type="button">
26 <noscript><p>You may not see it because you have JavaScript turned off. Uffff!</p></noscript>
27 <script src="tokens.js"></script>
28 <script src="parse.js"></script>
29 <script src="lambda2.js"></script>
30 <script src="json2.js"></script>
31 <pre id="OUTPUT">
32 </pre>
33 <script>
34 /*jslint evil: true */
35
36 /*members create, error, message, name, prototype, stringify, toSource,
37     toString, write
38 */
39
40 /*global JSON, make_parse, parse, source, tree */
41
42 // Make a new object that inherits members from an existing object.
43
44 if (typeof Object.create !== 'function') {
45     Object.create = function (o) {
46         function F() {}
47         F.prototype = o;
48         return new F();
49     };
50 }
51
52 // Transform a token object into an exception object and throw it.
53
54 Object.prototype.error = function (message, t) {
55     t = t || this;
56     t.name = "SyntaxError";
57     t.message = message;
58     throw t;
59 };
60
61
62 (function () {
63     var parse = make_parse();
64
65     function go(source) {
66         var string, tree;
67         try {
68             tree = parse(source);
69 //             string = JSON.stringify(tree, ['key', 'name', 'message', 'value', 'arity', 'first', 'second', 'third', 'fourth'], 4);
70 //             string = JSON.stringify(tree.handler(), ['key', 'name', 'message', 'value', 'arity', 'first', 'second', 'tag', 'variable', 'left', 'right', 'bound', 'body' ], 4);
71                         string = tree.handler().to_string();
72         } catch (e) {
73             string = JSON.stringify(e, ['name', 'message', 'from', 'to', 'key',
74                     'value', 'arity', 'first', 'second', 'third', 'fourth'], 4);
75         }
76         document.getElementById('OUTPUT').innerHTML = string
77             .replace(/&/g, '&amp;')
78             .replace(/[<]/g, '&lt;');
79     }
80
81 //     go("var make_parse = " + (make_parse.toSource ?
82 //             make_parse.toSource() : make_parse.toString()) + ";");
83
84     document.getElementById('PARSE').onclick = function (e) {
85         go(document.getElementById('INPUT').value);
86     };
87 }());
88
89 </script>
90
91 </body></html>