Added some elementary missing functionalities and minor bug fixes

Added multiply, add, etc. to process_input
Memory is now properly working (store, forget and access storage)
Command concatenation is now properly working (as is sophisticated numbers [bug fix])
This commit is contained in:
Diogo Cordeiro 2018-12-18 03:02:53 +00:00
parent b984b0efd4
commit 04199f670f

View File

@ -55,26 +55,32 @@ polyplay :-
(
parse_input(TIn, LA, NC),
(
TIn == void,
writeln("I didn't understand what you want."),
writeln(NC)
TIn == void,
writeln("I didn't understand what you want."),
writeln(NC)
;
process_input(TIn)
)
;
;
writeln("I didn't understand what you want.")
),
polyplay
),
polyplay
),
!.
process_input(show(P, void)) :-
P \== void,
polynomial_store(P, T),
write(P),
write(" = "),
print_polynomial_tree(T),
nl.
%process_input(show(P, void)) :-
% P \== void,
% polynomial_store(P, T),
% write(P),
% write(" = "),
% print_polynomial_tree(T),
% nl.
process_input(command(C1, C2)) :-
process_input(C1),
process_input(C2).
process_input(show_stored_polynomials) :-
findall(nm(X,Y), polynomial_store(X,Y),D),
nlp_print_memory(D).
process_input(show(P, T)) :-
P \== void,
T \== void,
@ -88,12 +94,19 @@ process_input(show(void, T)) :-
nl.
process_input(store(P, T)) :-
assertz(polynomial_store(P, T)),
write(P),
write(" = "),
print_polynomial_tree(T),
nl.
process_input(forget(P)) :-
retract(polynomial_store(P,_)).
process_input(simplify(P)) :-
polynomial_tree_to_polynomial(P, PP),
simpoly(PP, SP),
write(SP),
nl.
process_input(op(Op, TL, TR)) :-
process_input(simplify(op(Op, TL, TR))).
print_polynomial_tree(op(Op, TL, TR)) :-
write(TL),
@ -190,7 +203,9 @@ parse_number_explicit(_, T, T, [], []) :-
!.
parse_number(T, SL, NC) :-
parse_number_explicit(void, void, T, SL, NC).
parse_number_explicit(void, void, T1, SL, NC),
polynomial_tree_to_polynomial(T1, PP),
simpoly(PP, T).
%% Tests:
%% ?- parse_number(T, [two], _).
%@ T = 2.
@ -319,20 +334,22 @@ parse_polynomial_explicit(void-_, T) -->
%% ?- parse_polynomial(T, [two, plus, two, plus, one, times, y], _).
%@ T = op(+, op(+, 2, 2), op(*, 1, y)).
parse_command(show(void, T)) --> %% NOTE Probably easier if the tree is always binary
[show],
parse_polynomial(T).
parse_command(show(P, T)) -->
parse_command(show_stored_polynomials) -->
[show, stored, polynomials].
parse_command(store(P, T)) -->
[show],
parse_polynomial(T),
[as],
[P].
parse_command(forget(P)) -->
[forget],
[P].
parse_command(show(void, T)) --> %% NOTE Probably easier if the tree is always binary
[show],
parse_polynomial(T).
parse_command(show(P, void)) -->
[show],
[P].
parse_command(show_all) -->
[show, stored, polynomials].
parse_command(store(P, T)) -->
[let],
[P],
@ -351,6 +368,12 @@ parse_command(op(*, TN, TP)) -->
parse_number(TN),
[by],
parse_polynomial(TP).
parse_command(op(+, TN, TP)) -->
[add],
parse_polynomial(TN),
[with],
parse_polynomial(TP).
parse_input(command(TCL, TCR)) -->
parse_command(TCL),
[and],
@ -368,7 +391,8 @@ parse_input(void, [], _).
nlp_print_memory([nm(X,Y)|T]) :-
write(X),
write(" = "),
writeln(Y),
print_polynomial_tree(Y),
nl,
nlp_print_memory(T).
nlp_print_memory([]).