formatters #14

Merged
zxq9 merged 15 commits from formatters into master 2025-12-22 11:14:20 +09:00
Showing only changes of commit 401f8dec7a - Show all commits

View File

@ -402,11 +402,8 @@ one_gaju() -> 1_000_000_000_000_000_000.
-spec read(Format) -> Result
when Format :: string(),
Result :: {ok, Pucks} | {error, Reason},
Pucks :: integer(),
Reason :: {badarg, Partial :: string(), Rest :: term()}
| {incomplete, Partial :: string(), Rest :: binary()}
| format.
Result :: {ok, Pucks} | error,
Pucks :: integer().
%% @doc
%% Convery any valid string formatted representation and output a value in pucks.
%% This routine can fail in the special case of `ch' style formatting with a single
@ -433,13 +430,12 @@ read([C | Rest])
C =:= $\n ->
read(Rest);
read([C | Rest]) when $0 =< C andalso C =< $9 ->
read(Rest, [C], []);
read(Rest, [C], {[], []});
read([C | Rest]) when $ =< C andalso C =< $ ->
NumC = C - $ + $0,
read(Rest, [NumC], []);
read(Rest, [NumC], {[], []});
read(_) ->
io:format("Barfing~n"),
{error, format}.
error.
read_w_gajus([C | Rest], A) when $0 =< C andalso C =< $9 ->
read_w_gajus(Rest, [C | A]);
@ -462,9 +458,9 @@ read_w_gajus([], A) ->
G = list_to_integer(lists:reverse(A)) * one_gaju(),
{ok, G};
read_w_gajus([C, 32 | Rest], A) ->
read(Rest, [], [{C, A}]);
read(Rest, [], {[{C, A}], []});
read_w_gajus([32, $G | Rest], A) ->
case read(Rest, [], [{$G, A}]) of
case read(Rest, [], {[{$G, A}], []}) of
{ok, P} ->
G = list_to_integer(lists:reverse(A)) * one_gaju(),
{ok, G + P};
@ -472,8 +468,7 @@ read_w_gajus([32, $G | Rest], A) ->
Error
end;
read_w_gajus(_, _) ->
io:format("Derping~n"),
{error, format}.
error.
read_w_pucks([C | Rest], A) when $0 =< C andalso C =< $9 ->
read_w_pucks(Rest, [C | A]);
@ -494,27 +489,44 @@ read([C | Rest], A, S) when $0 =< C andalso C =< $9 ->
read([C | Rest], A, S) when $ =< C andalso C =< $ ->
NumC = C - $ + $0,
read(Rest, [NumC | A], S);
read([$木], A, S) ->
calc([{$木, A} | S]);
read([$木, 32 | Rest], A, S) ->
read(Rest, [], [{$木, A} | S]);
read([$本], A, S) ->
calc([{$本, A} | S]);
read([32, $G], A, S) ->
calc([{$G, A} | S]);
read([32, $G, 32 | Rest], A, S) ->
read(Rest, [], [{$G, A} | S]);
read([32, $P], A, S) ->
calc([{$P, A} | S]);
read([C, 32 | Rest], A, S) ->
read(Rest, [], [{C, A} | S]);
read([C | Rest], A, S) ->
read(Rest, [], [{C, A} | S]);
read([$木], A, {G, P}) ->
calc({[{$木, A} | G], P});
read([$木, 32 | Rest], A, {G, P}) ->
read(Rest, [], {[{$木, A} | G], P});
read([$本], A, {G, []}) ->
calc({[], [{$本, A} | G]});
read([$本], A, {G, P}) ->
calc({G, [{$本, A} | P]});
read([32, $G], A, {G, P}) ->
calc({[{$G, A} | G], P});
read([32, $G, 32 | Rest], A, {G, P}) ->
read(Rest, [], {[{$G, A} | G], P});
read([32, $P], A, {G, []}) ->
calc({[], [{$P, A} | G]});
read([32, $P], A, {G, P}) ->
calc({G, [{$P, A} | P]});
read([C, 32 | Rest], A, {G, []}) ->
read(Rest, [], {[{C, A} | G], []});
read([C, 32 | Rest], A, {G, P}) ->
read(Rest, [], {G, [{C, A} | P]});
read([C | Rest], A, {G, []}) ->
read(Rest, [], {[{C, A} | G], []});
read([C | Rest], A, {G, P}) ->
read(Rest, [], {G, [{C, A} | P]});
read(_, _, _) ->
{error, format}.
error.
calc(S) ->
calc(S, 0).
calc({G, P}) ->
io:format("G: ~tp, P: ~tp~n", [G, P]),
case calc(G, 0) of
{ok, Gajus} ->
case calc(P, 0) of
{ok, Pucks} -> Gajus + Pucks;
error -> error
end;
error ->
error
end.
calc([{_, []} | S], A) ->
calc(S, A);
@ -529,37 +541,23 @@ calc([{M, Cs} | S], A) ->
calc([], A) ->
{ok, A}.
%magnitude_j("") -> 1_0000;
%magnitude_j("") -> 1_0000_0000;
%magnitude_j("") -> 1_0000_0000_0000;
%magnitude_j("") -> 1_0000_0000_0000_0000;
%magnitude_j("") -> 1_0000_0000_0000_0000_0000;
%magnitude_j("") -> 1_0000_0000_0000_0000_0000_0000;
%magnitude_j("") -> 1_0000_0000_0000_0000_0000_0000_0000;
%magnitude_j("") -> 1_0000_0000_0000_0000_0000_0000_0000_0000;
%magnitude_j("") -> 1_0000_0000_0000_0000_0000_0000_0000_0000_0000;
%magnitude_j("") -> 1_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000;
%magnitude_j("") -> 1_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000;
%magnitude_j("") -> 1_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000;
%magnitude_j("") -> one_gaju();
%magnitude_j("") -> 1.
magnitude("") ->
magnitude($木) ->
{ok, one_gaju()};
magnitude("") ->
magnitude($本) ->
{ok, 1};
magnitude("G") ->
magnitude($G) ->
{ok, one_gaju()};
magnitude("P") ->
magnitude($P) ->
{ok, 1};
magnitude(Mark) ->
io:format("Mark: ~ts~n", [[Mark]]),
case rank(Mark, ranks(jp), 1_0000, 1) of
{ok, J} ->
{ok, J};
error ->
case rank(Mark, ranks(metric), 1_000, 1) of
{ok, N} -> {ok, N};
{ok, J} -> {ok, J};
error -> rank(Mark, ranks(heresy), 1_000, 1)
end
end.
@ -570,6 +568,7 @@ rank(Mark, [Mark | _], Magnitude, Sum) ->
rank(Mark, [_ | Rest], Magnitude, Sum) ->
rank(Mark, Rest, Magnitude, Sum * Magnitude);
rank(_, [], _, _) ->
io:format("failure to rank!~n"),
error.