Bug fixing and fixed using stored variables

This commit is contained in:
Hugo Sales 2018-12-20 00:21:07 +00:00
parent b14aeb8893
commit b4a94461a7

View File

@ -37,16 +37,15 @@
* NLP * * NLP *
*******************************/ *******************************/
debug_print(false).
%% polyplay() is det %% polyplay() is det
% %
% Interactive prompt for the NLP Interface % Interactive prompt for the NLP Interface
% %
polyplay :- polyplay :-
%% Replace the prompt and save the old one
prompt(OldPrompt, '> '), prompt(OldPrompt, '> '),
%% Read a line as codes, from the 'user_input' stream
read_line_to_codes(user_input, InCodes), read_line_to_codes(user_input, InCodes),
%% read_string(user_input, "\n", "\r\t ", _, In),
prompt(_, OldPrompt), prompt(_, OldPrompt),
split_string(InCodes, " ", "\r\t", LS), split_string(InCodes, " ", "\r\t", LS),
maplist(name, LA, LS), maplist(name, LA, LS),
@ -64,13 +63,21 @@ polyplay :-
writeln(NC) writeln(NC)
; ;
( (
debug_print(true), NC \== [],
write(LA), write("Syntax error in token: "),
nl, cons(H, _, NC),
write(TIn), write(H),
nl nl
; ;
process_input(TIn) (
debug_print(true),
write(LA),
nl,
write(TIn),
nl
;
process_input(TIn)
)
) )
) )
; ;
@ -80,6 +87,9 @@ polyplay :-
), ),
!. !.
%% Flag which determines whether to print the parsed input or to process it
debug_print(false).
process_input(command(CL, void)) :- process_input(command(CL, void)) :-
do_process_input(CL). do_process_input(CL).
process_input(command(CL, TCR)) :- process_input(command(CL, TCR)) :-
@ -92,12 +102,16 @@ process_input(command(CL, TCR)) :-
do_process_input(show_stored_polynomials) :- do_process_input(show_stored_polynomials) :-
findall(nm(X,Y), polynomial_store(X,Y), D), findall(nm(X,Y), polynomial_store(X,Y), D),
nlp_print_memory(D). print_all_stored_variables(D).
do_process_input(show(store(P), T)) :- do_process_input(show(store(P), T)) :-
( polynomial_store(P, _),
retractall(polynomial_store(P, _)); true ),
!,
assertz(polynomial_store(P, T)), assertz(polynomial_store(P, T)),
write(P), write(P),
write(" = "), write(" = "),
print_polynomial_tree(T), polynomial_tree_to_polynomial(T, Pl),
write(Pl),
nl. nl.
do_process_input(show(load(P), void)) :- do_process_input(show(load(P), void)) :-
P \== void, P \== void,
@ -105,7 +119,8 @@ do_process_input(show(load(P), void)) :-
polynomial_store(P, T), polynomial_store(P, T),
write(P), write(P),
write(" = "), write(" = "),
print_polynomial_tree(T), polynomial_tree_to_polynomial(T, Pl),
write(Pl),
nl nl
; ;
write("Variable not stored"), write("Variable not stored"),
@ -113,14 +128,17 @@ do_process_input(show(load(P), void)) :-
). ).
do_process_input(show(P, T)) :- do_process_input(show(P, T)) :-
P \== void, P \== void,
P \== store(_),
T \== void, T \== void,
write(P), write(P),
write(" = "), write(" = "),
print_polynomial_tree(T), polynomial_tree_to_polynomial(T, Pl),
write(Pl),
nl. nl.
do_process_input(show(void, T)) :- do_process_input(show(void, T)) :-
T \== void, T \== void,
print_polynomial_tree(T), polynomial_tree_to_polynomial(T, Pl),
write(Pl),
nl. nl.
do_process_input(store(P, T)) :- do_process_input(store(P, T)) :-
assertz(polynomial_store(P, T)). assertz(polynomial_store(P, T)).
@ -139,14 +157,19 @@ do_process_input(multiply(TN, PT)) :-
write(SP), write(SP),
nl. nl.
print_polynomial_tree(op(Op, TL, TR)) :-
!, %% print_all_stored_variables
write(TL), %
write(Op), % Prints the stored variables
TR \== void, %
print_polynomial_tree(TR). print_all_stored_variables([nm(X,T) | TS]) :-
print_polynomial_tree(T) :- write(X),
write(T). write(" = "),
polynomial_tree_to_polynomial(T, P),
write(P),
nl,
print_all_stored_variables(TS).
print_all_stored_variables([]).
polynomial_tree_to_polynomial(op(Op, TL, TR), P) :- polynomial_tree_to_polynomial(op(Op, TL, TR), P) :-
!, !,
@ -157,6 +180,9 @@ polynomial_tree_to_polynomial(op(Op, TL, TR), P) :-
atom_concat(TermL, Op, Temp), atom_concat(TermL, Op, Temp),
atom_concat(Temp, TermR, PA), atom_concat(Temp, TermR, PA),
term_to_atom(P, PA). term_to_atom(P, PA).
polynomial_tree_to_polynomial(load(P), Pl) :-
polynomial_store(P, TP),
polynomial_tree_to_polynomial(TP, Pl).
polynomial_tree_to_polynomial(T, T). polynomial_tree_to_polynomial(T, T).
%% Tests: %% Tests:
%% ?- polynomial_tree_to_polynomial(op(+, 2, op(+, 2, op(*, 1, y))), S). %% ?- polynomial_tree_to_polynomial(op(+, 2, op(+, 2, op(*, 1, y))), S).
@ -321,6 +347,7 @@ parse_operation(*) --> [times].
parse_polynomial_operand(T) --> parse_number(T). parse_polynomial_operand(T) --> parse_number(T).
parse_polynomial_operand(T) --> parse_power(T). parse_polynomial_operand(T) --> parse_power(T).
parse_polynomial_operand(load(T)) --> parse_stored_variable(T), { ! }.
:- dynamic polynomial_store/2. :- dynamic polynomial_store/2.
@ -355,9 +382,6 @@ parse_polynomial(T, NC, NC) :-
parse_polynomial(T) --> parse_polynomial(T) -->
parse_polynomial_explicit(_-_, T), parse_polynomial_explicit(_-_, T),
!. !.
parse_polynomial(T) -->
parse_stored_variable(T),
!.
%% Tests: %% Tests:
%% ?- parse_polynomial(T, [], _). %% ?- parse_polynomial(T, [], _).
%@ false. %@ false.
@ -425,7 +449,7 @@ parse_command(show(store(P), T)) -->
parse_polynomial(T), parse_polynomial(T),
[as], [as],
parse_stored_variable(P). parse_stored_variable(P).
parse_command(show(load(P), void)) --> parse_command(show(P, void)) -->
[show], [show],
parse_stored_variable(P). parse_stored_variable(P).
parse_command(show(void, T)) --> parse_command(show(void, T)) -->
@ -475,17 +499,6 @@ parse_input(command(TC, void)) -->
%% ?- parse_input(CT, [show, 3], _). %% ?- parse_input(CT, [show, 3], _).
%@ CT = command(show(void, 3), void). %@ CT = command(show(void, 3), void).
%% nlp_print_memory
%
% Prints the NLP memory
%
nlp_print_memory([nm(X,Y)|T]) :-
write(X),
write(" = "),
print_polynomial_tree(Y),
nl,
nlp_print_memory(T).
nlp_print_memory([]).
/******************************* /*******************************
* UI * * UI *