summaryrefslogtreecommitdiff
path: root/cons.c
blob: 42df8cb4faf7dd84c2728a2a51de03c05db209f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/************************************************

  cons.c -

  $Author: matz $
  $Date: 1995/01/10 10:30:37 $
  created at: Fri Jan  6 10:10:36 JST 1995

  Copyright (C) 1995 Yukihiro Matsumoto

************************************************/

#include "ruby.h"

VALUE C_Cons;

static ID eq;

VALUE rb_to_a();

VALUE
assoc_new(car, cdr)
    VALUE car, cdr;
{
    NEWOBJ(cons, struct RCons);
    OBJSETUP(cons, C_Cons, T_CONS);

    cons->car = car;
    cons->cdr = cdr;

    return (VALUE)cons;
}

#define cons_new assoc_new

static VALUE
Fcons_car(cons)
    struct RCons *cons;
{
    return cons->car;
}

static VALUE
Fcons_cdr(cons)
    struct RCons *cons;
{
    return cons->cdr;
}

static VALUE
Fcons_set_car(cons, val)
    struct RCons *cons;
    VALUE val;
{
    return cons->car = val;
}

static VALUE
Fcons_set_cdr(cons, val)
    struct RCons *cons;
    VALUE val;
{
    return cons->cdr = val;
}

static int
cons_length(list)
    struct RCons *list;
{
    int len = 1;

    while (TYPE(list) == T_CONS) {
	len++;
	list = RCONS(list->cdr);
    }
    return len;
}

static VALUE
Fcons_length(list)
    struct RCons *list;
{
    int len = cons_length(list);
    return INT2FIX(len);
}

static VALUE
cons_aref(list, nth)
    struct RCons *list;
    int nth;
{
    if (nth == 0) return list->car;
    list = RCONS(list->cdr);
    if (TYPE(list) != T_CONS) {
	if (nth == 1) return (VALUE)list;
	return Qnil;
    }

    return cons_aref(list, nth-1);
}

static VALUE
Fcons_aref(list, nth)
    struct RCons *list;
    VALUE nth;
{
    int n = NUM2INT(nth);

    if (n < 0) {
	n = cons_length(list)+n;
	if (n < 0) return Qnil;
    }
    return cons_aref(list, n);
}

static VALUE
cons_aset(list, nth, val)
    struct RCons *list;
    int nth;
    VALUE val;
{
    if (nth == 0) return list->car = val;
    if (TYPE(list->cdr) != T_CONS) {
	if (nth > 2) {
	    Fail("list too short");
	}
	if (nth == 1)
	    list->cdr = val;
	else
	    list->cdr = cons_new(list->cdr, val);
	return val;
    }
    return cons_aset(list->cdr, nth-1, val);
}

static VALUE
Fcons_aset(list, nth, val)
    struct RCons *list;
    VALUE nth, val;
{
    int n = NUM2INT(nth);

    if (n < 0) {
	n = cons_length(list)+n;
	if (n < 0) {
	    Fail("negative offset too big");
	}
    }
    return cons_aset(list, n, val);
}

static VALUE
Fcons_each(list)
    struct RCons *list;
{
    rb_yield(list->car);
    if (TYPE(list->cdr) != T_CONS) {
	rb_yield(list->cdr);
	return Qnil;
    }
    return Fcons_each(list->cdr);
}

static VALUE
Fcons_equal(cons1, cons2)
    struct RCons *cons1, *cons2;
{
    if (TYPE(cons2) != T_CONS) return FALSE;
    if (!rb_equal(cons1->car, cons2->car)) return FALSE;
    return rb_equal(cons1->cdr, cons2->cdr);
}

static ID hash;

static VALUE
Fcons_hash(cons)
    struct RCons *cons;
{
    int key;

    if (!hash) hash = rb_intern("hash");
    key = rb_funcall(cons->car, hash, 0, 0);
    key ^= rb_funcall(cons->cdr, hash, 0, 0);
    return INT2FIX(key);
}

static VALUE
Fcons_to_s(cons)
    struct RCons *cons;
{
    VALUE str1, str2;
    ID to_s = rb_intern("to_s");

    str1 = rb_funcall(cons->car, to_s, 0);
    cons = RCONS(cons->cdr);
    while (cons) {
	if (TYPE(cons) != T_CONS) {
	    str2 = rb_funcall(cons, to_s, 0);
	    str_cat(str1, RSTRING(str2)->ptr, RSTRING(str2)->len);
	    break;
	}
	str2 = rb_funcall(cons->car, to_s, 0);
	str_cat(str1, RSTRING(str2)->ptr, RSTRING(str2)->len);
	cons = RCONS(cons->cdr);
    }

    return str1;
}

static VALUE
Fcons_inspect(cons)
    struct RCons *cons;
{
    VALUE str1, str2;
    ID inspect = rb_intern("_inspect");

    str1 = rb_funcall(cons->car, inspect, 0, 0);
    str2 = rb_funcall(cons->cdr, inspect, 0, 0);
    str_cat(str1, "::", 2);
    str_cat(str1, RSTRING(str2)->ptr, RSTRING(str2)->len);

    return str1;
}

static VALUE
Fcons_copy(list)
    struct RCons *list;
{
    VALUE cdr = list->cdr;

    if (TYPE(cdr) == T_CONS)
	return cons_new(list->car, Fcons_copy(list->cdr));
    else
	return cons_new(list->car, cdr);
}

extern VALUE C_Kernel;
extern VALUE M_Enumerable;

Init_Cons()
{
    C_Cons  = rb_define_class("Cons", C_Object);

    rb_undef_method(CLASS_OF(C_Cons), "new");
    rb_undef_method(C_Cons, "clone");

    rb_include_module(C_Cons, M_Enumerable);

    rb_define_method(C_Cons, "car", Fcons_car, 0);
    rb_define_method(C_Cons, "cdr", Fcons_cdr, 0);

    rb_define_method(C_Cons, "car=", Fcons_set_car, 1);
    rb_define_method(C_Cons, "cdr=", Fcons_set_cdr, 1);

    rb_define_method(C_Cons, "==", Fcons_equal, 1);
    rb_define_method(C_Cons, "hash", Fcons_hash, 0);
    rb_define_method(C_Cons, "length", Fcons_length, 0);

    rb_define_method(C_Cons, "to_s", Fcons_to_s, 0);
    rb_define_method(C_Cons, "_inspect", Fcons_inspect, 0);

    /* methods to access as list */
    rb_define_method(C_Cons, "[]", Fcons_aref, 1);
    rb_define_method(C_Cons, "[]=", Fcons_aset, 2);
    rb_define_method(C_Cons, "each", Fcons_each, 0);

    rb_define_method(C_Cons, "copy", Fcons_copy, 0);

    rb_define_method(C_Kernel, "::", assoc_new, 1);
}