Example #1
0
            // Moves the buffer to the next token at the same level of nesting as the current token.
            // Should never be called if we're on an end token!!!
            // Returns false if no next token can be found at the same level.
            //      this is probably indicative of a template authoring problem, or possibly a buffer problem.
            private bool SeekToNextTokenAtSameLevel(IProcessorState processor, ref int bufferLength, ref int currentBufferPosition, out int token)
            {
                if (_definition.WholeLine)
                {
                    processor.SeekForwardThrough(processor.EncodingConfig.LineEndings, ref bufferLength, ref currentBufferPosition);
                }

                bool seekSucceeded = SeekToToken(processor, ref bufferLength, ref currentBufferPosition, out token);

                //Keep on scanning until we've hit a balancing token that belongs to us
                // each "if" found opens a new level of nesting
                while (IsTokenIndexOfType(token, IfTokenBaseIndex) || IsTokenIndexOfType(token, IfTokenActionableBaseIndex))
                {
                    int open = 1;
                    while (open != 0)
                    {
                        seekSucceeded &= SeekToToken(processor, ref bufferLength, ref currentBufferPosition, out token);

                        if (IsTokenIndexOfType(token, IfTokenBaseIndex) || IsTokenIndexOfType(token, IfTokenActionableBaseIndex))
                        {
                            ++open;
                        }
                        else if (IsTokenIndexOfType(token, EndTokenBaseIndex))
                        {
                            --open;
                        }
                    }

                    seekSucceeded &= SeekToToken(processor, ref bufferLength, ref currentBufferPosition, out token);
                }

                // this may be irrelevant. If it happens, the template is malformed (i think)
                return(seekSucceeded);
            }
        public IOperation GetOperation(Encoding encoding, IProcessorState processorState)
        {
            TokenTrie structureTrie      = new TokenTrie();
            TokenTrie closeConditionTrie = new TokenTrie();
            TokenTrie scanBackTrie       = new TokenTrie();

            IToken openOpenElementTokenBytes = Tokens.OpenOpenElementToken.ToToken(processorState.Encoding);

            scanBackTrie.AddToken(openOpenElementTokenBytes);
            int openOpenElementToken   = structureTrie.AddToken(openOpenElementTokenBytes);
            int openCloseElementToken  = structureTrie.AddToken(Tokens.OpenCloseElementToken.ToToken(processorState.Encoding));
            int closeCloseElementToken = structureTrie.AddToken(Tokens.CloseElementTagToken.ToToken(processorState.Encoding));

            int selfClosingElementEndToken = -1;

            if (Tokens.SelfClosingElementEndToken != null)
            {
                selfClosingElementEndToken = structureTrie.AddToken(Tokens.SelfClosingElementEndToken.ToToken(processorState.Encoding));
            }

            closeConditionTrie.AddToken(Tokens.CloseConditionExpression.ToToken(processorState.Encoding));
            MarkupTokenMapping mapping = new MarkupTokenMapping(
                openOpenElementToken,
                openCloseElementToken,
                closeCloseElementToken,
                selfClosingElementEndToken
                );

            IReadOnlyList <IToken> start = new[] { Tokens.OpenConditionExpression.ToToken(processorState.Encoding) };

            return(new Impl(this, start, structureTrie, closeConditionTrie, scanBackTrie, mapping, _id, _initialState));
        }
