public object visitGetExpr(Expr.Get expr)
        {
            Object instance = evaluate(expr.Instance);

            if (instance is LoxInstance)
            {
                return(((LoxInstance)instance).get(expr.Name));
            }

            throw new RuntimeError(expr.Name,
                                   "Only instances have properties");
        }
Example #2
0
        private Expr assignment()
        {
            Expr expr = or();

            if (match(TokenType.EQUAL))
            {
                Token equals = previous();
                Expr  value  = assignment();
                if (expr is Expr.Variable)
                {
                    Token name = ((Expr.Variable)expr).Name;
                    return(new Expr.Assign(name, value));
                }
                else if (expr is Expr.Get)
                {
                    Expr.Get get = (Expr.Get)expr;
                    return(new Expr.Set(get.Instance, get.Name, value));
                }
                error(equals, "Invalid assignment target.");
            }
            return(expr);
        }
Example #3
0
        private Expr call()
        {
            Expr expr = primary();

            while (true)
            {
                if (match(TokenType.LEFT_PAREN))
                {
                    expr = finishCall(expr);
                }
                if (match(TokenType.DOT))
                {
                    Token name = consume(TokenType.IDENTIFIER,
                                         "Expet property name after '.'");
                    expr = new Expr.Get(expr, name);
                }
                else
                {
                    break;
                }
            }
            return(expr);
        }
 public string visitGetExpr(Expr.Get expr)
 {
     throw new NotImplementedException();
 }
Example #5
0
 public object visitGetExpr(Expr.Get expr)
 {
     resolve(expr.Instance);
     return(null);
 }