thunk.c 4.66 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

double readR(Thunk* thunk) {
    assert(thunk != NULL);
    
    if (thunk->desc != (Desc*) __REAL__) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
52
        printf("readR: not a real: ");
53
54
55
56
57
58
        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
        thunk = thunk->_forward_ptr;
    }

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
73
    putchar('[');
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
74
75

    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
                fwrite("True",1,4,stdout);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
81
            } else {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
82
                fwrite("False",1,5,stdout);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
83
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
84
        } else if ((FunEntry*) thunk->desc == __CHAR__) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
85
86
87
            putchar('\'');
            putchar((char) thunk->_int);
            putchar('\'');
88
89
        } else if ((FunEntry*) thunk->desc == __REAL__) {
            printf("%G", thunk->_real);            
90
        } else if ((FunEntry*) thunk->desc == __STRING_PTR__) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
91
92
93
            putchar('"');
            fwrite(thunk->_string_ptr->chars,1,thunk->_string_ptr->length,stdout);
            putchar('"');            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
94
        } else if ((FunEntry*) thunk->desc == __ARRAY__) {
95
96
                        
            if(thunk->_array.type == AET_CHAR)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
97
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
98
99
100
                putchar('"');
                fwrite(thunk->_array._chars,1,thunk->_array.length,stdout);
                putchar('"');                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
101
102
103
            }
            else
            {
104
105
                bool first = true;
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
106
                putchar('{');                
107
108
109
110
                for(int i=0; i< thunk->_array.length; i++)
                {
                    if(!first)
                    {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
111
                        putchar(',');
112
113
114
115
116
117
118
119
120
121
122
                    }
                    else
                    {
                        first = false;
                    }
                    
                    switch(thunk->_array.type)
                    {
                        case AET_BOOL:
                            if(thunk->_array._bools[i])
                            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
123
                                fwrite("True",1,4,stdout);
124
125
126
                            }
                            else
                            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
127
                                fwrite("False",1,5,stdout);
128
129
130
                            }
                            break;
                        case AET_CHAR:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
131
                            putchar(thunk->_array._chars[i]);
132
133
134
135
136
137
138
139
140
141
142
143
144
145
                            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;
                    }
                    
                }                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
146
                putchar('}');            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
147
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
148
149
        } else {
            printf("print: unhandled BOXED LIT\n");
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
150
            printDesc(thunk->desc);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
151
152
153
154
155
            exit(-1);
        }
    } else {
        int arity = printDesc(thunk->desc);

156
157
158
159
160
161
        // 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
162
        for (int i = 0; i < arity; i++) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
163
            putchar(' ');
164
165

            if (force) peek_a()->desc->eval();
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
166
            print(force);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
167
        }
168
        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
169
170
    }

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
171
    putchar(']');
172
}