/* SCCS - @(#)CastNode.java 1.1 - 06/05/00 - 14:22:01 */ /* * Copyright (c) 1997 USC Brain Project. email nsl@java.usc.edu. */ /** * CastNode: implement cast operation *

* This class stores cast expressions and generates casted expressions. * In this implementation, only nslNumeric->nslNumeric and nslNumeric-> * native java object casting will be performed. Because of the implicit * creation of nslNumeric objects, native java number->nslNumeric object * is not implemented in this version. * @see util.ExprNode, nsl.lang.NslNumeric */ package npp.src.util; import pp.src.jbf.*; public class CastNode extends ExprNode { public CastNode(TypeNode ttype, ExprNode texpr) { super(ttype, texpr); tokentype=YYtokentypes.nt_cast; lvalue = false; casttype = ttype; expr = texpr; constructor_flag = false; } public int genCode(NslPreProcessor npp) { expr.genCode(npp); if(expr.processed) processed = true; code = "("+casttype.text; int dimension = casttype.dim - casttype.nslDim; if(dimension > 0) for (int i=0; i TypeNode.STRING) return NO_OP; // not numeric items if (!casttype.numeric && !casttype.nslNumeric) return NO_OP; if (casttype.dim != expr.type.dim) { System.err.println("Cast: line "+(casttype.lineno+1)+ " char "+(casttype.charno+1)+"\tCast dimension not match"); return CAST_DIMENSION_NOT_MATCH_ERROR; } if (expr.type.nslNumeric) { // caststring = getCastString(); code = "("+expr.code+").get"+getCastString()+"()"; processed = true; } else if (expr.type.numeric) { // special cases for array types if (expr instanceof ArrayIndexNode) { ArrayIndexNode array = (ArrayIndexNode)expr; // 98/8/24 aa: trying this if ((expr.type.dim == 3 && expr.type.nslDim == 4) || (expr.type.dim == 2 && expr.type.nslDim == 3) || (expr.type.dim == 1 && expr.type.nslDim == 2) || (expr.type.dim == 0 && expr.type.nslDim == 1)) { code = "("+array.base.code+").get"+getCastString()+"("+ array.index.code+")"; processed = true; } } /* else { code = "("+expr.code +").get"+getCastString()+"()"; } */ } // System.out.println("CastNode.GenCode: "+code); return SUCCESS; } /** Generate the method to call for nsl numeric object casting If the type to cast is not the same as the original object type, then we will call get[Nsl]{Type}[{dimension}d]. If the type to cast is the same as the original object type, then we will calll get[Nsl]. [todo: it needs the nsl/lang nsl numerical objects to take the advantage of the simpler call. The preprocessor will call the first group of method no matter whether the type and cast type the same] **/ String getCastString() { String s; if(casttype.nslNumeric) s = "Nsl"; else s = ""; // if (casttype.type!=expr.type.type) { if (casttype.type == TypeNode.INT) s+="int"; else if(casttype.type == TypeNode.FLOAT) s+="float"; else if(casttype.type == TypeNode.DOUBLE) s+="double"; else System.err.println("Internal error in precision determination [type="+ casttype.type+"]"); // } if(casttype.dim >0) // s+=(casttype.dim+"d"); s+=(casttype.dim); return s; } public void setConstructorFlag() { constructor_flag = true; } public TypeNode casttype; public ExprNode expr; // whether the cast statement is accompanied with // construction statement. If it is , do not process public boolean constructor_flag; // return error code final public static int NO_OP = 0; final public static int SUCCESS = 1; final public static int CAST_DIMENSION_NOT_MATCH_ERROR=2; }