示例#1
0
文件: Parser.cs 项目: sirgru/CsLox
        // assignment → identifier "=" assignment | logic_or ;
        private Expr Assignment()
        {
            Expr expr = Or();

            if (CheckCurrentToken(TokenType.Equal))
            {
                Token equalsToken = GetCurrentTokenAndAdvance();

                Expr value = Assignment();

                var nameToken = expr as VariableExpr;
                if (nameToken != null)
                {
                    return(new AssignExpr(nameToken.name, value));
                }

                else if (expr is GetExpr)
                {
                    GetExpr get = (GetExpr)expr;
                    return(new SetExpr(get.obj, get.name, value));
                }

                CreateError(equalsToken, ErrorType.InvalidAssignmentTarget);
            }

            return(expr);
        }
示例#2
0
        public Object Visit_GetExpr(GetExpr expr)
        {
            Object obj = Evaluate(expr.obj);

            if (obj is LoxInstance)
            {
                return(((LoxInstance)obj).Get(expr.name));
            }
            throw new RuntimeException(expr.name.line, expr.name.column, "Only instances have properties.");
        }
示例#3
0
        public object VisitGetExpr(GetExpr expr)
        {
            var obj = Evaluate(expr.Object) as LoxInstance;

            if (obj == null)
            {
                throw new LoxRuntimeException(expr.Name, "Only instances have properties.");
            }

            return(obj.Get(expr.Name));
        }
示例#4
0
        // Call -> Primary ( "(" Arguments? ")" )*
        private AstNode Call()
        {
            AstNode expr = Primary();

            while (true)
            {
                if (Match(TokenType.LEFT_PAREN))
                {
                    expr = FinishCall(expr);
                }
                else if (Match(TokenType.DOT))
                {
                    var name = Consume(TokenType.IDENTIFIER, "Expect property name after '.'.");
                    expr = new GetExpr(expr, name);
                }
                else
                {
                    break;
                }
            }
            return(expr);
        }
示例#5
0
文件: Parser.cs 项目: sirgru/CsLox
        private Expr Call()
        {
            Expr expr = Primary();

            while (true)
            {
                if (CheckCurrentTokenAndAdvance(TokenType.LeftParen))
                {
                    expr = FinishCall(expr);
                }
                else if (CheckCurrentTokenAndAdvance(TokenType.Dot))
                {
                    Token name = Expect(TokenType.Identifier, ErrorType.ExpectedIdentifier);
                    expr = new GetExpr(expr, name);
                }
                else
                {
                    break;
                }
            }
            return(expr);
        }
示例#6
0
        public object VisitGetExpr(GetExpr expr)
        {
            Resolve(expr.Object);

            return(null);
        }
示例#7
0
 public Void Visit_GetExpr(GetExpr expr)
 {
     Resolve(expr.obj);
     return(null);
 }
示例#8
0
        public string Visit_GetExpr(GetExpr expr)
        {
            var name = (string)expr.name.value;

            return(Parenthesize("Get " + name, expr.obj));
        }