thunk.c 4.71 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
48
49
50
51
52
53
54
55
56
57
58

double readR(Thunk* thunk) {
    assert(thunk != NULL);
    
    if (thunk->desc != (Desc*) __REAL__) {
        printf("readC: not a real: ");
        printDesc(thunk->desc);
        exit(-1);
    }
    
    return (char) thunk->_real;
}
59
#endif
60
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
61
62
bool is_hnf(Thunk* thunk)
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
63
    return thunk != NULL && thunk->desc->hnf;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
64
65
}

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
66
67
68
void print(bool force) {
    Thunk* thunk = pop_a();
    
69
    while (thunk->desc == (Desc*) __FORWARD_PTR__) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
70
71
72
73
74
75
        thunk = thunk->_forward_ptr;
    }

    printf("[");

    if (thunk->desc->type == FT_BOXED_LIT) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
76
        if ((FunEntry*) thunk->desc == __INT__) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
77
            printf("%d", thunk->_int);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
78
        } else if ((FunEntry*) thunk->desc == __BOOL__) {
79
            if (thunk->_int) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
80
81
82
83
                printf("True");
            } else {
                printf("False");
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
84
        } else if ((FunEntry*) thunk->desc == __CHAR__) {
85
            printf("%c", (char) thunk->_int);
86
87
        } else if ((FunEntry*) thunk->desc == __REAL__) {
            printf("%G", thunk->_real);            
88
        } else if ((FunEntry*) thunk->desc == __STRING_PTR__) {
89
            printf("\"");
90
            for(int i=0; i< thunk->_string_ptr->length; i++)
91
            {
92
                printf("%c", thunk->_string_ptr->chars[i]);
93
            }
94
            printf("\"");            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
95
        } else if ((FunEntry*) thunk->desc == __ARRAY__) {
96
97
                        
            if(thunk->_array.type == AET_CHAR)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
98
            {
99
                printf("\"");
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
100
101
102
                for(int i=0; i< thunk->_array.length; i++)
                {
                    printf("%c", thunk->_array._chars[i]);
103
104
                }
                printf("\"");                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
105
106
107
            }
            else
            {
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
                bool first = true;
                
                printf("{");                
                for(int i=0; i< thunk->_array.length; i++)
                {
                    if(!first)
                    {
                        printf(",");
                    }
                    else
                    {
                        first = false;
                    }
                    
                    switch(thunk->_array.type)
                    {
                        case AET_BOOL:
                            if(thunk->_array._bools[i])
                            {
                                printf("True");
                            }
                            else
                            {
                                printf("False");
                            }
                            break;
                        case AET_CHAR:
                            printf("%c", thunk->_array._chars[i]);
                            break;                            
                        case AET_INT:
                            printf("%d", thunk->_array._ints[i]);
                            break;
                        case AET_REAL:
                            printf("%G", thunk->_array._reals[i]);
                            break;
                        case AET_OTHER:
                            push_a(thunk->_array._elems[i]);
                            print(true);
                            break;
                    }
                    
                }                
                printf("}");            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
151
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
152
153
        } else {
            printf("print: unhandled BOXED LIT\n");
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
154
            printDesc(thunk->desc);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
155
156
157
158
159
            exit(-1);
        }
    } else {
        int arity = printDesc(thunk->desc);

160
161
162
163
164
165
        // 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
166
167
        for (int i = 0; i < arity; i++) {
            printf(" ");
168
169

            if (force) peek_a()->desc->eval();
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
170
            print(force);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
171
        }
172
        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
173
174
175
    }

    printf("]");
176
}