Esempio n. 1
0
        /// <summary>
        /// Backtracks to the current choice point
        /// </summary>
        public void BacktrackToChoicePoint()
        {
            // Restore the environment permanent variables
            if (!ReferenceEquals(_choicePoint.Environment, _environment))
            {
                for (var varIndex = 0; varIndex < _choicePoint.Environment.Variables.Length; ++varIndex)
                {
                    _registers[varIndex] = _choicePoint.Environment.Variables[varIndex];
                }

                for (var varIndex = _choicePoint.Environment.Variables.Length; varIndex < _environment.Variables.Length; ++varIndex)
                {
                    _registers[varIndex] = new SimpleReference();
                }
            }

            // Restore the argument registers
            var argumentIndex = _choicePoint.Environment.Variables.Length;

            foreach (var argument in _choicePoint.Arguments)
            {
                _registers[argumentIndex].SetTo(argument);
                ++argumentIndex;
            }

            // Environment is reset to the choice point environment
            _environment = _choicePoint.Environment;

            // Reset the trail
            _choicePoint.Trail.Reset();
            _trail = _choicePoint.Trail;
        }
Esempio n. 2
0
        public ChoicePoint(ChoicePoint previousChoice, ByteCodeEnvironment environment, IEnumerable<IReferenceLiteral> arguments, ITrail trail, int nextClause)
        {
            if (arguments == null) throw new ArgumentNullException(nameof(arguments));
            if (trail == null) throw new ArgumentNullException(nameof(trail));

            _previousChoicePoint    = previousChoice;
            _arguments              = arguments.Select(arg => new SimpleReference(arg)).ToArray();
            _trail                  = trail;
            _nextClause             = nextClause;
            _environment            = environment;
        }
Esempio n. 3
0
 public ByteCodeEnvironment(int numVariables, int numArguments, ByteCodeEnvironment continuationEnvironment)
 {
     Variables = new SimpleReference[numVariables];
     NumberOfArguments = numArguments;
     for (var x=0; x<numVariables; ++x)
     {
         Variables[x] = new SimpleReference();
     }
     ContinuationEnvironment = continuationEnvironment;
     ContinuationPointer = -1;
 }
Esempio n. 4
0
 public ByteCodeEnvironment(int numVariables, int numArguments, ByteCodeEnvironment continuationEnvironment)
 {
     Variables         = new SimpleReference[numVariables];
     NumberOfArguments = numArguments;
     for (var x = 0; x < numVariables; ++x)
     {
         Variables[x] = new SimpleReference();
     }
     ContinuationEnvironment = continuationEnvironment;
     ContinuationPointer     = -1;
 }
Esempio n. 5
0
        public ByteCodeExecutor(ByteCodePoint[] program, ILiteral[] literals, int maxVariableIndex)
        {
            if (program == null) throw new ArgumentNullException(nameof(program));

            _program        = program;
            _literals       = literals;
            _programCounter = 0;
            _environment    = new ByteCodeEnvironment(0, 0, null);
            _registers      = new SimpleReference[maxVariableIndex];

            for (var registerIndex = 0; registerIndex<_registers.Length; ++registerIndex)
            {
                _registers[registerIndex] = new SimpleReference();
            }
        }
Esempio n. 6
0
        public ChoicePoint(ChoicePoint previousChoice, ByteCodeEnvironment environment, IEnumerable <IReferenceLiteral> arguments, ITrail trail, int nextClause)
        {
            if (arguments == null)
            {
                throw new ArgumentNullException(nameof(arguments));
            }
            if (trail == null)
            {
                throw new ArgumentNullException(nameof(trail));
            }

            _previousChoicePoint = previousChoice;
            _arguments           = arguments.Select(arg => new SimpleReference(arg)).ToArray();
            _trail       = trail;
            _nextClause  = nextClause;
            _environment = environment;
        }
Esempio n. 7
0
        /// <summary>
        /// Allocates space for a number of permanent and argument variables.
        /// </summary>
        /// <remarks>
        /// Permanent variables are stored in the environment and are numbered from 0.
        /// Arguments occur after the temporary variables and have their values preserved
        /// from the previous state by this call.
        /// </remarks>
        private void Allocate(int numPermanent, int numArguments)
        {
            // Allocate a new environment
            var newEnvironment = new ByteCodeEnvironment(numPermanent, numArguments, _environment);

            newEnvironment.ContinuationEnvironment = _environment;

            // Make sure that we don't overwrite arguments in the previous environment by replacing them with new temporary variables
            if (numPermanent + numArguments < _environment.Variables.Length)
            {
                for (int oldPermanent = numPermanent + numArguments; oldPermanent < _environment.Variables.Length; ++oldPermanent)
                {
                    _registers[oldPermanent] = new SimpleReference();
                }
            }

            // Move arguments to their new position
            int oldArgStart = _environment.Variables.Length;

            if (oldArgStart > numPermanent)
            {
                // Moving arguments 'down', leaving a hole
                for (int argument = 0; argument < numArguments; ++argument)
                {
                    _registers[argument + numPermanent] = _registers[argument + oldArgStart];
                    _registers[argument + oldArgStart]  = new SimpleReference();
                }
            }
            else if (oldArgStart < numPermanent)
            {
                // Moving arguments 'up', old locations will be overwritten by new permanent variables
                for (int argument = numArguments - 1; argument >= 0; --argument)
                {
                    _registers[argument + numPermanent] = _registers[argument + oldArgStart];
                }
            }

            // Copy in the new permanent variables
            for (int permanent = 0; permanent < numPermanent; ++permanent)
            {
                _registers[permanent] = newEnvironment.Variables[permanent];
            }

            _environment = newEnvironment;
        }
