Skip to content

Commit ce7accc

Browse files
committed
proper implementation of print_any
1 parent 3912f4a commit ce7accc

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

runtime.c

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static int64_t* tospace_end;
1515
static int initialized = 0;
1616

1717
/*
18-
Object Tag (64 bits)
18+
Tuple Tag (64 bits)
1919
#b|- 7 bit unused -|- 50 bit field [50, 0] -| 6 bits length -| 1 bit isNotForwarding Pointer
2020
* If the bottom-most bit is zero, the tag is really a forwarding pointer.
2121
* Otherwise, its an object tag. In that case, the next
@@ -448,6 +448,42 @@ void print_ellipsis() {
448448
printf("#(...)");
449449
}
450450

451+
static const int ANY_TAG_MASK = 3;
452+
static const int ANY_TAG_LEN = 2;
453+
static const int ANY_TAG_INT = 0;
454+
static const int ANY_TAG_BOOL = 1;
455+
static const int ANY_TAG_VEC = 2;
456+
static const int ANY_TAG_FUN = 3;
457+
458+
int any_tag(int64_t any) {
459+
return any & ANY_TAG_MASK;
460+
}
461+
451462
void print_any(int64_t any) {
452-
printf("%" PRId64, any >> 2);
463+
switch (any_tag(any)) {
464+
case ANY_TAG_INT:
465+
printf("%" PRId64, any >> ANY_TAG_LEN);
466+
break;
467+
case ANY_TAG_BOOL:
468+
if (any >> ANY_TAG_LEN) {
469+
printf("#t");
470+
} else {
471+
printf("#f");
472+
}
473+
break;
474+
case ANY_TAG_VEC: {
475+
int64_t* vector_ptr = (int64_t*) (any & ~ANY_TAG_MASK);
476+
int64_t tag = vector_ptr[0];
477+
unsigned char len = get_length(tag);
478+
printf("#(");
479+
for (int i = 0; i != len; ++i) {
480+
print_any(vector_ptr[i + 1]);
481+
}
482+
printf(")");
483+
break;
484+
}
485+
case ANY_TAG_FUN:
486+
printf("#<procedure>");
487+
break;
488+
}
453489
}

tests/student-tests/r5_12.rkt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
(define (app [f : (Integer -> Integer)]) : Integer
1+
(define (app1 [f : (Integer -> Integer)]) : Integer
22
(f 42))
33

44
(define (add1 [n : Integer]) : Integer (+ n 1))
55

6-
(app (if (eq? (read) 0) add1 (lambda: ([x : Integer]) : Integer x)))
6+
(app1 (if (eq? (read) 0) add1 (lambda: ([x : Integer]) : Integer x)))

tests/student-tests/r5_5.rkt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
(define (app [f : (Integer -> Integer)] [x : Integer])
1+
(define (app1 [f : (Integer -> Integer)] [x : Integer])
22
: Integer
33
(f x))
44

5-
(app (lambda: ([x : Integer]) : Integer x) 42)
5+
(app1 (lambda: ([x : Integer]) : Integer x) 42)

0 commit comments

Comments
 (0)