Example #3
0
            public int HandleMatch(IProcessorState processor, int bufferLength, ref int currentBufferPosition, int token, Stream target)
            {
                bool flagsOn;

                if (!processor.Config.Flags.TryGetValue(SetFlag.OperationName, out flagsOn))
                {
                    flagsOn = true;
                }

                bool emit    = token < 2 || !flagsOn;
                bool turnOn  = (token % 2) == 0;
                int  written = 0;

                if (emit)
                {
                    byte[] tokenValue = Tokens[token];
                    target.Write(tokenValue, 0, tokenValue.Length);
                    written = tokenValue.Length;
                }

                //Only turn the flag in question back on if it's the "flags" flag.
                //  Yes, we still need to emit it as the common case is for this
                //  to be done in the template definition file
                if (flagsOn)
                {
                    processor.Config.Flags[_owner.Name] = token == 0;
                }
                else if (_owner.Name == SetFlag.OperationName && turnOn)
                {
                    processor.Config.Flags[SetFlag.OperationName] = true;
                }

                return(written);
            }
        public static bool Evaluate(IProcessorState processor, ref int bufferLength, ref int currentBufferPosition, out bool faulted)
        {
            ITokenTrie tokens = Instance.GetSymbols(processor);
            ScopeBuilder <Operators, TTokens> builder = processor.ScopeBuilder(tokens, Map, DereferenceInLiteralsSetting);
            bool       isFaulted = false;
            IEvaluable result    = builder.Build(ref bufferLength, ref currentBufferPosition, x => isFaulted = true);

            if (isFaulted)
            {
                faulted = true;
                return(false);
            }

            try
            {
                object evalResult = result.Evaluate();
                bool   r          = (bool)Convert.ChangeType(evalResult, typeof(bool));
                faulted = false;
                return(r);
            }
            catch
            {
                faulted = true;
                return(false);
            }
        }
Example #5
0
        public ScopeBuilder(IProcessorState processor, ITokenTrie tokens, IOperatorMap <TOperator, TToken> operatorMap, bool dereferenceInLiterals)
        {
            TokenTrie trie = new TokenTrie();

            trie.Append(tokens);

            _badSyntaxTokens = operatorMap.BadSyntaxTokens;
            _noops           = operatorMap.NoOpTokens;
            _openGroup       = operatorMap.OpenGroupToken;
            _closeGroup      = operatorMap.CloseGroupToken;
            _literal         = operatorMap.LiteralToken;
            _identity        = operatorMap.Identity;
            _literalSequenceBoundsMarkers = operatorMap.LiteralSequenceBoundsMarkers;
            _terminators = operatorMap.Terminators;
            _isSymbolDereferenceInLiteralSequenceRequired = dereferenceInLiterals;
            _processor        = processor;
            _knownTokensCount = tokens.Count;
            _valueEncoder     = operatorMap.Encode;
            _valueDecoder     = operatorMap.Decode;

            List <object> symbolValues = new List <object>();

            foreach (KeyValuePair <string, object> variable in processor.Config.Variables)
            {
                trie.AddToken(processor.Encoding.GetBytes(string.Format(processor.Config.VariableFormatString, variable.Key)));
                symbolValues.Add(variable.Value);
            }

            _symbolValues         = symbolValues;
            _tokenToOperatorMap   = operatorMap.TokensToOperatorsMap;
            _operatorScopeFactory = operatorMap.OperatorScopeLookupFactory;
            _tokens = trie;
        }
Example #6
0
 public VerticesListProcessor()
 {
     this.vertices         = new List <Vertex>();
     this.processorState   = NodeIDState.Instance;
     this.paramsToReadLeft = 0;
     this.finished         = false;
 }
        public IOperation GetOperation(Encoding encoding, IProcessorState processorState)
        {
            byte[] ifToken  = encoding.GetBytes(_ifToken);
            byte[] endToken = encoding.GetBytes(_endIfToken);

            List <byte[]> tokens = new List <byte[]>
            {
                ifToken,
                endToken
            };

            SimpleTrie trie = new SimpleTrie();

            trie.AddToken(ifToken, 0);
            trie.AddToken(endToken, 1);

            int elseIfTokenIndex = -1;

            if (!string.IsNullOrEmpty(_elseToken))
            {
                byte[] elseToken = encoding.GetBytes(_elseToken);
                trie.AddToken(elseToken, tokens.Count);
                tokens.Add(elseToken);
            }

            if (!string.IsNullOrEmpty(_elseIfToken))
            {
                byte[] elseIfToken = encoding.GetBytes(_elseIfToken);
                elseIfTokenIndex = tokens.Count;
                trie.AddToken(elseIfToken, elseIfTokenIndex);
                tokens.Add(elseIfToken);
            }

            return(new Impl(this, tokens, elseIfTokenIndex, trie));
        }
