Beispiel #1
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.object_, get.name, value));
                }

                error(equals, "Invalid assignment target.");
            }

            return(expr);
        }
Beispiel #2
0
        public object visitGetExpr(Expr.Get expr)
        {
            object object_ = evaluate(expr.object_);

            if (object_ is LoxInstance)
            {
                return(((LoxInstance)object_).get(expr.name));
            }

            throw new RuntimeError(expr.name, "Only instances have properties.");
        }
Beispiel #3
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);
        }
Beispiel #4
0
 public string visitGetExpr(Expr.Get expr)
 {
     return("");
 }
Beispiel #5
0
 public object visitGetExpr(Expr.Get expr)
 {
     resolve(expr.object_);
     return(null);
 }