Пример #1
0
        private void ProcessTokenAtTopLevel(Token thisToken)
        {
            if (thisToken is ModifierToken)
            {
                _state.NextTokenModifiers.Add((ModifierToken)thisToken);
            }
            else if (thisToken.IsVariableType)
            {
                bool wasFunction = false;
                do
                {
                    wasFunction = ParseAndAddGlobalVariableOrFunction(thisToken);
                }while ((!wasFunction) && (_source.NextIsKeyword(PredefinedSymbol.Comma)));

                if (!wasFunction)
                {
                    _source.ExpectKeyword(PredefinedSymbol.Semicolon);
                }
                _state.NextTokenModifiers.Clear();
            }
            else if (thisToken is KeywordToken)
            {
                PredefinedSymbol keyword = ((KeywordToken)thisToken).SymbolType;
                if (keyword == PredefinedSymbol.StructDefinition)
                {
                    _output.AddStruct(ProcessStructDeclaration());
                }
                else if (keyword == PredefinedSymbol.Enum)
                {
                    ProcessEnumDeclaration();
                }
                else if (keyword == PredefinedSymbol.Export)
                {
                    ProcessExportCommand();
                }
                else
                {
                    RecordError(ErrorCode.InvalidUseOfKeyword, "Invalid use of '" + thisToken.Name + "'");
                }
            }
            else if (thisToken.Name.StartsWith(Constants.NEW_SCRIPT_MARKER))
            {
                _scriptName = thisToken.Name.Substring(Constants.NEW_SCRIPT_MARKER.Length, thisToken.Name.Length - Constants.NEW_SCRIPT_MARKER.Length - 1);
            }
            else
            {
                RecordError(ErrorCode.UnexpectedToken, "Unexpected '" + thisToken.Name + "'");
            }
        }
Пример #2
0
        public static void SetArrayPropertiesOnTokenFromStream(ScriptReader source, Token variableName)
        {
            if (source.NextIsKeyword(PredefinedSymbol.OpenSquareBracket))
            {
                variableName.IsArray = true;

                if (source.NextIsKeyword(PredefinedSymbol.CloseSquareBracket))
                {
                    variableName.ArraySize = Token.ARRAY_SIZE_DYNAMIC;
                }
                else
                {
                    variableName.ArraySize = source.ReadNextAsConstInt();
                    source.ExpectKeyword(PredefinedSymbol.CloseSquareBracket);
                }
            }
        }
Пример #3
0
        public static void SetArrayPropertiesOnTokenFromStream(ScriptReader source, Token variableName)
        {
            if (source.NextIsKeyword(PredefinedSymbol.OpenSquareBracket))
            {
                variableName.IsArray = true;

                if (source.NextIsKeyword(PredefinedSymbol.CloseSquareBracket))
                {
                    variableName.ArraySize = Token.ARRAY_SIZE_DYNAMIC;
                }
                else
                {
                    variableName.ArraySize = source.ReadNextAsConstInt();
                    source.ExpectKeyword(PredefinedSymbol.CloseSquareBracket);
                }
            }
        }
Пример #4
0
        private void ProcessNextCodeStatement(bool allowVariableDeclarations)
        {
            Token nextToken = _source.PeekNextToken();

            while (nextToken is ModifierToken)
            {
                _source.ReadNextToken();

                _state.NextTokenModifiers.Add((ModifierToken)nextToken);

                nextToken = _source.PeekNextToken();
            }

            if (nextToken is EndOfStreamToken)
            {
                throw new CompilerMessage(ErrorCode.EndOfInputReached, "The end of the script was reached in the middle of a function");
            }

            if ((nextToken is KeywordToken) &&
                (((KeywordToken)nextToken).SymbolType == PredefinedSymbol.OpenBrace))
            {
                ProcessCodeBlock();
            }
            else if (nextToken is KeywordToken)
            {
                _source.ReadNextToken();
                ProcessKeyword((KeywordToken)nextToken);
            }
            else if (nextToken.IsVariableType)
            {
                _source.PushLocation();
                Token variableType = _source.ReadNextToken();

                if (_source.NextIsKeyword(PredefinedSymbol.Dot))
                {
                    // it's a static member access
                    _source.PopLocation();
                    GenerateCodeForExpression(ReadExpression(true, PredefinedSymbol.Semicolon));
                }
                else
                {
                    _source.DeletePushedLocation();

                    if (!allowVariableDeclarations)
                    {
                        throw new CompilerMessage(ErrorCode.VariableDeclarationNotAllowedHere, "The variable would go out of scope as soon as it was declared");
                    }
                    do
                    {
                        DeclareLocalVariable(variableType);
                    }while (_source.NextIsKeyword(PredefinedSymbol.Comma));

                    _source.ExpectKeyword(PredefinedSymbol.Semicolon);
                    _state.NextTokenModifiers.Clear();
                }
            }
            else
            {
                GenerateCodeForExpression(ReadExpression(true, PredefinedSymbol.Semicolon));
            }
        }