add also | and & as operations OR and AND.

This commit is contained in:
hneemann 2016-07-01 12:05:26 +02:00
parent 445dc5393f
commit d77cfcf6b1
2 changed files with 10 additions and 8 deletions

View File

@ -40,6 +40,8 @@ public class Parser {
tokenizer.wordChars('_', '_');
tokenizer.wordChars('^', '^');
tokenizer.wordChars('0', '9');
// tokenizer.ordinaryChar('∧'); StreamTokenizer can not handle ordinary chars > 255
// tokenizer.ordinaryChar('');
}
private boolean isNext(String str) throws IOException {
@ -76,7 +78,7 @@ public class Parser {
private Expression parseOr() throws IOException, ParseException {
Expression ex = parseAnd();
while (isNext('+') || isNext("")) {
while (isNext('+') || isNext("") || isNext('|')) {
ex = Operation.or(ex, parseAnd());
}
return ex;
@ -84,7 +86,7 @@ public class Parser {
private Expression parseAnd() throws IOException, ParseException {
Expression ex = parseSimpleExp();
while (isNext('*') || isNext("")) {
while (isNext('*') || isNext("") || isNext('&')) {
ex = Operation.and(ex, parseSimpleExp());
}
return ex;

View File

@ -21,15 +21,15 @@ public class ParserTest extends TestCase {
}
public void testParseOr() throws Exception {
Parser p = new Parser("a+b");
Expression exp = p.parse();
assertTrue(exp instanceof Operation.Or);
assertTrue(new Parser("a+b").parse() instanceof Operation.Or);
assertTrue(new Parser("a b").parse() instanceof Operation.Or);
assertTrue(new Parser("a|b").parse() instanceof Operation.Or);
}
public void testParseAnd() throws Exception {
Parser p = new Parser("a*b");
Expression exp = p.parse();
assertTrue(exp instanceof Operation.And);
assertTrue(new Parser("a*b").parse() instanceof Operation.And);
assertTrue(new Parser("a ∧ b").parse() instanceof Operation.And);
assertTrue(new Parser("a&b").parse() instanceof Operation.And);
}
public void testParseParenthesis() throws Exception {