Skip to content

Commit d674bad

Browse files
committed
Added XOR, NAND, NOR, XNOR operators
1 parent 181af2b commit d674bad

File tree

14 files changed

+78
-8
lines changed

14 files changed

+78
-8
lines changed

v2/BooleanExpressionParser/.vscode/launch.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
"preLaunchTask": "build",
99
"program": "${workspaceFolder}/bin/Debug/net6.0/BooleanExpressionParser.dll",
1010
"args": [
11-
"A.B",
12-
"A+B",
13-
"!(A+B+C)",
14-
"(((A.B&C) OR A) AND (NOT B + !C)) AND NOT D",
15-
"!S.D_0 + D_1 AND S",
11+
// "A.B",
12+
// "A+B",
13+
// "!(A+B+C)",
14+
// "(((A.B&C) OR A) AND (NOT B + !C)) AND NOT D",
15+
// "!S.D_0 + D_1 AND S",
1616
],
1717
"cwd": "${workspaceFolder}",
1818
"console": "integratedTerminal", // enables input (instead of internalConsole)

v2/BooleanExpressionParser/Evaluator.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ private bool Evaluate(Node node, bool[] values)
4343

4444
if (node is AndOperatorNode) return left && right;
4545
if (node is OrOperatorNode) return left || right;
46+
if (node is XorOperatorNode) return left ^ right;
47+
if (node is NandOperatorNode) return !(left && right);
48+
if (node is NorOperatorNode) return !(left || right);
49+
if (node is XnorOperatorNode) return !(left ^ right);
50+
4651
}
4752
else
4853
{

v2/BooleanExpressionParser/Formatter.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ public static string FormatTokens(IEnumerable<Token> tokens)
1010

1111
foreach (var token in tokens)
1212
{
13-
sb.Append(token.ToString());
13+
string s = token.ToString();
14+
if (s.Length > 1) s = $"[{s}]";
15+
sb.Append(s);
1416
}
1517

1618
return sb.ToString();
@@ -52,7 +54,7 @@ public static string FormatTruthTable(Ast ast, List<bool[]> table, string label=
5254

5355
sb.Append("┗━");
5456
sb.AppendJoin(null, variableLine);
55-
sb.AppendLine($"━┻{resultLine}┛");
57+
sb.Append($"━┻{resultLine}┛");
5658

5759
return sb.ToString();
5860
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class NandOperatorNode : OperatorNode
2+
{
3+
public NandOperatorNode(Node left, Node right) : base(left, right) { }
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class NorOperatorNode : OperatorNode
2+
{
3+
public NorOperatorNode(Node left, Node right) : base(left, right) { }
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class XnorOperatorNode : OperatorNode
2+
{
3+
public XnorOperatorNode(Node left, Node right) : base(left, right) { }
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class XorOperatorNode : OperatorNode
2+
{
3+
public XorOperatorNode(Node left, Node right) : base(left, right) { }
4+
}

v2/BooleanExpressionParser/Parser.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public IEnumerable<Token> ParseTokens(IEnumerable<Token> tokens)
2222
case AndOperatorToken:
2323
case OrOperatorToken:
2424
case NotOperatorToken:
25+
case XorOperatorToken:
26+
case NandOperatorToken:
27+
case NorOperatorToken:
28+
case XnorOperatorToken:
2529
while ((stack.Count > 0 && stack.Peek() is OperatorToken && stack.Peek() is not OpenParenToken) && ((stack.Peek() as OperatorToken)!.Precedence >= (token as OperatorToken)!.Precedence))
2630
{
2731
output.Enqueue(stack.Pop());
@@ -42,7 +46,8 @@ public IEnumerable<Token> ParseTokens(IEnumerable<Token> tokens)
4246
if (stack.Peek() is OpenParenToken)
4347
{
4448
stack.Pop();
45-
if (stack.Count > 0 && (stack.Peek() is AndOperatorToken or OrOperatorToken or NotOperatorToken))
49+
// NOTE: Changed this check from several specific checks to 'is OperatorToken'
50+
if (stack.Count > 0 && (stack.Peek() is OperatorToken))
4651
{
4752
output.Enqueue(stack.Pop());
4853
}
@@ -79,12 +84,24 @@ public Ast GrowAst(IEnumerable<Token> tokens)
7984

8085
case AndOperatorToken:
8186
case OrOperatorToken:
87+
case XorOperatorToken:
88+
case NandOperatorToken:
89+
case NorOperatorToken:
90+
case XnorOperatorToken:
8291
if (stack.Count < 2) throw new Exception($"2 parameters needed for operator ${token}");
8392

8493
if (token is AndOperatorToken)
8594
stack.Push(new AndOperatorNode(stack.Pop(), stack.Pop()));
8695
else if (token is OrOperatorToken)
8796
stack.Push(new OrOperatorNode(stack.Pop(), stack.Pop()));
97+
else if (token is NorOperatorToken)
98+
stack.Push(new NorOperatorNode(stack.Pop(), stack.Pop()));
99+
else if (token is NandOperatorToken)
100+
stack.Push(new NandOperatorNode(stack.Pop(), stack.Pop()));
101+
else if (token is XorOperatorToken)
102+
stack.Push(new XorOperatorNode(stack.Pop(), stack.Pop()));
103+
else if (token is XnorOperatorToken)
104+
stack.Push(new XnorOperatorNode(stack.Pop(), stack.Pop()));
88105
break;
89106

90107
case NotOperatorToken:

v2/BooleanExpressionParser/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ static void Main(params string[] args)
1717

1818
var parser = new Parser();
1919
var prefixTokens = parser.ParseTokens(infixTokens);
20+
21+
Console.WriteLine(expression);
22+
Console.WriteLine("Infix: " + Formatter.FormatTokens(infixTokens));
23+
Console.WriteLine("Prefix: " + Formatter.FormatTokens(prefixTokens));
24+
2025
var ast = parser.GrowAst(prefixTokens);
2126

2227
var evaluator = new Evaluator(ast);

v2/BooleanExpressionParser/Tokeniser.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public IEnumerable<Token> Tokenise()
3232
"AND" or "." or "&" => new AndOperatorToken(),
3333
"OR" or "+" or "|" => new OrOperatorToken(),
3434
"NOT" or "!" or "¬" => new NotOperatorToken(),
35+
"XOR" => new XorOperatorToken(),
36+
"NAND" => new NandOperatorToken(),
37+
"NOR" => new NorOperatorToken(),
38+
"XNOR" => new XnorOperatorToken(),
3539
_ => new VariableToken(token)
3640
};
3741
}

0 commit comments

Comments
 (0)