Back to index

wims  3.65+svn20090927
Node.java
Go to the documentation of this file.
00001 /*
00002  * @(#)Node.java
00003  * 
00004  * $Id: Node.java,v 1.28 2002/08/08 05:15:06 huaz Exp $
00005  * 
00006  * Created on October 18, 2000, 3:27 PM
00007 */
00008 package SharpTools;
00009 import java.io.*;
00010 import java.util.*;    
00011 
00033 public class Node {
00034     public static final int DEFAULT = 0; // reserved
00035     public static final int REL_ADDR = 1; // LETTERS+numbers: A1
00036     public static final int ABS_ADDR = 2; // $LETTERS$numbers: $A$1
00037     public static final int FUNCTION = 3; // LETTERS: SUM
00038     public static final int LPAREN = 4; // (
00039     public static final int RPAREN = 5; // )
00040     public static final int NUMBER = 6;  // all numbers and has .: 0.5
00041     public static final int OPERATOR = 7; // + - * / ^
00042     public static final int COMMA = 8; // ,
00043     public static final int COLON = 9; // :
00044     public static final int EXP = 10; // an expression.  the exp field counts.
00045     // for each function param, its type is EXP
00046     
00047     private static final String[] desc = {
00048        "Default", "Rel_Addr", "Abs_Addr", "Function",
00049        "LBracket", "RBracket", "Number", "Operator",
00050        "Comma", "Colon", "Param" };
00051 
00052     private int type; // which type the node is (see above 10 types)
00053     private String data; // the raw data
00054     private float number; // the numeric value
00055     private int row;
00056     private int col;
00057 
00058     private LinkedList exp; // a LinkedList for a function's params
00059 
00060     /*
00061      * Used for Function address range parameter (ADDR1:ADDR2)
00062      * An address range is ultimately represented as follows:
00063      *
00064      * node type: COLON
00065      * node.nextRange points to the start address (a node of REL_ADDR or
00066      * ABS_ADDR), the start address' nextRange points to the end address.
00067      */
00068     private Node nextRange;
00069 
00070     private boolean pending; // used for processing functions, see Formula
00071     
00075     Node() {
00076     }
00077 
00083     Node(Node node) {
00084        type = node.type;
00085        if (data != null)
00086            data = new String(node.data);
00087        number = node.number;
00088        row = node.row;
00089        col = node.col;
00090     }
00091 
00093     public int getType() { return type; }
00094     public boolean isType(int type) { return this.type == type; }
00095     
00096     public float getNumber() { return number; }
00097     public String getData() { return data; }
00098     
00099     public LinkedList getParams() { return exp; }
00100     public Node getNextRange() { return nextRange; }
00101     public LinkedList getExp() { return exp; }
00102     
00103     public int getRow() { return row; }
00104     public int getCol() { return col; }
00105     
00106     public void setType(int type) { this.type = type; }
00107     public void setNumber(float number) { this.number = number; }
00108 
00109     public void setData(String data) { this.data = data; }
00110     public void appendData(char data) { this.data += data; }
00111     public void appendData(String data) { this.data += data; }
00112     
00113     public void setParams(LinkedList list) { exp = list; }
00114     public void addParam(Node node) {
00115        if (node.getExp().size()>0)
00116            exp.add(node);
00117     }
00118     
00119     public void setNextRange(Node node) { nextRange = node; }
00120     public void setExp(LinkedList exp) { this.exp = exp; }
00121 
00122     public void setPending(boolean pending) { this.pending = pending; }
00123     public boolean isPending() { return pending; }
00124 
00125     public void setRow(int row) { this.row = row; }
00126     public void setCol(int col) { this.col = col; }
00127 
00128     public boolean hasValue() {
00129        return (type == Node.NUMBER || type == Node.REL_ADDR ||
00130               type == Node.ABS_ADDR || type == Node.FUNCTION ||
00131               type == Node.RPAREN);
00132     }
00133     
00138     public String toString() {
00139        //     return String.valueOf(type);
00140 
00141        switch (type) {
00142        case Node.NUMBER:        
00143            return Float.toString(number);
00144        case Node.REL_ADDR:
00145            StringBuffer buf1 = new StringBuffer();
00146            buf1.append('(');
00147            buf1.append(String.valueOf(col));
00148            buf1.append(',');
00149            buf1.append(row);
00150            buf1.append(')');
00151            return buf1.toString();
00152        case Node.ABS_ADDR:
00153            StringBuffer buf2 = new StringBuffer();
00154            buf2.append('$');
00155            buf2.append(translateColumn(col));
00156            buf2.append('$');
00157            buf2.append(translateRow(row));
00158            return buf2.toString();
00159        case Node.FUNCTION:
00160            return data+exp.toString();
00161        case Node.EXP:
00162            return exp.toString();
00163        case Node.LPAREN:
00164            return "(";
00165        case Node.RPAREN:
00166            return ")";
00167        case Node.COMMA:
00168            return ",";
00169        case Node.COLON:
00170            return nextRange.toString()+":"+nextRange.nextRange.toString();
00171            
00172        default:
00173            return data;
00174        }
00175 
00176     }
00177 
00184     public static int translateRow(String row) {
00185        int r = Integer.parseInt(row);
00186        return r + SharpTools.baseRow - 1;
00187     }
00188 
00195     public static String translateRow(int row) {
00196        if (row < 0)
00197            return null;
00198        else
00199            return String.valueOf(row+1-SharpTools.baseRow);
00200     }
00201 
00208     public static int translateColumn(String column) {
00209        int col = 0;
00210 
00211        for (int i = 0; i < column.length(); i++) {
00212            col  = col * 26 + (column.charAt(i) - 'A' + 1);
00213        }
00214 
00215        return col + SharpTools.baseCol -1;
00216     }
00217 
00224     public static String translateColumn(int column) {
00225 
00226        column = column - SharpTools.baseCol +1;
00227        if (column < 1)
00228            return null;
00229 
00230        StringBuffer buf = new StringBuffer();
00231        
00232        int div = 1;
00233 
00234        while (div > 0) {
00235            div = (column-1)/26;
00236            buf.insert(0, (char)('A'+(column-1 - div*26)));
00237            column = div;
00238        }
00239        
00240        return buf.toString();
00241     }
00242 
00246     public CellPoint toCellPoint(int row, int col) {
00247        if (isType(Node.REL_ADDR))
00248            return new CellPoint(getRow()+row, getCol()+col);
00249        else
00250            return new CellPoint(getRow(), getCol());
00251     }
00252 
00253     private static int min(int m, int n) {
00254        return m<n ? m:n;
00255     }
00256 
00257     private static int max(int m, int n) {
00258        return m>n ? m:n;
00259     }
00270     public CellPoint[] getAddressRange(int row, int col) {
00271 
00272        CellPoint[] addr = new CellPoint[2];
00273        if (isType(Node.COLON)) {
00274            // if it's an address range
00275            // get the start address
00276            CellPoint addr1 = getNextRange().toCellPoint(row, col);
00277            // get the end address
00278            CellPoint addr2 = getNextRange().getNextRange().
00279               toCellPoint(row, col);
00280 
00281            addr[0] = new CellPoint(
00282                                 min(addr1.getRow(), addr2.getRow()),
00283                                 min(addr1.getCol(), addr2.getCol())
00284                                 );
00285            addr[1] = new CellPoint(
00286                                 max(addr1.getRow(), addr2.getRow()),    
00287                                 max(addr1.getCol(), addr2.getCol())
00288                                 );
00289        }
00290        else {
00291            // otherwise it's a normal address
00292            CellPoint cell = toCellPoint(row, col);
00293            addr[0] = cell;
00294            addr[1] = cell;
00295        }
00296            
00297        return addr;  
00298     }
00299 
00300 }