Bug fixing and fixed using stored variables
This commit is contained in:
parent
b14aeb8893
commit
b4a94461a7
85
polymani.pl
85
polymani.pl
@ -37,16 +37,15 @@
|
||||
* NLP *
|
||||
*******************************/
|
||||
|
||||
debug_print(false).
|
||||
|
||||
%% polyplay() is det
|
||||
%
|
||||
% Interactive prompt for the NLP Interface
|
||||
%
|
||||
polyplay :-
|
||||
%% Replace the prompt and save the old one
|
||||
prompt(OldPrompt, '> '),
|
||||
%% Read a line as codes, from the 'user_input' stream
|
||||
read_line_to_codes(user_input, InCodes),
|
||||
%% read_string(user_input, "\n", "\r\t ", _, In),
|
||||
prompt(_, OldPrompt),
|
||||
split_string(InCodes, " ", "\r\t", LS),
|
||||
maplist(name, LA, LS),
|
||||
@ -64,13 +63,21 @@ polyplay :-
|
||||
writeln(NC)
|
||||
;
|
||||
(
|
||||
debug_print(true),
|
||||
write(LA),
|
||||
nl,
|
||||
write(TIn),
|
||||
NC \== [],
|
||||
write("Syntax error in token: "),
|
||||
cons(H, _, NC),
|
||||
write(H),
|
||||
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)) :-
|
||||
do_process_input(CL).
|
||||
process_input(command(CL, TCR)) :-
|
||||
@ -92,12 +102,16 @@ process_input(command(CL, TCR)) :-
|
||||
|
||||
do_process_input(show_stored_polynomials) :-
|
||||
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)) :-
|
||||
( polynomial_store(P, _),
|
||||
retractall(polynomial_store(P, _)); true ),
|
||||
!,
|
||||
assertz(polynomial_store(P, T)),
|
||||
write(P),
|
||||
write(" = "),
|
||||
print_polynomial_tree(T),
|
||||
polynomial_tree_to_polynomial(T, Pl),
|
||||
write(Pl),
|
||||
nl.
|
||||
do_process_input(show(load(P), void)) :-
|
||||
P \== void,
|
||||
@ -105,7 +119,8 @@ do_process_input(show(load(P), void)) :-
|
||||
polynomial_store(P, T),
|
||||
write(P),
|
||||
write(" = "),
|
||||
print_polynomial_tree(T),
|
||||
polynomial_tree_to_polynomial(T, Pl),
|
||||
write(Pl),
|
||||
nl
|
||||
;
|
||||
write("Variable not stored"),
|
||||
@ -113,14 +128,17 @@ do_process_input(show(load(P), void)) :-
|
||||
).
|
||||
do_process_input(show(P, T)) :-
|
||||
P \== void,
|
||||
P \== store(_),
|
||||
T \== void,
|
||||
write(P),
|
||||
write(" = "),
|
||||
print_polynomial_tree(T),
|
||||
polynomial_tree_to_polynomial(T, Pl),
|
||||
write(Pl),
|
||||
nl.
|
||||
do_process_input(show(void, T)) :-
|
||||
T \== void,
|
||||
print_polynomial_tree(T),
|
||||
polynomial_tree_to_polynomial(T, Pl),
|
||||
write(Pl),
|
||||
nl.
|
||||
do_process_input(store(P, T)) :-
|
||||
assertz(polynomial_store(P, T)).
|
||||
@ -139,14 +157,19 @@ do_process_input(multiply(TN, PT)) :-
|
||||
write(SP),
|
||||
nl.
|
||||
|
||||
print_polynomial_tree(op(Op, TL, TR)) :-
|
||||
!,
|
||||
write(TL),
|
||||
write(Op),
|
||||
TR \== void,
|
||||
print_polynomial_tree(TR).
|
||||
print_polynomial_tree(T) :-
|
||||
write(T).
|
||||
|
||||
%% print_all_stored_variables
|
||||
%
|
||||
% Prints the stored variables
|
||||
%
|
||||
print_all_stored_variables([nm(X,T) | TS]) :-
|
||||
write(X),
|
||||
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) :-
|
||||
!,
|
||||
@ -157,6 +180,9 @@ polynomial_tree_to_polynomial(op(Op, TL, TR), P) :-
|
||||
atom_concat(TermL, Op, Temp),
|
||||
atom_concat(Temp, TermR, 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).
|
||||
%% Tests:
|
||||
%% ?- 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_power(T).
|
||||
parse_polynomial_operand(load(T)) --> parse_stored_variable(T), { ! }.
|
||||
|
||||
:- dynamic polynomial_store/2.
|
||||
|
||||
@ -355,9 +382,6 @@ parse_polynomial(T, NC, NC) :-
|
||||
parse_polynomial(T) -->
|
||||
parse_polynomial_explicit(_-_, T),
|
||||
!.
|
||||
parse_polynomial(T) -->
|
||||
parse_stored_variable(T),
|
||||
!.
|
||||
%% Tests:
|
||||
%% ?- parse_polynomial(T, [], _).
|
||||
%@ false.
|
||||
@ -425,7 +449,7 @@ parse_command(show(store(P), T)) -->
|
||||
parse_polynomial(T),
|
||||
[as],
|
||||
parse_stored_variable(P).
|
||||
parse_command(show(load(P), void)) -->
|
||||
parse_command(show(P, void)) -->
|
||||
[show],
|
||||
parse_stored_variable(P).
|
||||
parse_command(show(void, T)) -->
|
||||
@ -475,17 +499,6 @@ parse_input(command(TC, void)) -->
|
||||
%% ?- parse_input(CT, [show, 3], _).
|
||||
%@ 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 *
|
||||
|
Reference in New Issue
Block a user