]> git.donarmstrong.com Git - lilypond.git/blob - scm/define-event-classes.scm
* lily/ various: Introduce stream events of types Prepare,
[lilypond.git] / scm / define-event-classes.scm
1 ;;;; stream-event-classes.scm -- define the tree of stream-event classes.
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;;
5 ;;;; (c) 2005-2006 Erik Sandberg <mandolaerik@gmail.com>
6
7
8 (use-modules (srfi srfi-1))
9
10 ;; Event class hierarchy. Each line is on the form ((List of children) . Parent)
11 (define event-classes
12   '(((StreamEvent) . '())
13     ((RemoveContext ChangeParent Override Revert UnsetProperty SetProperty 
14       MusicEvent CreateContext Prepare OneTimeStep Finish) . StreamEvent)
15     ((Announcement) . '())
16     ((AnnounceNewContext) . Announcement)
17     ))
18
19 ;; Maps event-class to a list of ancestors (inclusive)
20 ;; TODO: use resizable hash
21 (define ancestor-lookup (make-hash-table 1))
22
23 ;; Each class will be defined as
24 ;; (class parent grandparent .. )
25 ;; so that (eq? (cdr class) parent) holds.
26 (for-each
27  (lambda (rel)
28    (for-each
29     (lambda (type)
30       (hashq-set! ancestor-lookup type (cons type (hashq-ref ancestor-lookup (cdr rel) '())))) ;; `(define ,type (cons ',type ,(cdr rel)))))
31     (car rel)))
32  event-classes)
33
34 ;; TODO: Allow entering more complex classes, by taking unions.
35 (define-public (ly:make-event-class leaf)
36  (hashq-ref ancestor-lookup leaf))
37 ;; (primitive-eval leaf))
38
39 (defmacro-public make-stream-event (expr)
40   (Stream_event::undump (primitive-eval (list 'quasiquote expr))))
41
42 (define* (simplify e)
43   (cond
44    ;; Special case for lists reduces stack consumption.
45    ((list? e) (map simplify e))
46    ((pair? e) (cons (simplify (car e))
47                     (simplify (cdr e))))
48    ((ly:stream-event? e)
49     (list 'unquote `(make-stream-event ,(simplify (Stream_event::dump e)))))
50    ((ly:music? e)
51     (list 'unquote (music->make-music e)))
52    ((ly:moment? e)
53     (list 'unquote `(ly:make-moment
54                      ,(ly:moment-main-numerator e)
55                      ,(ly:moment-main-denominator e)
56                      . ,(if (eq? 0 (ly:moment-grace-numerator e))
57                             '()
58                             (list (ly:moment-grace-numerator e)
59                                   (ly:moment-grace-denominator e))))))
60    ((ly:duration? e)
61     (list 'unquote `(ly:make-duration
62                      ,(ly:duration-log e)
63                      ,(ly:duration-dot-count e)
64                      ,(car (ly:duration-factor e))
65                      ,(cdr (ly:duration-factor e)))))
66    ((ly:pitch? e)
67     (list 'unquote `(ly:make-pitch
68                      ,(ly:pitch-octave e)
69                      ,(ly:pitch-notename e)
70                      ,(ly:pitch-alteration e))))
71    ((ly:input-location? e)
72     (list 'unquote '(ly:dummy-input-location)))
73    (#t e)))
74
75 (define-public (ly:simplify-scheme e)
76   (list 'quasiquote (simplify e))
77 )
78
79 ; used by lily/dispatcher.cc
80 (define-public (car< a b) (< (car a) (car b)))