-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtetris.js
128 lines (108 loc) · 3.19 KB
/
tetris.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
tetris = {
children : [
/*for, while*/ "init", "test", "update",
/*block,program, while*/ "body",
/*expression*/ "expression",
/*assignment*/ "operator", "left", "right", "prefix",
/*call*/ "callee", "arguments",
/*identifier*/ "name",
/*literal*/ "value",
/*object*/ "properties",
/*if*/ "consequent", "alternate",
/*switch*/ "lexical", "cases", "discriminant",
/*return*/ "argument"],
/**
* traverses ReflectJS AST, calling visitor for each visited node. Visitor structure:
*
* pre visit callbacks:
* visitor.pre<NodeType> - called before entering node of given type
* example: visitor.preExpressionStatement - called before entering ExpressionStatement
* visitor.preVisit - called before entering node, if specific pre<NodeType> is not defined
*
* Returning false in pre-visitor prevents traversing current node.
*
* post visit callbacks:
* visitor.<NodeType> - called after visiting node of given type
* example: visitor.ExpressionStatement - called after visiting ExpressionStatement node
* visitor.visit - called after visiting node, if specific <NodeType> is not defined
*
* all callbacks get current node passed as first argument
*
* @param ast
* @param visitor
*/
traverse : function(ast, visitor) {
if (ast) {
var enter = undefined;
if (visitor["pre"+ast.type]) {
enter = visitor["pre"+ast.type](ast);
} else if (visitor.preVisit) {
enter = visitor.preVisit(ast);
}
if (enter !== false) {
for (var i in tetris.children) {
var child = tetris.children[i];
if (ast[child]) {
if (ast[child].length) {
for ( var i = 0; i < ast[child].length; i++) {
tetris.traverse(ast[child][i], visitor);
}
} else {
tetris.traverse(ast[child], visitor);
}
}
}
}
if (visitor[ast.type]) {
visitor[ast.type](ast);
} else if (visitor.visit) {
enter = visitor.visit(ast);
}
}
},
process : function(ast) {
console.log(ast);
visitor = {
/*preProgram : function(node) {
// empty
},
preExpressionStatement : function(node) {
// empty
},
preForStatement : function(node) {
// empty
},
preBlockStatement : function(node) {
// empty
},
preWhileStatement : function(node) {
// empty
},
preSwitchStatement : function(node) {
// empty
},
preReturnStatement : function(node) {
// empty
},
preIfStatement : function(ifNode) {
$("#dest").append("if? ");
},*/
str : "",
preVisit : function(node) {
this.str += "<div class='node'>"+node.type;
for (i in node) {
if ((jQuery.inArray(i, ["loc", "type"]) === -1) &&
(jQuery.inArray(i, tetris.children) === -1)) {
this.str += "<div class='member'>+"+node.type+"."+i+"</div>";
}
}
},
visit : function(node) {
this.str += "</div>";
}
};
tetris.traverse(ast, visitor);
$("#dest").empty();
$("#dest").append(visitor.str);
}
};