Exemplo n.º 1
0
        private GExpr.Expr assignment()
        {
            GExpr.Expr expr = or();

            if (Match(tt.EQUAL))
            {
                Token      equals = previous();
                GExpr.Expr value  = assignment();

                if (expr is GExpr.Variable)
                {
                    Token name = ((GExpr.Variable)expr).name;
                    return(new GExpr.Assign(name, value));
                }
                else if (expr is GExpr.Get)
                {
                    GExpr.Get get = (GExpr.Get)expr;
                    return(new GExpr.Set(get.Object, get.name, value));
                }

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

            return(expr);
        }
Exemplo n.º 2
0
        public object visit_Get_Expr(GExpr.Get expr)
        {
            object obj = evaluate(expr.Object);

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

            throw new RuntimeError(expr.name, "Only instances have properties");
        }
Exemplo n.º 3
0
        private GExpr.Expr call()
        {
            GExpr.Expr expr = primary();

            while (true)
            {
                if (Match(tt.LEFT_PAREN))
                {
                    expr = finishCall(expr);
                }
                else if (Match(tt.DOT))
                {
                    Token name = consume(tt.IDENTIFIER, "Expect property name after '.'.");
                    expr = new GExpr.Get(expr, name);
                }
                else
                {
                    break;
                }
            }

            return(expr);
        }
Exemplo n.º 4
0
 public object visit_Get_Expr(GExpr.Get expr)
 {
     resolve(expr.Object);
     return(null);
 }