]> git.donarmstrong.com Git - lilypond.git/blob - scm/pysk.scm
e999e00d257dad970f91543c3d7ae8e7f79bab85
[lilypond.git] / scm / pysk.scm
1 ;;; pysk.scm -- implement Python  output routines (for Sketch)
2 ;;;
3 ;;;  source file of the GNU LilyPond music typesetter
4 ;;; 
5 ;;; (c) 1998--2001 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8
9
10 (define-module (scm pysk)
11   :export (pysk-output-expression)
12   :no-backtrace
13   )
14
15 (use-modules (scm ps)
16              (ice-9 regex)
17              (ice-9 string-fun)
18              (guile-user)
19              (guile)
20              )
21
22 (define this-module (current-module))
23 (define (pysk-output-expression expr port)
24   (display (pythonify expr) port )
25   )
26
27 (define (ly-warn s) (display s))
28
29 (define (pythonify q)
30   (cond
31    ((string? q) (py-str q))
32    ((symbol? q) (py-str (symbol->string q)))
33    ((and (pair?  q)
34          (not (pair? (cdr q)))
35          (not (eq? '() (cdr q)))
36          ) (py-tuple q))
37    ((pair? q) (py-listify q))
38    ((number? q) (number->string q))
39    ((eq? q '()) '())
40    (else (begin
41            (ly-warn "Unknown object to pythonify:")
42            (write q)
43            (newline)
44            )
45   )))
46
47 (define (py-str s)
48   (string-append "'" s "'")
49   )
50
51 (define (py-tuple q)
52   (string-append "(" (pythonify (car  q)) "," (pythonify (cdr q)) ")")
53   )
54
55 (define (reduce-list list between)
56   "Create new list, inserting BETWEEN between elements of LIST"
57   (if (null? list)
58       '()
59       (if (null? (cdr list))
60           list
61           (cons (car list)
62                 (cons between (reduce-list (cdr list) between)))
63   
64   )))
65
66 (define (string-join str-list sep)
67   (apply string-append (reduce-list str-list sep))
68   )
69
70 (define (my-map f l)
71   (if (null? l)
72       '()
73       (if (pair? (cdr l))
74           (cons (f (car l)) (my-map f (cdr l)))
75           (cons (f (car l)) (f (cdr l)))
76           )
77   ))
78
79 (define (tuplify-list lst)
80   (if (null? lst)
81       '()
82       (if (pair? (cdr lst))
83           (cons (car lst) (tuplify-list (cdr lst)))
84           (if (eq? '() (cdr lst))
85               lst
86               (list (string-append "(" (car lst) ", " (cdr lst) ")" ))
87               ))
88           ))
89
90 (define (py-listify q)
91   (string-append
92    "["
93    (string-join
94     (tuplify-list (my-map pythonify q))   ",")
95    "]\n"
96    ))
97
98