[m-rev.] for post-commit review: speed up the parser

Peter Wang novalazy at gmail.com
Sat Oct 17 14:25:26 AEDT 2015


On Fri, 16 Oct 2015 20:48:54 +1100 (AEDT), "Zoltan Somogyi" <zoltan.somogyi at runbox.com> wrote:
> A minor speedup.
> 

> diff --git a/library/parser.m b/library/parser.m
> index d16e98d..be0e5c8 100644
> --- a/library/parser.m
> +++ b/library/parser.m
> @@ -382,93 +382,117 @@ parse_left_term(MaxPriority, TermKind, OpPriority, Term, !TokensLeft, !PS) :-
...
> +            % We test for unary minus inside the test for TokenName being
> +            % an operator, even though we don't use its OpInfos,
> +            % because this allows us to avoid a separate test in the
> +            % *very* common case that TokenName is NOT an operator.
> +            % The cost is that we look up of the op_infos even for
> +            % unary minus terms, but this cost should be minor in comparison.
> +            %
> +            % This scheme relies on "-" being an operator, but the language
> +            % guarantees that.
> +            ( if
> +                % Check for unary minus of an integer or a float.
> +                TokenName = "-",
> +                !.TokensLeft =
> +                    token_cons(NextToken, _NextContext, !:TokensLeft),
> +                (
> +                    NextToken = integer(X),
> +                    NegX = 0 - X,
> +                    NewFunctor = integer(NegX)
> +                ;
> +                    NextToken = big_integer(_, X),
> +                    -X = integer(min_int),
> +                    NegX = int.min_int,
> +                    NewFunctor = integer(NegX)
> +                ;
> +                    NextToken = float(F),
> +                    NegF = 0.0 - F,
> +                    NewFunctor = float(NegF)
> +                )

The language being parsed may not have "-" as an operator.

The parser previously parsed a "-" followed by an integer or float token
as a negative integer or float term, following these rules:

    % term --> name("-") integer    % priority 0
    % term --> name("-") float      % priority 0

I assume this comes from ISO Prolog, but I don't have a copy of the standard.

The new behaviour is adding or removing those rules based on whether "-"
acts as an operator in other rules.  I don't think it should do that.

Peter



More information about the reviews mailing list