Beispiel #1
0
        public object Visit(GetExpr expr)
        {
            var evaluatedValue = Evaluate(expr.Instance);

            if (evaluatedValue is LoxInstance instance)
            {
                return(instance.Get(expr.Name));
            }

            throw new LoxRuntimeErrorException(expr.Name, "Only Lox class instances can have properties.");
        }
Beispiel #2
0
        Expr CallExpression()
        {
            var expr = Primary();

            while (true)
            {
                if (MatchNext(TokenType.LeftParen))
                {
                    expr = FinishCallExpression(expr);
                }
                else if (MatchNext(TokenType.Dot))
                {
                    var name = Consume(TokenType.Identifier, "Expected a property identifier after '.'.");
                    expr = new GetExpr(expr, name);
                }
                else
                {
                    break;
                }
            }

            return(expr);
        }