mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-28 23:45:17 -04:00
allow bin numbers (0b11) in test cases
This commit is contained in:
parent
7c46da19e8
commit
17e07b70da
@ -124,7 +124,7 @@ public class Parser {
|
|||||||
line = new LineEmitterSimple(names.size(), tok.getLine());
|
line = new LineEmitterSimple(names.size(), tok.getLine());
|
||||||
switch (token) {
|
switch (token) {
|
||||||
case NUMBER:
|
case NUMBER:
|
||||||
Value num = new Value(tok.getIdent());
|
Value num = new Value(convToLong(tok.getIdent()));
|
||||||
line.add((vals, context) -> vals.add(num));
|
line.add((vals, context) -> vals.add(num));
|
||||||
break;
|
break;
|
||||||
case BITS:
|
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 {
|
private long parseInt() throws ParserException, IOException {
|
||||||
return parseExpression().value(new Context());
|
return parseExpression().value(new Context());
|
||||||
}
|
}
|
||||||
@ -349,12 +361,8 @@ public class Parser {
|
|||||||
String name = tok.getIdent();
|
String name = tok.getIdent();
|
||||||
return (c) -> c.getVar(name);
|
return (c) -> c.getVar(name);
|
||||||
case NUMBER:
|
case NUMBER:
|
||||||
try {
|
long num = convToLong(tok.getIdent());
|
||||||
long num = Long.decode(tok.getIdent());
|
return (c) -> num;
|
||||||
return (c) -> num;
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
throw new ParserException(Lang.get("err_notANumber_N0_inLine_N1", tok.getIdent(), tok.getLine()));
|
|
||||||
}
|
|
||||||
case SUB:
|
case SUB:
|
||||||
Expression negExp = parseIdent();
|
Expression negExp = parseIdent();
|
||||||
return (c) -> -negExp.value(c);
|
return (c) -> -negExp.value(c);
|
||||||
|
@ -44,6 +44,8 @@ public class ParserExpressionTest extends TestCase {
|
|||||||
assertEquals(1, new Parser("(n>>8)*(n&255)").getValue(new ContextWithVar("n").setValue(257)));
|
assertEquals(1, new Parser("(n>>8)*(n&255)").getValue(new ContextWithVar("n").setValue(257)));
|
||||||
|
|
||||||
assertEquals(0x11, new Parser("0x10+1").getValue());
|
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)));
|
assertEquals(6, new Parser("a*b").getValue(new ContextWithVar(new ContextWithVar("a").setValue(2),"b").setValue(3)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,6 +51,20 @@ public class ParserTest extends TestCase {
|
|||||||
assertEquals(Value.Type.NORMAL, td.getLines().get(0)[1].getType());
|
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 {
|
public void testMissingValue() throws IOException, ParserException {
|
||||||
try {
|
try {
|
||||||
new Parser("A B\n0 0\n1").parse().getLines().emitLines(values -> {}, new Context());
|
new Parser("A B\n0 0\n1").parse().getLines().emitLines(values -> {}, new Context());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user