aboutsummaryrefslogtreecommitdiff
path: root/libs/fstring.neb
blob: 5edddcafc848f5fc3ca513b4a0bbc53035032f07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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 ""))