mirror of
https://github.com/justinethier/cyclone.git
synced 2025-06-11 05:15:05 +02:00
Cleaning up Cyc_length
This commit is contained in:
parent
1b92040258
commit
f11fec9f82
3 changed files with 14 additions and 11 deletions
|
@ -11,7 +11,7 @@
|
||||||
|
|
||||||
/* Error checking definitions */
|
/* Error checking definitions */
|
||||||
#define Cyc_check_num_args(data, fnc_name, num_args, args) { \
|
#define Cyc_check_num_args(data, fnc_name, num_args, args) { \
|
||||||
object l = Cyc_length2(data, args); \
|
object l = Cyc_length(data, args); \
|
||||||
if (num_args > obj_obj2int(l)) { \
|
if (num_args > obj_obj2int(l)) { \
|
||||||
char buf[128]; \
|
char buf[128]; \
|
||||||
snprintf(buf, 127, "Expected %d arguments but received %ld.", num_args, obj_obj2int(l)); \
|
snprintf(buf, 127, "Expected %d arguments but received %ld.", num_args, obj_obj2int(l)); \
|
||||||
|
@ -148,6 +148,7 @@ object __num_lte(void *, object x, object y);
|
||||||
object Cyc_eq(object x, object y);
|
object Cyc_eq(object x, object y);
|
||||||
object Cyc_set_car(void *, object l, object val) ;
|
object Cyc_set_car(void *, object l, object val) ;
|
||||||
object Cyc_set_cdr(void *, object l, object val) ;
|
object Cyc_set_cdr(void *, object l, object val) ;
|
||||||
|
object Cyc_length(void *d, object l);
|
||||||
object Cyc_length2(void *d, object l);
|
object Cyc_length2(void *d, object l);
|
||||||
integer_type Cyc_length_as_object(void *d, object l);
|
integer_type Cyc_length_as_object(void *d, object l);
|
||||||
object Cyc_vector_length(void *data, object v);
|
object Cyc_vector_length(void *data, object v);
|
||||||
|
|
20
runtime.c
20
runtime.c
|
@ -996,7 +996,7 @@ object Cyc_vector_length(void *data, object v) {
|
||||||
Cyc_rt_raise_msg(data, "vector-length - invalid parameter, expected vector\n"); }
|
Cyc_rt_raise_msg(data, "vector-length - invalid parameter, expected vector\n"); }
|
||||||
|
|
||||||
|
|
||||||
object Cyc_length2(void *data, object l){
|
object Cyc_length(void *data, object l){
|
||||||
int len = 0;
|
int len = 0;
|
||||||
while(!nullp(l)){
|
while(!nullp(l)){
|
||||||
if (is_value_type(l) || ((list)l)->tag != cons_tag){
|
if (is_value_type(l) || ((list)l)->tag != cons_tag){
|
||||||
|
@ -1007,6 +1007,8 @@ object Cyc_length2(void *data, object l){
|
||||||
}
|
}
|
||||||
return obj_int2obj(len);
|
return obj_int2obj(len);
|
||||||
}
|
}
|
||||||
|
object Cyc_length2(void *data, object l){
|
||||||
|
return Cyc_length(data, l); }
|
||||||
|
|
||||||
object Cyc_number2string(void *data, object cont, object n) {
|
object Cyc_number2string(void *data, object cont, object n) {
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
|
@ -1048,7 +1050,7 @@ object Cyc_list2string(void *data, object cont, object lst){
|
||||||
|
|
||||||
Cyc_check_cons_or_nil(data, lst);
|
Cyc_check_cons_or_nil(data, lst);
|
||||||
|
|
||||||
len = Cyc_length2(data, lst); // Inefficient, walks whole list
|
len = Cyc_length(data, lst); // Inefficient, walks whole list
|
||||||
buf = alloca(sizeof(char) * (obj_obj2int(len) + 1));
|
buf = alloca(sizeof(char) * (obj_obj2int(len) + 1));
|
||||||
while(!nullp(lst)){
|
while(!nullp(lst)){
|
||||||
buf[i++] = obj_obj2char(car(lst));
|
buf[i++] = obj_obj2char(car(lst));
|
||||||
|
@ -1355,7 +1357,7 @@ object Cyc_list2vector(void *data, object cont, object l) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
Cyc_check_cons_or_nil(data, l);
|
Cyc_check_cons_or_nil(data, l);
|
||||||
len = Cyc_length2(data, l);
|
len = Cyc_length(data, l);
|
||||||
v = alloca(sizeof(vector_type));
|
v = alloca(sizeof(vector_type));
|
||||||
((vector)v)->hdr.mark = gc_color_red;
|
((vector)v)->hdr.mark = gc_color_red;
|
||||||
((vector)v)->hdr.grayed = 0;
|
((vector)v)->hdr.grayed = 0;
|
||||||
|
@ -1797,7 +1799,7 @@ void _equal_127(void *data, object cont, object args){
|
||||||
return_closcall1(data, cont, equalp(car(args), cadr(args))); }
|
return_closcall1(data, cont, equalp(car(args), cadr(args))); }
|
||||||
void _length(void *data, object cont, object args){
|
void _length(void *data, object cont, object args){
|
||||||
Cyc_check_num_args(data, "length", 1, args);
|
Cyc_check_num_args(data, "length", 1, args);
|
||||||
{ object obj = Cyc_length2(data, car(args));
|
{ object obj = Cyc_length(data, car(args));
|
||||||
return_closcall1(data, cont, obj); }}
|
return_closcall1(data, cont, obj); }}
|
||||||
void _vector_91length(void *data, object cont, object args){
|
void _vector_91length(void *data, object cont, object args){
|
||||||
Cyc_check_num_args(data, "vector_91length", 1, args);
|
Cyc_check_num_args(data, "vector_91length", 1, args);
|
||||||
|
@ -1991,11 +1993,11 @@ void _string_91cmp(void *data, object cont, object args) {
|
||||||
{ object obj = Cyc_string_cmp(data, car(args), cadr(args));
|
{ object obj = Cyc_string_cmp(data, car(args), cadr(args));
|
||||||
return_closcall1(data, cont, obj);}}
|
return_closcall1(data, cont, obj);}}
|
||||||
void _string_91append(void *data, object cont, object args) {
|
void _string_91append(void *data, object cont, object args) {
|
||||||
object argc = Cyc_length2(data, args);
|
object argc = Cyc_length(data, args);
|
||||||
dispatch(data, obj_obj2int(argc), (function_type)dispatch_string_91append, cont, cont, args); }
|
dispatch(data, obj_obj2int(argc), (function_type)dispatch_string_91append, cont, cont, args); }
|
||||||
void _make_91vector(void *data, object cont, object args) {
|
void _make_91vector(void *data, object cont, object args) {
|
||||||
Cyc_check_num_args(data, "make-vector", 1, args);
|
Cyc_check_num_args(data, "make-vector", 1, args);
|
||||||
{ object argc = Cyc_length2(data, args);
|
{ object argc = Cyc_length(data, args);
|
||||||
if (obj_obj2int(argc) >= 2) {
|
if (obj_obj2int(argc) >= 2) {
|
||||||
Cyc_make_vector(data, cont, car(args), cadr(args));}
|
Cyc_make_vector(data, cont, car(args), cadr(args));}
|
||||||
else {
|
else {
|
||||||
|
@ -2063,11 +2065,11 @@ void _Cyc_91write_91char(void *data, object cont, object args) {
|
||||||
return_closcall1(data, cont, Cyc_write_char(data, car(args), cadr(args)));}
|
return_closcall1(data, cont, Cyc_write_char(data, car(args), cadr(args)));}
|
||||||
void _Cyc_91write(void *data, object cont, object args) {
|
void _Cyc_91write(void *data, object cont, object args) {
|
||||||
Cyc_check_num_args(data, "write", 1, args);
|
Cyc_check_num_args(data, "write", 1, args);
|
||||||
{ object argc = Cyc_length2(data, args);
|
{ object argc = Cyc_length(data, args);
|
||||||
dispatch(data, obj_obj2int(argc), (function_type)dispatch_write_va, cont, cont, args); }}
|
dispatch(data, obj_obj2int(argc), (function_type)dispatch_write_va, cont, cont, args); }}
|
||||||
void _display(void *data, object cont, object args) {
|
void _display(void *data, object cont, object args) {
|
||||||
Cyc_check_num_args(data, "display", 1, args);
|
Cyc_check_num_args(data, "display", 1, args);
|
||||||
{ object argc = Cyc_length2(data, args);
|
{ object argc = Cyc_length(data, args);
|
||||||
dispatch(data, obj_obj2int(argc), (function_type)dispatch_display_va, cont, cont, args); }}
|
dispatch(data, obj_obj2int(argc), (function_type)dispatch_display_va, cont, cont, args); }}
|
||||||
void _call_95cc(void *data, object cont, object args){
|
void _call_95cc(void *data, object cont, object args){
|
||||||
Cyc_check_num_args(data, "call/cc", 1, args);
|
Cyc_check_num_args(data, "call/cc", 1, args);
|
||||||
|
@ -2107,7 +2109,7 @@ object apply(void *data, object cont, object func, object args){
|
||||||
case closure3_tag:
|
case closure3_tag:
|
||||||
case closure4_tag:
|
case closure4_tag:
|
||||||
case closureN_tag:
|
case closureN_tag:
|
||||||
count = Cyc_length2(data, args);
|
count = Cyc_length(data, args);
|
||||||
// TODO: validate number of args provided:
|
// TODO: validate number of args provided:
|
||||||
Cyc_check_num_args(data, "<procedure>", ((closure)func)->num_args, args); // TODO: could be more efficient, eg: cyc_length(args) is called twice.
|
Cyc_check_num_args(data, "<procedure>", ((closure)func)->num_args, args); // TODO: could be more efficient, eg: cyc_length(args) is called twice.
|
||||||
dispatch(data, obj_obj2int(count), ((closure)func)->fn, func, cont, args);
|
dispatch(data, obj_obj2int(count), ((closure)func)->fn, func, cont, args);
|
||||||
|
|
|
@ -547,7 +547,7 @@
|
||||||
((eq? p 'memq) "memqp")
|
((eq? p 'memq) "memqp")
|
||||||
((eq? p 'memv) "memqp")
|
((eq? p 'memv) "memqp")
|
||||||
((eq? p 'member) "memberp")
|
((eq? p 'member) "memberp")
|
||||||
((eq? p 'length) "Cyc_length2")
|
((eq? p 'length) "Cyc_length")
|
||||||
((eq? p 'set-car!) "Cyc_set_car")
|
((eq? p 'set-car!) "Cyc_set_car")
|
||||||
((eq? p 'set-cdr!) "Cyc_set_cdr")
|
((eq? p 'set-cdr!) "Cyc_set_cdr")
|
||||||
((eq? p 'eq?) "Cyc_eq")
|
((eq? p 'eq?) "Cyc_eq")
|
||||||
|
|
Loading…
Add table
Reference in a new issue