Skip to content

Commit 8b55e44

Browse files
committed
Worked on CLI support, multiple expressions
1 parent 6898f02 commit 8b55e44

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

v2/BooleanExpressionParser/Program.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,28 @@ public enum Command
1616
/// <param name="expression">The expression to evaluate</param>
1717
/// <param name="secondExpression">The expression to compare to (if using Compare sub-command)</param>
1818

19-
static void Main(Command subCommand = Command.Evaluate, string? expression, string? secondExpression)
19+
static void Main(Command? subCommand, string? expression, string? secondExpression)
2020
{
2121
Console.WriteLine("Tom's boolean expression parser and evaluator.");
2222
Console.WriteLine($"Command: {subCommand}");
2323
Console.WriteLine($"Expression: {expression ?? "Not specified"}");
2424
Console.WriteLine($"Second Expression: {secondExpression ?? "Not specified"}");
2525

26+
if (subCommand is null)
27+
{
28+
Console.WriteLine("Enter a command (Evaluate or Compare):");
29+
Console.WriteLine("> ");
30+
var command = Console.ReadLine();
31+
subCommand = command.ToLower() switch {
32+
"evaluate" => Command.Evaluate,
33+
"e" => Command.Evaluate,
34+
"compare" => Command.Compare,
35+
"c" => Command.Compare,
36+
_ => throw new ArgumentException($"Invalid command: {command}")
37+
};
38+
39+
}
40+
2641
switch (subCommand)
2742
{
2843
case Command.Evaluate:
@@ -33,7 +48,13 @@ static void Main(Command subCommand = Command.Evaluate, string? expression, stri
3348
expression = Console.ReadLine();
3449
}
3550

51+
var parsed = RunExpression(expression);
52+
53+
Console.WriteLine($"Truth table for '{expression}':");
54+
Console.WriteLine(parsed.TableString);
55+
3656
break;
57+
3758
case Command.Compare:
3859
if (expression is null)
3960
{
@@ -48,11 +69,22 @@ static void Main(Command subCommand = Command.Evaluate, string? expression, stri
4869
secondExpression = Console.ReadLine();
4970
}
5071

51-
break;
52-
}
72+
var parsed1 = RunExpression(expression);
73+
var parsed2 = RunExpression(secondExpression);
74+
75+
Console.WriteLine($"Truth table for '{expression}':");
76+
Console.WriteLine(parsed1.TableString);
77+
78+
Console.WriteLine($"Truth table for '{secondExpression}':");
79+
Console.WriteLine(parsed2.TableString);
5380

81+
bool equal = parsed1.ASTString == parsed2.ASTString;
82+
Console.WriteLine($"The two expressions are {(equal ? "equal" : "not equal")}.");
83+
84+
break;
5485

5586

87+
}
5688
}
5789

5890
static ParsedExpression RunExpression(string expression)

0 commit comments

Comments
 (0)