示例#1
0
文件: Parser.cs 项目: ztmurphy21/Klip
        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;
                }
            }
        }
示例#2
0
 static void CompileAssign(Assign data)
 {
     CompileExpr(data.value);
     Write("setVar " + data.ident);
 }