2 closure.cc -- chained closures.
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "lily-guile.hh"
12 static scm_t_bits simple_closure_tag;
15 is_simple_closure (SCM s)
17 return (SCM_NIMP (s) && SCM_CELL_TYPE (s) == simple_closure_tag);
21 simple_closure_expression (SCM smob)
23 assert (is_simple_closure (smob));
24 return (SCM) SCM_CELL_WORD_1(smob);
27 SCM evaluate_with_simple_closure (SCM delayed_argument, SCM expr);
30 evaluate_args (SCM delayed_argument, SCM args)
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)),
38 tail = SCM_CDRLOC (*tail);
45 evaluate_with_simple_closure (SCM delayed_argument,
48 if (is_simple_closure (expr))
50 SCM inside = simple_closure_expression (expr);
51 return scm_apply_1 (scm_car (inside),
53 evaluate_args (delayed_argument, scm_cdr (inside)));
55 else if (!scm_is_pair (expr))
57 else if (scm_car (expr) == ly_symbol2scm ("quote"))
58 return scm_cadr (expr);
59 else if (ly_is_procedure (scm_car (expr)))
61 return scm_apply_0 (scm_car (expr),
62 evaluate_args (delayed_argument, scm_cdr (expr)));
65 // ugh. deviation from standard. Should print error?
66 return evaluate_args (delayed_argument, scm_cdr (expr));
72 LY_DEFINE(ly_simple_closure_p, "ly:simple-closure?",
76 return scm_from_bool (is_simple_closure (clos));
79 LY_DEFINE(ly_make_simple_closure, "ly:make-simple-closure",
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} ... )}.")
87 SCM_NEWSMOB(z, simple_closure_tag, expr);
92 print_simple_closure (SCM s, SCM port, scm_print_state *)
94 scm_puts ("#<simple-closure ", port);
95 scm_display (scm_cdr (s), port);
96 scm_puts (" >", port);
101 void init_simple_closure ()
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);
110 ADD_SCM_INIT_FUNC(simple_closure, init_simple_closure);