1 /* <Built-in procedures> */
3 /* Creates a closure and adds it to the top-level
4 @param name (string) the name of the built-in procedure
5 @param code (void*) the address of the built-in procedure (label address)
7 #define CREATE_BUILTIN_CLOS(name,code) \
8 r_res = (int)getSymbol((name),topLevel); /* create a hash bucket */ \
9 ((SymbolEntry*)r_res)->sob = makeSchemeClosure(NULL,(code)); \
10 ASSERT_ALWAYS( ((SymbolEntry*)r_res)->sob!=NULL,"" ); \
11 ((SymbolEntry*)r_res)->isDefined = 1;
13 /* The stack when in a built-in precedure (there is no fp):
15 | ret | - return address
16 | env | - points to the enviroment vector
17 | n | - number of arguments
21 | An-1 | - argument n-1
23 Macros for built-in procedures:
25 #define BI_ST_ARG(n) (stack[sp-4-(n)])
26 #define BI_ST_ARG_COUNT() (stack[sp-3])
27 #define BI_ST_RET() (stack[sp-1])
28 #define BI_RETURN() goto *pop()
30 goto Lstart
; /* skip all definitions */
33 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==1,MSG_ERR_ARGCOUNT("car",1) );
35 ASSERT_ALWAYS( IS_SOB_PAIR(r
[0]), MSG_ERR_NOTPAIR
);
36 r_res
= (int)SOB_PAIR_CAR(r
[0]);
40 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==1,MSG_ERR_ARGCOUNT("cdr",1) );
42 ASSERT_ALWAYS( IS_SOB_PAIR(r
[0]), MSG_ERR_NOTPAIR
);
43 r_res
= (int)SOB_PAIR_CDR(r
[0]);
47 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==1,MSG_ERR_ARGCOUNT("symbol?",1) );
49 if ( IS_SOB_SYMBOL(r
[0]) ) goto Lsymbol_true
;
50 r_res
= (int)&sc_false
;
53 r_res
= (int)&sc_true
;
57 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==2,MSG_ERR_ARGCOUNT("set-car!",2) );
60 ASSERT_ALWAYS( IS_SOB_PAIR(r
[0]), MSG_ERR_NOTPAIR
);
61 SOB_PAIR_CAR(r
[0]) = (SchemeObject
*)r
[1];
62 r_res
= (int)&sc_void
;
66 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==2,MSG_ERR_ARGCOUNT("set-cdr!",2) );
69 ASSERT_ALWAYS( IS_SOB_PAIR(r
[0]), MSG_ERR_NOTPAIR
);
70 SOB_PAIR_CDR(r
[0]) = (SchemeObject
*)r
[1];
71 r_res
= (int)&sc_void
;
75 ASSERT_ALWAYS( BI_ST_ARG_COUNT()>=1,MSG_ERR_ARGCOUNT("make-vector",1) );
77 ASSERT_ALWAYS(IS_SOB_INT((SchemeObject
*)r
[0]), "");
78 r
[0] = SOB_INT_VALUE(r
[0]);
79 r_res
= (int)makeSchemeVectorInit(r
[0],&sc_void
);
80 /* initialize vector elements
81 for (int i=1; i<BI_ST_ARG_COUNT(); ++i)
82 SOB_VECTOR_SET(r_res,i,BI_ST_ARG(i));
87 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==3,MSG_ERR_ARGCOUNT("vector-set!",3) );
89 ASSERT_ALWAYS(IS_SOB_VECTOR((SchemeObject
*)r
[0]), "");
91 ASSERT_ALWAYS(IS_SOB_INT((SchemeObject
*)r
[1]), "");
92 r
[1] = SOB_INT_VALUE((SchemeObject
*)r
[1]);
94 SOB_VECTOR_SET((SchemeObject
*)r
[0],r
[1],(SchemeObject
*)r
[2]);
95 r_res
= (int)&sc_void
;
99 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==2,MSG_ERR_ARGCOUNT("vector-ref",2) );
101 ASSERT_ALWAYS(IS_SOB_VECTOR((SchemeObject
*)r
[0]), "");
103 ASSERT_ALWAYS(IS_SOB_INT((SchemeObject
*)r
[1]), "");
104 r
[1] = SOB_INT_VALUE((SchemeObject
*)r
[1]);
105 r_res
= (int)SOB_VECTOR_REF((SchemeObject
*)r
[0],r
[1]);
109 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==2,MSG_ERR_ARGCOUNT("binary=?",2) );
111 ASSERT_ALWAYS(IS_SOB_INT((SchemeObject
*)r
[0]), "");
112 r
[0] = SOB_INT_VALUE((SchemeObject
*)r
[0]);
114 ASSERT_ALWAYS(IS_SOB_INT((SchemeObject
*)r
[1]), "");
115 r
[1] = SOB_INT_VALUE((SchemeObject
*)r
[1]);
116 if (r
[0] != r
[1]) goto LbinaryEQ_false
;
117 r_res
= (int)&sc_true
;
120 r_res
= (int)&sc_false
;
124 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==2,MSG_ERR_ARGCOUNT("binary-add",2) );
126 ASSERT_ALWAYS(IS_SOB_INT((SchemeObject
*)r
[0]), "");
127 r
[0] = SOB_INT_VALUE((SchemeObject
*)r
[0]);
129 ASSERT_ALWAYS(IS_SOB_INT((SchemeObject
*)r
[1]), "");
130 r
[1] = SOB_INT_VALUE((SchemeObject
*)r
[1]);
132 r_res
= (int)makeSchemeInt( r_res
);
136 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==2,MSG_ERR_ARGCOUNT("binary-sub",2) );
138 ASSERT_ALWAYS(IS_SOB_INT((SchemeObject
*)r
[0]), "");
139 r
[0] = SOB_INT_VALUE((SchemeObject
*)r
[0]);
141 ASSERT_ALWAYS(IS_SOB_INT((SchemeObject
*)r
[1]), "");
142 r
[1] = SOB_INT_VALUE((SchemeObject
*)r
[1]);
144 r_res
= (int)makeSchemeInt( r_res
);
148 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==2,MSG_ERR_ARGCOUNT("binary-mul",2) );
150 ASSERT_ALWAYS(IS_SOB_INT((SchemeObject
*)r
[0]), "");
151 r
[0] = SOB_INT_VALUE((SchemeObject
*)r
[0]);
153 ASSERT_ALWAYS(IS_SOB_INT((SchemeObject
*)r
[1]), "");
154 r
[1] = SOB_INT_VALUE((SchemeObject
*)r
[1]);
155 r_res
= (int)r
[0] * (int)r
[1]; /* multiply as signed integers */
156 r_res
= (int)makeSchemeInt( r_res
);
160 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==2,MSG_ERR_ARGCOUNT("binary-div",2) );
162 ASSERT_ALWAYS(IS_SOB_INT((SchemeObject
*)r
[0]), "");
163 r
[0] = SOB_INT_VALUE((SchemeObject
*)r
[0]);
165 ASSERT_ALWAYS(IS_SOB_INT((SchemeObject
*)r
[1]), "");
166 r
[1] = SOB_INT_VALUE((SchemeObject
*)r
[1]);
167 ASSERT_ALWAYS( (int)r
[1]!=0, MSG_ERR_DIVZERO
);
168 r_res
= (int)r
[0] / (int)r
[1]; /* divide as signed integers */
169 r_res
= (int)makeSchemeInt( r_res
);
173 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==2,MSG_ERR_ARGCOUNT("binary<?",2) );
175 ASSERT_ALWAYS(IS_SOB_INT((SchemeObject
*)r
[0]), "");
176 r
[0] = SOB_INT_VALUE((SchemeObject
*)r
[0]);
178 ASSERT_ALWAYS(IS_SOB_INT((SchemeObject
*)r
[1]), "");
179 r
[1] = SOB_INT_VALUE((SchemeObject
*)r
[1]);
180 r_res
= ((int)r
[0] < (int)r
[1]); /* compare as signed integers */
181 if ( r_res
) goto LbinaryLT_true
;
182 r_res
= (int)&sc_false
;
185 r_res
= (int)&sc_true
;
189 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==1,MSG_ERR_ARGCOUNT("box",1) );
191 r_res
= (int)makeSchemeVectorInit( 1,(SchemeObject
*)(r
[0]) );
195 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==1,MSG_ERR_ARGCOUNT("null?",1) );
197 if ( IS_SOB_NIL(r
[0]) ) goto Lnull_true
;
198 r_res
= (int)&sc_false
;
201 r_res
= (int)&sc_true
;
205 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==1,MSG_ERR_ARGCOUNT("char->integer",1) );
207 ASSERT_ALWAYS( IS_SOB_CHAR(r
[0]), "" );
208 r
[0] = SOB_CHAR_VALUE(r
[0]);
209 r_res
= (int)makeSchemeInt( (int)r
[0] );
213 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==1,MSG_ERR_ARGCOUNT("integer->char",1) );
215 ASSERT_ALWAYS( IS_SOB_INT(r
[0]), "" );
216 r
[0] = SOB_INT_VALUE(r
[0]);
217 r_res
= (int)makeSchemeChar( (char)r
[0] );
223 ASSERT_ALWAYS( ST_ARG_COUNT()==2,MSG_ERR_ARGCOUNT("apply",2) );
224 r
[0] = ST_ARG(0); /* r[0] is now the closure */
225 ASSERT_ALWAYS( IS_SOB_CLOSURE(r
[0]), MSG_ERR_APPNONPROC
);
226 r
[1] = ST_ARG(1); /* r[1] is now the list of arguments */
227 ASSERT_ALWAYS( (r
[1]==(int)&sc_nil
) | (IS_SOB_PAIR(r
[1])), MSG_ERR_NOTLIST
);
228 /* push arguments (backwards) and number of arguments */
229 pushArgsList( (SchemeObject
*)r
[1] );
230 /* push env. of closure */
231 push( (int)SOB_CLOSURE_ENV(r
[0]) );
232 /* push current return address (it's a tail call) */
233 push( (int)ST_RET() );
234 /* override current frame (it's a tail call) */
237 goto *SOB_CLOSURE_CODE(r
[0]);
240 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==1,MSG_ERR_ARGCOUNT("boolean?",1) );
242 r_res
= (IS_SOB_BOOL(r
[0])) ? (int)&sc_true
: (int)&sc_false
;
246 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==1,MSG_ERR_ARGCOUNT("char?",1) );
248 r_res
= (IS_SOB_CHAR(r
[0])) ? (int)&sc_true
: (int)&sc_false
;
252 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==1,MSG_ERR_ARGCOUNT("integer?",1) );
254 r_res
= (IS_SOB_INT(r
[0])) ? (int)&sc_true
: (int)&sc_false
;
258 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==1,MSG_ERR_ARGCOUNT("pair?",1) );
260 r_res
= (IS_SOB_PAIR(r
[0])) ? (int)&sc_true
: (int)&sc_false
;
264 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==1,MSG_ERR_ARGCOUNT("procedure?",1) );
266 r_res
= (IS_SOB_CLOSURE(r
[0])) ? (int)&sc_true
: (int)&sc_false
;
270 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==2,MSG_ERR_ARGCOUNT("cons",2) );
273 r_res
= (int)makeSchemePair( (SchemeObject
*)r
[0], (SchemeObject
*)r
[1] );
277 /* The eq? procedure compare the
282 and the ADDRESSES of pairs,
286 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==2,MSG_ERR_ARGCOUNT("eq?",2) );
289 if ( IS_SOB_BOOL(r
[0]) ) goto Leq_boolval
;
290 if ( IS_SOB_CHAR(r
[0]) ) goto Leq_charval
;
291 if ( IS_SOB_INT(r
[0]) ) goto Leq_intval
;
292 if ( IS_SOB_SYMBOL(r
[0]) ) goto Leq_symval
;
293 if ( r
[0]==r
[1] ) goto Leq_true
;
296 if ( !IS_SOB_BOOL(r
[1]) ) goto Leq_false
;
297 if ( SOB_BOOL_VALUE(r
[0])==SOB_BOOL_VALUE(r
[1]) ) goto Leq_true
;
300 if ( !IS_SOB_CHAR(r
[1]) ) goto Leq_false
;
301 if ( SOB_CHAR_VALUE(r
[0])==SOB_CHAR_VALUE(r
[1]) ) goto Leq_true
;
304 if ( !IS_SOB_INT(r
[1]) ) goto Leq_false
;
305 if ( SOB_INT_VALUE(r
[0])==SOB_INT_VALUE(r
[1]) ) goto Leq_true
;
308 if ( !IS_SOB_SYMBOL(r
[1]) ) goto Leq_false
;
309 if ( SOB_SYMBOL_ENTRY(r
[0])==SOB_SYMBOL_ENTRY(r
[1]) ) goto Leq_true
;
312 r_res
= (int)&sc_true
;
315 r_res
= (int)&sc_false
;
319 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==2,MSG_ERR_ARGCOUNT("make-string",2) );
321 ASSERT_ALWAYS(IS_SOB_INT((SchemeObject
*)r
[0]), "");
322 r
[0] = SOB_INT_VALUE(r
[0]);
324 ASSERT_ALWAYS(IS_SOB_CHAR((SchemeObject
*)r
[1]), "");
325 r
[1] = (int)SOB_CHAR_VALUE(r
[1]);
326 r_res
= (int)makeSchemeString(r
[0],(char)r
[1]);
330 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==2,MSG_ERR_ARGCOUNT("remainder",2) );
332 ASSERT_ALWAYS( IS_SOB_INT(r
[0]), "" );
333 r
[0] = SOB_INT_VALUE(r
[0]);
335 ASSERT_ALWAYS( IS_SOB_INT(r
[1]), "" );
336 r
[1] = SOB_INT_VALUE(r
[1]);
337 ASSERT_ALWAYS( (int)r
[1]!=0, MSG_ERR_DIVZERO
);
338 r_res
= (int)r
[0] % (int)r
[1]; /* divide as signed integers */
339 r_res
= (int)makeSchemeInt( r_res
);
343 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==1,MSG_ERR_ARGCOUNT("string->symbol",1) );
345 ASSERT_ALWAYS( IS_SOB_STRING(r
[0]), "" );
346 r
[0] = (int)SOB_STRING_VALUE(r
[0]);
347 r_res
= (int)makeSchemeSymbol((char*)r
[0]);
351 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==1,MSG_ERR_ARGCOUNT("string-length",1) );
353 ASSERT_ALWAYS( IS_SOB_STRING(r
[0]), "" );
354 r_res
= SOB_STRING_LENGTH(r
[0]);
355 r_res
= (int)makeSchemeInt(r_res
);
359 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==2,MSG_ERR_ARGCOUNT("string-ref",2) );
361 ASSERT_ALWAYS( IS_SOB_STRING(r
[0]), "" );
363 ASSERT_ALWAYS( IS_SOB_INT(r
[1]), "" );
364 r
[1] = SOB_INT_VALUE( r
[1] );
365 ASSERT_ALWAYS( (r
[1]>=0) & (r
[1]<SOB_STRING_LENGTH(r
[0])), MSG_ERR_NDXOUTOFBOUNDS
);
366 r_res
= SOB_STRING_REF(r
[0],r
[1]);
367 r_res
= (int)makeSchemeChar(r_res
);
371 ASSERT_ALWAYS( BI_ST_ARG_COUNT()==3,MSG_ERR_ARGCOUNT("string-set!",3) );
373 ASSERT_ALWAYS( IS_SOB_STRING(r
[0]), "" );
376 ASSERT_ALWAYS( IS_SOB_INT(r
[1]), "" );
377 r
[1] = SOB_INT_VALUE( r
[1] );
378 ASSERT_ALWAYS( (r
[1]>=0) & (r
[1]<SOB_STRING_LENGTH(r
[0])), MSG_ERR_NDXOUTOFBOUNDS
);
381 ASSERT_ALWAYS( IS_SOB_CHAR(r
[2]), "" );
382 r
[2] = SOB_CHAR_VALUE( r
[2] );
383 SOB_STRING_SET((SchemeObject
*)r
[0],r
[1],(char)r
[2]);
384 r_res
= (int)&sc_void
;
388 /* create closures for the free variables of the built-in procedures */
389 CREATE_BUILTIN_CLOS("car" ,&&Lcar
);
390 CREATE_BUILTIN_CLOS("cdr" ,&&Lcdr
);
391 CREATE_BUILTIN_CLOS("symbol?" ,&&Lsymbol
);
392 CREATE_BUILTIN_CLOS("set-car!" ,&&Lset_car
);
393 CREATE_BUILTIN_CLOS("set-cdr!" ,&&Lset_cdr
);
394 CREATE_BUILTIN_CLOS("make-vector" ,&&Lmake_vector
);
395 CREATE_BUILTIN_CLOS("vector-set!" ,&&Lvector_set
);
396 CREATE_BUILTIN_CLOS("vector-ref" ,&&Lvector_ref
);
397 CREATE_BUILTIN_CLOS("binary=?" ,&&LbinaryEQ
);
398 CREATE_BUILTIN_CLOS("binary-add" ,&&LbinaryADD
);
399 CREATE_BUILTIN_CLOS("binary-sub" ,&&LbinarySUB
);
400 CREATE_BUILTIN_CLOS("binary-mul" ,&&LbinaryMUL
);
401 CREATE_BUILTIN_CLOS("binary-div" ,&&LbinaryDIV
);
402 CREATE_BUILTIN_CLOS("binary<?" ,&&LbinaryLT
);
403 CREATE_BUILTIN_CLOS("box" ,&&Lbox
);
404 CREATE_BUILTIN_CLOS("null?" ,&&Lnull
);
405 CREATE_BUILTIN_CLOS("char->integer" ,&&Lchar_to_integer
);
406 CREATE_BUILTIN_CLOS("integer->char" ,&&Linteger_to_char
);
407 CREATE_BUILTIN_CLOS("apply" ,&&Lapply
);
408 CREATE_BUILTIN_CLOS("boolean?" ,&&Lboolean
);
409 CREATE_BUILTIN_CLOS("char?" ,&&Lchar
);
410 CREATE_BUILTIN_CLOS("cons" ,&&Lcons
);
411 CREATE_BUILTIN_CLOS("eq?" ,&&Leq
);
412 CREATE_BUILTIN_CLOS("integer?" ,&&Linteger
);
413 CREATE_BUILTIN_CLOS("make-string" ,&&Lmake_string
);
414 CREATE_BUILTIN_CLOS("number?" ,&&Linteger
); /* same as integer? */
415 CREATE_BUILTIN_CLOS("pair?" ,&&Lpair
);
416 CREATE_BUILTIN_CLOS("procedure?" ,&&Lprocedure
);
417 CREATE_BUILTIN_CLOS("remainder" ,&&Lremainder
);
418 CREATE_BUILTIN_CLOS("string->symbol",&&Lstring_to_symbol
);
419 CREATE_BUILTIN_CLOS("string-length" ,&&Lstring_length
);
420 CREATE_BUILTIN_CLOS("string-ref" ,&&Lstring_ref
);
421 CREATE_BUILTIN_CLOS("string-set!" ,&&Lstring_set
);
423 /* </Built-in procedures> */