Пример #1
0
        static ElseIfBlock ParseElseIf()
        {
            ElseIfBlock ret = null;
            Symbol      op  = 0;

            if (tokens.Peek().TokenName == Lexer.Tokens.LeftParan)
            {
                tokens.pos++;
            }

            Expr lexpr = ParseExpr();

            if (tokens.Peek().TokenName == Lexer.Tokens.DoubleEqual)
            {
                op = Symbol.doubleEqual;
                tokens.pos++;
            }
            else if (tokens.Peek().TokenName == Lexer.Tokens.NotEqual)
            {
                op = Symbol.notEqual;
                tokens.pos++;
            }

            Expr rexpr = ParseExpr();

            if (tokens.Peek().TokenName == Lexer.Tokens.RightParan)
            {
                tokens.pos++;
            }

            ret = new ElseIfBlock(lexpr, op, rexpr);

            return(ret);
        }
Пример #2
0
        static void CompileElseIf(ElseIfBlock data)
        {
            CompileExpr(data.leftExpr);
            CompileExpr(data.rightExpr);

            if (data.op == Symbol.doubleEqual)
            {
                Write("elseife");
            }
            else if (data.op == Symbol.notEqual)
            {
                Write("elseifn");
            }

            CompileStmtList(data.statements);
        }
Пример #3
0
        public Parser(TokenList t)
        {
            tokens       = t;
            currentBlock = null;
            blockstack   = new Stack <Block>();
            Token tok = null;

            tree    = new List <Stmt>();
            running = true;

            while (running)
            {
                try
                {
                    tok = tokens.GetToken();
                }
                catch { }

                if (tok.TokenName == Lexer.Tokens.Import)
                {
                    Program.imports.Add(ParseImport());
                }
                else if (tok.TokenName == Lexer.Tokens.Function)
                {
                    Func func = ParseFunc();

                    if (currentBlock == null)
                    {
                        currentBlock = func;
                    }
                    else
                    {
                        currentBlock.AddStmt(new Return(null));
                        tree.Add(currentBlock);
                        currentBlock = func;
                    }
                }
                else if (tok.TokenName == Lexer.Tokens.If)
                {
                    IfBlock ifblock = ParseIf();

                    if (currentBlock != null)
                    {
                        blockstack.Push(currentBlock);
                        currentBlock = ifblock;
                    }
                }
                else if (tok.TokenName == Lexer.Tokens.ElseIf)
                {
                    ElseIfBlock elseifblock = ParseElseIf();

                    if (currentBlock != null)
                    {
                        blockstack.Push(currentBlock);
                        currentBlock = elseifblock;
                    }
                }
                else if (tok.TokenName == Lexer.Tokens.Else)
                {
                    if (currentBlock != null)
                    {
                        blockstack.Push(currentBlock);
                        currentBlock = new ElseBlock();
                    }
                }
                else if (tok.TokenName == Lexer.Tokens.Repeat)
                {
                    if (currentBlock != null)
                    {
                        blockstack.Push(currentBlock);
                        currentBlock = new RepeatBlock();
                    }
                }
                else if (tok.TokenName == Lexer.Tokens.Ident)
                {
                    if (tokens.Peek().TokenName == Lexer.Tokens.Equal)
                    {
                        tokens.pos--;
                        Assign a = ParseAssign();
                        currentBlock.AddStmt(a);
                    }
                    else if (tokens.Peek().TokenName == Lexer.Tokens.LeftParan)
                    {
                        tokens.pos--;
                        Call c = ParseCall();
                        currentBlock.AddStmt(c);
                    }
                }
                else if (tok.TokenName == Lexer.Tokens.Return)
                {
                    Return r = ParseReturn();
                    currentBlock.AddStmt(r);
                }
                else if (tok.TokenName == Lexer.Tokens.RightParan)
                {
                    if (currentBlock is Func)
                    {
                        currentBlock.AddStmt(new Return(null));
                        tree.Add(currentBlock);
                        currentBlock = null;
                    }
                    else if (currentBlock is IfBlock || currentBlock is ElseIfBlock || currentBlock is ElseBlock)
                    {
                        currentBlock.AddStmt(new EndIf());
                        Block block = currentBlock;

                        if (blockstack.Count > 0)
                        {
                            currentBlock = blockstack.Pop();
                            currentBlock.AddStmt(block);
                        }
                    }
                    else if (currentBlock is RepeatBlock)
                    {
                        Block block = currentBlock;

                        if (blockstack.Count > 0)
                        {
                            currentBlock = blockstack.Pop();
                            currentBlock.AddStmt(block);
                        }
                    }
                }
                else if (tok.TokenName == Lexer.Tokens.EOF)
                {
                    tree.Add(currentBlock);
                    running = false;
                }
            }
        }