Prolog "format"

Admin User, erstellt 19. Dez. 2023
         
/**
* Modern Albufeira Prolog Interpreter
*
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
*
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
*
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
*
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
:-(ensure_loaded(library(/(util, charsio)))).
:-(tab(N), ','(current_output(S), tab(S, N))).
:-(tab(S, N), ','(sys_check_integer(N), ;(->(>(N, 0), sys_tab(N, S)), true))).
:-(sys_tab(0, _), !).
:-(sys_tab(N, S), ','(put_code(S, 0' ), ','(is(M, -(N, 1)), sys_tab(M, S)))).
:-(format(T, L), ','(current_output(S), format(S, T, L))).
:-(format(S, T, L), ','(atom_codes(T, C), sys_format(L, S, C, []))).
:-(format_atom(T, L, A), ','(open_output_atom_stream(K), ','(format(K, T, L), ','(flush_output(K), close_output_atom_stream(K, A))))).
-->(sys_format(L, S), ','(sys_format_fill(F), ','({}(','(atom_codes(A, F), put_atom(S, A))), sys_format_more(L, S)))).
-->(sys_format_more(V, _), {}(','(var(V), throw(error(instantiation_error, _))))).
-->(sys_format_more('.'(X, L), S), ','(!, ','(sys_format_rest, ','(sys_format_char(C), ','(sys_format_unpara(C, X, S), sys_format(L, S)))))).
-->(sys_format_more([], _), ','(!, sys_format_eof)).
-->(sys_format_more(L, _), {}(throw(error(type_error(list, L), _)))).
-->(sys_format_eof, ','('.'(_, []), {}(throw(error(syntax_error(superflous_format), _))))).
-->(sys_format_eof, []).
-->(sys_format_rest, ','('.'(_, []), !)).
-->(sys_format_rest, {}(throw(error(syntax_error(format_missing), _)))).
-->(sys_format_unpara(C, X, S), ','({}(','(=<(0'0, C), =<(C, 0'9))), ','(!, ','({}(is(N, -(C, 0'0))), ','(sys_format_char(D), sys_format_para(D, N, X, S)))))).
-->(sys_format_unpara(0'a, X, S), ','(!, {}(','(xml_escape(X, A), put_atom(S, A))))).
-->(sys_format_unpara(0'c, X, S), ','(!, {}(','(percent_encode(X, A), put_atom(S, A))))).
-->(sys_format_unpara(0'd, X, S), ','(!, {}(','(is(H, X), ','(atom_integer(A, 10, H), put_atom(S, A)))))).
-->(sys_format_unpara(0'e, X, S), ','(!, {}(','(is(H, X), ','(sys_float_atom(H, e, 6, A), put_atom(S, A)))))).
-->(sys_format_unpara(0'f, X, S), ','(!, {}(','(is(H, X), ','(sys_float_atom(H, f, 6, A), put_atom(S, A)))))).
-->(sys_format_unpara(0'g, X, S), ','(!, {}(','(is(H, X), ','(sys_float_atom(H, g, 6, A), put_atom(S, A)))))).
-->(sys_format_unpara(0'k, X, S), ','(!, {}(write_canonical(S, X)))).
-->(sys_format_unpara(0'q, X, S), ','(!, {}(writeq(S, X)))).
-->(sys_format_unpara(0'r, X, S), ','(!, {}(','(is(H, X), ','(atom_integer(A, 8, H), put_atom(S, A)))))).
-->(sys_format_unpara(0'w, X, S), ','(!, {}(write(S, X)))).
-->(sys_format_unpara(C, _, _), {}(','(char_code(S, C), throw(error(domain_error(illegal_format, S), _))))).
-->(sys_format_para(C, N, X, S), ','({}(','(=<(0'0, C), =<(C, 0'9))), ','(!, ','({}(is(M, -(+(*(N, 10), C), 0'0))), ','(sys_format_char(D), sys_format_para(D, M, X, S)))))).
-->(sys_format_para(0'e, N, X, S), ','(!, {}(','(is(H, X), ','(sys_float_atom(H, e, N, A), put_atom(S, A)))))).
-->(sys_format_para(0'f, N, X, S), ','(!, {}(','(is(H, X), ','(sys_float_atom(H, f, N, A), put_atom(S, A)))))).
-->(sys_format_para(0'g, N, X, S), ','(!, {}(','(is(H, X), ','(sys_float_atom(H, g, N, A), put_atom(S, A)))))).
-->(sys_format_para(0'r, N, X, S), ','(!, {}(','(is(H, X), ','(atom_integer(A, N, H), put_atom(S, A)))))).
-->(sys_format_para(C, _, _, _), {}(','(char_code(S, C), throw(error(domain_error(illegal_format, S), _))))).
-->(sys_format_char(C), ','('.'(C, []), !)).
-->(sys_format_char(_), {}(throw(error(syntax_error(char_missing), _)))).
-->(sys_format_fill('.'(X, F)), ','('.'(X, []), ','({}(=\=(X, 0'~)), ','(!, sys_format_fill(F))))).
-->(sys_format_fill('.'(0'~, F)), ','('.'(0'~, '.'(0'~, [])), ','(!, sys_format_fill(F)))).
-->(sys_format_fill('.'(0'\n, F)), ','('.'(0'~, '.'(0'n, [])), ','(!, sys_format_fill(F)))).
-->(sys_format_fill([]), []).
:-(ensure_loaded(foreign(/(util, strlib)))).
:-(multifile(/(strings, 3))).
strings('syntax_error.superflous_format', de, 'Nicht genug Argumente.').
strings('syntax_error.format_missing', de, 'Zu viele Argumente.').
strings('syntax_error.char_missing', de, 'Ungültiger Formatbezeichner.').
strings('syntax_error.superflous_format', '', 'Not enough arguments.').
strings('syntax_error.format_missing', '', 'Too many arguments.').
strings('syntax_error.char_missing', '', 'Illegal format specifier.').