Exemplo n.º 1
0
        /// <summary>
        /// Parse a constant declaration (only "int" type is supported)
        /// </summary>
        SyntaxExpr ParseConstStatement()
        {
            // Create "const" statement
            SyntaxExpr expr = new SyntaxExpr(Accept("const"));

            if (mTokenName != "int")
            {
                Reject("Expecting keyword 'int'", REJECT_LINE);
                return(expr);
            }
            // First parameter is the type, "int"
            expr.AddParam(new SyntaxExpr(Accept()));

            // Variable name must be identifier
            if (mToken.Type != eTokenType.Identifier)
            {
                Reject("Expecting a variable name", REJECT_LINE);
                return(expr);
            }
            Token variableName = Accept();

            // Const statement requires an assignment
            if (mTokenName != "=")
            {
                Reject("Expecting '=' - Assignment is required");
                return(expr);
            }
            // Second parameter is the assignment expression
            expr.AddParam(new SyntaxExpr(Accept(), new SyntaxExpr(variableName), ParseExpr()));

            return(expr);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Make a bit declaration.  First parameter is declaration (with optional
        /// array parameter).  Second parameter is optional assignment.
        /// </summary>
        public SyntaxExpr ParseBitStatement(string[] rejects)
        {
            // Create "bit" statement
            SyntaxExpr expr = new SyntaxExpr(Accept("bit"));

            if (mToken.Type != eTokenType.Identifier)
            {
                Reject("Expecting a variable name", rejects);
                return(expr);
            }
            // Create declaration (array size is optional parameter)
            Token      variableName = Accept();
            SyntaxExpr declaration  = new SyntaxExpr(variableName);

            if (mTokenName == "[")
            {
                declaration.AddParam(ParseParen());
            }

            // First parameter is declaration
            expr.AddParam(declaration);

            // Second parameter (optional) is assignment statement
            if (mTokenName == "=")
            {
                expr.AddParam(new SyntaxExpr(Accept(), new SyntaxExpr(variableName), ParseExpr()));
            }

            return(expr);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Parse an if statement (expects 'if' at the input)
        /// </summary>
        SyntaxExpr ParseIfStatement(SyntaxBox box)
        {
            // Parse "if" token, condition, and statements
            Token ifToken = mToken;

            Accept("if");
            SyntaxExpr ifExpr = new SyntaxExpr(ifToken);

            ifExpr.AddParam(ParseIfCond());
            ifExpr.AddParam(ParseStatements(box, false));

            // Parse "elif" statements
            SyntaxExpr elseExpr = ifExpr;

            while (mTokenName == "elif")
            {
                // Parse "elif" token, condition, and statements
                // NOTE: "elif" token is converted to "if"
                Connect(ifToken, mToken);
                Accept("elif");
                SyntaxExpr newIf = new SyntaxExpr(ifToken);
                newIf.AddParam(ParseIfCond());
                newIf.AddParam(ParseStatements(box, false));

                // Convert the new "elif" to "else if"
                elseExpr.AddParam(newIf);
                elseExpr = newIf;
            }

            // Parse the "else" clause (if it exists)
            if (mTokenName == "else")
            {
                Connect(ifToken, mToken);
                Accept("else");
                elseExpr.AddParam(ParseStatements(box, false));
            }

            // Parse end of box statement
            if (mTokenName == "end")
            {
                mToken.InfoString = "end if";
                Connect(ifToken, mToken);
                Accept();
            }
            else
            {
                Reject("Expecting 'end' - end of if statement body", REJECT_BOX);
            }

            // Parse "else" part
            return(ifExpr);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Parse multiple statements until 'box', 'end', 'else', or 'elif'
        /// is found.  Returns the statements that are parsed.
        /// Adds declarations to the box locals.  Never returns NULL.
        /// </summary>
        public SyntaxExpr ParseStatements(SyntaxBox box, bool topLevel)
        {
            // Create an "{" expression, which (for now means "statements")
            SyntaxExpr statements = new SyntaxExpr(mInternalTokenStatement);

            // While not end of file and not end of box and not new box
            while (mTokenName != "" &&
                   mTokenName != "box" &&
                   mTokenName != "end" &&
                   mTokenName != "else" &&
                   mTokenName != "elif")
            {
                // Skip blank statements
                while (mTokenName == ";")
                {
                    Accept();
                }

                // Parse the statement
                SyntaxExpr statement = ParseStatement(box, topLevel);
                if (statement != null)
                {
                    statements.AddParam(statement);
                }

                while (mTokenName == ";")
                {
                    Accept();
                }
            }
            return(statements);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Parse parameters '(' param1 ',' param2 ... ')'
        /// NOTE: firstParam is the function name, and will be the first
        /// parameter of the result.  The open and close '()' or '[]' will
        /// be connected.
        /// </summary>
        SyntaxExpr ParseParameters(SyntaxExpr firstParam)
        {
            // Read open '(' or '['
            Token openToken = mToken;

            if (openToken != "(" && openToken != "[")
            {
                throw new Exception("Compiler error: Expecting '(' or '[' while parsing parameters");
            }

            // Create an expression with '(' or '['
            SyntaxExpr result        = new SyntaxExpr(Accept(), firstParam);
            string     tokenExpected = openToken == "(" ? ")" : "]";

            string [] rejectTokens = openToken == "(" ? REJECT_PAREN : REJECT_BRACKET;

            // Empty () function call?
            if (mTokenName == tokenExpected)
            {
                // Return an empty () or []
                Connect(openToken, mToken);
                Accept();
                return(result);
            }

            // Parse all parameters
            result.AddParam(ParseExpr());
            while (mTokenName == ",")
            {
                Accept();
                result.AddParam(ParseExpr());
            }

            // If not ended properly, reject this expression
            if (mTokenName != tokenExpected)
            {
                // The rest of the line is rejected
                Reject("Expecting '" + tokenExpected + "' or ','", rejectTokens);
            }

            if (mTokenName == tokenExpected)
            {
                Connect(openToken, mToken);
                Accept();
            }
            return(result);
        }