From 6a0607b93a53ff891daab52a9069d8c239ad4220 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 18 Jul 2025 05:36:38 -0400 Subject: [PATCH] sample-code: Add a super hacky format-string implementation using `eval` and `fmt` builtins --- sample-code/fstring.cl | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 sample-code/fstring.cl diff --git a/sample-code/fstring.cl b/sample-code/fstring.cl new file mode 100644 index 0000000..c3d91f3 --- /dev/null +++ b/sample-code/fstring.cl @@ -0,0 +1,32 @@ + +fn f(__fmt: str) -> str { + let __out = ""; + let __expr = ""; + let __depth = 0; + for __c in chars(__fmt) { + match __c { + '{' => { + __depth += 1 + if __depth <= 1 { + continue + } + }, + '}' => { + __depth -= 1 + if __depth <= 0 { + __out += fmt(eval(__expr)) + __expr = "" + continue + } + }, + ':' => if __depth == 1 { + __out += __expr + ": " + }, + _ => {} + } + if __depth > 0 { + __expr += __c + } else __out += __c; + } + __out +}