Example #1
0
        public static IInstruction BuildInstruction(List<Token> tokens, ref int index)
        {
            // if we're just starting, encase the root-level instructions with a section statement
            if (index == 0)
            {
                tokens.Insert(0, new Token(InstructionList.Keyword, TokenType.Word, 0));
                tokens.Add(new Token("end", TokenType.Word, 0));
                index++;
                InstructionList il = new InstructionList();
                il.Build(tokens, ref index);
                return il;
            }

            Token token = tokens[index];

            // special case: if a standalone literal is found, create a "show" instruction to process it
            if (token.TokenType == TokenType.StringLiteral)
            {
                ShowInstruction si = new ShowInstruction();
                si.Build(tokens, ref index, true);
                return si;
            }

            // find the relevant creator/processor for the instruction
            if (token.TokenType == TokenType.Word)
            {
                if (instructionCreators.ContainsKey(token.Value))
                {
                    IInstruction instruction = instructionCreators[token.Value].Create();
                    index++;
                    instruction.Build(tokens, ref index);
                    return instruction;
                }
            }

            throw new TokenParserException("I have no idea what \"" + token.Value + "\" means or at least what I'm supposed to do with it here.", token);
        }