Example #8
0
            // moves to the next token
            // returns false if the end of the buffer was reached without finding a token.
            private bool SeekToToken(IProcessorState processor, ref int bufferLength, ref int currentBufferPosition, out int token)
            {
                bool bufferAdvanceFailed = false;

                while (bufferLength >= _trie.MinLength)
                {
                    for (; currentBufferPosition < bufferLength - _trie.MinLength + 1; ++currentBufferPosition)
                    {
                        if (_trie.GetOperation(processor.CurrentBuffer, bufferLength, ref currentBufferPosition, out token))
                        {
                            if (bufferAdvanceFailed || (currentBufferPosition != bufferLength))
                            {
                                return(true);
                            }
                        }
                    }

                    if (bufferAdvanceFailed)
                    {
                        break;
                    }

                    bufferAdvanceFailed   = !processor.AdvanceBuffer(bufferLength - _trie.MaxLength + 1);
                    currentBufferPosition = processor.CurrentBufferPosition;
                    bufferLength          = processor.CurrentBufferLength;
                }

                //If we run out of places to look, assert that the end of the buffer is the end
                token = EndTokenBaseIndex;
                currentBufferPosition = bufferLength;
                return(false);   // no terminator found
            }
Example #9
0
        /// <inheritdoc/>
        public override void PerformAction(
            IParsedInstruction instruction,
            IEmulatedMemory memory,
            IProcessorState state,
            IALU alu)
        {
            ushort addressA = (ushort)state.Registers[instruction.Register1];
            ushort addressB = (ushort)state.Registers[instruction.Register2];

            byte a = 0;
            byte b = 0;

            for (ushort i = 0; i < instruction.Immediate; i++)
            {
                a = (byte)memory.GetValue(0, (ushort)(addressA + i));
                b = (byte)memory.GetValue(0, (ushort)(addressB + i));

                if (a != b)
                {
                    break;
                }
            }

            state.High  = a > b;
            state.Equal = a == b;
            state.Low   = !state.High;
        }
Example #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PC"/> class.
 /// </summary>
 public PC()
 {
     this.SetupContainerGraph();
     this.processor = this.container.GetInstance <IProcessor>();
     this.memory    = this.container.GetInstance <IEmulatedMemory>();
     this.state     = this.container.GetInstance <IProcessorState>();
 }
Example #11
0
        public IOperation GetOperation(Encoding encoding, IProcessorState processorState)
        {
            IToken startToken = _start.ToToken(encoding);
            IToken endToken   = _end.ToToken(encoding);

            return(new Impl(this, startToken, endToken, _include, _toggle, _id, _initialState));
        }
Example #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Processor"/> class.
 /// </summary>
 /// <param name="parsedInstruction">Parsed instruction.</param>
 /// <param name="memory">Memory.</param>
 /// <param name="alu">ALU.</param>
 /// <param name="state">Processor state.</param>
 public Processor(IParsedInstruction parsedInstruction, IEmulatedMemory memory, IALU alu, IProcessorState state)
 {
     this.parsedInstruction = parsedInstruction;
     this.memory            = memory;
     this.alu   = alu;
     this.state = state;
 }
Example #13
0
                internal bool Evaluate(IProcessorState processor, ref int bufferLength, ref int currentBufferPosition)
                {
                    bool faulted;

                    BranchTaken = _impl._definition._evaluator(processor, ref bufferLength, ref currentBufferPosition, out faulted);
                    return(BranchTaken);
                }
Example #14
0
 public EdgeListProcessor()
 {
     this.outEdges         = new List <OutEdge>();
     this.inEdges          = new List <InEdge>();
     this.finished         = false;
     this.processorState   = EdgeIDState.Instance;
     this.paramsToReadLeft = 0;
 }
Example #15
0
 public TableDictProcessor()
 {
     this.dict           = new Dictionary <string, Table>();
     this.finished       = false;
     this.processorState = TableDictLeftSquareBraceState.Instance;
     this.newTable       = null;
     this.newPropName    = null;
 }
Example #16
0
 /// <inheritdoc/>
 public override void PerformAction(
     IParsedInstruction instruction,
     IEmulatedMemory memory,
     IProcessorState state,
     IALU alu)
 {
     alu.PerformAction(this.calculation, instruction.Immediate);
 }
Example #17
0
        public IOperation GetOperation(Encoding encoding, IProcessorState processorState)
        {
            IToken startToken     = _startToken.ToToken(encoding);
            IToken realEndToken   = _realEndToken.ToToken(encoding);
            IToken pseudoEndToken = _pseudoEndToken.ToToken(encoding);

            return(new Impl(startToken, realEndToken, pseudoEndToken, _id, _resetFlag, _initialState));
        }
