mirror of
https://github.com/dancojocaru2000/logic-circuits-simulator.git
synced 2025-02-23 01:29:36 +02:00
138 lines
3.4 KiB
Dart
138 lines
3.4 KiB
Dart
abstract class LogicOperator {
|
||
bool apply(List<bool> inputs);
|
||
List<String> get representations;
|
||
String get defaultRepresentation => representations[0];
|
||
bool fromRepresentation(String repr) => representations.contains(repr);
|
||
}
|
||
|
||
abstract class ZeroOpLogicOperator extends LogicOperator {}
|
||
|
||
class FalseLogicOperator extends ZeroOpLogicOperator {
|
||
@override
|
||
bool apply(List<bool> inputs) => false;
|
||
|
||
@override
|
||
List<String> get representations => ['0'];
|
||
|
||
@override
|
||
bool operator==(other) => other is FalseLogicOperator;
|
||
|
||
@override
|
||
int get hashCode => defaultRepresentation.hashCode;
|
||
}
|
||
|
||
class TrueLogicOperator extends ZeroOpLogicOperator {
|
||
@override
|
||
bool apply(List<bool> inputs) => true;
|
||
|
||
@override
|
||
List<String> get representations => ['1'];
|
||
|
||
@override
|
||
bool operator==(other) => other is TrueLogicOperator;
|
||
|
||
@override
|
||
int get hashCode => defaultRepresentation.hashCode;
|
||
}
|
||
|
||
abstract class OneOpLogicOperator extends LogicOperator {}
|
||
|
||
class NotLogicOperator extends OneOpLogicOperator {
|
||
@override
|
||
bool apply(List<bool> inputs) => !inputs[0];
|
||
|
||
@override
|
||
List<String> get representations => const ['~', '!', '¬'];
|
||
|
||
@override
|
||
bool operator==(other) => other is NotLogicOperator;
|
||
|
||
@override
|
||
int get hashCode => defaultRepresentation.hashCode;
|
||
}
|
||
|
||
abstract class TwoOpLogicOperator extends LogicOperator {}
|
||
|
||
class AndLogicOperator extends TwoOpLogicOperator {
|
||
@override
|
||
bool apply(List<bool> inputs) => inputs[0] && inputs[1];
|
||
|
||
@override
|
||
List<String> get representations => const ['&', '∧'];
|
||
|
||
@override
|
||
bool operator==(other) => other is AndLogicOperator;
|
||
|
||
@override
|
||
int get hashCode => defaultRepresentation.hashCode;
|
||
}
|
||
|
||
class OrLogicOperator extends TwoOpLogicOperator {
|
||
@override
|
||
bool apply(List<bool> inputs) => inputs[0] || inputs[1];
|
||
|
||
@override
|
||
List<String> get representations => const ['|', '∨'];
|
||
|
||
@override
|
||
bool operator==(other) => other is OrLogicOperator;
|
||
|
||
@override
|
||
int get hashCode => defaultRepresentation.hashCode;
|
||
}
|
||
|
||
class XorLogicOperator extends TwoOpLogicOperator {
|
||
@override
|
||
bool apply(List<bool> inputs) => inputs[0] != inputs[1];
|
||
|
||
@override
|
||
List<String> get representations => const ['^', '⊕', '⊻'];
|
||
|
||
@override
|
||
bool operator==(other) => other is XorLogicOperator;
|
||
|
||
@override
|
||
int get hashCode => defaultRepresentation.hashCode;
|
||
}
|
||
|
||
class NandLogicOperator extends TwoOpLogicOperator {
|
||
@override
|
||
bool apply(List<bool> inputs) => !(inputs[0] && inputs[1]);
|
||
|
||
@override
|
||
List<String> get representations => const ['~&', '!&', '¬&', '~∧', '!∧', '¬∧'];
|
||
|
||
@override
|
||
bool operator==(other) => other is NandLogicOperator;
|
||
|
||
@override
|
||
int get hashCode => defaultRepresentation.hashCode;
|
||
}
|
||
|
||
class NorLogicOperator extends TwoOpLogicOperator {
|
||
@override
|
||
bool apply(List<bool> inputs) => !(inputs[0] || inputs[1]);
|
||
|
||
@override
|
||
List<String> get representations => const ['~|', '!|', '¬|', '~∨', '!∨', '¬∨'];
|
||
|
||
@override
|
||
bool operator==(other) => other is NorLogicOperator;
|
||
|
||
@override
|
||
int get hashCode => defaultRepresentation.hashCode;
|
||
}
|
||
|
||
class XnorLogicOperator extends TwoOpLogicOperator {
|
||
@override
|
||
bool apply(List<bool> inputs) => inputs[0] == inputs[1];
|
||
|
||
@override
|
||
List<String> get representations => const ['~^', '!^', '¬^', '~⊕', '!⊕', '¬⊕', '~⊻', '!⊻', '¬⊻'];
|
||
|
||
@override
|
||
bool operator==(other) => other is XnorLogicOperator;
|
||
|
||
@override
|
||
int get hashCode => defaultRepresentation.hashCode;
|
||
}
|