picrin/tools/mkeval.scm

67 lines
1.7 KiB
Scheme
Raw Normal View History

2017-04-03 11:02:00 -04:00
(define (generate-rom)
2017-03-23 09:47:55 -04:00
2017-04-15 02:45:28 -04:00
(define open-output-string open-output-bytevector)
(define (get-output-string port)
(list->string (map integer->char (bytevector->list (get-output-bytevector port)))))
2017-04-03 09:09:19 -04:00
(define (with-output-to-string thunk)
(let ((port (open-output-string)))
(parameterize ((current-output-port port))
(thunk)
(let ((s (get-output-string port)))
(close-port port)
s))))
(define text
(with-output-to-string
(lambda ()
2017-04-14 10:40:07 -04:00
(write (read)))))
2017-03-23 09:47:55 -04:00
2017-04-03 09:09:19 -04:00
(define (escape-string s)
(with-output-to-string
(lambda ()
(string-for-each
(lambda (c)
(case c
2017-04-15 02:45:28 -04:00
((#\\) (display "\\\\"))
((#\") (display "\\\""))
((#\newline) (display "\\n"))
(else (display c))))
2017-04-03 09:09:19 -04:00
s))))
2017-03-23 09:47:55 -04:00
2017-04-03 09:09:19 -04:00
(define (group-string i s)
(let loop ((t s) (n (string-length s)) (acc '()))
(if (= n 0)
(reverse acc)
(if (< n i)
(loop "" 0 (cons t acc))
(loop (string-copy t i) (- n i) (cons (string-copy t 0 i) acc))))))
(define lines (map escape-string (group-string 80 text)))
(let loop ((lines lines) (acc ""))
(if (null? lines)
acc
(loop (cdr lines) (string-append acc "\"" (car lines) "\",\n")))))
2017-03-23 09:47:55 -04:00
(for-each
2017-04-03 13:56:50 -04:00
display
`("#include \"picrin.h\"\n"
"#include \"picrin/extra.h\"\n"
"\n"
2017-04-04 06:00:37 -04:00
"#if PIC_USE_EVAL\n"
2017-04-14 10:58:02 -04:00
"static const char eval_rom[][80] = {\n"
2017-04-03 11:02:00 -04:00
,(generate-rom)
2017-04-03 13:56:50 -04:00
"};\n"
"#endif\n"
"\n"
"void\n"
2017-04-14 10:58:02 -04:00
"pic_init_eval(pic_state *PIC_UNUSED(pic))\n"
2017-04-03 13:56:50 -04:00
"{\n"
2017-04-04 06:00:37 -04:00
"#if PIC_USE_EVAL\n"
2017-04-14 10:58:02 -04:00
" pic_load_native(pic, &eval_rom[0][0]);\n"
2017-04-03 13:56:50 -04:00
"#endif\n"
"}\n"))
2017-03-23 09:47:55 -04:00