allow bin numbers (0b11) in test cases

This commit is contained in:
hneemann 2017-07-06 19:55:07 +02:00
parent 7c46da19e8
commit 17e07b70da
3 changed files with 31 additions and 7 deletions

View File

@ -124,7 +124,7 @@ public class Parser {
line = new LineEmitterSimple(names.size(), tok.getLine());
switch (token) {
case NUMBER:
Value num = new Value(tok.getIdent());
Value num = new Value(convToLong(tok.getIdent()));
line.add((vals, context) -> vals.add(num));
break;
case BITS:
@ -157,6 +157,18 @@ public class Parser {
}
}
private long convToLong(String num) throws ParserException {
try {
num = num.trim().toLowerCase();
if (num.startsWith("0b"))
return Long.parseLong(num.substring(2), 2);
else
return Long.decode(num);
} catch (NumberFormatException e) {
throw new ParserException(Lang.get("err_notANumber_N0_inLine_N1", tok.getIdent(), tok.getLine()));
}
}
private long parseInt() throws ParserException, IOException {
return parseExpression().value(new Context());
}
@ -349,12 +361,8 @@ public class Parser {
String name = tok.getIdent();
return (c) -> c.getVar(name);
case NUMBER:
try {
long num = Long.decode(tok.getIdent());
return (c) -> num;
} catch (NumberFormatException e) {
throw new ParserException(Lang.get("err_notANumber_N0_inLine_N1", tok.getIdent(), tok.getLine()));
}
long num = convToLong(tok.getIdent());
return (c) -> num;
case SUB:
Expression negExp = parseIdent();
return (c) -> -negExp.value(c);

View File

@ -44,6 +44,8 @@ public class ParserExpressionTest extends TestCase {
assertEquals(1, new Parser("(n>>8)*(n&255)").getValue(new ContextWithVar("n").setValue(257)));
assertEquals(0x11, new Parser("0x10+1").getValue());
assertEquals(0b11, new Parser("0b10+1").getValue());
assertEquals(6, new Parser("a*b").getValue(new ContextWithVar(new ContextWithVar("a").setValue(2),"b").setValue(3)));
}

View File

@ -51,6 +51,20 @@ public class ParserTest extends TestCase {
assertEquals(Value.Type.NORMAL, td.getLines().get(0)[1].getType());
}
public void testBin() throws TestingDataException, IOException, ParserException {
Parser parser = new Parser("A B\n0 0b11111111").parse();
LineCollector td = new LineCollector(parser);
assertEquals(2, td.getNames().size());
assertEquals(1, td.getLines().size());
assertEquals(0, td.getLines().get(0)[0].getValue());
assertEquals(Value.Type.NORMAL, td.getLines().get(0)[0].getType());
assertEquals(255, td.getLines().get(0)[1].getValue());
assertEquals(Value.Type.NORMAL, td.getLines().get(0)[1].getType());
}
public void testMissingValue() throws IOException, ParserException {
try {
new Parser("A B\n0 0\n1").parse().getLines().emitLines(values -> {}, new Context());