Skip to content

Commit 8a197a3

Browse files
committed
flatten var at top of functions.
1 parent 8704759 commit 8a197a3

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

src/callcc/declVars.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function declToAssign(decl: t.VariableDeclarator): t.AssignmentExpression | null
2727

2828
function getFunctionArgs(path: NodePath<t.Node>): string[] {
2929
const node = path.node;
30-
if (node.type === 'FunctionDeclaration' ||
30+
if (node.type === 'FunctionDeclaration' ||
3131
node.type === 'FunctionExpression') {
3232
return (<any>node).params.map((x: t.Identifier) => x.name);
3333
}
@@ -37,7 +37,7 @@ function getFunctionArgs(path: NodePath<t.Node>): string[] {
3737
}
3838

3939
function getBlock(node: t.Node): t.Statement[] {
40-
if (node.type === 'FunctionDeclaration' ||
40+
if (node.type === 'FunctionDeclaration' ||
4141
node.type === 'FunctionExpression') {
4242
return (<t.FunctionDeclaration>node).body.body;
4343
}
@@ -49,7 +49,36 @@ function getBlock(node: t.Node): t.Statement[] {
4949
}
5050
}
5151

52+
const func = {
53+
enter(path: NodePath<t.FunctionDeclaration|t.FunctionExpression>) {
54+
(<any>path.node).toDecl = []
55+
},
56+
exit(path: NodePath<t.FunctionDeclaration|t.FunctionExpression>) {
57+
const toDecl: t.Identifier[] | undefined = (<any>path.node).toDecl
58+
if(toDecl && toDecl.length > 0) {
59+
path.node.body.body.unshift(t.variableDeclaration('var',
60+
lifted(toDecl.map(d => t.variableDeclarator(d)))))
61+
}
62+
}
63+
}
64+
65+
const prog = {
66+
enter(path: NodePath<t.Program>) {
67+
(<any>path.node).toDecl = []
68+
},
69+
exit(path: NodePath<t.Program>) {
70+
const toDecl: t.Identifier[] | undefined = (<any>path.node).toDecl
71+
if(toDecl && toDecl.length > 0) {
72+
path.node.body.unshift(t.variableDeclaration('var',
73+
lifted(toDecl.map(d => t.variableDeclarator(d)))))
74+
}
75+
}
76+
}
77+
5278
const lift: Visitor = {
79+
Program: prog,
80+
FunctionDeclaration: func,
81+
FunctionExpression: func,
5382
VariableDeclaration(path: NodePath<Lifted<t.VariableDeclaration>>) {
5483
if (path.node.lifted) {
5584
return;
@@ -79,9 +108,10 @@ const lift: Visitor = {
79108
// Therefore, we do not need to lift x. Instead, we eliminate the
80109
// declaration and only turn it into an assignment.
81110
if ((kind === 'var' && topArgs.includes(id)) === false) {
82-
const newDecl = t.variableDeclaration(kind,
83-
[t.variableDeclarator(decl.id)]);
84-
getBlock(topScope.node).unshift(lifted(newDecl));
111+
(<any>topScope.node).toDecl.push(decl.id)
112+
//const newDecl = t.variableDeclaration(kind,
113+
//[t.variableDeclarator(decl.id)]);
114+
//getBlock(topScope.node).unshift(lifted(newDecl));
85115
}
86116
if (decl.init !== null) {
87117
// If we call path.insertAfter here, we will add assignments in reverse

0 commit comments

Comments
 (0)