2 closure.cc -- chained closures.
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "simple-closure.hh"
12 #include "lily-guile.hh"
14 static scm_t_bits simple_closure_tag;
17 is_simple_closure (SCM s)
19 return (SCM_NIMP (s) && SCM_CELL_TYPE (s) == simple_closure_tag);
23 simple_closure_expression (SCM smob)
25 assert (is_simple_closure (smob));
26 return (SCM) SCM_CELL_WORD_1(smob);
30 evaluate_args (SCM delayed_argument, SCM args, bool pure, int start, int end)
32 SCM new_args = SCM_EOL;
33 SCM *tail = &new_args;
34 for (SCM s = args; scm_is_pair (s); s = scm_cdr (s))
36 *tail = scm_cons (evaluate_with_simple_closure (delayed_argument, scm_car (s),
39 if (scm_car (*tail) == SCM_UNSPECIFIED)
40 return SCM_UNSPECIFIED;
41 tail = SCM_CDRLOC (*tail);
48 evaluate_with_simple_closure (SCM delayed_argument,
54 if (is_simple_closure (expr))
56 SCM inside = simple_closure_expression (expr);
57 SCM args = scm_cons (delayed_argument,
58 evaluate_args (delayed_argument, scm_cdr (inside),
60 if (scm_cdr (args) == SCM_UNSPECIFIED)
61 return SCM_UNSPECIFIED;
63 return call_pure_function (scm_car (inside), args, start, end);
64 return scm_apply_0 (scm_car (inside), args);
66 else if (!scm_is_pair (expr))
68 else if (scm_car (expr) == ly_symbol2scm ("quote"))
69 return scm_cadr (expr);
70 else if (ly_is_procedure (scm_car (expr)))
72 SCM args = evaluate_args (delayed_argument, scm_cdr (expr), pure, start, end);
73 if (args == SCM_UNSPECIFIED)
74 return SCM_UNSPECIFIED;
76 return call_pure_function (scm_car (expr), args, start, end);
77 return scm_apply_0 (scm_car (expr), args);
80 // ugh. deviation from standard. Should print error?
81 return evaluate_args (delayed_argument, scm_cdr (expr), pure, start, end);
87 LY_DEFINE(ly_simple_closure_p, "ly:simple-closure?",
91 return scm_from_bool (is_simple_closure (clos));
94 LY_DEFINE(ly_make_simple_closure, "ly:make-simple-closure",
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} ... )}.")
102 SCM_NEWSMOB(z, simple_closure_tag, expr);
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.")
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);
120 print_simple_closure (SCM s, SCM port, scm_print_state *)
122 scm_puts ("#<simple-closure ", port);
123 scm_display (scm_cdr (s), port);
124 scm_puts (" >", port);
129 void init_simple_closure ()
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);
138 ADD_SCM_INIT_FUNC (simple_closure, init_simple_closure);