comparison compiler.js @ 8:04ae32e91598

Move compiler and test page related code out of parser.js
author Mike Pavone <pavone@retrodev.com>
date Wed, 21 Mar 2012 20:33:39 -0700
parents
children 59e83296e331
comparison
equal deleted inserted replaced
7:8af72f11714e 8:04ae32e91598
1 function indent(str)
2 {
3 return str.split('\n').join('\n\t');
4 }
5
6 function osymbols(parent)
7 {
8 this.parent = parent;
9 this.names = {};
10 this.lastname = null;
11 }
12 osymbols.prototype.find = function(name) {
13 if (name in this.names) {
14 if (this.names[name] instanceof funcall && this.names[name].name == 'foreign:') {
15 return {
16 type: 'foreign',
17 def: this.names[name]
18 };
19 }
20 return {
21 type: 'self',
22 def: this.names[name],
23 };
24 } else if(this.parent) {
25 var ret = this.parent.find(name);
26 if (ret) {
27 if(ret.type == 'self') {
28 ret.type = 'parent';
29 ret.depth = 1;
30 } else if(ret.type == 'parent') {
31 ret.depth++;
32 }
33 }
34 return ret;
35 }
36 return null;
37 };
38 osymbols.prototype.defineMsg = function(name, def) {
39 this.lastname = name;
40 this.names[name] = def;
41 }
42 osymbols.prototype.parentObject = function() {
43 if (!this.parent) {
44 return 'null';
45 }
46 return 'this';
47 }
48
49 function lsymbols(parent)
50 {
51 this.parent = parent;
52 this.names = {};
53 this.lastname = null;
54 this.needsSelfVar = false;
55 }
56 lsymbols.prototype.find = function(name) {
57 if (name in this.names) {
58 if (this.names[name] instanceof funcall && this.names[name].name == 'foreign:') {
59 return {
60 type: 'foreign',
61 def: this.names[name]
62 };
63 }
64 return {
65 type: 'local',
66 def: this.names[name]
67 };
68 } else if(this.parent) {
69 var ret = this.parent.find(name);
70 if (ret && ret.type == 'local') {
71 ret.type = 'upvar';
72 }
73 return ret;
74 }
75 return null;
76 };
77 lsymbols.prototype.defineVar = function(name, def) {
78 this.lastname = name;
79 this.names[name] = def;
80 };
81 lsymbols.prototype.selfVar = function() {
82 if (this.parent && this.parent instanceof lsymbols) {
83 this.parent.needsSelf();
84 return 'self';
85 } else {
86 return 'this';
87 }
88 };
89 lsymbols.prototype.needsSelf = function() {
90 if (this.parent && this.parent instanceof lsymbols) {
91 this.parent.needsSelf();
92 } else {
93 this.needsSelfVar = true;
94 }
95 };
96