annotate jsbackend.js @ 19:132c7756860e

Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
author Mike Pavone <pavone@retrodev.com>
date Sun, 25 Mar 2012 16:11:19 -0700
parents 6e4851a204a5
children bf03c9f0dd55
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1 var mainModule;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
2
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
3 function toobj(val)
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
4 {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
5 switch(typeof val)
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
6 {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
7 case 'boolean':
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
8 if(val) {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
9 return mainModule.strue;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
10 } else {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
11 return mainModule.sfalse;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
12 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
13 case 'number':
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
14 return mainModule.snumber(val);
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
15 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
16 throw new Error("can't make val into object");
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
17 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
18
10
02b20292f187 Added fib sample
Mike Pavone <pavone@retrodev.com>
parents: 9
diff changeset
19 function setP(o, p, val)
02b20292f187 Added fib sample
Mike Pavone <pavone@retrodev.com>
parents: 9
diff changeset
20 {
02b20292f187 Added fib sample
Mike Pavone <pavone@retrodev.com>
parents: 9
diff changeset
21 o[p] = val;
02b20292f187 Added fib sample
Mike Pavone <pavone@retrodev.com>
parents: 9
diff changeset
22 return o;
02b20292f187 Added fib sample
Mike Pavone <pavone@retrodev.com>
parents: 9
diff changeset
23 }
02b20292f187 Added fib sample
Mike Pavone <pavone@retrodev.com>
parents: 9
diff changeset
24
02b20292f187 Added fib sample
Mike Pavone <pavone@retrodev.com>
parents: 9
diff changeset
25 function getP(o, p)
02b20292f187 Added fib sample
Mike Pavone <pavone@retrodev.com>
parents: 9
diff changeset
26 {
02b20292f187 Added fib sample
Mike Pavone <pavone@retrodev.com>
parents: 9
diff changeset
27 return o[p];
02b20292f187 Added fib sample
Mike Pavone <pavone@retrodev.com>
parents: 9
diff changeset
28 }
02b20292f187 Added fib sample
Mike Pavone <pavone@retrodev.com>
parents: 9
diff changeset
29
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
30 op.prototype.toJS = function(isReceiver) {
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
31 var ret = '(' + this.left.toJS() +' '+ (this.op == '=' ? '==' : this.op) +' '+ this.right.toJS() + ')';
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
32 if (isReceiver) {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
33 ret = 'toobj' + ret;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
34 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
35 return ret;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
36 };
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
37
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
38 symbol.prototype.toJS = function() {
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
39 var name = this.cleanName();
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
40 if (name == 'self') {
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
41 return this.symbols.selfVar();
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
42 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
43 name = name.replace("_", "UN_").replace(":", "CN_").replace("!", "EX_").replace('?', 'QS_').replace('@', 'AT_');
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
44 var reserved = {'true': true, 'false': true, 'this': true, 'if': true, 'else': true, 'NaN': true};
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
45 if (name in reserved) {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
46 name = 's' + name;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
47 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
48 return name;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
49 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
50
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
51 intlit.prototype.toJS = function() {
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
52 return this.val.toString();
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
53 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
54
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
55 floatlit.prototype.toJS = function() {
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
56 return this.val.toString();
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
57 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
58
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
59 strlit.prototype.toJS = function() {
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
60 return '"' + this.val.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r') + '"';
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
61 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
62
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
63 funcall.prototype.toJS = function() {
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
64 var name = this.name[this.name.length-1] == ':' ? this.name.substr(0, this.name.length-1) : this.name;
12
6e4851a204a5 Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents: 10
diff changeset
65 if (name == 'foreign') {
6e4851a204a5 Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents: 10
diff changeset
66 if ((this.args[0] instanceof lambda) || (this.args[0] instanceof object)) {
6e4851a204a5 Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents: 10
diff changeset
67 return null;
6e4851a204a5 Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents: 10
diff changeset
68 } else if(this.args[0] instanceof symbol) {
6e4851a204a5 Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents: 10
diff changeset
69 return this.args[0].name;
6e4851a204a5 Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents: 10
diff changeset
70 } else {
6e4851a204a5 Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents: 10
diff changeset
71 throw new Error("Unexpected AST type for foreign:");
6e4851a204a5 Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents: 10
diff changeset
72 }
6e4851a204a5 Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents: 10
diff changeset
73 }
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
74 var args = this.args.slice(0, this.args.length);
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
75 if (this.receiver) {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
76 args.splice(0, 0, this.receiver);
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
77 }
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
78 var funinfo = this.symbols.find(name);
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
79 if (!funinfo) {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
80 var receiver = args[0];
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
81 args.splice(0, 1);
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
82 for (var i in args) {
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
83 args[i] = args[i].toJS();
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
84 }
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
85 return receiver.toJS(true) + '.' + (new symbol(name, this.symbols)).toJS() + '(' + args.join(', ') + ')';
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
86 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
87 switch(funinfo.type)
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
88 {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
89 case 'self':
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
90 if (args.length < funinfo.def.args.length || funinfo.def.args[0].name != 'self') {
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
91 var receiver = new symbol('self', this.symbols);
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
92 } else {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
93 var receiver = args[0];
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
94 args.splice(0, 1);
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
95 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
96 for (var i in args) {
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
97 args[i] = args[i].toJS();
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
98 }
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
99 return receiver.toJS(true) + '.' + (new symbol(name, this.symbols)).toJS() + '(' + args.join(', ') + ')';
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
100 case 'parent':
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
101 var ret = 'this';
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
102 for (var i = 0; i < funinfo.depth; ++i) {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
103 ret += '.parent';
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
104 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
105 for (var i in args) {
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
106 args[i] = args[i].toJS();
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
107 }
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
108 ret += (new symbol(name, this.symbols)).toJS() + '(' + args.join(', ') + ')';
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
109 return ret;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
110 case 'local':
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
111 case 'upvar':
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
112 case 'foreign':
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
113 for (var i in args) {
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
114 args[i] = args[i].toJS();
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
115 }
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
116 return (new symbol(name, this.symbols)).toJS() + '(' + args.join(', ') + ')';
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
117 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
118 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
119
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
120 object.prototype.toJS = function() {
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
121 var messages = this.messages;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
122 var compiled = []
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
123 for (var i in messages) {
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
124 var js = messages[i].toJSObject();
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
125 if (js) {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
126 compiled.push(indent(js));
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
127 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
128 }
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
129 return '{\n\tparent: ' + this.symbols.parentObject() + ',\n\t' + compiled.join(',\n\t') + '\n}';
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
130 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
131
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
132 object.prototype.toJSModule = function() {
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
133 this.populateSymbols(null);
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
134 return '(function () {\n\tvar module = ' + indent(this.toJS()) + ';\n\treturn module;\n})'
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
135 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
136
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
137 lambda.prototype.toJS = function() {
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
138 var args = this.args ? this.args.slice(0, this.args.length) : [];
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
139 if (args.length && args[0].cleanName() == 'self') {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
140 args.splice(0, 1);
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
141 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
142 var exprs = this.expressions;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
143 for (var i in args) {
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
144 args[i] = args[i].toJS();
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
145 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
146 var compiled = []
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
147 for (var i in exprs) {
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
148 var js = exprs[i].toJS();
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
149 if (js) {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
150 compiled.push(indent(js));
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
151 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
152 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
153 exprs = compiled;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
154 if (exprs.length) {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
155 exprs[exprs.length-1] = 'return ' + exprs[exprs.length-1] + ';';
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
156 }
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
157 return 'function (' + args.join(', ') + ') {\n\t' + (this.symbols.needsSelfVar ? 'var self = this;\n\t' : '') + exprs.join(';\n\t') + '\n}'
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
158 };
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
159 lambda.prototype.toJSModule = function() {
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
160 this.populateSymbols(null);
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
161 return this.toJS();
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
162 }
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
163
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
164 assignment.prototype.toJS = function() {
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
165 var existing = this.symbols.find(this.symbol.name);
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
166 var prefix = '';
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
167 if (!existing) {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
168 prefix = 'var ';
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
169 } else {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
170 switch (existing.type)
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
171 {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
172 case 'self':
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
173 prefix = 'this.';
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
174 break;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
175 case 'parent':
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
176 prefix = 'this.';
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
177 for (var i = 0; i < existing.depth; ++i) {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
178 prefix += 'parent.';
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
179 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
180 break;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
181 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
182 }
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
183 var val = this.expression.toJS();
12
6e4851a204a5 Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents: 10
diff changeset
184 if (val === null) {
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
185 return null;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
186 }
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
187 return prefix + this.symbol.toJS() + ' = ' + val;
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
188 };
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
189 assignment.prototype.toJSObject = function() {
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
190 var val = this.expression.toJS();
12
6e4851a204a5 Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents: 10
diff changeset
191 if (val === null) {
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
192 return null;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
193 }
19
132c7756860e Use populateSymbols to generate symbol tables during compilation rather than populating them as we go. This change allows us to refer to symbols defined later in the input stream and also gives the symbol table logic a single home that can be used both by the compiler and editor.
Mike Pavone <pavone@retrodev.com>
parents: 12
diff changeset
194 return this.symbol.toJS() + ': ' + val;
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
195 };