Esempio n. 8
0
        public ByteCodeExecutor(ByteCodePoint[] program, ILiteral[] literals, int maxVariableIndex)
        {
            if (program == null)
            {
                throw new ArgumentNullException(nameof(program));
            }

            _program        = program;
            _literals       = literals;
            _programCounter = 0;
            _environment    = new ByteCodeEnvironment(0, 0, null);
            _registers      = new SimpleReference[maxVariableIndex];

            for (var registerIndex = 0; registerIndex < _registers.Length; ++registerIndex)
            {
                _registers[registerIndex] = new SimpleReference();
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Deallocates the last allocated block, restoring the permanent variables from the preceding environment
        /// </summary>
        private void Deallocate()
        {
            var oldEnvironment = _environment.ContinuationEnvironment;

            if (oldEnvironment == null)
            {
                return;
            }

            // Restore any permanent variables from the new environment
            for (var varIndex = 0; varIndex < oldEnvironment.Variables.Length; ++varIndex)
            {
                _registers[varIndex] = oldEnvironment.Variables[varIndex];
            }

            // Reallocate any temporary variables that have newly appeared
            for (var varIndex = oldEnvironment.Variables.Length; varIndex < _environment.Variables.Length; ++varIndex)
            {
                _registers[varIndex] = new SimpleReference();
            }

            // Finally, restore the environment
            _environment = oldEnvironment;
        }
Esempio n. 10
0
        /// <summary>
        /// Backtracks to the current choice point
        /// </summary>
        public void BacktrackToChoicePoint()
        {
            // Restore the environment permanent variables
            if (!ReferenceEquals(_choicePoint.Environment, _environment))
            {
                for (var varIndex = 0; varIndex < _choicePoint.Environment.Variables.Length; ++varIndex)
                {
                    _registers[varIndex] = _choicePoint.Environment.Variables[varIndex];
                }

                for (var varIndex = _choicePoint.Environment.Variables.Length; varIndex < _environment.Variables.Length; ++varIndex)
                {
                    _registers[varIndex] = new SimpleReference();
                }
            }

            // Restore the argument registers
            var argumentIndex = _choicePoint.Environment.Variables.Length;
            foreach (var argument in _choicePoint.Arguments)
            {
                _registers[argumentIndex].SetTo(argument);
                ++argumentIndex;
            }

            // Environment is reset to the choice point environment
            _environment = _choicePoint.Environment;

            // Reset the trail
            _choicePoint.Trail.Reset();
            _trail = _choicePoint.Trail;
        }
Esempio n. 11
0
        /// <summary>
        /// Deallocates the last allocated block, restoring the permanent variables from the preceding environment
        /// </summary>
        private void Deallocate()
        {
            var oldEnvironment = _environment.ContinuationEnvironment;
            if (oldEnvironment == null) return;

            // Restore any permanent variables from the new environment
            for (var varIndex = 0; varIndex < oldEnvironment.Variables.Length; ++varIndex)
            {
                _registers[varIndex] = oldEnvironment.Variables[varIndex];
            }

            // Reallocate any temporary variables that have newly appeared
            for (var varIndex = oldEnvironment.Variables.Length; varIndex < _environment.Variables.Length; ++varIndex)
            {
                _registers[varIndex] = new SimpleReference();
            }

            // Finally, restore the environment
            _environment = oldEnvironment;
        }
Esempio n. 12
0
        /// <summary>
        /// Allocates space for a number of permanent and argument variables.
        /// </summary>
        /// <remarks>
        /// Permanent variables are stored in the environment and are numbered from 0.
        /// Arguments occur after the temporary variables and have their values preserved
        /// from the previous state by this call.
        /// </remarks>
        private void Allocate(int numPermanent, int numArguments)
        {
            // Allocate a new environment
            var newEnvironment = new ByteCodeEnvironment(numPermanent, numArguments, _environment);
            newEnvironment.ContinuationEnvironment = _environment;

            // Make sure that we don't overwrite arguments in the previous environment by replacing them with new temporary variables
            if (numPermanent + numArguments < _environment.Variables.Length)
            {
                for (int oldPermanent = numPermanent + numArguments; oldPermanent < _environment.Variables.Length; ++oldPermanent)
                {
                    _registers[oldPermanent] = new SimpleReference();
                }
            }

            // Move arguments to their new position
            int oldArgStart = _environment.Variables.Length;

            if (oldArgStart > numPermanent)
            {
                // Moving arguments 'down', leaving a hole
                for (int argument = 0; argument < numArguments; ++argument)
                {
                    _registers[argument + numPermanent] = _registers[argument + oldArgStart];
                    _registers[argument + oldArgStart] = new SimpleReference();
                }
            }
            else if (oldArgStart < numPermanent)
            {
                // Moving arguments 'up', old locations will be overwritten by new permanent variables
                for (int argument = numArguments-1; argument >=0; --argument)
                {
                    _registers[argument + numPermanent] = _registers[argument + oldArgStart];
                }
            }

            // Copy in the new permanent variables
            for (int permanent = 0; permanent < numPermanent; ++permanent)
            {
                _registers[permanent] = newEnvironment.Variables[permanent];
            }

            _environment = newEnvironment;
        }