]> git.donarmstrong.com Git - lilypond.git/blob - lily/simple-closure.cc
ddf39efdcfe9dfdb085e6de4be99fd15d5ca1d35
[lilypond.git] / lily / simple-closure.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 #include "simple-closure.hh"
21
22 #include "grob.hh"
23
24 static scm_t_bits simple_closure_tag;
25
26 bool
27 is_simple_closure (SCM s)
28 {
29   return (SCM_NIMP (s) && SCM_CELL_TYPE (s) == simple_closure_tag);
30 }
31
32 SCM
33 simple_closure_expression (SCM smob)
34 {
35   assert (is_simple_closure (smob));
36   return (SCM) SCM_CELL_WORD_1(smob);
37 }
38
39 SCM
40 evaluate_args (SCM delayed_argument, SCM args, bool pure, int start, int end)
41 {
42   SCM new_args = SCM_EOL;
43   SCM *tail = &new_args;
44   for (SCM s = args; scm_is_pair (s); s = scm_cdr (s))
45     {
46       *tail = scm_cons (evaluate_with_simple_closure (delayed_argument, scm_car (s),
47                                                       pure, start, end),
48                         SCM_EOL);
49       if (scm_car (*tail) == SCM_UNSPECIFIED)
50         return SCM_UNSPECIFIED;
51       tail = SCM_CDRLOC (*tail);
52     }
53   
54   return new_args;
55 }
56
57 SCM
58 evaluate_with_simple_closure (SCM delayed_argument,
59                               SCM expr,
60                               bool pure,
61                               int start,
62                               int end)
63 {
64   if (is_simple_closure (expr))
65     {
66       SCM inside = simple_closure_expression (expr);
67       SCM args = scm_cons (delayed_argument,
68                            evaluate_args (delayed_argument, scm_cdr (inside),
69                                           pure, start, end));
70       if (scm_cdr (args) == SCM_UNSPECIFIED)
71         return SCM_UNSPECIFIED;
72       if (pure)
73         return call_pure_function (scm_car (inside), args, start, end);
74       return scm_apply_0 (scm_car (inside), args);
75     }
76   else if (!scm_is_pair (expr))
77     return expr;
78   else if (scm_car (expr) == ly_symbol2scm ("quote"))
79     return scm_cadr (expr);
80   else if (ly_is_procedure (scm_car (expr)))
81     {
82       SCM args = evaluate_args (delayed_argument, scm_cdr (expr), pure, start, end);
83       if (args == SCM_UNSPECIFIED)
84         return SCM_UNSPECIFIED;
85       if (pure)
86         return call_pure_function (scm_car (expr), args, start, end);
87       return scm_apply_0 (scm_car (expr), args);
88     }
89   else
90     // ugh. deviation from standard. Should print error? 
91     return evaluate_args (delayed_argument, scm_cdr (expr), pure, start, end); 
92   
93   assert (false);
94   return SCM_EOL;
95 }
96
97 LY_DEFINE (ly_simple_closure_p, "ly:simple-closure?",
98           1, 0, 0, (SCM clos),
99           "Is @var{clos} a simple closure?")
100 {
101   return scm_from_bool (is_simple_closure (clos));
102 }
103
104 LY_DEFINE (ly_make_simple_closure, "ly:make-simple-closure",
105           1, 0, 0, (SCM expr),
106           "Make a simple closure.  @var{expr} should be form of"
107           " @code{(@var{func} @var{a1} @var{A2} @dots{})}, and will be"
108           " invoked as @code{(@var{func} @var{delayed-arg} @var{a1}"
109           " @var{a2} @dots{})}.")
110 {
111   SCM z;
112
113   SCM_NEWSMOB (z, simple_closure_tag, expr);
114   return z;
115 }
116
117 LY_DEFINE (ly_eval_simple_closure, "ly:eval-simple-closure",
118           2, 2, 0, (SCM delayed, SCM closure, SCM scm_start, SCM scm_end),
119           "Evaluate a simple @var{closure} with the given @var{delayed}"
120           " argument.  If @var{scm-start} and @var{scm-end} are defined,"
121           " evaluate it purely with those start and end points.")
122 {
123   bool pure = (scm_is_number (scm_start) && scm_is_number (scm_end));
124   int start = robust_scm2int (scm_start, 0);
125   int end = robust_scm2int (scm_end, 0);
126   SCM expr = simple_closure_expression (closure);
127   return evaluate_with_simple_closure (delayed, expr, pure, start, end);
128 }
129  
130 int
131 print_simple_closure (SCM s, SCM port, scm_print_state *)
132 {
133   scm_puts ("#<simple-closure ", port);
134   scm_display (scm_cdr (s), port);
135   scm_puts (" >", port);
136   return 1;
137 }
138
139
140 void init_simple_closure ()
141 {
142   simple_closure_tag = scm_make_smob_type ("simple-closure", 0);
143   scm_set_smob_mark (simple_closure_tag, scm_markcdr);
144   scm_set_smob_print (simple_closure_tag, print_simple_closure);
145 };
146
147
148
149 ADD_SCM_INIT_FUNC (simple_closure, init_simple_closure);