Example #18
0
        public IOperation GetOperation(Encoding encoding, IProcessorState processorState)
        {
            byte[] startToken     = encoding.GetBytes(_startToken);
            byte[] realEndToken   = encoding.GetBytes(_realEndToken);
            byte[] pseudoEndToken = encoding.GetBytes(_pseudoEndToken);

            return(new Impl(startToken, realEndToken, pseudoEndToken, _id, _resetFlag));
        }
Example #19
0
 /// <inheritdoc/>
 public void PutData(
     IParsedInstruction instruction,
     IEmulatedMemory memory,
     IProcessorState state,
     IALU alu,
     long value)
 {
     throw new NotImplementedException();
 }
 /// <inheritdoc/>
 public void PutData(
     IParsedInstruction instruction,
     IEmulatedMemory memory,
     IProcessorState state,
     IALU alu,
     long value)
 {
     state.Registers[instruction.Register2] = value;
 }
Example #21
0
 /// <inheritdoc/>
 public void PutData(
     IParsedInstruction instruction,
     IEmulatedMemory memory,
     IProcessorState state,
     IALU alu,
     long value)
 {
     memory.SetValue(instruction.Width, (ulong)value, (ushort)state.Registers[instruction.Register1]);
 }
Example #22
0
 /// <inheritdoc/>
 public void PutData(
     IParsedInstruction instruction,
     IEmulatedMemory memory,
     IProcessorState state,
     IALU alu,
     long value)
 {
     alu.StageValue(0, value);
 }
 /// <inheritdoc/>
 public void PutData(
     IParsedInstruction instruction,
     IEmulatedMemory memory,
     IProcessorState state,
     IALU alu,
     long value)
 {
     state.ProgramCounter = (ushort)value;
 }
Example #24
0
        public IOperation GetOperation(Encoding encoding, IProcessorState processorState)
        {
            byte[]    tokenBytes      = encoding.GetBytes(StartToken);
            byte[]    endTokenBytes   = encoding.GetBytes(EndToken);
            TokenTrie endTokenMatcher = new TokenTrie();

            endTokenMatcher.AddToken(endTokenBytes);
            return(new Impl(tokenBytes, endTokenMatcher, this, _id));
        }
Example #25
0
 /// <inheritdoc/>
 public void PutData(
     IParsedInstruction instruction,
     IEmulatedMemory memory,
     IProcessorState state,
     IALU alu,
     long value)
 {
     memory.SetValue(instruction.Width, (ulong)value, instruction.Address);
 }
Example #26
0
        public IOperation GetOperation(Encoding encoding, IProcessorState processorState)
        {
            IToken    tokenBytes      = StartToken.ToToken(encoding);
            IToken    endTokenBytes   = EndToken.ToToken(encoding);
            TokenTrie endTokenMatcher = new TokenTrie();

            endTokenMatcher.AddToken(endTokenBytes);
            return(new Impl(tokenBytes, endTokenMatcher, this, _id, _initialState));
        }
