From: <ni...@us...> - 2008-08-29 22:05:58
|
Revision: 295 http://mspsim.svn.sourceforge.net/mspsim/?rev=295&view=rev Author: nifi Date: 2008-08-29 22:05:55 +0000 (Fri, 29 Aug 2008) Log Message: ----------- Fixed CLI to parse numerical arguments and addresses prefixed with '$','0x','#' as hexadecimal, '0' as octal, and '%' as binary Modified Paths: -------------- mspsim/se/sics/mspsim/cli/CommandContext.java mspsim/se/sics/mspsim/util/Utils.java Modified: mspsim/se/sics/mspsim/cli/CommandContext.java =================================================================== --- mspsim/se/sics/mspsim/cli/CommandContext.java 2008-08-29 21:58:33 UTC (rev 294) +++ mspsim/se/sics/mspsim/cli/CommandContext.java 2008-08-29 22:05:55 UTC (rev 295) @@ -3,6 +3,7 @@ import se.sics.mspsim.core.MSP430Constants; import se.sics.mspsim.util.MapTable; +import se.sics.mspsim.util.Utils; public class CommandContext { @@ -83,19 +84,13 @@ public int getArgumentAsAddress(int index) { String adr = getArgument(index); if (adr == null || adr.length() == 0) return 0; - adr = adr.trim(); - if (adr.charAt(0) == '$') { + char c = adr.charAt(0); + if (!Character.isLetter(c) && c != '_' && c != '.') { try { - return Integer.parseInt(adr.substring(1), 16); + return Utils.decodeInt(adr); } catch (Exception e) { - err.println("Illegal hex number format: " + adr); + err.println("Illegal address format: " + adr); } - } else if (Character.isDigit(adr.charAt(0))) { - try { - return Integer.parseInt(adr); - } catch (Exception e) { - err.println("Illegal number format: " + adr); - } } else { // Assume that it is a symbol if (mapTable != null) { @@ -112,7 +107,7 @@ return i; } } - String reg = symbol.startsWith("R") ? symbol.substring(1) : symbol; + String reg = (symbol.startsWith("R") || symbol.startsWith("r")) ? symbol.substring(1) : symbol; try { int register = Integer.parseInt(reg); if (register >= 0 && register <= 15) { @@ -127,40 +122,56 @@ } public int getArgumentAsInt(int index) { + return getArgumentAsInt(index, 0); + } + + public int getArgumentAsInt(int index, int defaultValue) { try { - return Integer.parseInt(getArgument(index)); + return Utils.decodeInt(getArgument(index)); } catch (Exception e) { err.println("Illegal number format: " + getArgument(index)); + return defaultValue; } - return 0; } public long getArgumentAsLong(int index) { + return getArgumentAsLong(index, 0L); + } + + public long getArgumentAsLong(int index, long defaultValue) { try { - return Long.parseLong(getArgument(index)); + return Utils.decodeLong(getArgument(index)); } catch (Exception e) { err.println("Illegal number format: " + getArgument(index)); + return defaultValue; } - return 0L; } public float getArgumentAsFloat(int index) { + return getArgumentAsFloat(index, 0f); + } + + public float getArgumentAsFloat(int index, float defaultValue) { try { return Float.parseFloat(getArgument(index)); } catch (Exception e) { err.println("Illegal number format: " + getArgument(index)); + return defaultValue; } - return 0f; } public double getArgumentAsDouble(int index) { + return getArgumentAsDouble(index, 0.0); + } + + public double getArgumentAsDouble(int index, double defaultValue) { String arg = getArgument(index); try { return Double.parseDouble(arg); } catch (Exception e) { err.println("Illegal number format: " + getArgument(index)); + return defaultValue; } - return 0.0; } public int executeCommand(String command) { Modified: mspsim/se/sics/mspsim/util/Utils.java =================================================================== --- mspsim/se/sics/mspsim/util/Utils.java 2008-08-29 21:58:33 UTC (rev 294) +++ mspsim/se/sics/mspsim/util/Utils.java 2008-08-29 22:05:55 UTC (rev 295) @@ -108,6 +108,78 @@ return array; } + public static int decodeInt(String value) throws NumberFormatException { + int radix = 10; + int index = 0; + boolean negative = false; + if (value.startsWith("-")) { + index++; + negative = true; + } + + if (value.startsWith("$", index) || value.startsWith("#", index)) { + radix = 16; + index++; + } else if (value.startsWith("0x", index) || value.startsWith("0X", index)) { + radix = 16; + index += 2; + } else if (value.startsWith("0", index) && value.length() > index + 1) { + radix = 8; + index++; + } else if (value.startsWith("%", index)) { + radix = 2; + index++; + } + String intValue = value; + if (radix != 10) { + if (value.startsWith("-", index)) { + throw new NumberFormatException("unexpected negative sign: " + value); + } + if (negative) { + intValue = '-' + value.substring(index); + } else { + intValue = value.substring(index); + } + } + return Integer.parseInt(intValue, radix); + } + + public static long decodeLong(String value) throws NumberFormatException { + int radix = 10; + int index = 0; + boolean negative = false; + if (value.startsWith("-")) { + index++; + negative = true; + } + + if (value.startsWith("$", index) || value.startsWith("#", index)) { + radix = 16; + index++; + } else if (value.startsWith("0x", index) || value.startsWith("0X", index)) { + radix = 16; + index += 2; + } else if (value.startsWith("0", index) && value.length() > index + 1) { + radix = 8; + index++; + } else if (value.startsWith("%", index)) { + radix = 2; + index++; + } + String longValue = value; + if (radix != 10) { + if (value.startsWith("-", index)) { + throw new NumberFormatException("unexpected negative sign: " + value); + } + if (negative) { + longValue = '-' + value.substring(index); + } else { + longValue = value.substring(index); + } + } + return Long.parseLong(longValue, radix); + } + // public static void main(String[] args) { // System.out.println("Hex 47 = " + hex8(47)); // } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |