Example #1
0
        private static CircuitGate ParseGate(string gateString, WireTable wires)
        {
            var gate = new CircuitGate();

            if (gateString.Contains("NOT"))
            {
                gate.Operator = CircuitGateType.NOT;

                gate.Wire1 = ParseOperand(gateString.Substring(3).Trim(), wires);
            }
            else
            {
                //Two operands separated by an operator
                var parsed = Regex.Match(gateString, "(?<in1>\\w+)\\s+(?<op>\\w+)\\s+(?<in2>\\w+)");

                if (parsed.Groups.Count <= 3)
                {
                    Console.WriteLine(string.Format("Problem parsing gate: '{0}'.", gateString));
                }

                //Match the operator
                foreach (var oper in Enum.GetNames(typeof(CircuitGateType)))
                {
                    if (oper.ToUpper() == parsed.Groups["op"].Value.Trim())
                    {
                        gate.Operator = (CircuitGateType)Enum.Parse(typeof(CircuitGateType), oper);
                        break;
                    }
                }

                //Parse the operands
                gate.Wire1 = ParseOperand(parsed.Groups["in1"].Value, wires);
                gate.Wire2 = ParseOperand(parsed.Groups["in2"].Value, wires);
            }

            return gate;
        }
Example #2
0
        private static CircuitWire ParseOperand(string operand, WireTable wires)
        {
            ushort value;

            if (ushort.TryParse(operand, out value))
            {
                return new CircuitWire("signal") { Value = value };
            }
            else
            {
                return wires.GetWire(operand);
            }
        }