Skip to content

Instantly share code, notes, and snippets.

@agumonkey agumonkey/fmt.el
Created Mar 14, 2019

Embed
What would you like to do?
fmt.el - ala python string formatting
;;; ala python string formatting
;;;
;;; (let ((some "Object") (i 1)) (f "<{some}... [{i}:{i}>")) => "<Object... [1:1]>
;;;
;;; todo: {...} parser
;;; todo: wrapping defmacro
;;; todo: wrapping defun
;;;
;;; state: alpha²
(defmacro fmt (s)
:todo)
(defmacro example (&rest body) `(quote ,@body))
(example
(let <bindings>
(fmt "{s} ... {t} ... {u}>")))
(defvar *test* "{s} ... {t} ... {u}")
(defun f/parse (s)
(split-string s "[{}]" t ""))
(defun um (s b e)
(let ((r '())
(s (string-to-list s))
(c s))
(cond ((null c) r)
((= b (car c)) (push (jump-to e) r))
(t (push (parse-str ..) r)
(loop ...)))))
(let ((r '(0)))
(push 1 r)
(push 2 r))
(string-to-list *test*)
(defun string-split-at (s p)
(values (substring-no-properties s 0 p)
(substring-no-properties s p)))
(string-split-at "foobar,bazduh" 6)
(defun dewrap (s) (substring-no-properties s 1 (1- (length s))))
(dewrap "{foo}")
(defun x (s)
;; s = data
;; s = subst data
;; s = data <s>
(let ((p (string-match "{+" s)))
(cond ((null p) (list (cons :data s)))
((= 0 p) (let* ((end (string-match "}+" s)))
;;; UNHANDLED FAILURE on end
(cl-multiple-value-bind (a b) (string-split-at s (1+ end))
(cons (cons :subst (dewrap a)) (x b)))))
(t (cl-multiple-value-bind (a b) (string-split-at s p)
(cons (cons :data a) (x b)))))))
(x "<Class >")
(x "{some}...")
(x "...{some}")
(x "...{some}...")
(x "BEGIN...{some}...{some}...{also}. END.")
(x "
;;;;;;;;;; Chapter {chp}
(defpackage :paip-{chp}
(:use common-lisp))
;;; :paip-{chp} ends here.
")
(x "{{{x}}}")
(string-match "{+" "{{{x}}}")
;;; ohh seems to work
(example
(cl-multiple-value-bind (a b) (string-split-at "foobar" 3)
(list :a a :b b)))
(defmacro gen (tokens)
;; data d => (concat d (gen ...))
;; subst s => (let ((,s s)) (gen ...))
(cond ((null tokens) '())
((eq (car (car tokens)) :data) `(concatenate 'string
,(cdr (car tokens))
(gen ,(cdr tokens))))
((eq (car (car tokens)) :subst) `(let ((,(intern (cdr (car tokens)))
,(intern (cdr (car tokens)))))
(gen ,(cdr tokens))))
(t :NOP)))
(example
(let (({foo} 1))
(+ {foo} {foo})))
(let ((tokens (x "data")))
(gen ))
(defun -gen (tokens)
;; data d => (concat d (gen ...))
;; subst s => (let ((,s s)) (gen ...))
(cond ((null tokens) '())
((eq (car (car tokens)) :data) (concatenate 'string
(cdr (car tokens))
(-gen (cdr tokens))))
((eq (car (car tokens)) :subst) (let ((n (cdr (car tokens)))
(v (stringify (valueof n))))
(concatenate 'string (gen (cdr tokens)))))
(t :NOP)))
(defun get (n e)
(if-let (b (assoc n e))
(cdr b)
"?"))
(get "foo" '(("foo" . bar)))
(get "duh" '(("foo" . bar) ("duh" . :meh)))
(get "" '(("foo" . bar)))
(defun str (o)
(if (stringp o) o (format "%S" o)))
(defun xe (s e)
(let ((red (lambda (a b)
(concat a (case (car b)
(:subst (str (get (cdr b) e)))
(:data (cdr b)))))))
(-reduce-from red "" (x s))))
(x "Class {foo} of {bar} is {duh}.")
(xe "Class {foo} of {bar} is {duh}." '(("foo" . bar) ("duh" . :meh)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.