aboutsummaryrefslogtreecommitdiff
path: root/src/as_tokenizer.c
blob: 6c40b9fb60eada6f9ec01fab6f458e68007e2aed (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "as_tokenizer.h"
#include "as_op.h"
#include "utils.h"

int input_stream_next_char(struct input_stream * s) {
    if (s->cursor == -1) {
        return EOF;
    }
    if (s->buf_pos == s->cursor) {
        size_t n = fread(s->buf, 1, INPUT_STREAM_BUF_SIZE, s->fp);
        if (n == 0) {
            s->cursor = -1;
            return EOF;
        }
        s->buf_pos = n;
        s->cursor = 0;
    }
    int c = s->buf[s->cursor];
    s->cursor++;
    if (c == '\n') {
        s->line++;
        s->col = 1;
    } else {
        s->col++;
    }
    return c;
}

int input_stream_peek_char(struct input_stream * s) {
    if (s->cursor == -1) {
        return EOF;
    }
    if (s->buf_pos == s->cursor) {
        size_t n = fread(s->buf, 1, INPUT_STREAM_BUF_SIZE, s->fp);
        if (n == 0) {
            return EOF;
        }
        s->buf_pos = n;
        s->cursor = 0;
    }
    return s->buf[s->cursor];
}

int is_start_of_identifier(int c) {
    if (c >= 'a' && c <= 'z') {
        return 1;
    }
    if (c >= 'A' && c <= 'Z') {
        return 1;
    }
    if (c == '_') {
        return 1;
    }
    if (c == '.') {
        return 1;
    }
    return 0;
}

int is_part_of_identifier(int c) {
    if (is_start_of_identifier(c)) {
        return 1;
    }
    if (c >= '0' && c <= '9') {
        return 1;
    }
    return 0;
}

struct token * next_token_impl(struct allocator * alct, struct input_stream * s) {
    struct token  * t = allocate(alct, sizeof(struct token));
    int c;
    while (1) {
        c = input_stream_peek_char(s);
        if (c == EOF) {
            break;
        }
        if (c == '\n') {
            input_stream_next_char(s);
            *t = (struct token){.type = TK_NEWLINE, .line = s->line, .col = s->col};
            return t;
        }
        if (c == ':') {
            input_stream_next_char(s);
            *t = (struct token){.type = TK_COLON, .line = s->line, .col = s->col};
            return t;
        }
        if (c == ' ' || c == '\t') {
            input_stream_next_char(s);
            continue;
        }
        if (c >= '0' && c <= '9') {
            int64_t ival = 0;
            while (1) {
                input_stream_next_char(s);
                ival = ival * 10 + (c - '0');
                c = input_stream_peek_char(s);
                if (c < '0' || c > '9') {
                    break;
                }
            } 
            *t = (struct token){.type = TK_ARG, .ival = ival, .line = s->line, .col = s->col};
            return t;
        }
        if (is_start_of_identifier(c)) {
            size_t line = s->line;
            size_t col = s->col;
            char *sval = allocate(alct, 256);
            size_t i = 0;
            while(1) {
                if (i >= 255) {
                    fprintf(stderr, "error: identifier too long\n");
                    exit(1);
                }
                input_stream_next_char(s);
                sval[i++] = c;
                c = input_stream_peek_char(s);
                if (!is_part_of_identifier(c)) {
                    break;
                }
            }
            sval[i] = '\0';
            if (isOp(sval)) {
                *t = (struct token){.type = TK_OP, .sval = sval, .line = line, .col = col};
                return t;
            }
            *t = (struct token){.type = TK_TAG, .sval = sval, .line = line, .col = col};
            return t;
        }
        fprintf(stderr, "error: invalid character %c at line %d, col %d\n", c, s->line, s->col);
    }
    // end of file
    *t = (struct token){.type = TK_ENDOFFILE};
    return t;
}

struct token * next_token(struct allocator * alct, struct token_stream * ts) {
    if (ts->buf != NULL) {
        struct token * t = ts->buf;
        ts->buf = NULL;
        return t;
    }
    struct token  * t = next_token_impl(alct, ts->s);
    return t;
}

struct token * peek_token(struct allocator * alct, struct token_stream * ts) {
    if (ts->buf != NULL) {
        return ts->buf;
    }
    ts->buf = next_token_impl(alct, ts->s);
    return ts->buf;
}

void print_token(struct token  * t) {
    switch (t->type) {
        case TK_OP:
            printf("OP: %s, line: %d, col: %d\n", t->sval, t->line, t->col);
            break;
        case TK_ARG:
            printf("ARG: %ld, line: %d, col: %d\n", t->ival, t->line, t->col);
            break;
        case TK_TAG:
            printf("LABEL: %s, line: %d, col: %d\n", t->sval, t->line, t->col);
            break;
        case TK_COLON:
            printf("COLON\n");
            break;
        case TK_NEWLINE:
            printf("NEWLINE\n");
            break;
        case TK_ENDOFFILE:
            printf("ENDOFFILE\n");
            break;
    }
}

struct token_stream * new_token_stream(struct allocator * alct, FILE* fp) {
    struct input_stream * s = allocate(alct, sizeof(struct input_stream));
    s->fp = fp;
    s->buf = allocate(alct, INPUT_STREAM_BUF_SIZE);
    s->buf_pos = 0;
    s->cursor = 0;
    s->line = 1;
    s->col = 1;
    struct token_stream * ts = allocate(alct, sizeof(struct token_stream));
    ts->s = s;
    ts->buf = NULL;
    return ts;
}