Exemple #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);
        }
Exemple #2
0
        public object Visit(Expr.Set expr)
        {
            Resolve(expr.value);
            Resolve(expr.obj);

            return(null);
        }
Exemple #3
0
        public object Visit(Expr.Set expr)
        {
            object obj = Evaluate(expr.obj);

            if (!(obj is LoxInstance))
            {
                throw new RuntimeError(expr.name, "Only instances have fields.");
            }

            object value = Evaluate(expr.value);

            ((LoxInstance)obj).Set(expr.name, value);

            return(value);
        }