Beispiel #1
0
    // Parse Vars and Labels
    bool Pass1()
    {
        Tokenizer.Token currentToken = tokenizer.GetNextToken();

        if (currentToken.Type == Tokenizer.TokenType.Empty)
        {
            return(false);
        }

        int instrIdx = 0;

        while (currentToken.Type != Tokenizer.TokenType.EOF && currentToken.Type != Tokenizer.TokenType.Unknown)
        {
            // ===================================================================
            // Skip end of lines
            if (currentToken.Type == Tokenizer.TokenType.EOL)
            {
                currentToken = tokenizer.GetNextToken();
            }
            // ===================================================================
            // Parse variables
            else if (currentToken.Type == Tokenizer.TokenType.Rsvd_Var)
            {
                currentToken = tokenizer.GetNextToken();

                if (currentToken.Type == Tokenizer.TokenType.Ident)
                {
                    if (!tables.AddVar(currentToken.Lexeme, scope, false))
                    {
                        errorHandler.ParserLogError("Var Already Exists");
                        return(false);
                    }

                    if (scope != -1)
                    {
                        if (!tables.FuncIncrementFrameSize(scope))
                        {
                            errorHandler.ParserLogError("Could not Find Function that Corresponds to Scope");
                            return(false);
                        }
                    }
                }
                else
                {
                    errorHandler.ParserLogError("Ident Expected");
                    return(false);
                }

                currentToken = tokenizer.GetNextToken();
            }
            // ===================================================================
            // Parse arguments
            else if (currentToken.Type == Tokenizer.TokenType.Rsvd_Arg)
            {
                currentToken = tokenizer.GetNextToken();

                if (currentToken.Type == Tokenizer.TokenType.Ident)
                {
                    if (scope != -1)
                    {
                        if (!tables.AddVar(currentToken.Lexeme, scope, true))
                        {
                            errorHandler.ParserLogError("Arg Already Exists");
                            return(false);
                        }

                        if (!tables.FuncIncrementArgFrameSize(scope))
                        {
                            errorHandler.ParserLogError("Could not Find Function that Corresponds to Scope");
                            return(false);
                        }
                    }
                    else
                    {
                        errorHandler.ParserLogError("Declaring Arguments outside Functions is Illegal");
                        return(false);
                    }
                }
                else
                {
                    errorHandler.ParserLogError("Ident Expected");
                    return(false);
                }

                currentToken = tokenizer.GetNextToken();
            }
            // ===================================================================
            // Parse functions
            else if (currentToken.Type == Tokenizer.TokenType.Rsvd_Func)
            {
                if (scope != -1)
                {
                    errorHandler.ParserLogError("Declaring Functions in Functions is Illegal");
                    return(false);
                }

                currentToken = tokenizer.GetNextToken();

                if (currentToken.Type == Tokenizer.TokenType.Ident)
                {
                    if (!tables.AddFunc(currentToken.Lexeme, instrIdx, out scope))
                    {
                        errorHandler.ParserLogError("Function Already Exists");
                        return(false);
                    }
                }
                else
                {
                    errorHandler.ParserLogError("Ident Expected");
                    return(false);
                }

                currentToken = tokenizer.GetNextToken();
            }
            else if (currentToken.Type == Tokenizer.TokenType.Rsvd_EndFunc)
            {
                scope        = -1;
                currentToken = tokenizer.GetNextToken();
            }
            // ===================================================================
            // Parse instructions and labels
            else if (currentToken.Type == Tokenizer.TokenType.Ident)
            {
                string ident = currentToken.Lexeme;

                currentToken = tokenizer.GetNextToken();

                // ===================================================================
                // Is it a label?
                if (currentToken.Type == Tokenizer.TokenType.Colon)
                {
                    tables.AddLabel(ident, instrIdx, scope);

                    currentToken = tokenizer.GetNextToken();
                }
                // ===================================================================
                // It's an instruction
                else
                {
                    if (scope == -1 && tables.GetStartPC() == -1)
                    {
                        tables.SetStartPC(instrIdx);
                    }

                    instrIdx++;                     // Increment counter

                    // Skip to next line
                    if (currentToken.Type != Tokenizer.TokenType.EOL)
                    {
                        currentToken = tokenizer.SkipToNextLine();
                    }
                }
            }
            else
            {
                errorHandler.ParserLogError("Unexpected Token");
                return(false);
            }
        }

        return(true);
    }
Beispiel #2
0
    // Parse Vars and Labels
    bool Pass1()
    {
        Tokenizer.Token currentToken = tokenizer.GetNextToken();

        if (currentToken.Type == Tokenizer.TokenType.Empty)
        {
            return(false);
        }

        int instrIdx = 0;

        while (currentToken.Type != Tokenizer.TokenType.EOF && currentToken.Type != Tokenizer.TokenType.Unknown)
        {
            // ===================================================================
            // Skip end of lines
            if (currentToken.Type == Tokenizer.TokenType.EOL)
            {
                currentToken = tokenizer.GetNextToken();
            }
            // ===================================================================
            // Parse variables
            else if (currentToken.Type == Tokenizer.TokenType.Rsvd_Var)
            {
                currentToken = tokenizer.GetNextToken();

                if (currentToken.Type == Tokenizer.TokenType.Ident)
                {
                    if (!tables.AddVar(currentToken.Lexeme))
                    {
                        logger.Log("Parser Error: Var already exists");
                        return(false);
                    }
                }
                else
                {
                    logger.Log("Parser Error: Ident expected");
                    return(false);
                }

                currentToken = tokenizer.GetNextToken();
            }
            // ===================================================================
            // Parse instructions and labels
            else if (currentToken.Type == Tokenizer.TokenType.Ident)
            {
                string ident = currentToken.Lexeme;

                currentToken = tokenizer.GetNextToken();

                // ===================================================================
                // Is it a label?
                if (currentToken.Type == Tokenizer.TokenType.Colon)
                {
                    tables.AddLabel(ident, instrIdx);

                    currentToken = tokenizer.GetNextToken();
                }
                // ===================================================================
                // It's an instruction
                else
                {
                    instrIdx++;                     // Increment counter

                    // Skip to next line
                    currentToken = tokenizer.SkipToNextLine();
                }
            }
            else
            {
                logger.Log("Parser Error: unexpected token");
                return(false);
            }
        }

        return(true);
    }