diff options
| author | mryouse | 2022-07-12 21:51:13 +0000 |
|---|---|---|
| committer | mryouse | 2022-07-12 21:51:13 +0000 |
| commit | 4d6a16e5a3059d2fd17b64e19538f1463e40b0cb (patch) | |
| tree | efc2c03f267d752bc767a7f36664ff8dc4e0827e | |
| parent | 7732a2a3f4d7171f253bf663d85006e8a69bea2a (diff) | |
initial commit of an f-string function
| -rw-r--r-- | libs/fstring.neb | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/libs/fstring.neb b/libs/fstring.neb new file mode 100644 index 0000000..5edddca --- /dev/null +++ b/libs/fstring.neb @@ -0,0 +1,35 @@ + +; bugs: +;#16> (fmt "{x} x {") +;exception! <class 'NameError'> name 'ev' is not defined + + +(func fmt :string (inp :string) + ; need two variables + ; 1. the current processed string + ; 2. the neb code to run + ; + ; algorithm + ; - read characters one by one, adding to result + ; - if reach a { + ; - read characters one by one, adding to neb code + ; - when reach }, try to execute the neb code, + ; then append to the end of the result + ; - if the end of the string is successfully reached, return the result + + (func brace-parse :[:string] (str :string cur :string) + ; returns (remaining value) + (if (eq? "}" (first-char str)) + (list (rest-char str) (->string (eval (parse-neb cur)))) + (brace-parse (rest-char str) (concat cur (first-char str))))) + + (func inner-parse (str cur) + (branch + ((eq? 0 (length str)) cur) + ((eq? "{" (first-char str)) + (block + (def tmp (brace-parse (rest-char str) "")) + (inner-parse (first tmp) (concat cur (first (rest tmp)))))) + (#true (inner-parse (rest-char str) (concat cur (first-char str)))))) + + (inner-parse inp "")) |
