thunk.c 2.93 KB
Newer Older
1
2
#include <stdio.h>
#include <stdlib.h>
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
3
#include <stdbool.h>
4
#include <string.h>
5

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
6
#include "debug.h"
7
8
#include "thunk.h"
#include "mem.h"
9
#include "desc.h"
10

11
#ifdef DEBUG
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
12
int readI(Thunk* thunk) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
13
    assert(thunk != NULL);
14
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
15
    if (thunk->desc != (Desc*) __INT__) {
16
        printf("readI: not an integer: ");
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
17
18
19
        printDesc(thunk->desc);
        exit(-1);
    }
20
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
21
    return thunk->_int;
22
23
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
24
int readB(Thunk* thunk) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
25
26
    assert(thunk != NULL);
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
27
    if (thunk->desc != (Desc*) __BOOL__) {
28
29
        printf("readB: not a boolean: ");
        printDesc(thunk->desc);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
30
31
        exit(-1);
    }
32
    
33
    return thunk->_int;
34
}
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
35
36
37
38
39
40
41
42
43
44

char readC(Thunk* thunk) {
    assert(thunk != NULL);
    
    if (thunk->desc != (Desc*) __CHAR__) {
        printf("readC: not a char: ");
        printDesc(thunk->desc);
        exit(-1);
    }
    
45
    return (char) thunk->_int;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
46
}
47
#endif
48
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
49
50
bool is_hnf(Thunk* thunk)
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
51
    return thunk != NULL && thunk->desc->hnf;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
52
53
}

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
54
55
56
void print(bool force) {
    Thunk* thunk = pop_a();
    
57
    while (thunk->desc == (Desc*) __FORWARD_PTR__) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
58
59
60
61
62
63
        thunk = thunk->_forward_ptr;
    }

    printf("[");

    if (thunk->desc->type == FT_BOXED_LIT) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
64
        if ((FunEntry*) thunk->desc == __INT__) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
65
            printf("%d", thunk->_int);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
66
        } else if ((FunEntry*) thunk->desc == __BOOL__) {
67
            if (thunk->_int) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
68
69
70
71
                printf("True");
            } else {
                printf("False");
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
72
        } else if ((FunEntry*) thunk->desc == __CHAR__) {
73
            printf("%c", (char) thunk->_int);
74
75
76
77
78
        } else if ((FunEntry*) thunk->desc == __STRING_PTR__) {
            for(int i=0; i< thunk->_string_ptr->length; i++)
            {
                printf("%c", thunk->_string_ptr->chars[i]);
            }            
79
80
        } else if ((FunEntry*) thunk->desc == __STRING_PTR__) {
            for(int i=0; i< thunk->_string_ptr->length; i++)
81
            {
82
                printf("%c", thunk->_string_ptr->chars[i]);
83
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
84
85
86
87
88
89
90
91
92
93
94
95
        } else if ((FunEntry*) thunk->desc == __ARRAY__) {
            if(thunk->_array.is_string)
            {
                for(int i=0; i< thunk->_array.length; i++)
                {
                    printf("%c", thunk->_array._chars[i]);
                }                            
            }
            else
            {
                printf("print: unhandled ARRAY type\n");
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
96
97
        } else {
            printf("print: unhandled BOXED LIT\n");
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
98
            printDesc(thunk->desc);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
99
100
101
102
103
            exit(-1);
        }
    } else {
        int arity = printDesc(thunk->desc);

104
105
106
107
108
109
        // Put the arguments on the stack before any "eval", otherwise GC
        // may screws up the pointers
        for (int i = 0; i < arity; i++) {
            push_a(thunk->_args[arity-i-1]);
        }
        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
110
111
        for (int i = 0; i < arity; i++) {
            printf(" ");
112
113

            if (force) peek_a()->desc->eval();
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
114
            print(force);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
115
        }
116
        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
117
118
119
    }

    printf("]");
120
}