Example #27
0
                public void ToggleActionableOperations(bool enabled, IProcessorState processor)
                {
                    ActionableOperationsEnabled = enabled;

                    foreach (string otherOptionDisableFlag in _impl._definition.Tokens.ActionableOperations)
                    {
                        processor.Config.Flags[otherOptionDisableFlag] = enabled;
                    }
                }
        private static ITokenTrie GetSymbols(IProcessorState processor)
        {
            if (!TokenCache.TryGetValue(processor.Encoding, out ITokenTrie tokens))
            {
                TokenTrie trie = new TokenTrie();

                //Logic
                trie.AddToken(processor.Encoding.GetBytes("&&"));
                trie.AddToken(processor.Encoding.GetBytes("||"));
                trie.AddToken(processor.Encoding.GetBytes("!"));
                trie.AddToken(processor.Encoding.GetBytes(">"));
                trie.AddToken(processor.Encoding.GetBytes(">="));
                trie.AddToken(processor.Encoding.GetBytes("<"));
                trie.AddToken(processor.Encoding.GetBytes("<="));
                trie.AddToken(processor.Encoding.GetBytes("=="));
                trie.AddToken(processor.Encoding.GetBytes("!="));

                //Braces
                trie.AddToken(processor.Encoding.GetBytes("("));
                trie.AddToken(processor.Encoding.GetBytes(")"));

                //Whitespace
                trie.AddToken(processor.Encoding.GetBytes(" "));
                trie.AddToken(processor.Encoding.GetBytes("\t"));

                //EOLs
                trie.AddToken(processor.Encoding.GetBytes("\r\n"));
                trie.AddToken(processor.Encoding.GetBytes("\n"));
                trie.AddToken(processor.Encoding.GetBytes("\r"));

                // quotes
                trie.AddToken(processor.Encoding.GetBytes("'"));

                //Shifts
                trie.AddToken(processor.Encoding.GetBytes("<<"));
                trie.AddToken(processor.Encoding.GetBytes(">>"));

                //Maths
                trie.AddToken(processor.Encoding.GetBytes("+"));
                trie.AddToken(processor.Encoding.GetBytes("-"));
                trie.AddToken(processor.Encoding.GetBytes("*"));
                trie.AddToken(processor.Encoding.GetBytes("/"));

                //Bitwise operators
                trie.AddToken(processor.Encoding.GetBytes("&"));
                trie.AddToken(processor.Encoding.GetBytes("|"));

                // quotes
                trie.AddToken(processor.Encoding.GetBytes("'"));
                trie.AddToken(processor.Encoding.GetBytes("\""));

                TokenCache[processor.Encoding] = tokens = trie;
            }

            return(tokens);
        }
Example #29
0
        /// <inheritdoc/>
        public override void PerformAction(
            IParsedInstruction instruction,
            IEmulatedMemory memory,
            IProcessorState state,
            IALU alu)
        {
            long value = this.from.GetData(instruction, memory, state, alu);

            this.to.PutData(instruction, memory, state, alu, value);
        }
        protected override ITokenTrie GetSymbols(IProcessorState processor)
        {
            if (!TokenCache.TryGetValue(processor.Encoding, out ITokenTrie tokens))
            {
                TokenTrie trie = new TokenTrie();

                //Logic
                trie.AddToken(processor.Encoding.GetBytes("And"));
                trie.AddToken(processor.Encoding.GetBytes("AndAlso"));
                trie.AddToken(processor.Encoding.GetBytes("Or"));
                trie.AddToken(processor.Encoding.GetBytes("OrElse"));
                trie.AddToken(processor.Encoding.GetBytes("Not"));
                trie.AddToken(processor.Encoding.GetBytes(">"));
                trie.AddToken(processor.Encoding.GetBytes(">="));
                trie.AddToken(processor.Encoding.GetBytes("<"));
                trie.AddToken(processor.Encoding.GetBytes("<="));
                trie.AddToken(processor.Encoding.GetBytes("="));
                trie.AddToken(processor.Encoding.GetBytes("<>"));
                trie.AddToken(processor.Encoding.GetBytes("Xor"));

                //Braces
                trie.AddToken(processor.Encoding.GetBytes("("));
                trie.AddToken(processor.Encoding.GetBytes(")"));

                //Whitespace
                trie.AddToken(processor.Encoding.GetBytes(" "));
                trie.AddToken(processor.Encoding.GetBytes("\t"));

                //EOLs
                trie.AddToken(processor.Encoding.GetBytes("\r\n"));
                trie.AddToken(processor.Encoding.GetBytes("\n"));
                trie.AddToken(processor.Encoding.GetBytes("\r"));

                // quotes
                trie.AddToken(processor.Encoding.GetBytes("'"));

                //Shifts
                trie.AddToken(processor.Encoding.GetBytes("<<"));
                trie.AddToken(processor.Encoding.GetBytes(">>"));

                //Maths
                trie.AddToken(processor.Encoding.GetBytes("+"));
                trie.AddToken(processor.Encoding.GetBytes("-"));
                trie.AddToken(processor.Encoding.GetBytes("*"));
                trie.AddToken(processor.Encoding.GetBytes("/"));
                trie.AddToken(processor.Encoding.GetBytes("^"));

                // quotes
                trie.AddToken(processor.Encoding.GetBytes("\""));

                TokenCache[processor.Encoding] = tokens = trie;
            }

            return(tokens);
        }