Proper handling of multiplication

This commit is contained in:
Hugo Sales 2018-12-20 21:52:17 +00:00
parent 1232fa0b3c
commit 9a181a076a
1 changed files with 20 additions and 19 deletions

View File

@ -82,8 +82,8 @@ polyplay :-
)
)
;
%% Parsing failed
writeln("I didn't understand what you want.")
%% Parsing failed
writeln("Could not parse input, so I didn't understand what you want.")
),
%% Go back to the beginning
polyplay
@ -247,13 +247,8 @@ do_process_input(simplify(PT)) :-
do_process_input(store_multiplication(TN, PT, V)) :-
polynomial_tree_to_polynomial(TN, N),
polynomial_tree_to_polynomial(PT, P),
(
not(number(N)),
scalepoly(N, P, P2)
;
number(N),
scalepoly(P, N, P2)
),
%% Left is always number. Enforced by parser
scalepoly(P, N, P2),
simpoly(P2, SP),
assertz(polynomial_store(V, SP)),
write(V),
@ -265,13 +260,8 @@ do_process_input(multiply(TN, PT)) :-
polynomial_tree_to_polynomial(TN, N),
polynomial_tree_to_polynomial(PT, P),
%% Use function from UI layer
(
not(number(N)),
scalepoly(N, P, P2)
;
number(N),
scalepoly(P, N, P2)
),
%% Left is always the number. Enforced by parser
scalepoly(N, P, P2),
simpoly(P2, SP),
writeln(SP).
do_process_input(op(+, TN, PT)) :-
@ -671,7 +661,6 @@ parse_polynomial(T) -->
%% ?- parse_polynomial(T, [two, plus, x, times, four, plus, y, raised, to, five], _).
%@ T = op(+, 2, op(+, op(*, x, 4), op(^, y, 5))).
%% ?- parse_polynomial(T, [two, plus, two, plus, one, times, y], _).
%@ true.
%@ T = op(+, op(+, 2, 2), op(*, 1, y)).
%% ?- parse_polynomial(T, [polynomial, 2, plus, 3, plus, 4, y], NC).
%@ T = op(+, op(+, 2, 3), op(*, 4, y)),
@ -782,16 +771,28 @@ parse_command(simplify(T)) -->
parse_polynomial(T).
parse_command(store_multiplication(TN, TP, V)) -->
[multiply],
parse_polynomial(TN),
parse_floating_number(TN),
[by],
parse_polynomial(TP),
[as],
[V].
parse_command(store_multiplication(TN, TP, V)) -->
[multiply],
parse_polynomial(TP),
[by],
parse_floating_number(TN),
[as],
[V].
parse_command(multiply(TN, TP)) -->
[multiply],
parse_polynomial(TN),
parse_floating_number(TN),
[by],
parse_polynomial(TP).
parse_command(multiply(TN, TP)) -->
[multiply],
parse_polynomial(TP),
[by],
parse_floating_number(TN).
parse_command(op(-, TN, TP)) -->
[sub],
parse_polynomial(TN),