Repository navigation
Expand file tree
/
Copy pathOperator.cs
More file actions
67 lines (60 loc) · 2.11 KB
/
Copy pathOperator.cs
File metadata and controls
67 lines (60 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
namespace LogicParser
{
enum Operators { OR, AND, IMPLIES, NAND, NOR, XOR }
class Operator
{
public readonly Operators op;
public static readonly Operator OR = new('v');
public static readonly Operator AND = new('^');
public static readonly Operator IMPLIES = new('>');
public static readonly Operator NAND = new('/');
public static readonly Operator NOR = new('\\');
public static readonly Operator XOR = new('+');
public Operator(char c)
{
op = (c.ToString().ToCharArray()[0]) switch
{
'v' => Operators.OR,
'^' => Operators.AND,
'>' => Operators.IMPLIES,
'\\' => Operators.NOR,
'/' => Operators.NAND,
'+' => Operators.XOR,
_ => throw new ArgumentException("Invalid operator!"),
};
}
public override string ToString()
{
return op switch
{
Operators.AND => "^",
Operators.IMPLIES => "->",
Operators.OR => "v",
Operators.NOR => "\\",
Operators.NAND => "/",
Operators.XOR => "+",
_ => " ",
};
}
public Func<bool> Run(Expression Left, Expression Right, Dictionary<string, bool> vars)
{
return () =>
{
bool left = Left.Evaluate(vars) ^ (Left.HasOperator && Left.not);
bool right = Right.Evaluate(vars) ^ (Right.HasOperator && Right.not);
return op switch
{
Operators.AND => (left && right),
Operators.OR => (left || right),
Operators.IMPLIES => (!left || right),
Operators.NOR => !(left || right),
Operators.NAND => !(left && right),
Operators.XOR => ((left && !right) || (right && !left)),
_ => false,
};
};
}
}
}