1 package gnu.expr; 2 import gnu.mapping.*; 3 import java.util.Vector ; 4 import gnu.lists.*; 5 6 15 16 public class BeginExp extends Expression 17 { 18 Expression[] exps; 19 int length; 20 21 33 Vector compileOptions; 34 35 public BeginExp () { } 36 37 public BeginExp (Expression[] ex) { exps = ex; length = ex.length; } 38 39 public BeginExp(Expression exp0, Expression exp1) 40 { 41 exps = new Expression[2]; 42 exps[0] = exp0; 43 exps[1] = exp1; 44 length = 2; 45 } 46 47 50 public static final Expression canonicalize(Expression exp) 51 { 52 if (exp instanceof BeginExp) 53 { 54 BeginExp bexp = (BeginExp) exp; 55 if (bexp.compileOptions != null) 56 return exp; 57 int len = bexp.length; 58 if (len == 0) 59 return QuoteExp.voidExp; 60 if (len == 1) 61 return canonicalize(bexp.exps[0]); 62 } 63 return exp; 64 } 65 66 public static final Expression canonicalize(Expression[] exps) 67 { 68 int len = exps.length; 69 if (len == 0) 70 return QuoteExp.voidExp; 71 if (len == 1) 72 return canonicalize(exps[0]); 73 return new BeginExp(exps); 74 } 75 76 public final void add(Expression exp) 77 { 78 if (exps == null) 79 exps = new Expression[8]; 80 if (length == exps.length) 81 { 82 Expression[] ex = new Expression[2 * length]; 83 System.arraycopy(exps, 0, ex, 0, length); 84 exps = ex; 85 } 86 exps[length++] = exp; 87 } 88 89 public final Expression[] getExpressions() { return exps; } 90 public final int getExpressionCount() { return length; } 91 92 public final void setExpressions(Expression[] exps) 93 { 94 this.exps = exps; 95 length = exps.length; 96 } 97 98 public void setCompileOptions (Vector options) 99 { 100 compileOptions = options; 101 } 102 103 protected boolean mustCompile () { return false; } 104 105 public void apply (CallContext ctx) throws Throwable 106 { 107 int n = length; 108 int i; 109 Consumer consumerSave = ctx.consumer; 110 ctx.consumer = VoidConsumer.instance; 111 try 112 { 113 for (i = 0; i < n - 1; i++) 114 exps[i].eval(ctx); 115 } 116 finally 117 { 118 ctx.consumer = consumerSave; 119 } 120 exps[i].apply(ctx); 121 } 122 123 public void pushOptions (Compilation comp) 124 { 125 if (compileOptions != null && comp != null) 126 comp.currentOptions.pushOptionValues(compileOptions); 127 } 128 129 public void popOptions (Compilation comp) 130 { 131 if (compileOptions != null && comp != null) 132 comp.currentOptions.popOptionValues(compileOptions); 133 } 134 135 public void compile (Compilation comp, Target target) 136 { 137 pushOptions(comp); 138 try 139 { 140 int n = length, i; 141 for (i = 0; i < n - 1; i++) 142 exps[i].compileWithPosition(comp, Target.Ignore); 143 exps[i].compileWithPosition(comp, target); 144 } 145 finally 146 { 147 popOptions(comp); 148 } 149 } 150 151 protected Expression walk (ExpWalker walker) 152 { 153 return walker.walkBeginExp(this); 154 } 155 156 protected void walkChildren(ExpWalker walker) 157 { 158 pushOptions(walker.comp); 159 try 160 { 161 exps = walker.walkExps(exps, length); 162 } 163 finally 164 { 165 popOptions(walker.comp); 166 } 167 } 168 169 public void print (OutPort out) 170 { 171 out.startLogicalBlock("(Begin", ")", 2); 172 out.writeSpaceFill(); 173 printLineColumn(out); 174 if (compileOptions != null) 175 { 176 out.writeSpaceFill(); 177 out.startLogicalBlock("(CompileOptions", ")", 2); 178 int sizeOptions = compileOptions.size(); 179 for (int i = 0; i < sizeOptions; i += 3) 180 { 181 Object key = compileOptions.elementAt(i); 182 Object value = compileOptions.elementAt(i+2); 183 out.writeSpaceFill(); 184 out.startLogicalBlock("", "", 2); 185 out.print(key); out.print(':'); 186 out.writeSpaceLinear(); 187 out.print(value); 188 out.endLogicalBlock(""); 189 } 190 out.endLogicalBlock(")"); 191 } 192 int n = length; 193 for (int i = 0; i < n; i++) 194 { 195 out.writeSpaceLinear(); 196 exps[i].print(out); 197 } 198 out.endLogicalBlock(")"); 199 } 200 201 public gnu.bytecode.Type getType() 202 { 203 return exps[length - 1].getType(); 204 } 205 } 206 | Popular Tags |