/* * BinaryExp.java 1.0 2006-02-14 * * Copyright (C) 2006 by Ifi, Uio. */ import java.util.Map; import java.util.HashMap; import antlr.Token; /** * Simple AST class for a binary expression. * TBD: Must also use the getFirstChild() and getNextSibling() methods in * getLhs() and getRhs() impls. * * @version $Revision:$ * @author Sven-Jørgen Karlsen * @since 1.0 */ public final class BinaryExp extends BaseASTAdapter implements Exp { private final Operator op; // These should actually be final, but AST construction with Antlr seems // to happen before the children are parsed. private Exp lhs; private Exp rhs; public BinaryExp(Operator op, Exp lhs, Exp rhs) { super(); this.op = op; this.lhs = lhs; this.rhs = rhs; } /** * Special ctor version for use by Antlr's tree construction. */ public BinaryExp(Token op) { super(); this.op = Exp.Operator.fromString(op.getText()); } // Unfortunately, Antlr's AST libraries seizes the toString() method. // This original version of Object.toString() had to be renamed to avoid // collision. public String toLispString() { return "(" + op + " " + lhs.toLispString() + " " + rhs.toLispString() + ")"; } public String toString() { return op.toString(); } // Cumbersome Antlr callback to have this class usable in a tree // grammar. /** Get the token text for this node */ public String getText() { return op.toString(); } /** * Get the token type for this node */ public int getType() { Map m = new HashMap(); m.put(Operator.PLUS, AmbigiousExpTokenTypes.PLUS); m.put(Operator.MINUS, AmbigiousExpTokenTypes.MINUS); m.put(Operator.MULT, AmbigiousExpTokenTypes.MULT); m.put(Operator.DIV, AmbigiousExpTokenTypes.DIV); return m.get(op); } } /* BinaryExp.java ends here */