annotate jsbackend.js @ 23:068d63627b16

Populate in scope symbol buttons when clicking on a symbol in the source
author Mike Pavone <pavone@retrodev.com>
date Mon, 26 Mar 2012 21:29:03 -0700
parents 6c8ae6b47ab5
children 4d87c38404d6
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
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 {
23
068d63627b16 Populate in scope symbol buttons when clicking on a symbol in the source
Mike Pavone <pavone@retrodev.com>
parents: 21
diff changeset
79 var callee = rJS + '.' + (new symbol(name, this.symbols)).toJS();
068d63627b16 Populate in scope symbol buttons when clicking on a symbol in the source
Mike Pavone <pavone@retrodev.com>
parents: 21
diff changeset
80 var callCode = callee + '(' + args.join(', ') + ')';
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
81 if (args.length == 0) {
23
068d63627b16 Populate in scope symbol buttons when clicking on a symbol in the source
Mike Pavone <pavone@retrodev.com>
parents: 21
diff changeset
82 return '(' + callee + ' instanceof Function ? ' + callCode + ' : ' + callee + ')';
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
83 } 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
84 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
85 }
20
bf03c9f0dd55 Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents: 19
diff changeset
86 }
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
87 }
20
bf03c9f0dd55 Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents: 19
diff changeset
88 var ret = '';
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
89 switch(funinfo.type)
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
90 {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
91 case 'self':
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
92 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
93 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
94 } else {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
95 var receiver = args[0];
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
96 args.splice(0, 1);
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
97 }
20
bf03c9f0dd55 Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents: 19
diff changeset
98 ret = receiver.toJS(true) + '.';
bf03c9f0dd55 Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents: 19
diff changeset
99 break;
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
100 case 'parent':
20
bf03c9f0dd55 Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents: 19
diff changeset
101 ret = 'this';
8
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 }
20
bf03c9f0dd55 Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents: 19
diff changeset
105 break;
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
106 }
20
bf03c9f0dd55 Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents: 19
diff changeset
107 for (var i in args) {
bf03c9f0dd55 Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents: 19
diff changeset
108 args[i] = args[i].toJS();
bf03c9f0dd55 Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents: 19
diff changeset
109 }
bf03c9f0dd55 Initial work on proper property support
Mike Pavone <pavone@retrodev.com>
parents: 19
diff changeset
110 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
111 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
112
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
113 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
114 var messages = this.messages;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
115 var compiled = []
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
116 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
117 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
118 if (js) {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
119 compiled.push(indent(js));
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
120 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
121 }
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
122 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
123 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
124
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
125 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
126 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
127 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
128 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
129
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
130 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
131 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
132 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
133 args.splice(0, 1);
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
134 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
135 var exprs = this.expressions;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
136 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
137 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
138 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
139 var compiled = []
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
140 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
141 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
142 if (js) {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
143 compiled.push(indent(js));
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 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
146 exprs = compiled;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
147 if (exprs.length) {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
148 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
149 }
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
150 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
151 };
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
152 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
153 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
154 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
155 }
8
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 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
158 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
159 var prefix = '';
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
160 if (!existing) {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
161 prefix = 'var ';
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
162 } else {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
163 switch (existing.type)
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
164 {
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
165 case 'self':
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
166 prefix = 'this.';
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
167 break;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
168 case 'parent':
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
169 prefix = 'this.';
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
170 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
171 prefix += 'parent.';
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
172 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
173 break;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
174 }
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
175 }
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
176 var val = this.expression.toJS();
12
6e4851a204a5 Add ability to load code into editor
Mike Pavone <pavone@retrodev.com>
parents: 10
diff changeset
177 if (val === null) {
8
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
178 return null;
04ae32e91598 Move compiler and test page related code out of parser.js
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
179 }
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
180 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
181 };
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
182 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
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 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 };