|
2 | 2 |
|
3 | 3 | internal class Program |
4 | 4 | { |
5 | | - static void Main(string[] args) |
| 5 | + |
| 6 | + public enum Command |
| 7 | + { |
| 8 | + Evaluate, |
| 9 | + Compare |
| 10 | + } |
| 11 | + |
| 12 | + /// <summary> |
| 13 | + /// Tom's boolean expression parser and evaluator. |
| 14 | + /// </summary> |
| 15 | + /// <param name="subCommand">The command to perform</param> |
| 16 | + /// <param name="expression">The expression to evaluate</param> |
| 17 | + /// <param name="secondExpression">The expression to compare to (if using Compare sub-command)</param> |
| 18 | + |
| 19 | + static void Main(Command subCommand = Command.Evaluate, string? expression, string? secondExpression) |
6 | 20 | { |
7 | | - string expression = @"(!(A & B) | C) & D"; |
| 21 | + Console.WriteLine("Tom's boolean expression parser and evaluator."); |
| 22 | + Console.WriteLine($"Command: {subCommand}"); |
| 23 | + Console.WriteLine($"Expression: {expression ?? "Not specified"}"); |
| 24 | + Console.WriteLine($"Second Expression: {secondExpression ?? "Not specified"}"); |
| 25 | + |
| 26 | + switch (subCommand) |
| 27 | + { |
| 28 | + case Command.Evaluate: |
| 29 | + if (expression is null) |
| 30 | + { |
| 31 | + Console.WriteLine("Enter expression:"); |
| 32 | + Console.Write("> "); |
| 33 | + expression = Console.ReadLine(); |
| 34 | + } |
| 35 | + |
| 36 | + break; |
| 37 | + case Command.Compare: |
| 38 | + if (expression is null) |
| 39 | + { |
| 40 | + Console.WriteLine("Enter expression:"); |
| 41 | + Console.Write("> "); |
| 42 | + expression = Console.ReadLine(); |
| 43 | + } |
| 44 | + if (secondExpression is null) |
| 45 | + { |
| 46 | + Console.WriteLine("Enter second expression:"); |
| 47 | + Console.Write("> "); |
| 48 | + secondExpression = Console.ReadLine(); |
| 49 | + } |
| 50 | + |
| 51 | + break; |
| 52 | + } |
8 | 53 |
|
9 | | - expression = expression.Replace(" ", ""); |
| 54 | + |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + static ParsedExpression RunExpression(string expression) |
| 59 | + { |
| 60 | + var strippedExpression = expression.Replace(" ", ""); |
10 | 61 |
|
11 | 62 | var tokens = new List<Token>(); |
12 | | - var reader = new StringReader(expression); |
| 63 | + var reader = new StringReader(strippedExpression); |
13 | 64 |
|
14 | 65 | Console.WriteLine("Tokenising..."); |
15 | 66 | Token token; |
@@ -58,17 +109,28 @@ static void Main(string[] args) |
58 | 109 | } |
59 | 110 |
|
60 | 111 | var result = Evaluate(root, values); |
61 | | - Console.WriteLine($"Combination {i} ({binary}) = {result}"); |
| 112 | + Console.WriteLine($"Combination {i} ({binary}) = {(result ? "1" : "0")}"); |
62 | 113 |
|
63 | | - tableRows.Add(values.Values.Concat(new bool[] { result }).ToArray()); |
| 114 | + tableRows.Add(values.Values.Append(result).ToArray()); |
64 | 115 | } |
65 | 116 |
|
66 | 117 | Console.WriteLine(); |
67 | | - Console.WriteLine("Truth table:"); |
| 118 | + Console.WriteLine($"Truth table for '{expression}':"); |
68 | 119 |
|
69 | 120 | var table = FormatTruthTable(variables, tableRows); |
70 | 121 |
|
71 | | - Console.WriteLine(table); |
| 122 | + return new ParsedExpression |
| 123 | + { |
| 124 | + Expression = expression, |
| 125 | + StrippedExpression = strippedExpression, |
| 126 | + Tokens = tokens, |
| 127 | + PolishTokens = polishTokens, |
| 128 | + RootNode = root, |
| 129 | + ASTString = text, |
| 130 | + Table = tableRows, |
| 131 | + TableString = table |
| 132 | + }; |
| 133 | + |
72 | 134 | } |
73 | 135 |
|
74 | 136 | static List<Token> ToPolish(List<Token> normalTokens) |
|
0 commit comments