예제 #1
0
        public object VisitGetExpr(Expr.Get expr)
        {
            var obj = Evaluate(expr.Obj);

            if (obj is LoxInstance li)
            {
                return(li.Get(expr.Name));
            }

            throw new RuntimeError(expr.Name, "Only instances have properties");
        }
예제 #2
0
        object Expr.IVisitor <object> .Visit(Expr.Get _get)
        {
            object target = Evaluate(_get.target);

            if (target is LoxInstance)
            {
                return(((LoxInstance)target).Get(_get.name));
            }

            throw new RuntimeError(_get.name, "Only instances have properties.");
        }
예제 #3
0
        public object VisitGetExpr(Expr.Get expr)
        {
            if (Evaluate(expr.obj) is LoxInstance instance)
            {
                var value = instance[expr.name];
                if (value is LoxFunction function && function.IsGetter)
                {
                    value = function.Call(this, null);
                }

                return(value);
            }

            throw new RuntimeError(expr.name, "Only instances have properties");
        }
예제 #4
0
        public object visitGetExpr(Expr.Get get)
        {
            Object _object = evaluate(get._object);

            if (_object is LoxInstance && !(_object is LoxClass))
            {
                object result = ((LoxInstance)_object).get(get.name);
                if (result is LoxFunction)
                {
                    if (((LoxFunction)result).isGetter())
                    {
                        result = ((LoxFunction)result).call(this, null);
                    }
                }


                return(result);
            }

            throw new Exceptions.RuntimeError(get.name, "Only instances have properties.");
        }
예제 #5
0
        private Expr Call()
        {
            Expr expr = Primary();

            while (true)
            {
                if (Match(TokenType.LEFT_PAREN))
                {
                    expr = FinishCall(expr);
                }
                else if (Match(TokenType.DOT))
                {
                    Token name = Consume(TokenType.IDENTIFIER, "Expect property name after '.'.");
                    expr = new Expr.Get(expr, name);
                }
                else
                {
                    break;
                }
            }

            return(expr);
        }
예제 #6
0
        private Expr assignment()
        {
            Expr expr = ternaryExpression();

            if (match(TokenType.EQUALS))
            {
                Token equals = previous();
                Expr  value  = assignment();

                if (expr is Expr.Variable)
                {
                    Token name = ((Expr.Variable)expr).name;
                    return(new Expr.AssignExpr(name, value));
                }
                else if (expr is Expr.Get)
                {
                    Expr.Get get = (Expr.Get)expr;
                    return(new Expr.Set(get._object, get.name, value, get.isCascading));
                }
                throw error(equals, "Invalid assignment target: " + expr.GetType());
            }
            return(expr);
        }
예제 #7
0
        private Expr Call()
        {
            Expr expr = Primary();

            while (true)
            {
                if (Match(LEFT_PAREN))
                {
                    // Trying to call previous expr as a function.
                    expr = FinishCall(expr);
                }
                else if (Match(DOT))
                {
                    Token name = Consume(IDENTIFIER, "expect property name after '.'.");
                    expr = new Expr.Get(expr, name);
                }
                else
                {
                    break;
                }
            }

            return(expr);
        }
예제 #8
0
        private Expr call()
        {
            Expr expr = primary();

            while (true)
            {
                if (match(TokenType.LEFT_PAREN))
                {
                    expr = finishCall(expr);
                }
                else if (match(TokenType.DOT))
                {
                    Token name = consume(TokenType.IDENTIFIER, "Expect property name after '.'.");
                    expr = new Expr.Get(expr, name, false);
                }
                else if (match(TokenType.QUESTION_DOT))
                {
                    Token nil           = new Token(TokenType.NIL, "nil", null, -1, -1);
                    Expr  callIfNotNull = new Expr.Get(expr, consume(TokenType.IDENTIFIER, "Expect property after '?."), false);
                    if (match(TokenType.LEFT_PAREN))
                    {
                        callIfNotNull = finishCall(callIfNotNull);
                    }
                    expr = new Expr.TernaryExpr(
                        new Expr.BinaryExpr(expr, new Token(TokenType.EQUALS_EQUALS, "==", null, -1, -1), new Expr.Literal(null, nil)), // ?
                        new Expr.Literal(null, nil),                                                                                    // :
                        callIfNotNull);                                                                                                 // new Expr.Get(expr, consume(TokenType.IDENTIFIER, "Expect property after '?."), false));
                    ((Expr.TernaryExpr)expr).isNullAccessor = true;
                }
                else
                {
                    break;
                }
            }
            return(expr);
        }
예제 #9
0
 string Expr.IVisitor <string> .Visit(Expr.Get _get)
 {
     throw new NotImplementedException();
 }
예제 #10
0
 public object visitGetExpr(Expr.Get get)
 {
     resolve(get._object);
     return(null);
 }
예제 #11
0
 public object VisitGetExpr(Expr.Get expr)
 {
     Resolve(expr.obj);
     return(null);
 }
예제 #12
0
 public string visitGetExpr(Expr.Get get)
 {
     return("(" + get._object.accept(this) + "." + get.name.lexeme + ")");
 }
예제 #13
0
 public Void VisitGetExpr(Expr.Get expr)
 {
     Resolve(expr.Obj);
     return(null);
 }
예제 #14
0
 public string VisitGetExpr(Expr.Get expr)
 {
     return($"(value of {expr.Accept(this)})");
 }
예제 #15
0
        public object Visit(Expr.Get _get)
        {
            Resolve(_get.target);

            return(null);
        }