예제 #1
0
파일: AST.cs 프로젝트: wsdt/AlmostBinary
        public static List <string> ParseArgs(TokenList _tokens)
        {
            List <string> ret = new List <string>();

            while (true)
            {
                Token tok = _tokens.GetToken();

                if (tok.TokenName == Lexer.Tokens.Ident)
                {
                    ret.Add(tok.TokenValue.ToString());
                }

                if (_tokens.PeekToken().TokenName == Lexer.Tokens.Comma)
                {
                    _tokens.Pos++;
                }
                else if (_tokens.PeekToken().TokenName == Lexer.Tokens.RightParan)
                {
                    _tokens.Pos++;
                    break;
                }
            }

            return(ret);
        }
예제 #2
0
파일: AST.cs 프로젝트: wsdt/AlmostBinary
        public static Func Parse(TokenList _tokens)
        {
            string        ident = "";
            List <string> vars  = new List <string>();

            if (_tokens.PeekToken().TokenName == Lexer.Tokens.Ident)
            {
                ident = _tokens.GetToken().TokenValue.ToString();
            }

            if (_tokens.PeekToken().TokenName == Lexer.Tokens.LeftParan)
            {
                _tokens.Pos++;
            }

            if (_tokens.PeekToken().TokenName == Lexer.Tokens.RightParan)
            {
                _tokens.Pos++;
            }
            else
            {
                vars = ParseArgs(_tokens);
            }

            if (_tokens.PeekToken().TokenName == Lexer.Tokens.LeftBrace)
            {
                _tokens.Pos++;
            }

            return(new Func(ident, vars));
        }
예제 #3
0
파일: AST.cs 프로젝트: wsdt/AlmostBinary
        public static Call Parse(TokenList _tokens)
        {
            string      ident = "";
            Token       tok   = _tokens.GetToken();
            List <Expr> args  = new List <Expr>();

            if (tok.TokenName == Lexer.Tokens.Ident)
            {
                ident = tok.TokenValue.ToString();
            }

            if (_tokens.PeekToken().TokenName == Lexer.Tokens.LeftParan)
            {
                _tokens.Pos++;
            }

            if (_tokens.PeekToken().TokenName == Lexer.Tokens.RightParan)
            {
                _tokens.Pos++;
            }
            else
            {
                args = Call.ParseArgs(_tokens);
            }

            return(new Call(ident, args));
        }
예제 #4
0
파일: AST.cs 프로젝트: wsdt/AlmostBinary
        public static Assign Parse(TokenList _tokens)
        {
            string ident = "";

            Token t = _tokens.GetToken();

            ident = t.TokenValue.ToString();

            _tokens.Pos++;

            Expr value = Expr.Parse(_tokens);

            Assign ret = new Assign(ident, value);

            return(ret);
        }
예제 #5
0
파일: AST.cs 프로젝트: wsdt/AlmostBinary
        public static Expr Parse(TokenList _tokens)
        {
            Expr? ret = null;
            Token t   = _tokens.GetToken();

            if (_tokens.PeekToken().TokenName == Lexer.Tokens.LeftParan)
            {
                string ident = "";

                if (t.TokenName == Lexer.Tokens.Ident)
                {
                    ident = t.TokenValue.ToString();
                }

                _tokens.Pos++;

                if (_tokens.PeekToken().TokenName == Lexer.Tokens.RightParan)
                {
                    ret = new CallExpr(ident, new List <Expr>());
                }
                else
                {
                    ret = new CallExpr(ident, Call.ParseArgs(_tokens));
                }
            }
            else if (t.TokenName == Lexer.Tokens.IntLiteral)
            {
                string originalStr = t.TokenValue.ToString().BinaryStrToStr()
                                     .Replace(ITokens.IPartial.INT_LITERAL, ""); // remove int-literals to parse to number
                IntLiteral i = new IntLiteral(
                    Convert.ToInt32(
                        originalStr
                        )
                    );
                ret = i;
            }
            else if (t.TokenName == Lexer.Tokens.StringLiteral)
            {
                StringLiteral s = new StringLiteral(t.TokenValue.ToString());
                ret = s;
            }
            else if (t.TokenName == Lexer.Tokens.Ident)
            {
                string ident = t.TokenValue.ToString();

                Ident i = new Ident(ident);
                ret = i;
            }
            else if (t.TokenName == Lexer.Tokens.LeftParan)
            {
                Expr e = Expr.Parse(_tokens);

                if (_tokens.PeekToken().TokenName == Lexer.Tokens.RightParan)
                {
                    _tokens.Pos++;
                }

                ParanExpr p = new ParanExpr(e);

                if (_tokens.PeekToken().TokenName == Lexer.Tokens.Add)
                {
                    _tokens.Pos++;
                    Expr expr = Expr.Parse(_tokens);
                    ret = new MathExpr(p, Symbol.add, expr);
                }
                else if (_tokens.PeekToken().TokenName == Lexer.Tokens.Sub)
                {
                    _tokens.Pos++;
                    Expr expr = Expr.Parse(_tokens);
                    ret = new MathExpr(p, Symbol.sub, expr);
                }
                else if (_tokens.PeekToken().TokenName == Lexer.Tokens.Mul)
                {
                    _tokens.Pos++;
                    Expr expr = Expr.Parse(_tokens);
                    ret = new MathExpr(p, Symbol.mul, expr);
                }
                else if (_tokens.PeekToken().TokenName == Lexer.Tokens.Div)
                {
                    _tokens.Pos++;
                    Expr expr = Expr.Parse(_tokens);
                    ret = new MathExpr(p, Symbol.div, expr);
                }
                else
                {
                    ret = p;
                }
            }

            if (_tokens.PeekToken().TokenName == Lexer.Tokens.Add ||
                _tokens.PeekToken().TokenName == Lexer.Tokens.Sub ||
                _tokens.PeekToken().TokenName == Lexer.Tokens.Mul ||
                _tokens.PeekToken().TokenName == Lexer.Tokens.Div)
            {
                Expr?  lexpr = ret;
                Symbol op    = 0;

                if (_tokens.PeekToken().TokenName == Lexer.Tokens.Add)
                {
                    op = Symbol.add;
                }
                else if (_tokens.PeekToken().TokenName == Lexer.Tokens.Sub)
                {
                    op = Symbol.sub;
                }
                else if (_tokens.PeekToken().TokenName == Lexer.Tokens.Mul)
                {
                    op = Symbol.mul;
                }
                else if (_tokens.PeekToken().TokenName == Lexer.Tokens.Div)
                {
                    op = Symbol.div;
                }

                _tokens.Pos++;

                Expr rexpr = Expr.Parse(_tokens);

                ret = new MathExpr(
                    lexpr ?? throw new Exception($"Left expression of mathematical expression is missing: '{op}'->'{rexpr}'"),
                    op,
                    rexpr);
            }

            if (ret == null)
            {
                throw new Exception($"Caught unexpected expression -> '{t.TokenName}':'{t.TokenValue}' at position '{_tokens.Pos}', total-length: '{_tokens.Tokens.Count}'");
            }
            return(ret);
        }