Beispiel #1
0
        private bool ReadInputOutputDecleration(string line, bool isInput)
        {
            var index = line.IndexOf("=");
            Debug.Assert(index != -1);
            var tokens = line.Substring(index + 1).Split(new char[] { ',' });

            for (int i = 0; i < tokens.Length; i++)
            {
                var token = tokens[i].Trim();
                CheckVariableSynthax(token);
                var wire = new Wire(i, isInput);

                if (varsToWires.ContainsKey(token))
                {
                    if (isInput)
                        varsToWires[token].InputIndex = index;
                    else
                        varsToWires[token].IsOutput = true;
                }
                else
                    varsToWires[token] = wire;

                if (!isInput)
                {
                    Debug.Assert(outputVar == null);
                    outputVar = token;
                }
                else
                    inputs.Add(token);
            }
            return true;
        }
Beispiel #2
0
        private void SetGate(IList<Wire> inputWires, Wire outputWire, Operation op)
        {
            Gate gate = new Gate(inputWires, new List<Wire>() { outputWire }, op, prime);
            foreach (Wire wire in inputWires)
                wire.TargetGate = gate;

            outputWire.SourceGate = gate;
        }
Beispiel #3
0
        private Wire CreateMulGates(Wire firstWireOrNull, Operation firstOpOrNull, string line)
        {
            var tokens = new List<string>();
            var operations = new List<Operation>();

            SplitMul(line, tokens, operations);
            Debug.Assert(tokens.Count >= 2);
            var firstWire = firstWireOrNull == null ? GetWireFromString(tokens[0]) : firstWireOrNull;
            Operation currOperation = firstOpOrNull;

            if (currOperation == Operation.None)
                currOperation = operations[0];

            for (int i = firstWireOrNull == null ? 1 : 0; i < tokens.Count; i++)
            {
                var secondWire = GetWireFromString(tokens[i]);
                var outputWire = new Wire();
                SetGate(new List<Wire>() { firstWire, secondWire }, outputWire, currOperation);
                firstWire = outputWire;
                if (operations.Count <= i)
                    break;

                currOperation = operations[i];
            }
            return firstWire;
        }
Beispiel #4
0
        private Wire GetWireFromCondition(string line, Wire conditionWire)
        {
            int index1 = line.IndexOf("?");
            int index2 = line.IndexOf(":");
            Debug.Assert(index1 != -1 && index2 != -1 && index1 < index2);

            // get all 3 expression wires
            if (conditionWire == null)
                conditionWire = GetWireFromExpression(line.Substring(0, index1));

            var firstWire = GetWireFromExpression(line.Substring(index1 + 1, index2 - (index1 + 1)));
            var secondWire = GetWireFromExpression(line.Substring(index2 + 1));

            // normalizedConditionWire = conditionWire/conditionWire
            var normalizedConditionWire = new Wire();
            SetGate(new List<Wire>() { conditionWire, conditionWire }, normalizedConditionWire, Operation.Div);

            // reverseCondition = 1 - normalizedConditionWire
            var reverseCondition = new Wire();
            SetGate(new List<Wire>() { new Wire(new Zp(prime, 1)), normalizedConditionWire }, reverseCondition, Operation.Sub);

            // firstWireMultiplied = firstWire * normalizedConditionWire
            var firstWireMultiplied = new Wire();
            SetGate(new List<Wire>() { firstWire, normalizedConditionWire }, firstWireMultiplied, Operation.Mul);

            // secondWireMultiplied = secondWire * reverseCondition
            var secondWireMultiplied = new Wire();
            SetGate(new List<Wire>() { secondWire, reverseCondition }, secondWireMultiplied, Operation.Mul);

            // result = firstWireMultiplied + secondWireMultiplied
            var resultWire = new Wire();
            SetGate(new List<Wire>() { firstWireMultiplied, secondWireMultiplied }, resultWire, Operation.Add);
            return resultWire;
        }
Beispiel #5
0
        private Wire CreateEqualsGate(string line, Wire firstWire)
        {
            int equalsIndex = line.IndexOf("==");
            Debug.Assert(equalsIndex != -1);
            if (firstWire == null)
                firstWire = GetWireFromExpression(line.Substring(0, equalsIndex));

            var secondWire = GetWireFromExpression(line.Substring(equalsIndex + 2));
            var compareWire = new Wire();
            SetGate(new List<Wire>() { firstWire, secondWire }, compareWire, Operation.Sub);
            var normilizedWire = new Wire();
            SetGate(new List<Wire>() { compareWire, compareWire }, normilizedWire, Operation.Div);
            var resultWire = new Wire();
            SetGate(new List<Wire>() { new Wire(new Zp(prime, 1)), normilizedWire }, resultWire, Operation.Sub);
            return resultWire;
        }
