Esempio n. 1
0
        /// <summary>
        /// Reads the next token from the stream and returns a Const Int that
        /// it represents. If it doens't, throws an exception.
        /// </summary>
        public int ReadNextAsConstInt()
        {
            Token nextToken = PeekNextToken();

            if (nextToken.Type == TokenType.Constant)
            {
                ReadNextToken();
                return((int)nextToken.Value);
            }

            LiteralToken next = ReadNextAsLiteral();

            if (!next.IsInteger)
            {
                throw new CompilerMessage(ErrorCode.ConstIntExpected, "A constant integer was expected at '" + next.Name + "'");
            }

            return(Convert.ToInt32(next.Name));
        }
Esempio n. 2
0
        /// <summary>
        /// Reads the next token from the stream. If it's not a literal,
        /// throws an exception.
        /// </summary>
        private LiteralToken ReadNextAsLiteral()
        {
            Token nextToken = ReadNextToken();
            bool isNegative = false;

            if ((nextToken is OperatorToken) &&
                (nextToken.Name == "-"))
            {
                isNegative = true;
                nextToken = ReadNextToken();
            }

            if (!(nextToken is LiteralToken))
            {
                throw new CompilerMessage(ErrorCode.UnexpectedToken, "Literal expected at '" + nextToken.Name + "'");
            }

            Token tokenToReturn = nextToken;

            if (isNegative)
            {
                if (_source == null)
                {
                    throw new CompilerMessage(ErrorCode.InternalError, "Internal error: attempted to use tokenized script with no source");
                }
                tokenToReturn = _source.FindTokenWithName("-" + nextToken.Name);
                if (tokenToReturn == null)
                {
                    tokenToReturn = new LiteralToken("-" + nextToken.Name);
                    _source.AddToken(tokenToReturn);
                }
            }

            return (LiteralToken)tokenToReturn;
        }