Skip to content

Commit 9e0ffcc

Browse files
committed
Re-implemented functionality
The program now takes in expressions, tokenises, parses, builds an AST and then prints out a truth table.
1 parent 38fc557 commit 9e0ffcc

File tree

12 files changed

+45
-274
lines changed

12 files changed

+45
-274
lines changed

v2/BooleanExpressionParser/.vscode/launch.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
"request": "launch",
88
"preLaunchTask": "build",
99
"program": "${workspaceFolder}/bin/Debug/net6.0/BooleanExpressionParser.dll",
10-
"args": [],
10+
"args": [
11+
"A.B",
12+
"A+B"
13+
],
1114
"cwd": "${workspaceFolder}",
1215
"console": "integratedTerminal", // enables input (instead of internalConsole)
1316
"stopAtEntry": false,

v2/BooleanExpressionParser/Evaluator.cs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,44 @@ public Evaluator(Ast ast)
99
Ast = ast;
1010
}
1111

12-
public List<bool[]> RunAll()
12+
public List<bool[]> EvaluateAll()
1313
{
1414
int numCombinations = (int)Math.Pow(2, Ast.Variables.Count);
1515
var table = new List<bool[]>();
1616

1717
for (int i = 0; i < numCombinations; i++)
1818
{
1919
var binary = Convert.ToString(i, 2).PadLeft(Ast.Variables.Count, '0');
20-
var row = new bool[Ast.Variables.Count + 1];
20+
var values = binary.Select(c => c == '1').ToArray();
2121

22-
for (int j = 0; j < Ast.Variables.Count; j++)
23-
{
24-
row[j] = binary[j] == '1';
25-
}
22+
var result = Evaluate(Ast.Root, values);
23+
table.Add(values.Append(result).ToArray());
2624
}
25+
26+
return table;
2727
}
2828

29-
private bool Evaluate(Node root, bool[] values)
29+
private bool Evaluate(Node node, bool[] values)
3030
{
31-
31+
if (node is VariableNode var)
32+
{
33+
var index = Ast.Variables.IndexOf(var.Name);
34+
return values[index];
35+
}
36+
else if (node is OperatorNode op)
37+
{
38+
var left = Evaluate(op.Left, values);
39+
var right = Evaluate(op.Right!, values);
40+
41+
if (node is AndOperatorNode) return left && right;
42+
if (node is OrOperatorNode) return left || right;
43+
if (node is NotOperatorNode) return !left;
44+
}
45+
else
46+
{
47+
throw new Exception($"Unknown node type: {node.GetType()}");
48+
}
49+
50+
throw new Exception("This should never happen");
3251
}
3352
}

v2/BooleanExpressionParser/Formatter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,20 @@ public static string FormatPrefixTokens(IEnumerable<Token> tokens)
6262
return sb.ToString();
6363
}
6464

65-
public static string FormatTruthTable(List<string> variables, List<bool[]> table)
65+
public static string FormatTruthTable(Ast ast, List<bool[]> table)
6666
{
6767
var sb = new StringBuilder();
6868

69-
sb.AppendLine($"┏{Repeat('━', variables.Count * 3 + 2)}{Repeat('━', 8)}┓");
70-
sb.AppendLine($"┃ {String.Join(" ", variables)} ┃ Result ┃");
71-
sb.AppendLine($"┣{Repeat('━', variables.Count * 3 + 2)}{Repeat('━', 8)}┫");
69+
sb.AppendLine($"┏{Repeat('━', ast.Variables.Count * 3 + 2)}{Repeat('━', 8)}┓");
70+
sb.AppendLine($"┃ {String.Join(" ", ast.Variables)} ┃ Result ┃");
71+
sb.AppendLine($"┣{Repeat('━', ast.Variables.Count * 3 + 2)}{Repeat('━', 8)}┫");
7272

7373
foreach (var row in table)
7474
{
7575
sb.AppendLine($"┃ {String.Join(" ", row[0..^1].Select(b => b ? "1" : "0"))}{(row[^1] ? "1" : "0")} ┃");
7676
}
7777

78-
sb.AppendLine($"┗{Repeat('━', variables.Count * 3 + 2)}{Repeat('━', 8)}┛");
78+
sb.AppendLine($"┗{Repeat('━', ast.Variables.Count * 3 + 2)}{Repeat('━', 8)}┛");
7979

8080
return sb.ToString();
8181
}
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
public class AndOperatorNode : OperatorNode
22
{
33
public AndOperatorNode(Node left, Node right) : base(left, right) { }
4-
5-
public override bool Evaluate()
6-
{
7-
return Left.Evaluate() && Right!.Evaluate();
8-
}
94
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
public abstract class Node
22
{
3-
4-
public abstract bool Evaluate();
53

64
}
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
public class NotOperatorNode : OperatorNode
22
{
33
public NotOperatorNode(Node left) : base(left, null) { }
4-
5-
public override bool Evaluate()
6-
{
7-
return !Left.Evaluate();
8-
}
94
}

v2/BooleanExpressionParser/Nodes/OperatorNode.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
public abstract class OperatorNode : Node
22
{
3-
43
public OperatorNode(Node left, Node? right)
54
{
65
Left = left;
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
public class OrOperatorNode : OperatorNode
22
{
33
public OrOperatorNode(Node left, Node right) : base(left, right) { }
4-
5-
public override bool Evaluate()
6-
{
7-
return Left.Evaluate() || Right!.Evaluate();
8-
}
94
}

v2/BooleanExpressionParser/Nodes/VariableNode.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,4 @@ public VariableNode(String name)
77

88
public String Name { get; set; }
99
public bool Value { get; set; }
10-
11-
public override bool Evaluate() => Value;
1210
}

v2/BooleanExpressionParser/ParsedExpression.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)