001: package gnu.expr;
002:
003: import gnu.mapping.*;
004:
005: /**
006: * This class represents a sequence of Expressions.
007: * The expressions are evaluated for their side-effects,
008: * and the value of the last Expression is the result.
009: * @author Per Bothner
010: */
011:
012: public class BeginExp extends Expression {
013: Expression[] exps;
014: int length;
015:
016: public BeginExp() {
017: }
018:
019: public BeginExp(Expression[] ex) {
020: exps = ex;
021: length = ex.length;
022: }
023:
024: public BeginExp(Expression exp0, Expression exp1) {
025: exps = new Expression[2];
026: exps[0] = exp0;
027: exps[1] = exp1;
028: length = 2;
029: }
030:
031: /** Simplifies BeginExp.
032: * (In the future, nested BeginExps may be "flattened" as well.)
033: */
034: public static final Expression canonicalize(Expression exp) {
035: if (exp instanceof BeginExp) {
036: BeginExp bexp = (BeginExp) exp;
037: int len = bexp.length;
038: if (len == 0)
039: return QuoteExp.voidExp;
040: if (len == 1)
041: return canonicalize(bexp.exps[0]);
042: }
043: return exp;
044: }
045:
046: public final void add(Expression exp) {
047: if (exps == null)
048: exps = new Expression[8];
049: if (length == exps.length) {
050: Expression[] ex = new Expression[2 * length];
051: System.arraycopy(exps, 0, ex, 0, length);
052: exps = ex;
053: }
054: exps[length++] = exp;
055: }
056:
057: public final Expression[] getExpressions() {
058: return exps;
059: }
060:
061: public final void setExpressions(Expression[] exps) {
062: this .exps = exps;
063: length = exps.length;
064: }
065:
066: public Object eval(Environment env) throws Throwable {
067: int n = length;
068: int i;
069: for (i = 0; i < n - 1; i++)
070: exps[i].eval(env);
071: return exps[i].eval(env);
072: }
073:
074: public void compile(Compilation comp, Target target) {
075: int n = length, i;
076: for (i = 0; i < n - 1; i++) {
077: exps[i].compileWithPosition(comp, Target.Ignore);
078: if (!comp.getCode().reachableHere())
079: return;
080: }
081: exps[i].compileWithPosition(comp, target);
082: }
083:
084: protected Expression walk(ExpWalker walker) {
085: return walker.walkBeginExp(this );
086: }
087:
088: protected void walkChildren(ExpWalker walker) {
089: exps = walker.walkExps(exps, length);
090: }
091:
092: public void print(OutPort out) {
093: out.startLogicalBlock("(Begin", ")", 2);
094: out.writeSpaceFill();
095: printLineColumn(out);
096: int n = length;
097: for (int i = 0; i < n; i++) {
098: out.writeSpaceLinear();
099: exps[i].print(out);
100: }
101: out.endLogicalBlock(")");
102: }
103:
104: public gnu.bytecode.Type getType() {
105: return exps[length - 1].getType();
106: }
107: }
|