blob: 32ce56c1902ae21e61228fc9563d5162eae7dc9b (
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
; bugs:
;#16> (fmt "{x} x {")
;exception! <class 'NameError'> name 'ev' is not defined
(func .fmt-parse (str cur)
(branch
((eq? 0 (length str)) cur)
((eq? "{" (first str))
(block
(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 fmt :string (inp :string)
; { <neb code> [, [[^<>][:digits:]]]}
(.fmt-parse inp ""))
|