Beispiel #6
0
        private Wire CreateMulGate(Wire firstWireOrNull, Operation firstOpOrNull, string line)
        {
            int addIndex = FindCorrectIndex(line, SearchFor.AddMin);
            if (addIndex == -1)
                return CreateMulGates(firstWireOrNull, firstOpOrNull, line);

            var firstWire = CreateMulGates(firstWireOrNull, firstOpOrNull, line.Substring(0, addIndex));
            return CreateAddGates(line.Substring(addIndex), firstWire);
        }
Beispiel #7
0
 private static bool IsTargetInGates(IList<Gate> gates, Wire outputWire)
 {
     foreach (Gate gate in gates)
     {
         if (outputWire.TargetGate == gate)
             return true;
     }
     return false;
 }
Beispiel #8
0
        private Wire CreateAddGates(string line, Wire firstWire)
        {
            var op = GetOperation(line.Substring(0, 1));
            line = line.Substring(1);

            while (op == Operation.Sub)
            {
                int firstIndex = GetFirstParamterIndex(line);
                if (firstIndex == -1)
                    break;

                if (firstIndex == 0 && !line.StartsWith("(") || firstIndex == -1)
                    break;

                if (getIndexOfDivOrMul(line, 0) == firstIndex)
                    break;

                bool isPar = line.StartsWith("(");
                Wire secondWire = isPar ? CreateParenthesesWire(line, false) : GetWireFromExpression(line.Substring(0, firstIndex));
                Wire outputWire = new Wire();

                SetGate(new List<Wire>() { firstWire, secondWire }, outputWire, op);
                line = line.Substring(isPar ? FindEndingParantheses(line) + 1 : firstIndex);

                if (line.Length == 0)
                    return outputWire;

                op = GetOperation(line.Substring(0, 1));
                line = line.Substring(1);
                firstWire = outputWire;
            }
            if (op != Operation.Add && op != Operation.Sub)
                return CreateMulGate(firstWire, op, line);

            var sndWire = GetWireFromExpression(line);
            var outWire = new Wire();
            SetGate(new List<Wire>() { firstWire, sndWire }, outWire, op);
            return outWire;
        }
Beispiel #9
0
        public virtual void Parse()
        {
            StreamReader reader;
            if (File.Exists(pathOrString))
                reader = new StreamReader(pathOrString);
            else
                reader = new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(pathOrString)));

            varsToWires = new Dictionary<string, Wire>();
            string line = null;
            int lineNumber = 0;
            while (!reader.EndOfStream)
            {
                line = reader.ReadLine();
                if (line == null)
                    break;

                int commentIndex = line.IndexOf("//");
                if (commentIndex != -1)
                    line = line.Substring(0, commentIndex);

                try
                {
                    ReadLine(line.Trim().Replace("\t", ""));	// remove white spaces
                    lineNumber++;
                }
                catch (Exception e)
                {
                    throw new Exception("Error in line " + lineNumber + "\n" + e.Message);
                }
            }
            outputWire = GetWireFromString(outputVar);
            outputWire.IsOutput = true;
            circuit = CreateCircuit(prime, inputs);
            if (circuit == null)
                throw new Exception("Error in parsing the circuit!");
        }
Beispiel #10
0
 public void AddOutputWire(Wire wire)
 {
     OutputWires.Add(wire);
 }
Beispiel #11
0
 public void AddInputWire(Wire wire)
 {
     InputWires.Add(wire);
 }
Beispiel #12
0
        private readonly int prime; // the prime number

        #endregion Fields

        #region Constructors

        public Gate(IList<Wire> inputWires, Wire outputWire, Operation operation, int prime)
            : this(inputWires, new List<Wire>() { outputWire }, operation, prime)
        {
        }
Beispiel #13
0
 public Wire Clone()
 {
     var wire = new Wire(InputIndex, IsOutput, ConstValue);
     wire.SourceGate = sourceGate;
     wire.TargetGate = targetGate;
     if (wire.SourceGate != null)
         wire.SourceGate.AddOutputWire(wire);
     return wire;
 }