/** * Add or replace initialization statement in this VarInit * * <p>Note: We create a Literal node (of the appropriate type) and add it to "varInit.expression" * * @param varType : Variable type * @param varInit : Variable initialization * @param vals : Value to assign */ boolean initializeArgs(Type varType, VariableInit varInit, ArrayList<String> vals) { boolean usedVal = true; try { Literal literal = null; if (varType.isList(Type.STRING)) { // Create literal LiteralListString lit = new LiteralListString(varInit, null); literal = lit; lit.setValue(vals); // Set literal value } else throw new RuntimeException( "Cannot convert command line argument to variable type '" + varType + "'"); // Set varInit to literal varInit.setExpression(literal); } catch (Exception e) { // Error parsing 'val'? throw new RuntimeException("Cannot convert argument '" + vals + "' to type " + varType); } return usedVal; }
/** * Add or replace initialization statement in this VarInit * * <p>Note: We create a Literal node (of the appropriate type) and add it to "varInit.expression" * * @param varType : Variable type * @param varInit : Variable initialization * @param valStr : Value to assign */ boolean initializeArgs(Type varType, VariableInit varInit, String valStr) { boolean usedVal = true; try { Literal literal = null; // Create a different literal for each primitive type if (varType.isBool()) { // Create literal LiteralBool lit = new LiteralBool(varInit, null); literal = lit; // Set literal value boolean valBool = true; // Default value is 'true' if (valStr != null) { // Parse boolean valStr = valStr.toLowerCase(); if (valStr.equals("true") || valStr.equals("t") || valStr.equals("1")) valBool = true; else if (valStr.equals("false") || valStr.equals("f") || valStr.equals("0")) valBool = false; else usedVal = false; // Not any valid value? => This // argument is not used } lit.setValue(valBool); } else if (varType.isInt()) { // Create literal LiteralInt lit = new LiteralInt(varInit, null); literal = lit; // Set literal value long valInt = Long.parseLong(valStr); lit.setValue(valInt); } else if (varType.isReal()) { // Create literal LiteralReal lit = new LiteralReal(varInit, null); literal = lit; // Set literal value double valReal = Double.parseDouble(valStr); lit.setValue(valReal); } else if (varType.isString()) { // Create literal LiteralString lit = new LiteralString(varInit, null); literal = lit; // Set literal value if (valStr == null) valStr = ""; // We should never have 'null' values lit.setValue(valStr); } else throw new RuntimeException( "Cannot convert command line argument to variable type '" + varType + "'"); // Set varInit to literal varInit.setExpression(literal); } catch (Exception e) { // Error parsing 'val'? throw new RuntimeException("Cannot convert argument '" + valStr + "' to type " + varType); } return usedVal; }