]> git.donarmstrong.com Git - lilypond.git/blob - lily/simple-closure.cc
Merge with git+ssh://jneem@git.sv.gnu.org/srv/git/lilypond.git
[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 #include "simple-closure.hh"
10
11 #include "grob.hh"
12 #include "lily-guile.hh"
13
14 static scm_t_bits simple_closure_tag;
15
16 bool
17 is_simple_closure (SCM s)
18 {
19   return (SCM_NIMP (s) && SCM_CELL_TYPE (s) == simple_closure_tag);
20 }
21
22 SCM
23 simple_closure_expression (SCM smob)
24 {
25   assert (is_simple_closure (smob));
26   return (SCM) SCM_CELL_WORD_1(smob);
27 }
28
29 SCM
30 evaluate_args (SCM delayed_argument, SCM args, bool pure, int start, int end)
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                                                       pure, start, end),
38                         SCM_EOL);
39       if (scm_car (*tail) == SCM_UNSPECIFIED)
40         return SCM_UNSPECIFIED;
41       tail = SCM_CDRLOC (*tail);
42     }
43   
44   return new_args;
45 }
46
47 SCM
48 evaluate_with_simple_closure (SCM delayed_argument,
49                               SCM expr,
50                               bool pure,
51                               int start,
52                               int end)
53 {
54   if (is_simple_closure (expr))
55     {
56       SCM inside = simple_closure_expression (expr);
57       SCM args = scm_cons (delayed_argument,
58                            evaluate_args (delayed_argument, scm_cdr (inside),
59                                           pure, start, end));
60       if (scm_cdr (args) == SCM_UNSPECIFIED)
61         return SCM_UNSPECIFIED;
62       if (pure)
63         return call_pure_function (scm_car (inside), args, start, end);
64       return scm_apply_0 (scm_car (inside), args);
65     }
66   else if (!scm_is_pair (expr))
67     return expr;
68   else if (scm_car (expr) == ly_symbol2scm ("quote"))
69     return scm_cadr (expr);
70   else if (ly_is_procedure (scm_car (expr)))
71     {
72       SCM args = evaluate_args (delayed_argument, scm_cdr (expr), pure, start, end);
73       if (args == SCM_UNSPECIFIED)
74         return SCM_UNSPECIFIED;
75       if (pure)
76         return call_pure_function (scm_car (expr), args, start, end);
77       return scm_apply_0 (scm_car (expr), args);
78     }
79   else
80     // ugh. deviation from standard. Should print error? 
81     return evaluate_args (delayed_argument, scm_cdr (expr), pure, start, end); 
82   
83   assert (false);
84   return SCM_EOL;
85 }
86
87 LY_DEFINE(ly_simple_closure_p, "ly:simple-closure?",
88           1,0,0, (SCM clos),
89           "Type predicate.")
90 {
91   return scm_from_bool (is_simple_closure (clos));
92 }
93
94 LY_DEFINE(ly_make_simple_closure, "ly:make-simple-closure",
95           1, 0, 0, (SCM expr),
96           "Make a simple closure. @var{expr} should be form of "
97           "@code{(@var{func} @var{a1} @var{A2} ...)}, and will be invoked "
98           "as @code{(@var{func} @var{delayed-arg} @var{a1} @var{a2} ... )}.")
99 {
100   SCM z;
101
102   SCM_NEWSMOB(z, simple_closure_tag, expr);
103   return z;
104 }
105
106 LY_DEFINE(ly_eval_simple_closure, "ly:eval-simple-closure",
107           2, 2, 0, (SCM delayed, SCM closure, SCM scm_start, SCM scm_end),
108           "Evaluate a simple closure with the given delayed argument. "
109           "If start and end are defined, evaluate it purely with those "
110           "start- and end-points.")
111 {
112   bool pure = (scm_is_number (scm_start) && scm_is_number (scm_end));
113   int start = robust_scm2int (scm_start, 0);
114   int end = robust_scm2int (scm_end, 0);
115   SCM expr = simple_closure_expression (closure);
116   return evaluate_with_simple_closure (delayed, expr, pure, start, end);
117 }
118  
119 int
120 print_simple_closure (SCM s, SCM port, scm_print_state *)
121 {
122   scm_puts ("#<simple-closure ", port);
123   scm_display (scm_cdr (s), port);
124   scm_puts (" >", port);
125   return 1;
126 }
127
128
129 void init_simple_closure ()
130 {
131   simple_closure_tag = scm_make_smob_type ("simple-closure", 0);
132   scm_set_smob_mark (simple_closure_tag, scm_markcdr);
133   scm_set_smob_print (simple_closure_tag, print_simple_closure);
134 };
135
136
137
138 ADD_SCM_INIT_FUNC (simple_closure, init_simple_closure);