More concise number parsing
This commit is contained in:
parent
040f3bd859
commit
ea80d57ef9
46
polymani.pl
46
polymani.pl
@ -236,12 +236,15 @@ parse_number(TL, T, [WN | R], NC) :-
|
||||
parse_number(op(*, TL, N), T, R, NC),
|
||||
!.
|
||||
parse_number(TL, T, [and, WN | R], NC) :-
|
||||
special_word_number(WN, _, _),
|
||||
parse_number(TL, T, [WN | R], NC),
|
||||
!.
|
||||
parse_number(T, T, [WN | R], [WN | R]) :-
|
||||
not(word_tree(WN, _)),
|
||||
T \= void,
|
||||
not(special_word_number(WN, _, _)),
|
||||
!.
|
||||
parse_number(T, T, [], []) :-
|
||||
T \= void,
|
||||
!.
|
||||
%% Tests:
|
||||
%% ?- parse_number(void, T, [two], _).
|
||||
@ -262,14 +265,47 @@ parse_number(T, T, [], []) :-
|
||||
%@ T = op(+, op(*, 2, 100), 1).
|
||||
%% ?- parse_number(void, T, [twenty, one, hundred, and, twenty, one], _).
|
||||
%@ T = op(+, op(+, op(*, op(+, 20, 1), 100), 20), 1).
|
||||
%% ?- parse_number(void, T, [twenty, one, hundred, and, twenty, one, foo, bar, blah], _).
|
||||
%@ T = op(+, op(+, op(*, op(+, 20, 1), 100), 20), 1).
|
||||
%% ?- parse_number(void, T, [twenty, one, hundred, and, bleg, twenty, quux, one, foo, bar], _).
|
||||
%@ T = op(*, op(+, 20, 1), 100).
|
||||
%% ?- parse_number(void, T, [twenty, one, hundred, and, twenty, one, foo, bar, blah], NC).
|
||||
%@ T = op(+, op(+, op(*, op(+, 20, 1), 100), 20), 1),
|
||||
%@ NC = [foo, bar, blah].
|
||||
%% ?- parse_number(void, T, [twenty, one, hundred, and, bleg, twenty, quux, one, foo, bar], NC).
|
||||
%@ T = op(*, op(+, 20, 1), 100),
|
||||
%@ NC = [and, bleg, twenty, quux, one, foo, bar].
|
||||
%% ?- parse_number(void, T, [two, hundred, thousand], _).
|
||||
%@ T = op(*, op(*, 2, 100), 1000).
|
||||
%% ?- parse_number(void, T, [twenty, one, hundred, thousand], _).
|
||||
%@ T = op(*, op(*, op(+, 20, 1), 100), 1000).
|
||||
%% ?- parse_number(void, T, [thirty, five, million], _).
|
||||
%@ T = op(*, op(+, 30, 5), 1000000).
|
||||
%% ?- parse_number(void, T, [foo, five, million], NC).
|
||||
%@ false.
|
||||
|
||||
operations(times, *).
|
||||
operations(plus, +).
|
||||
|
||||
parse_operation(Op) --> [WOp], { operations(WOp, Op) }.
|
||||
|
||||
parse_expression(TLP-TL, op(Op, TLP-TL, TR)) --> parse_number(void, TL),
|
||||
parse_operation(Op),
|
||||
parse_expression(op(Op, TL, TRP)-TRP, TR).
|
||||
parse_expression(void, T) --> parse_number(void, T), { T \= void }.
|
||||
parse_expression(TL, TL-TR) --> parse_number(void, TR), { TR \= void, TL \= void }.
|
||||
%% Tests:
|
||||
%% ?- parse_expression(T, [], _).
|
||||
%@ false.
|
||||
%% ?- parse_expression(T, [two], _).
|
||||
%@ T = 2.
|
||||
%% ?- parse_expression(void, T, [two, times, two], _).
|
||||
%@ T = 2 ;
|
||||
%@ false.
|
||||
%@ T = 2 ;
|
||||
%@ false.
|
||||
%@ T = op(*, 2, 2).
|
||||
%% ?- parse_expression(T, [two, times, three, plus, two], _).
|
||||
%@ T = op(*, 2, 3).
|
||||
%@ T = op(*, 2, op(+, 3, 2)).
|
||||
%% ?- parse_expression(T, [two, times, times, two], _).
|
||||
%@ false.
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user