aboutsummaryrefslogtreecommitdiff
path: root/temper.lisp
blob: 68930711751d91720f91bb0d44461284ea499ce3 (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
(defun read-all ()
  (let ((line (read-line *standard-input* nil)))
    (if line
      (concatenate 'string line '(#\linefeed) (read-all))
      "")))


(defconstant +temper-open+ "<%")
(defconstant +temper-close+ "%>")
(defconstant +temper-close-len+ (length +temper-close+))

(defun lex (input)
  (let ((pos (search +temper-open+ input)))
    (if (null pos)
      (list (list 'string input))
      (let* ((before    (subseq input 0 pos))
             (input     (subseq input pos))
             (end-pos   (search +temper-close+ input))
             (code      (subseq input +temper-close-len+ end-pos))
             (input     (subseq input (+ end-pos +temper-close-len+))))
        (append (list (list 'string before) (list 'code code)) (lex input))))))


(defun generate (tokens)
  `(progn
    (setq buf "")
    (eval ,(read-from-string
      (concatenate 'string
        (map
          'string
          #'(lambda (token)
             (let ((token-type    (first token))
                   (token-content (second token)))
                 (if (eql token-type 'string)
                   (format nil "~A"
                     `(setf buf (concatenate 'string buf ,token-content)))
                   token-content)))
          tokens))))))


(setq res (generate (lex (read-all))))
(setq str (format t "~{~A~^ ~}" res))

; (princ str)

; (print (eval (read-from-string str)))

; (read-from-string (lex (read-all)))