/* SCCS - @(#)UnaryExprNode.java 1.6 - 08/31/99 - 11:33:51 */ /* * Copyright (c) 1997 USC Brain Project. email nsl@java.usc.edu. */ // // UnaryExprNode //////////////////////////////////////////////////////////// /** * UnaryExprNode - Unary Expression Node * unary expressions like negative, complement */ package npp.src.util; import pp.src.jbf.*; public class UnaryExprNode extends ExprNode { public UnaryExprNode(ExprNode e, int o) { super(e); expr = e; op = o; } /** * Code generation. * Only - is implemented * @param npp Nsl Preprocessor * @return error value */ public int genCode(NslPreProcessor npp) { String decl_stmt; String inst_stmt; String init_stmt; int tmp_dim; int tmp_prec; int tmp_num; String tmp_size1=""; String tmp_size2=""; String index2 = ""; String tmp_precision; expr.genCode(npp); if (expr.processed) processed = true; code = (char)op+"("+expr.code+")"; switch (op) { case '-': if(expr.type == null) { return NO_OP; } // assume java can handle CLASS/METHOD type rexpression if(expr.type.type >= TypeNode.STRING || !(expr.type.nslNumeric)) { return NO_OP; } // error in processing java native object of higher degree // or nsl numeric object with degree higher than 2 if(expr.type.dim > expr.type.nslDim) { return HIGHER_DEGREE_ERROR; } /* generate temporary variable for the negation operation */ /* precision of the output is the same as the input */ tmp_prec = type.type = expr.type.type; /* dimension of output is the same as the input */ tmp_dim = expr.type.dim; //////////////////////////////////////////////////////////// if(Use_Nsl_As_Temp_Variable) { /* generate temp variable type */ if(tmp_prec == TypeNode.INT) { if (tmp_dim==0) tmp_precision = "NslInt0 "; else tmp_precision = "NslInt"+tmp_dim+" "; } else if (tmp_prec == TypeNode.FLOAT) { if (tmp_dim==0) tmp_precision = "NslFloat0 "; else tmp_precision = "NslFloat"+tmp_dim+" "; } else if (tmp_prec == TypeNode.DOUBLE) { if (tmp_dim==0) tmp_precision = "NslDouble0 "; else tmp_precision = "NslDouble"+tmp_dim+" "; } else { return NO_OP; } /* size of temp variable is the same as the input variable */ if(tmp_dim>0) { tmp_size1=getTempSize1(expr, npp); if(tmp_dim>1) index2 = getTempSize2(expr, npp); tmp_size2=", "+index2; } // get temp number from the preprocessor tmp_number = tmp_num = npp.getNextTempNumber(); // construct delcaration statment decl_stmt = npp.TEMP_ACCESS_FLAG+tmp_precision+ npp.TEMP_NUMBER_PREFIX+tmp_num+";"; // construct instantiation statement inst_stmt = npp.TEMP_NUMBER_PREFIX+tmp_num+" = new "+ tmp_precision+"("+tmp_size1+tmp_size2+");"; // construct initialization statement if (tmp_dim == 0) init_stmt = npp.TEMP_NUMBER_PREFIX+tmp_num+" = 0;"; else if (tmp_dim == 1) init_stmt = "for (int i = 0; i < "+npp.TEMP_NUMBER_PREFIX+tmp_num+".length; i++) {\n\t\t"+ npp.TEMP_NUMBER_PREFIX+tmp_num+"[i] = 0;\n}"; else // if (tmp_dim == 2) init_stmt = "for (int i = 0; i < "+npp.TEMP_NUMBER_PREFIX+tmp_num+".length; i++) {\n"+ "\t\tfor (int j = 0; j < "+npp.TEMP_NUMBER_PREFIX+tmp_num+"[0].length; j++) {\n\t\t\t"+ npp.TEMP_NUMBER_PREFIX+tmp_num+"[i][j] = 0;\n\t\t}\n\t}"; npp.registerTempVar(tmp_num, decl_stmt, inst_stmt, init_stmt); // still lazy!!!!!!!!!!!!!!!!!!!1 code = npp.TEMP_NUMBER_PREFIX+tmp_num+ "=nslj.src.math.NslSub.eval"+"("+npp.TEMP_NUMBER_PREFIX+ tmp_num+",0,"+expr.code+")"; /* code = "("+expr.code+").subRev("+ npp.TEMP_NUMBER_PREFIX+tmp_num+", 0)"; */ } /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// if (!Use_Nsl_As_Temp_Variable) { /* generate temp variable type */ if(tmp_prec == TypeNode.INT) { // if (tmp_dim==0) tmp_precision = "int"; // else // tmp_precision = "int["; } else if (tmp_prec == TypeNode.FLOAT) { // if (tmp_dim==0) tmp_precision = "float"; // else // tmp_precision = "float["; } else if (tmp_prec == TypeNode.DOUBLE) { // if (tmp_dim==0) tmp_precision = "double"; // else // tmp_precision = "double["; } else { return NO_OP; } /* size of temp variable is the same as the input variable */ if(tmp_dim>0) { tmp_size1=getTempSize1(expr, npp); if(tmp_dim>1) index2 = getTempSize2(expr, npp); tmp_size2="]["+index2; } if(tmp_dim == 0) { code = "nslj.src.math.NslSub.eval(0,"+expr.code+")"; } else { // get temp number from the preprocessor tmp_number = tmp_num = npp.getNextTempNumber(); if (tmp_dim==1) { // construct delcaration statment decl_stmt = npp.TEMP_ACCESS_FLAG+" "+tmp_precision+"[] "+ npp.TEMP_NUMBER_PREFIX+tmp_num+";"; // construct instantiation statement inst_stmt = npp.TEMP_NUMBER_PREFIX+tmp_num+" = new "+ // tmp_precision+tmp_size1+"];"; tmp_precision+"[1];"; /* // construct initialization statement if (tmp_dim == 0) init_stmt = npp.TEMP_NUMBER_PREFIX+tmp_num+" = 0;"; else */ init_stmt = "for (int i = 0; i < "+npp.TEMP_NUMBER_PREFIX+tmp_num+".length; i++) {\n\t\t"+ npp.TEMP_NUMBER_PREFIX+tmp_num+"[i] = 0;\n}"; npp.registerTempVar(tmp_num, decl_stmt, inst_stmt, init_stmt); } else if (tmp_dim == 2) { // construct delcaration statment decl_stmt = npp.TEMP_ACCESS_FLAG+" "+tmp_precision+"[][] "+ npp.TEMP_NUMBER_PREFIX+tmp_num+";"; inst_stmt = npp.TEMP_NUMBER_PREFIX+tmp_num+" = new "+ tmp_precision+"[1][1];"; // construct initialization statement init_stmt = "for (int i = 0; i < "+npp.TEMP_NUMBER_PREFIX+tmp_num+".length; i++) {\n"+ "\t\tfor (int j = 0; j < "+npp.TEMP_NUMBER_PREFIX+tmp_num+"[0].length; j++) {\n\t\t\t"+ npp.TEMP_NUMBER_PREFIX+tmp_num+"[i][j] = 0;\n\t\t}\n\t}"; npp.registerTempVar(tmp_num, decl_stmt, inst_stmt, init_stmt); } else if (tmp_dim == 3) { // construct delcaration statment decl_stmt = npp.TEMP_ACCESS_FLAG+" "+tmp_precision+"[][][] "+ npp.TEMP_NUMBER_PREFIX+tmp_num+";"; inst_stmt = npp.TEMP_NUMBER_PREFIX+tmp_num+" = new "+ tmp_precision+"[1][1][1];"; // construct initialization statement init_stmt = "for (int i = 0; i < "+npp.TEMP_NUMBER_PREFIX+tmp_num+".length; i++) {\n"+ "\t\tfor (int j = 0; j < "+npp.TEMP_NUMBER_PREFIX+tmp_num+"[0].length; j++) {\n"+ "\t\t\tfor (int k = 0; k < "+npp.TEMP_NUMBER_PREFIX+tmp_num+"[0][0].length; k++) {\n\t\t\t\t"+ npp.TEMP_NUMBER_PREFIX+tmp_num+"[i][j][k] = 0;\n\t\t\t}\n\t\t}\n\t}"; npp.registerTempVar(tmp_num, decl_stmt, inst_stmt, init_stmt); } else if (tmp_dim == 4) { // construct delcaration statment decl_stmt = npp.TEMP_ACCESS_FLAG+" "+tmp_precision+"[][][][] "+ npp.TEMP_NUMBER_PREFIX+tmp_num+";"; inst_stmt = npp.TEMP_NUMBER_PREFIX+tmp_num+" = new "+ tmp_precision+"[1][1][1][1];"; // construct initialization statement init_stmt = "for (int i = 0; i < "+npp.TEMP_NUMBER_PREFIX+tmp_num+".length; i++) {\n"+ "\t\tfor (int j = 0; j < "+npp.TEMP_NUMBER_PREFIX+tmp_num+"[0].length; j++) {\n"+ "\t\t\tfor (int k = 0; k < "+npp.TEMP_NUMBER_PREFIX+tmp_num+"[0][0].length; k++) {\n"+ "\t\t\t\tfor (int l = 0; l < "+npp.TEMP_NUMBER_PREFIX+tmp_num+"[0][0][0].length; l++) {\n\t\t\t\t"+ npp.TEMP_NUMBER_PREFIX+tmp_num+"[i][j][k][l] = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}"; npp.registerTempVar(tmp_num, decl_stmt, inst_stmt, init_stmt); } if (expr.nslSymbol) expr.code+=".get()"; code = "\n "+npp.TEMP_NUMBER_PREFIX+tmp_num+ "=nslj.src.math.NslSub.eval("+npp.TEMP_NUMBER_PREFIX+ tmp_num+",0,"+expr.code+")"; } } type.numeric = false; type.nslNumeric = true; //System.out.println("GenCode: "+code); processed = true; return SUCCESS; case PRE_INCR: code = "++("+expr.code+")"; return NO_OP; case PRE_DECR: code = "--("+expr.code+")"; return NO_OP; case POST_INCR: code = "("+expr.code+")++"; return NO_OP; case POST_DECR: code = "("+expr.code+")--"; return NO_OP; case '+': case '!': if (expr.type.nslBool) { code = "nslj.src.math.NslNot.eval("+expr.code+")"; processed = true; return NO_OP; } case '~': default: } return NO_OP; } /* return error codes */ final public static int NO_OP = 0; final public static int SUCCESS = 1; final public static int INVALID_OP_ERROR = 2; final public static int HIGHER_DEGREE_ERROR = 3; final public static int DIM_NOT_MATCH_ERROR = 4;// for assignment statements final public static int PRE_INCR = 1; final public static int PRE_DECR = 2; final public static int POST_INCR = 3; final public static int POST_DECR = 4; public ExprNode expr; public int op; }