|
| 1 | +package com.stuffwithstuff.bantam; |
| 2 | + |
| 3 | +import com.stuffwithstuff.bantam.expressions.Expression; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + public static void main(String[] args) { |
| 7 | + // Function call. |
| 8 | + test("a()", "a()"); |
| 9 | + test("a(b)", "a(b)"); |
| 10 | + test("a(b, c)", "a(b, c)"); |
| 11 | + test("a(b)(c)", "a(b)(c)"); |
| 12 | + test("a(b) + c(d)", "(a(b) + c(d))"); |
| 13 | + test("a(b ? c : d, e + f)", "a((b ? c : d), (e + f))"); |
| 14 | + |
| 15 | + // Unary precedence. |
| 16 | + test("~!-+a", "(~(!(-(+a))))"); |
| 17 | + test("a!!!", "(((a!)!)!)"); |
| 18 | + |
| 19 | + // Unary and binary predecence. |
| 20 | + test("-a * b", "((-a) * b)"); |
| 21 | + test("!a + b", "((!a) + b)"); |
| 22 | + test("~a ^ b", "((~a) ^ b)"); |
| 23 | + test("-a!", "(-(a!))"); |
| 24 | + test("!a!", "(!(a!))"); |
| 25 | + |
| 26 | + // Binary precedence. |
| 27 | + test("a = b + c * d ^ e - f / g", "(a = ((b + (c * (d ^ e))) - (f / g)))"); |
| 28 | + |
| 29 | + // Binary associativity. |
| 30 | + test("a = b = c", "(a = (b = c))"); |
| 31 | + test("a + b - c", "((a + b) - c)"); |
| 32 | + test("a * b / c", "((a * b) / c)"); |
| 33 | + test("a ^ b ^ c", "(a ^ (b ^ c))"); |
| 34 | + |
| 35 | + // Conditional operator. |
| 36 | + test("a ? b : c ? d : e", "(a ? b : (c ? d : e))"); |
| 37 | + test("a ? b ? c : d : e", "(a ? (b ? c : d) : e)"); |
| 38 | + test("a + b ? c * d : e / f", "((a + b) ? (c * d) : (e / f))"); |
| 39 | + |
| 40 | + // Grouping. |
| 41 | + test("a + (b + c) + d", "((a + (b + c)) + d)"); |
| 42 | + test("a ^ (b + c)", "(a ^ (b + c))"); |
| 43 | + test("(!a)!", "((!a)!)"); |
| 44 | + |
| 45 | + // Show the results. |
| 46 | + if (sFailed == 0) { |
| 47 | + System.out.println("Passed all " + sPassed + " tests."); |
| 48 | + } else { |
| 49 | + System.out.println("----"); |
| 50 | + System.out.println("Failed " + sFailed + " out of " + |
| 51 | + (sFailed + sPassed) + " tests."); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Parses the given chunk of code and verifies that it matches the expected |
| 57 | + * pretty-printed result. |
| 58 | + */ |
| 59 | + public static void test(String source, String expected) { |
| 60 | + Lexer lexer = new Lexer(source); |
| 61 | + Parser parser = new BantamParser(lexer); |
| 62 | + |
| 63 | + try { |
| 64 | + Expression result = parser.parseExpression(); |
| 65 | + StringBuilder builder = new StringBuilder(); |
| 66 | + result.print(builder); |
| 67 | + String actual = builder.toString(); |
| 68 | + |
| 69 | + if (expected.equals(actual)) { |
| 70 | + sPassed++; |
| 71 | + } else { |
| 72 | + sFailed++; |
| 73 | + System.out.println("[FAIL] Expected: " + expected); |
| 74 | + System.out.println(" Actual: " + actual); |
| 75 | + } |
| 76 | + } catch(ParseException ex) { |
| 77 | + sFailed++; |
| 78 | + System.out.println("[FAIL] Expected: " + expected); |
| 79 | + System.out.println(" Error: " + ex.getMessage()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static int sPassed = 0; |
| 84 | + private static int sFailed = 0; |
| 85 | +} |
0 commit comments