aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormryouse2022-07-21 20:01:10 +0000
committermryouse2022-07-21 20:01:10 +0000
commit6374dfe7cd7af01a5270d49f5e314941c36090a5 (patch)
treedc8b64f86ef112abf2db8d26a45fad8cfde3eac0
parent076fb364c686b8a483be614b5b6f5f3250a9b0ef (diff)
refactor: inner functions to outer hidden functions
-rw-r--r--libs/fstring.neb115
1 files changed, 50 insertions, 65 deletions
diff --git a/libs/fstring.neb b/libs/fstring.neb
index 0147965..32ce56c 100644
--- a/libs/fstring.neb
+++ b/libs/fstring.neb
@@ -4,70 +4,55 @@
;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
-
- (def digits (split "0123456789"))
- (def aligns (split "^<>"))
-
- (func repeat (cnt)
- (def out "")
- (for-count cnt
- (redef out (concat out " ")))
- out)
-
- (func special-parse (str cur val)
- (if (eq? "}" (first str))
+(func .fmt-parse (str cur)
+ (branch
+ ((eq? 0 (length str)) cur)
+ ((eq? "{" (first str))
(block
- ; [align][width] (to start)
- (def align-width
- (if (in? (first cur) aligns)
- (list (first cur) (string->int (rest cur)))
- (list "<" (string->int cur))))
- (branch
- ((>= (length val) (last align-width))
- (list (rest str) val))
- ((eq? "<" (first align-width)) ; align left
- (list (rest str) (concat val (repeat (- (last align-width) (length val))))))
- ((eq? ">" (first align-width)) ; align right
- (list (rest str) (concat (repeat (- (last align-width) (length val))) val)))
- (#true ; align center
- (block
- (def half (/ (- (last align-width) (length val)) 2))
- (if (int? half)
- (list (rest str) (concat (repeat half) val (repeat half)))
- (list (rest str) (concat (repeat (floor half)) val (repeat (+ 1 (floor half))))))))))
-
- (special-parse (rest str) (concat cur (first str)) val)))
+ (def tmp (.brace-parse (rest str) ""))
+ (.fmt-parse (first tmp) (concat cur (first (rest tmp))))))
+ (#true (.fmt-parse (rest str) (concat cur (first str))))))
+
+(func .brace-parse :[:string] (str :string cur :string)
+ ; returns (remaining value)
+ (branch
+ ((eq? "}" (first str))
+ (list (rest str) (->string (eval (first (parse-neb cur))))))
+ ((eq? "," (first str))
+ (.special-parse (rest str) "" (->string (eval (first (parse-neb cur))))))
+ (#true
+ (.brace-parse (rest str) (concat cur (first str))))))
+
+(func .special-parse (str cur val)
+ (if (eq? "}" (first str))
+ (block
+ ; [align][width] (to start)
+ (def align-width
+ (if (in? (first cur) (split "^<>"))
+ (list (first cur) (string->int (rest cur)))
+ (list "<" (string->int cur))))
+ (branch
+ ((>= (length val) (last align-width))
+ (list (rest str) val))
+ ((eq? "<" (first align-width)) ; align left
+ (list (rest str) (concat val (.repeat (- (last align-width) (length val))))))
+ ((eq? ">" (first align-width)) ; align right
+ (list (rest str) (concat (.repeat (- (last align-width) (length val))) val)))
+ (#true ; align center
+ (block
+ (def half (/ (- (last align-width) (length val)) 2))
+ (if (int? half)
+ (list (rest str) (concat (.repeat half) val (.repeat half)))
+ (list (rest str) (concat (.repeat (floor half)) val (.repeat (+ 1 (floor half))))))))))
+
+ (.special-parse (rest str) (concat cur (first str)) val)))
+
+(func .repeat (cnt)
+ (def out "")
+ (for-count cnt
+ (redef out (concat out " ")))
+ out)
-
- (func brace-parse :[:string] (str :string cur :string)
- ; returns (remaining value)
- (branch
- ((eq? "}" (first str))
- (list (rest str) (->string (eval (first (parse-neb cur))))))
- ((eq? "," (first str))
- (special-parse (rest str) "" (->string (eval (first (parse-neb cur))))))
- (#true
- (brace-parse (rest str) (concat cur (first str))))))
-
- (func inner-parse (str cur)
- (branch
- ((eq? 0 (length str)) cur)
- ((eq? "{" (first str))
- (block
- (def tmp (brace-parse (rest str) ""))
- (inner-parse (first tmp) (concat cur (first (rest tmp))))))
- (#true (inner-parse (rest str) (concat cur (first str))))))
-
- (inner-parse inp ""))
+(func fmt :string (inp :string)
+ ; { <neb code> [, [[^<>][:digits:]]]}
+ (.fmt-parse inp ""))