Debian Codegraph @

src/c/reference.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * reference.d - reference in Constants and Variables
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 #include <ecl/ecl.h>
16 #include <ecl/internal.h>
17 #include <ecl/ecl-inl.h>
18
19 /* Symbol-function returns */
20 /* function-closure for function */
21 /* (macro . function-closure) for macros */
22 /* special for special forms. */
23 cl_object
24 cl_symbol_function(cl_object sym)
25 {
26 cl_object output;
27 int type = ecl_symbol_type(sym);
28 if (type & ecl_stp_special_form) {
29 output = @'special';
30 } else if (Null(sym)) {
31 FEundefined_function(sym);
32 } else if (type & ecl_stp_macro) {
33 output = CONS(@'si::macro', sym->symbol.macfun);
34 } else if (ECL_FBOUNDP(sym)) {
35 output = ECL_SYM_FUN(sym);
36 } else {
37 FEundefined_function(sym);
38 }
39 @(return output);
40 }
41
42 cl_object
43 cl_fdefinition(cl_object fname)
44 {
45 @(return ((ECL_SYMBOLP(fname))? cl_symbol_function(fname) : ecl_fdefinition(fname)));
46 }
47
48 cl_object
49 cl_fboundp(cl_object fname)
50 {
51 if (Null(fname)) {
52 @(return ECL_NIL);
53 } else if (ECL_SYMBOLP(fname)) {
54 @(return (((fname->symbol.stype & (ecl_stp_special_form | ecl_stp_macro))
55 || ECL_FBOUNDP(fname))? ECL_T : ECL_NIL));
56 } else if (LISTP(fname)) {
57 if (CAR(fname) == @'setf') {
58 cl_object sym = CDR(fname);
59 if (CONSP(sym) && CDR(sym) == ECL_NIL) {
60 cl_object pair;
61 sym = CAR(sym);
62 if (ECL_SYMBOLP(sym)) {
63 pair = ecl_setf_definition(sym, ECL_NIL);
64 @(return ecl_cdr(pair));
65 }
66 }
67 }
68 }
69 FEinvalid_function_name(fname);
70 }
71
72 cl_object
73 ecl_fdefinition(cl_object fun)
74 {
75 cl_type t = ecl_t_of(fun);
76 cl_object output;
77
78 if (t == t_symbol) {
79 unlikely_if (!ECL_FBOUNDP(fun) ||
80 fun->symbol.stype & (ecl_stp_macro | ecl_stp_special_form))
81 FEundefined_function(fun);
82 if (fun->symbol.stype & ecl_stp_macro)
83 output = fun->symbol.macfun;
84 else if (fun->symbol.stype & ecl_stp_special_form)
85 output = ECL_NIL;
86 else
87 output = ECL_SYM_FUN(fun);
88 } else unlikely_if (Null(fun)) {
89 FEundefined_function(fun);
90 } else if (t == t_list) {
91 cl_object sym = CDR(fun);
92 unlikely_if (!CONSP(sym))
93 FEinvalid_function_name(fun);
94 if (CAR(fun) == @'setf') {
95 unlikely_if (CDR(sym) != ECL_NIL)
96 FEinvalid_function_name(fun);
97 sym = CAR(sym);
98 unlikely_if (ecl_t_of(sym) != t_symbol)
99 FEinvalid_function_name(fun);
100 output = ecl_setf_definition(sym, ECL_NIL);
101 unlikely_if (Null(ecl_cdr(output)))
102 FEundefined_function(fun);
103 output = ECL_CONS_CAR(output);
104 } else if (CAR(fun) == @'lambda') {
105 return si_make_lambda(ECL_NIL, sym);
106 } else if (CAR(fun) == @'ext::lambda-block') {
107 return si_make_lambda(CAR(sym), CDR(sym));
108 } else {
109 FEinvalid_function_name(fun);
110 }
111 } else {
112 FEinvalid_function_name(fun);
113 }
114 return output;
115 }
116
117 cl_object
118 si_coerce_to_function(cl_object fun)
119 {
120 cl_type t = ecl_t_of(fun);
121 if (!(t == t_cfun || t == t_cfunfixed || t == t_cclosure
122 || t == t_bytecodes || t == t_bclosure
123 || (t == t_instance && fun->instance.isgf))) {
124 fun = ecl_fdefinition(fun);
125 }
126 @(return fun);
127 }
128
129 cl_object
130 cl_symbol_value(cl_object sym)
131 {
132 const cl_env_ptr the_env = ecl_process_env();
133 cl_object value;
134 if (Null(sym)) {
135 value = sym;
136 } else {
137 if (ecl_unlikely(!ECL_SYMBOLP(sym))) {
138 FEwrong_type_only_arg(@[symbol-value], sym, @[symbol]);
139 }
140 value = ECL_SYM_VAL(the_env, sym);
141 if (ecl_unlikely(value == OBJNULL)) {
142 FEunbound_variable(sym);
143 }
144 }
145 @(return value);
146 }
147
148 bool
149 ecl_boundp(cl_env_ptr env, cl_object sym)
150 {
151 if (Null(sym)) {
152 return 1;
153 } else {
154 if (ecl_unlikely(!ECL_SYMBOLP(sym)))
155 FEwrong_type_only_arg(@[boundp], sym, @[symbol]);
156 return ECL_SYM_VAL(env, sym) != OBJNULL;
157 }
158 }
159
160 cl_object
161 cl_boundp(cl_object sym)
162 {
163 const cl_env_ptr the_env = ecl_process_env();
164 ecl_return1(the_env, ecl_boundp(the_env,sym)? ECL_T : ECL_NIL);
165 }
166
167 cl_object
168 cl_special_operator_p(cl_object form)
169 {
170 const cl_env_ptr the_env = ecl_process_env();
171 int special = ecl_symbol_type(form) & ecl_stp_special_form;
172 ecl_return1(the_env, special? ECL_T : ECL_NIL);
173 }