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;
                    expr = new Expr.Assign(name, value);
                }
                else if (expr is Expr.Get)
                {
                    Expr.Get get = (Expr.Get)expr;
                    expr = new Expr.Set(get.obj, get.name, value);
                }
                else
                {
                    Error(equals, "Invalid assignment target.");
                }
            }

            return(expr);
        }
Beispiel #2
0
        public object Visit(Expr.Get expr)
        {
            object 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(TokenType.LeftParen))
                {
                    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 object Visit(Expr.Get expr)
        {
            Resolve(expr.obj);

            return(null);
        }