From e5db8a4b2518a0b8799174267698cbfa7c738d56 Mon Sep 17 00:00:00 2001 From: Roy Ward Date: Wed, 14 Aug 2024 10:25:01 -0700 Subject: [PATCH 1/2] fix to format-args with bad args --- src/canary/metta_eval.pl | 3 +-- .../baseline_compat/hyperon-mettalog_sanity/string-tests.metta | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/canary/metta_eval.pl b/src/canary/metta_eval.pl index 36c4a414a17..d685d6652c6 100755 --- a/src/canary/metta_eval.pl +++ b/src/canary/metta_eval.pl @@ -1230,8 +1230,7 @@ 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,['flip'],Bool):- ignore(RetType='Bool'), !, as_tf(random(0,2,0),Bool), diff --git a/tests/baseline_compat/hyperon-mettalog_sanity/string-tests.metta b/tests/baseline_compat/hyperon-mettalog_sanity/string-tests.metta index 9bbbbc24d78..782ae9826aa 100644 --- a/tests/baseline_compat/hyperon-mettalog_sanity/string-tests.metta +++ b/tests/baseline_compat/hyperon-mettalog_sanity/string-tests.metta @@ -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} {{{}}}{{{}}}") From 1e98bc5192058f7b1afe2a6e0c1f1b105161a12a Mon Sep 17 00:00:00 2001 From: Roy Ward Date: Wed, 14 Aug 2024 14:50:14 -0700 Subject: [PATCH 2/2] cleaned up code section header, error checking for format-args --- src/canary/metta_eval.pl | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/canary/metta_eval.pl b/src/canary/metta_eval.pl index d685d6652c6..0fc69298000 100755 --- a/src/canary/metta_eval.pl +++ b/src/canary/metta_eval.pl @@ -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 @@ -1227,10 +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), - is_list(EArgs),string_chars(EFormat, FormatChars), user_io(with_output_to(string(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),