Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format-args improvements #75

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/canary/metta_eval.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1177,19 +1177,19 @@
get_symbol_metatype(_Vl,'%Undefined%','Symbol').
get_symbol_metatype(_Vl,_Want,'Grounded').

as_metta_char(X,'#\\'(X)).

eval_20(Eq,RetType,Depth,Self,['stringToChars',String],Chars):- !, eval_args(Eq,RetType,Depth,Self,String,SS), string_chars(SS,Chars0), maplist(as_metta_char,Chars0,Chars).
eval_20(Eq,RetType,Depth,Self,['charsToString',Chars],String):- !, eval_args(Eq,RetType,Depth,Self,Chars,CC), maplist(as_metta_char,CC0,CC), string_chars(String,CC0).

% =================================================================
% =================================================================
% =================================================================
% FORMAT-ARGS
% STRINGS
% =================================================================
% =================================================================
% =================================================================

as_metta_char(X,'#\\'(X)).

eval_20(Eq,RetType,Depth,Self,['stringToChars',String],Chars):- !, eval_args(Eq,RetType,Depth,Self,String,SS), string_chars(SS,Chars0), maplist(as_metta_char,Chars0,Chars).
eval_20(Eq,RetType,Depth,Self,['charsToString',Chars],String):- !, eval_args(Eq,RetType,Depth,Self,Chars,CC), maplist(as_metta_char,CC0,CC), string_chars(String,CC0).

% We deal with indexing, but not formatting (the stuff following the ':')(yet)
% https://doc.rust-lang.org/std/fmt/ used as a reference

Expand Down Expand Up @@ -1227,11 +1227,14 @@
format_args(FormatRest3, Iterator2, Args).
format_args([C|FormatRest], Iterator, Args) :- put(C), format_args(FormatRest, Iterator, Args).

eval_20(Eq,RetType,Depth,Self,['format-args',Format,Args],Result):- !,
eval_20(Eq,RetType,Depth,Self,['format-args',Format,Args],Result):-
eval_args(Eq,RetType,Depth,Self,Format,EFormat),
eval_args(Eq,RetType,Depth,Self,Args,EArgs),
string_chars(EFormat, FormatChars), user_io(with_output_to(string(Result), format_args(FormatChars, 0, EArgs))).
% string_chars(EFormat, FormatChars), wots(Result, format_args(FormatChars, 0, EArgs)).
is_list(EArgs),string_chars(EFormat, FormatChars), !,
user_io(with_output_to(string(Result), format_args(FormatChars, 0, EArgs))).
eval_20(Eq,RetType,Depth,Self,['format-args',_Fmt,Args],_Result) :-
eval_args(Eq,RetType,Depth,Self,Args,EArgs),
\+ is_list(EArgs),!,throw_metta_return(['Error',Args,'BadType']).

eval_20(Eq,RetType,_Depth,_Self,['flip'],Bool):-
ignore(RetType='Bool'), !, as_tf(random(0,2,0),Bool),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@
!(assertEqual (format-args "Two digit index={11}" (0 1 2 3 4 5 6 7 8 9 10 "eleven")) "Two digit index=eleven")

;; malformed examples
!(assertEqual (format-args "Bad args list {1} {} {0} {}" x) "Bad args list {1} {} {0} {}")
!(assertEqual (format-args "Bad args list {1} {} {0} {}" x) (format-args "Bad args list {1} {} {0} {}" x))

!(assertEqual (format-args "Malformed format}{{}{{{}{} {4} { } {-1} {x} {{{{{{}}}}}}{{{{{}}}}}" ("success1" "success2")) "Malformed format}{}{success1success2 {4} { } {-1} {x} {{{}}}{{{}}}")
Loading