01: package gnu.expr;
02:
03: /** Sets up the firstChild/nextSibling links of each LambdaExp.
04: * Setup 'outer' links of ScopeExp and its sub-classes.
05: * Also generates a class name for each ClassExp and registers each class.
06: * Also, if lambda is bound to a unique declaration, make that its name.
07: */
08:
09: public class ChainLambdas extends ExpWalker {
10: ScopeExp currentScope;
11:
12: public static void chainLambdas(Expression exp, Compilation comp) {
13: ChainLambdas walker = new ChainLambdas();
14: walker.setContext(comp);
15: walker.walk(exp);
16: }
17:
18: protected Expression walkScopeExp(ScopeExp exp) {
19: ScopeExp saveScope = currentScope;
20: try {
21: exp.outer = currentScope;
22: currentScope = exp;
23: exp.walkChildren(this );
24: exp.setIndexes();
25: if (exp.mustCompile())
26: comp.mustCompileHere();
27: return exp;
28: } finally {
29: currentScope = saveScope;
30: }
31: }
32:
33: protected Expression walkLambdaExp(LambdaExp exp) {
34: LambdaExp parent = currentLambda;
35: if (parent != null && !(parent instanceof ClassExp)) {
36: exp.nextSibling = parent.firstChild;
37: parent.firstChild = exp;
38: }
39:
40: ScopeExp saveScope = currentScope;
41: try {
42: exp.outer = currentScope;
43: exp.firstChild = null;
44: currentScope = exp;
45: exp.walkChildrenOnly(this );
46: } finally {
47: currentScope = saveScope;
48: }
49: exp.walkProperties(this );
50:
51: // Put list of children in proper order.
52: LambdaExp prev = null, child = exp.firstChild;
53: while (child != null) {
54: LambdaExp next = child.nextSibling;
55: child.nextSibling = prev;
56: prev = child;
57: child = next;
58: }
59: exp.firstChild = prev;
60:
61: if (exp.getName() == null && exp.nameDecl != null)
62: exp.setName(exp.nameDecl.getName());
63: exp.setIndexes();
64: if (exp.mustCompile())
65: comp.mustCompileHere();
66: return exp;
67: }
68:
69: protected Expression walkClassExp(ClassExp exp) {
70: LambdaExp parent = currentLambda;
71: if (parent != null && !(parent instanceof ClassExp)) {
72: exp.nextSibling = parent.firstChild;
73: parent.firstChild = exp;
74: }
75:
76: walkScopeExp(exp);
77:
78: return exp;
79: }
80: }
|