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.obj, get.name, value));
                }

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

            return(expr);
        }
Beispiel #2
0
        public object VisitGetExpr(Expr.Get expr)
        {
            var obj = Evaluate(expr.obj);

            if (obj is LoxInstance)
            {
                return(((LoxInstance)obj).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(LEFT_PAREN))
                {
                    expr = FinishCall(expr);
                }
                else if (Match(DOT))
                {
                    var name = Consume(IDENTIFIER, "Expect property name after '.'.");
                    expr = new Expr.Get(expr, name);
                }
                else
                {
                    break;
                }
            }

            return(expr);
        }
Beispiel #4
0
 public object VisitGetExpr(Expr.Get expr)
 {
     Resolve(expr.obj);
     return(null);
 }
Beispiel #5
0
 public string VisitGetExpr(Expr.Get expr)
 {
     throw new NotImplementedException();
 }
Beispiel #6
0
 public Void Visit(Expr.Get expr)
 {
     Resolve(expr.Obj);
     return(Void.Instance);
 }
Beispiel #7
0
 object Expr.IVisitor <object> .VisitGetExpr(Expr.Get expr)
 {
     Resolve(expr.obj);
     return(null);
 }