Skip to content

Commit 6898f02

Browse files
committed
Started modularising program with CLI args
Need to fix, and implement expression comparisons
1 parent 38a96e7 commit 6898f02

File tree

3 files changed

+90
-8
lines changed

3 files changed

+90
-8
lines changed

v2/BooleanExpressionParser/BooleanExpressionParser.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
12+
<PackageReference Include="System.CommandLine.DragonFruit" Version="0.4.0-alpha.22272.1" />
13+
</ItemGroup>
14+
1015
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public record ParsedExpression
2+
{
3+
public string Expression { get; set; }
4+
public string StrippedExpression { get; set; }
5+
6+
public List<Token> Tokens { get; set; }
7+
public List<Token> PolishTokens { get; set; }
8+
9+
public Node RootNode { get; set; }
10+
public String ASTString { get; set; }
11+
12+
public List<bool[]> Table { get; set; }
13+
public String TableString { get; set; }
14+
15+
}

v2/BooleanExpressionParser/Program.cs

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,65 @@
22

33
internal class Program
44
{
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)
620
{
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+
}
853

9-
expression = expression.Replace(" ", "");
54+
55+
56+
}
57+
58+
static ParsedExpression RunExpression(string expression)
59+
{
60+
var strippedExpression = expression.Replace(" ", "");
1061

1162
var tokens = new List<Token>();
12-
var reader = new StringReader(expression);
63+
var reader = new StringReader(strippedExpression);
1364

1465
Console.WriteLine("Tokenising...");
1566
Token token;
@@ -58,17 +109,28 @@ static void Main(string[] args)
58109
}
59110

60111
var result = Evaluate(root, values);
61-
Console.WriteLine($"Combination {i} ({binary}) = {result}");
112+
Console.WriteLine($"Combination {i} ({binary}) = {(result ? "1" : "0")}");
62113

63-
tableRows.Add(values.Values.Concat(new bool[] { result }).ToArray());
114+
tableRows.Add(values.Values.Append(result).ToArray());
64115
}
65116

66117
Console.WriteLine();
67-
Console.WriteLine("Truth table:");
118+
Console.WriteLine($"Truth table for '{expression}':");
68119

69120
var table = FormatTruthTable(variables, tableRows);
70121

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+
72134
}
73135

74136
static List<Token> ToPolish(List<Token> normalTokens)

0 commit comments

Comments
 (0)