]> git.donarmstrong.com Git - lilypond.git/blob - lily/simple-closure.cc
77764b2bc6a1f6ee908bb86ff14be2f2e9b7b042
[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--2006 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 if (ly_is_procedure (scm_car (expr)))
60     {
61       return scm_apply_0 (scm_car (expr),
62                           evaluate_args (delayed_argument, scm_cdr (expr)));
63     }
64   else
65     // ugh. deviation from standard. Should print error? 
66     return evaluate_args (delayed_argument, scm_cdr (expr)); 
67   
68   assert (false);
69   return SCM_EOL;
70 }
71
72 LY_DEFINE(ly_simple_closure_p, "ly:simple-closure?",
73           1,0,0, (SCM clos),
74           "Type predicate.")
75 {
76   return scm_from_bool (is_simple_closure (clos));
77 }
78
79 LY_DEFINE(ly_make_simple_closure, "ly:make-simple-closure",
80           1, 0, 0, (SCM expr),
81           "Make a simple closure. @var{expr} should be form of "
82           "@code{(@var{func} @var{a1} @var{A2} ...)}, and will be invoked "
83           "as @code{(@var{func} @var{delayed-arg} @var{a1} @var{a2} ... )}.")
84 {
85   SCM z;
86
87   SCM_NEWSMOB(z, simple_closure_tag, expr);
88   return z;
89 }
90  
91 int
92 print_simple_closure (SCM s, SCM port, scm_print_state *)
93 {
94   scm_puts ("#<simple-closure ", port);
95   scm_display (scm_cdr (s), port);
96   scm_puts (" >", port);
97   return 1;
98 }
99
100
101 void init_simple_closure ()
102 {
103   simple_closure_tag = scm_make_smob_type ("simple-closure", 0);
104   scm_set_smob_mark (simple_closure_tag, scm_markcdr);
105   scm_set_smob_print (simple_closure_tag, print_simple_closure);
106 };
107
108
109
110 ADD_SCM_INIT_FUNC (simple_closure, init_simple_closure);