]> git.donarmstrong.com Git - lilypond.git/blob - lily/simple-closure.cc
* lily/rest.cc (y_offset_callback): merge function of 3 callbacks.
[lilypond.git] / lily / simple-closure.cc
1 /*
2   closure.cc -- chained closures.
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 */
9
10 #include "lily-guile.hh"
11
12 static scm_t_bits simple_closure_tag;
13
14 bool
15 is_simple_closure (SCM s)
16 {
17   return (SCM_NIMP (s) && SCM_CELL_TYPE (s) == simple_closure_tag);
18 }
19
20 SCM
21 simple_closure_expression (SCM smob)
22 {
23   assert (is_simple_closure (smob));
24   return (SCM) SCM_CELL_WORD_1(smob);
25 }
26
27 SCM evaluate_with_simple_closure (SCM delayed_argument, SCM expr);
28
29 SCM
30 evaluate_args (SCM delayed_argument, SCM args)
31 {
32   SCM new_args = SCM_EOL;
33   SCM *tail = &new_args;
34   for (SCM s = args; scm_is_pair (s); s = scm_cdr (s))
35     {
36       *tail = scm_cons (evaluate_with_simple_closure (delayed_argument, scm_car (s)),
37                         SCM_EOL);
38       tail = SCM_CDRLOC (*tail);
39     }
40   
41   return new_args;
42 }
43
44 SCM
45 evaluate_with_simple_closure (SCM delayed_argument,
46                               SCM expr)
47 {
48   if (is_simple_closure (expr))
49     {
50       SCM inside = simple_closure_expression (expr);
51       return scm_apply_1 (scm_car (inside),
52                           delayed_argument,
53                           evaluate_args (delayed_argument, scm_cdr (inside)));
54     }
55   else if (!scm_is_pair (expr))
56     return expr;
57   else if (scm_car (expr) == ly_symbol2scm ("quote"))
58     return scm_cadr (expr);
59   else
60     {
61       return scm_apply_0 (scm_car (expr), evaluate_args (delayed_argument, scm_cdr (expr)));
62     }
63
64   assert (false);
65   return SCM_EOL;
66 }
67
68 LY_DEFINE(ly_simple_closure_p, "ly:simple-closure?",
69           1,0,0, (SCM clos),
70           "Type predicate.")
71 {
72   return scm_from_bool (is_simple_closure (clos));
73 }
74
75 LY_DEFINE(ly_make_simple_closure, "ly:make-simple-closure",
76           0, 0, 1, (SCM expr),
77           "Make a simple closure. @var{expr} should be form of "
78           "@code{(@var{func} @var{a1} @var{A2} ...)}, and will be invoked "
79           "as @code{(@var{func} @var{delayed-arg} @var{a1} @var{a2} ... )}.")
80 {
81   SCM z;
82
83   SCM_NEWSMOB(z, simple_closure_tag, expr);
84   return z;
85 }
86  
87 int
88 print_simple_closure (SCM s, SCM port, scm_print_state *)
89 {
90   scm_puts ("#<simple-closure ", port);
91   scm_display (scm_cdr (s), port);
92   scm_puts (" >", port);
93   return 1;
94 }
95
96
97 void init_simple_closure ()
98 {
99   simple_closure_tag = scm_make_smob_type ("simple-closure", 0);
100   scm_set_smob_mark (simple_closure_tag, scm_markcdr);
101   scm_set_smob_print (simple_closure_tag, print_simple_closure);
102 };
103
104
105
106 ADD_SCM_INIT_FUNC(simple_closure, init_simple_closure);