aboutsummaryrefslogtreecommitdiff
path: root/lib/ast.js
diff options
context:
space:
mode:
authorMihai Bazon <mihai@bazon.net>2012-05-27 17:25:31 +0300
committerMihai Bazon <mihai@bazon.net>2012-06-03 23:10:31 +0300
commit861e26a66639ca61eab2af53de45760370c4d534 (patch)
treec35c94fe6978dfdff6887a9e5ea03703f2e4bed4 /lib/ast.js
parent22bb5e8306687fb6324f094d208b564c9e874f77 (diff)
downloadtracifyjs-861e26a66639ca61eab2af53de45760370c4d534.tar.gz
tracifyjs-861e26a66639ca61eab2af53de45760370c4d534.zip
WIP
Diffstat (limited to 'lib/ast.js')
-rw-r--r--lib/ast.js343
1 files changed, 278 insertions, 65 deletions
diff --git a/lib/ast.js b/lib/ast.js
index 9920e1b7..683f602f 100644
--- a/lib/ast.js
+++ b/lib/ast.js
@@ -18,42 +18,75 @@ function DEFNODE(type, props, methods, base) {
if (type) {
ctor.prototype.TYPE = ctor.TYPE = type;
}
- if (methods) for (var i in methods) if (HOP(methods, i)) {
+ if (methods) for (i in methods) if (HOP(methods, i)) {
ctor.prototype[i] = methods[i];
}
return ctor;
};
-var AST_Token = DEFNODE("Token", "type value line col pos endpos nlb", {
+var AST_Token = DEFNODE("Token", "type value line col pos endpos nlb comments_before", {
}, null);
var AST_Node = DEFNODE("Node", "start end", {
-
+ renew: function(args) {
+ var ctor = this.CTOR, props = ctor.props;
+ for (var i in props) if (!HOP(args, i)) args[i] = this[i];
+ return new ctor(args);
+ },
+ walk: function(w) {
+ w._visit(this);
+ }
}, null);
var AST_Directive = DEFNODE("Directive", "value", {
-
+ print: function(output) {
+ output.string(this.value);
+ }
});
var AST_Debugger = DEFNODE("Debugger", null, {
-
+ print: function(output) {
+ output.print("debugger");
+ }
});
var AST_Parenthesized = DEFNODE("Parenthesized", "expression", {
- documentation: "Represents an expression which is always parenthesized. Used for the \
-conditions in IF/WHILE."
+ $documentation: "Represents an expression which is always parenthesized. Used for the \
+conditions in IF/WHILE/DO and expression in SWITCH/WITH.",
+ walk: function(w) {
+ w._visit(this, function(){
+ this.expression.walk(w);
+ });
+ }
});
var AST_Bracketed = DEFNODE("Bracketed", "body", {
- documentation: "Represents a block of statements that are always included in brackets. \
-Used for bodies of FUNCTION/TRY/CATCH/THROW/SWITCH."
+ $documentation: "Represents a block of statements that are always included in brackets. \
+Used for bodies of FUNCTION/TRY/CATCH/THROW/SWITCH.",
+ walk: function(w) {
+ w._visit(this, function(){
+ this.body.forEach(function(stat){
+ stat.walk(w);
+ });
+ });
+ }
});
/* -----[ loops ]----- */
var AST_LabeledStatement = DEFNODE("LabeledStatement", "label body", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ if (this.label) this.label.walk(w);
+ if (this.body) {
+ if (this.body instanceof Array)
+ AST_Bracketed.prototype.walk.call(this, w);
+ else
+ this.body.walk(w);
+ }
+ });
+ }
});
var AST_Statement = DEFNODE("Statement", null, {
@@ -61,39 +94,64 @@ var AST_Statement = DEFNODE("Statement", null, {
}, AST_LabeledStatement);
var AST_Do = DEFNODE("Do", "condition", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.condition.walk(w);
+ AST_LabeledStatement.prototype.walk.call(this, w);
+ });
+ }
}, AST_LabeledStatement);
var AST_While = DEFNODE("While", "condition", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.condition.walk(w);
+ AST_LabeledStatement.prototype.walk.call(this, w);
+ });
+ }
}, AST_LabeledStatement);
var AST_For = DEFNODE("For", "init condition step", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ if (this.init) this.init.walk(w);
+ if (this.condition) this.condition.walk(w);
+ if (this.step) this.step.walk(w);
+ AST_LabeledStatement.prototype.walk.call(this, w);
+ });
+ }
}, AST_LabeledStatement);
var AST_ForIn = DEFNODE("ForIn", "init name object", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ if (this.init) this.init.walk(w);
+ this.object.walk(w);
+ AST_LabeledStatement.prototype.walk.call(this, w);
+ });
+ }
}, AST_LabeledStatement);
var AST_With = DEFNODE("With", "expression body", {
-
-});
-
-var AST_LoopControl = DEFNODE("LoopControl", "label", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.expression.walk(w);
+ AST_LabeledStatement.prototype.walk.call(this, w);
+ });
+ }
});
-var AST_Break = DEFNODE("Break", null, {
-
-}, AST_LoopControl);
-var AST_Continue = DEFNODE("Continue", null, {
-
-}, AST_LoopControl);
/* -----[ functions ]----- */
var AST_Scope = DEFNODE("Scope", "identifiers body", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ if (this.identifiers) this.identifiers.forEach(function(el){
+ el.walk(w);
+ });
+ AST_LabeledStatement.prototype.walk.call(this, w);
+ });
+ }
});
var AST_Toplevel = DEFNODE("Toplevel", null, {
@@ -101,37 +159,84 @@ var AST_Toplevel = DEFNODE("Toplevel", null, {
}, AST_Scope);
var AST_Lambda = DEFNODE("Lambda", "name argnames", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ if (this.name) this.name.walk(w);
+ this.argnames.forEach(function(el){
+ el.walk(w);
+ });
+ AST_Scope.prototype.walk.call(this, w);
+ });
+ }
}, AST_Scope);
+
var AST_Function = DEFNODE("Function", null, {
}, AST_Lambda);
+
var AST_Defun = DEFNODE("Defun", null, {
}, AST_Function);
/* -----[ JUMPS ]----- */
-var AST_Jump = DEFNODE("Jump", "value");
+var AST_Jump = DEFNODE("Jump", null, {
-var AST_Return = DEFNODE("Return", null, {
+});
+var AST_Exit = DEFNODE("Exit", "value", {
+ walk: function(w) {
+ w._visit(this, function(){
+ if (this.value) this.value.walk(w);
+ });
+ }
}, AST_Jump);
+var AST_Return = DEFNODE("Return", null, {
+
+}, AST_Exit);
+
var AST_Throw = DEFNODE("Throw", null, {
+}, AST_Exit);
+
+var AST_LoopControl = DEFNODE("LoopControl", "label", {
+ walk: function(w) {
+ w._visit(this, function(){
+ if (this.label) this.label.walk(w);
+ });
+ }
}, AST_Jump);
+var AST_Break = DEFNODE("Break", null, {
+
+}, AST_LoopControl);
+
+var AST_Continue = DEFNODE("Continue", null, {
+
+}, AST_LoopControl);
+
/* -----[ IF ]----- */
var AST_If = DEFNODE("If", "condition consequent alternative", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.condition.walk(w);
+ this.consequent.walk(w);
+ if (this.alternative) this.alternative.walk(w);
+ });
+ }
});
/* -----[ SWITCH ]----- */
var AST_Switch = DEFNODE("Switch", "expression", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.expression.walk(w);
+ AST_LabeledStatement.prototype.walk.call(this, w);
+ });
+ }
}, AST_LabeledStatement);
var AST_SwitchBlock = DEFNODE("SwitchBlock", null, {
@@ -143,21 +248,41 @@ var AST_SwitchBranch = DEFNODE("SwitchBranch", "body", {
});
var AST_Default = DEFNODE("Default", null, {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ AST_Statement.prototype.walk.call(this, w);
+ });
+ }
}, AST_SwitchBranch);
var AST_Case = DEFNODE("Case", "expression", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.expression.walk(w);
+ AST_Statement.prototype.walk.call(this, w);
+ });
+ }
}, AST_SwitchBranch);
/* -----[ EXCEPTIONS ]----- */
var AST_Try = DEFNODE("Try", "btry bcatch bfinally", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.btry.walk(w);
+ if (this.bcatch) this.bcatch.walk(w);
+ if (this.bfinally) this.bfinally.walk(w);
+ });
+ }
});
var AST_Catch = DEFNODE("Catch", "argname body", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.argname.walk(w);
+ this.body.walk(w);
+ });
+ }
});
var AST_Finally = DEFNODE("Finally", null, {
@@ -167,7 +292,13 @@ var AST_Finally = DEFNODE("Finally", null, {
/* -----[ VAR/CONST ]----- */
var AST_Definitions = DEFNODE("Definitions", "definitions", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.definitions.forEach(function(el){
+ el.walk(w);
+ });
+ });
+ }
});
var AST_Var = DEFNODE("Var", null, {
@@ -179,13 +310,25 @@ var AST_Const = DEFNODE("Const", null, {
}, AST_Definitions);
var AST_VarDef = DEFNODE("VarDef", "name value", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.name.walk(w);
+ if (this.value) this.value.walk(w);
+ });
+ }
});
/* -----[ OTHER ]----- */
var AST_Call = DEFNODE("Call", "expression args", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.expression.walk(w);
+ this.args.forEach(function(el){
+ el.walk(w);
+ });
+ });
+ }
});
var AST_New = DEFNODE("New", null, {
@@ -193,7 +336,12 @@ var AST_New = DEFNODE("New", null, {
}, AST_Call);
var AST_Seq = DEFNODE("Seq", "first second", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.first.walk(w);
+ this.second.walk(w);
+ });
+ }
});
var AST_PropAccess = DEFNODE("PropAccess", "expression property", {
@@ -201,15 +349,28 @@ var AST_PropAccess = DEFNODE("PropAccess", "expression property", {
});
var AST_Dot = DEFNODE("Dot", null, {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.expression.walk(w);
+ });
+ }
}, AST_PropAccess);
var AST_Sub = DEFNODE("Sub", null, {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.expression.walk(w);
+ this.property.walk(w);
+ });
+ }
}, AST_PropAccess);
var AST_Unary = DEFNODE("Unary", "operator expression", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.expression.walk(w);
+ });
+ }
});
var AST_UnaryPrefix = DEFNODE("UnaryPrefix", null, {
@@ -221,77 +382,129 @@ var AST_UnaryPostfix = DEFNODE("UnaryPostfix", null, {
}, AST_Unary);
var AST_Binary = DEFNODE("Binary", "left operator right", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.left.walk(w);
+ this.right.walk(w);
+ });
+ }
});
var AST_Conditional = DEFNODE("Conditional", "condition consequent alternative", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.condition.walk(w);
+ this.consequent.walk(w);
+ this.alternative.walk(w);
+ });
+ }
});
-var AST_Assign = DEFNODE("Assign", "left operator right", {
+var AST_Assign = DEFNODE("Assign", null, {
-});
+}, AST_Binary);
/* -----[ LITERALS ]----- */
-var AST_RegExp = DEFNODE("Regexp", "pattern mods", {
-
-});
-
var AST_Array = DEFNODE("Array", "elements", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.elements.forEach(function(el){
+ el.walk(w);
+ });
+ });
+ }
});
var AST_Object = DEFNODE("Object", "properties", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.properties.forEach(function(prop){
+ prop.walk(w);
+ });
+ });
+ }
});
var AST_ObjectProperty = DEFNODE("ObjectProperty");
var AST_ObjectKeyVal = DEFNODE("ObjectKeyval", "key value", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.value.walk(w);
+ });
+ }
}, AST_ObjectProperty);
var AST_ObjectSetter = DEFNODE("ObjectSetter", "name func", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.func.walk(w);
+ });
+ }
}, AST_ObjectProperty);
var AST_ObjectGetter = DEFNODE("ObjectGetter", "name func", {
-
+ walk: function(w) {
+ w._visit(this, function(){
+ this.func.walk(w);
+ });
+ }
}, AST_ObjectProperty);
var AST_Symbol = DEFNODE("Symbol", "name", {
+});
+
+var AST_This = DEFNODE("This", null, {
+
+}, AST_Symbol);
+
+var AST_SymbolRef = DEFNODE("SymbolRef", "scope symbol", {
+
+}, AST_Symbol);
+var AST_Label = DEFNODE("Label", null, {
+
+}, AST_SymbolRef);
+
+var AST_Constant = DEFNODE("Constant", null, {
+ getValue: function() {
+ return this.value;
+ }
});
var AST_String = DEFNODE("String", "value", {
-});
+}, AST_Constant);
var AST_Number = DEFNODE("Number", "value", {
-});
-
-var AST_Boolean = DEFNODE("Boolean", "value", {
+}, AST_Constant);
-});
+var AST_RegExp = DEFNODE("Regexp", "pattern mods", {
+ getValue: function() {
+ return this._regexp || (
+ this._regexp = new RegExp(this.pattern, this.mods)
+ );
+ }
+}, AST_Constant);
var AST_Atom = DEFNODE("Atom", null, {
-});
+}, AST_Constant);
var AST_Null = DEFNODE("Null", null, {
-
+ getValue: function() { return null }
}, AST_Atom);
var AST_Undefined = DEFNODE("Undefined", null, {
-
+ getValue: function() { return (function(){}()) }
}, AST_Atom);
var AST_False = DEFNODE("False", null, {
-
+ getValue: function() { return false }
}, AST_Atom);
var AST_True = DEFNODE("True", null, {
-
+ getValue: function() { return true }
}, AST_Atom);