示例#1
0
        private Statements ParseStatements()
        {
            Statements statements = new Statements();
            Statement  st;

            while ((st = this.ParseStatement()) != null)
            {
                statements.AddStatement(st);
            }

            return(statements);
        }
示例#2
0
        public Program Parse(List <Token> tokens)
        {
            this.tokens = tokens;

            Program    prog = new Program();
            Statements st   = this.ParseStatements();

            if (st != null)
            {
                prog.AddStatement(st);
            }

            return(prog);
        }
示例#3
0
        private Function ParseFunction()
        {
            string name = "";

            if (this.CurrentType == TokenType.ALPHANUMERIC)
            {
                name = this.Pop().Value;
            }

            if (this.CurrentType != TokenType.L_PAREN)
            {
                return(null);
            }

            this.Pop();
            List <string> parameters = new List <string>();

            while (this.CurrentType == TokenType.ALPHANUMERIC)
            {
                parameters.Add(this.Pop().Value);
                if (this.CurrentType == TokenType.COMMA)
                {
                    this.Pop();
                }
            }

            if (this.CurrentType != TokenType.R_PAREN)
            {
                return(null);
            }

            this.Pop();

            if (this.CurrentType != TokenType.L_BRACE)
            {
                return(null);
            }

            this.Pop();

            Statements st          = this.ParseStatements();
            Expression returnValue = null;

            if (this.CurrentType == TokenType.RETURN)
            {
                this.Pop();
                returnValue = this.ParseExpression();
                if (this.CurrentType != TokenType.SEMI)
                {
                    return(null);
                }

                this.Pop();
            }

            if (this.CurrentType != TokenType.R_BRACE)
            {
                return(null);
            }

            this.Pop();
            return(new Function(name, st, parameters, returnValue));
        }
示例#4
0
        private Statement ParseStatement()
        {
            while (this.CurrentType == TokenType.LINE_END)
            {
                this.Pop();
            }

            TokenType type = this.CurrentType;

            if (type == TokenType.FUNCTION)
            {
                this.Pop();
                Function fun = this.ParseFunction();
                return(new FunctionStatement(StatementType.FUNC_DECL, fun));
            }
            else if (type == TokenType.IF)
            {
                this.Pop();
                Condition cnd = this.ParseCondition();

                if (this.CurrentType != TokenType.L_BRACE)
                {
                    return(null);
                }

                this.Pop();
                Statements st1 = this.ParseStatements();

                if (this.CurrentType != TokenType.R_BRACE)
                {
                    return(null);
                }

                this.Pop();

                if (this.CurrentType == TokenType.ELSE)
                {
                    this.Pop();

                    if (this.CurrentType != TokenType.L_BRACE)
                    {
                        return(null);
                    }

                    this.Pop();

                    Statements st2 = this.ParseStatements();

                    if (this.CurrentType != TokenType.R_BRACE)
                    {
                        return(null);
                    }

                    this.Pop();

                    return(new IfElseStatement(StatementType.IF_ELSE, cnd, st1, st2));
                }
                else
                {
                    return(new IfStatement(StatementType.IF, cnd, st1));
                }
            }
            else if (type == TokenType.WHILE)
            {
                this.Pop();
                Condition cnd = this.ParseCondition();

                if (this.CurrentType != TokenType.L_BRACE)
                {
                    return(null);
                }

                this.Pop();
                Statements st = this.ParseStatements();

                if (this.CurrentType != TokenType.R_BRACE)
                {
                    return(null);
                }
                this.Pop();

                return(new WhileStatement(StatementType.WHILE, cnd, st));
            }
            else if (type == TokenType.RETURN)
            {
                this.Pop();

                Expression returnValue = this.ParseExpression();

                if (this.CurrentType != TokenType.SEMI)
                {
                    return(null);
                }

                this.Pop();
                return(new FunctionStatement(StatementType.RETURN, returnValue));
            }
            else
            {
                if (this.NextType != TokenType.L_PAREN)
                {
                    Assignment asg = this.ParseAssignment();
                    if (asg == null)
                    {
                        return(null);
                    }
                    else
                    {
                        return(new AssignmentStatement(StatementType.ASSIGN, asg));
                    };
                }
                else
                {
                    Expression fun = this.ParseExpression();
                    if (fun == null)
                    {
                        return(null);
                    }
                    else
                    {
                        if (this.CurrentType != TokenType.SEMI)
                        {
                            return(null);
                        }
                        this.Pop();
                        return(new FunctionStatement(StatementType.FUNCTION, fun));
                    }
                }
            }
        }
示例#5
0
 public void AddStatement(Statements st)
 {
     this.statemets.AddRange(st.statemets);
 }
示例#6
0
 public Function(string name, Statements inner, List <string> parameters, Expression returnValue)
 {
     this.Name            = name;
     this.Parameters      = parameters;
     this.innerStatements = inner;
 }