Mercurial > repos > tabletprog
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/compiler.js Wed Mar 21 20:33:39 2012 -0700 @@ -0,0 +1,96 @@ +function indent(str) +{ + return str.split('\n').join('\n\t'); +} + +function osymbols(parent) +{ + this.parent = parent; + this.names = {}; + this.lastname = null; +} +osymbols.prototype.find = function(name) { + if (name in this.names) { + if (this.names[name] instanceof funcall && this.names[name].name == 'foreign:') { + return { + type: 'foreign', + def: this.names[name] + }; + } + return { + type: 'self', + def: this.names[name], + }; + } else if(this.parent) { + var ret = this.parent.find(name); + if (ret) { + if(ret.type == 'self') { + ret.type = 'parent'; + ret.depth = 1; + } else if(ret.type == 'parent') { + ret.depth++; + } + } + return ret; + } + return null; +}; +osymbols.prototype.defineMsg = function(name, def) { + this.lastname = name; + this.names[name] = def; +} +osymbols.prototype.parentObject = function() { + if (!this.parent) { + return 'null'; + } + return 'this'; +} + +function lsymbols(parent) +{ + this.parent = parent; + this.names = {}; + this.lastname = null; + this.needsSelfVar = false; +} +lsymbols.prototype.find = function(name) { + if (name in this.names) { + if (this.names[name] instanceof funcall && this.names[name].name == 'foreign:') { + return { + type: 'foreign', + def: this.names[name] + }; + } + return { + type: 'local', + def: this.names[name] + }; + } else if(this.parent) { + var ret = this.parent.find(name); + if (ret && ret.type == 'local') { + ret.type = 'upvar'; + } + return ret; + } + return null; +}; +lsymbols.prototype.defineVar = function(name, def) { + this.lastname = name; + this.names[name] = def; +}; +lsymbols.prototype.selfVar = function() { + if (this.parent && this.parent instanceof lsymbols) { + this.parent.needsSelf(); + return 'self'; + } else { + return 'this'; + } +}; +lsymbols.prototype.needsSelf = function() { + if (this.parent && this.parent instanceof lsymbols) { + this.parent.needsSelf(); + } else { + this.needsSelfVar = true; + } +}; +