Mercurial > repos > tabletprog
annotate jsbackend.js @ 21:6c8ae6b47ab5
Small improvements to property support and elimination of setP and getP functions as they are no longer needed
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 25 Mar 2012 21:11:10 -0700 |
parents | bf03c9f0dd55 |
children | 068d63627b16 |
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 |
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
|
19 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
|
20 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
|
21 if (isReceiver) { |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 ret = 'toobj' + ret; |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 } |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 return ret; |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 }; |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 |
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
|
27 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
|
28 var name = this.cleanName(); |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 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
|
30 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
|
31 } |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 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
|
33 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
|
34 if (name in reserved) { |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 name = 's' + name; |
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 return name; |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 } |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 |
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
|
40 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
|
41 return this.val.toString(); |
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 |
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
|
44 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
|
45 return this.val.toString(); |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 } |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 |
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
|
48 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
|
49 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
|
50 } |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 |
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
|
52 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
|
53 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
|
54 if (name == 'foreign') { |
6e4851a204a5
Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
55 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
|
56 return null; |
6e4851a204a5
Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
57 } else if(this.args[0] instanceof symbol) { |
6e4851a204a5
Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
58 return this.args[0].name; |
6e4851a204a5
Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
59 } else { |
6e4851a204a5
Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
60 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
|
61 } |
6e4851a204a5
Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
62 } |
8
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 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
|
64 if (this.receiver) { |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 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
|
66 } |
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
|
67 var funinfo = this.symbols.find(name); |
20
bf03c9f0dd55
Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents:
19
diff
changeset
|
68 if (!funinfo || funinfo.def instanceof setter) { |
8
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 var receiver = args[0]; |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 args.splice(0, 1); |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 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
|
72 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
|
73 } |
20
bf03c9f0dd55
Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents:
19
diff
changeset
|
74 var rJS = receiver.toJS(true); |
bf03c9f0dd55
Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents:
19
diff
changeset
|
75 if ((name[name.length-1] == '!' && args.length == 1) || (funinfo && funinfo.def instanceof setter)) { |
21
6c8ae6b47ab5
Small improvements to property support and elimination of setP and getP functions as they are no longer needed
Mike Pavone <pavone@retrodev.com>
parents:
20
diff
changeset
|
76 console.log(name.substr(0, name.length-1)); |
6c8ae6b47ab5
Small improvements to property support and elimination of setP and getP functions as they are no longer needed
Mike Pavone <pavone@retrodev.com>
parents:
20
diff
changeset
|
77 return '(' + rJS + '.' + (new symbol(name.substr(0, name.length-1), this.symbols)).toJS() + ' = ' + args[0] + ', ' + rJS + ')' |
20
bf03c9f0dd55
Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents:
19
diff
changeset
|
78 } else { |
21
6c8ae6b47ab5
Small improvements to property support and elimination of setP and getP functions as they are no longer needed
Mike Pavone <pavone@retrodev.com>
parents:
20
diff
changeset
|
79 var callCode = rJS + '.' + (new symbol(name, this.symbols)).toJS() + '(' + args.join(', ') + ')'; |
6c8ae6b47ab5
Small improvements to property support and elimination of setP and getP functions as they are no longer needed
Mike Pavone <pavone@retrodev.com>
parents:
20
diff
changeset
|
80 if (args.length == 0) { |
6c8ae6b47ab5
Small improvements to property support and elimination of setP and getP functions as they are no longer needed
Mike Pavone <pavone@retrodev.com>
parents:
20
diff
changeset
|
81 return '(' + rJS + ' instanceof Function ? ' + callCode + ' : ' + callCode.substr(0, callCode.length-2) + ')'; |
6c8ae6b47ab5
Small improvements to property support and elimination of setP and getP functions as they are no longer needed
Mike Pavone <pavone@retrodev.com>
parents:
20
diff
changeset
|
82 } else { |
6c8ae6b47ab5
Small improvements to property support and elimination of setP and getP functions as they are no longer needed
Mike Pavone <pavone@retrodev.com>
parents:
20
diff
changeset
|
83 return callCode; |
6c8ae6b47ab5
Small improvements to property support and elimination of setP and getP functions as they are no longer needed
Mike Pavone <pavone@retrodev.com>
parents:
20
diff
changeset
|
84 } |
20
bf03c9f0dd55
Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents:
19
diff
changeset
|
85 } |
8
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
86 } |
20
bf03c9f0dd55
Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents:
19
diff
changeset
|
87 var ret = ''; |
8
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
88 switch(funinfo.type) |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
89 { |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
90 case 'self': |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
91 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
|
92 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
|
93 } else { |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
94 var receiver = args[0]; |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
95 args.splice(0, 1); |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
96 } |
20
bf03c9f0dd55
Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents:
19
diff
changeset
|
97 ret = receiver.toJS(true) + '.'; |
bf03c9f0dd55
Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents:
19
diff
changeset
|
98 break; |
8
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
99 case 'parent': |
20
bf03c9f0dd55
Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents:
19
diff
changeset
|
100 ret = 'this'; |
8
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
101 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
|
102 ret += '.parent'; |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
103 } |
20
bf03c9f0dd55
Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents:
19
diff
changeset
|
104 break; |
8
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
105 } |
20
bf03c9f0dd55
Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents:
19
diff
changeset
|
106 for (var i in args) { |
bf03c9f0dd55
Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents:
19
diff
changeset
|
107 args[i] = args[i].toJS(); |
bf03c9f0dd55
Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents:
19
diff
changeset
|
108 } |
bf03c9f0dd55
Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents:
19
diff
changeset
|
109 return 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
|
110 } |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
111 |
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
|
112 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
|
113 var messages = this.messages; |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
114 var compiled = [] |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
115 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
|
116 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
|
117 if (js) { |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
118 compiled.push(indent(js)); |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
119 } |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
120 } |
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
|
121 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
|
122 } |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
123 |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
124 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
|
125 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
|
126 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
|
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 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
|
130 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
|
131 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
|
132 args.splice(0, 1); |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
133 } |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
134 var exprs = this.expressions; |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
135 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
|
136 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
|
137 } |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
138 var compiled = [] |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
139 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
|
140 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
|
141 if (js) { |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
142 compiled.push(indent(js)); |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
143 } |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
144 } |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
145 exprs = compiled; |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
146 if (exprs.length) { |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
147 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
|
148 } |
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
|
149 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
|
150 }; |
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
|
151 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
|
152 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
|
153 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
|
154 } |
8
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
155 |
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
|
156 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
|
157 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
|
158 var prefix = ''; |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
159 if (!existing) { |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
160 prefix = 'var '; |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
161 } else { |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
162 switch (existing.type) |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
163 { |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
164 case 'self': |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
165 prefix = 'this.'; |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
166 break; |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
167 case 'parent': |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
168 prefix = 'this.'; |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
169 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
|
170 prefix += 'parent.'; |
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 break; |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
173 } |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
174 } |
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
|
175 var val = this.expression.toJS(); |
12
6e4851a204a5
Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
176 if (val === null) { |
8
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
177 return null; |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
178 } |
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
|
179 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
|
180 }; |
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
|
181 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
|
182 var val = this.expression.toJS(); |
12
6e4851a204a5
Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
183 if (val === null) { |
8
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
184 return null; |
04ae32e91598
Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
185 } |
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
|
186 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
|
187 }; |