blob: 2007a3b4749061a0cf2b6d5c422453e05b9f0dcb (
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
59
60
61
62
|
(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 tokens-to-code-string (tokens)
(apply #'concatenate 'string
(map
'list
#'(lambda (token)
(let ((token-type (first token))
(token-content (second token)))
(if (eql token-type 'string)
(format nil "~S"
`(setf buf (concatenate
'string
buf
,(string-trim '(#\linefeed #\space) token-content)
'(#\linefeed))))
token-content)))
tokens)))
(defun generate (tokens)
`(progn
(setq buf "")
(eval ,(read-from-string
(concatenate 'string
"(progn "
(tokens-to-code-string tokens)
")")))))
(setq res (generate (lex (read-all))))
(format t "~A" (eval res))
; (setq str (format t "~{~S~^ ~}" res))
; (princ str)
; (print (eval (read-from-string str)))
; (read-from-string (lex (read-all)))
|