aboutsummaryrefslogtreecommitdiff
path: root/src/as_tokenizer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/as_tokenizer.c')
-rw-r--r--src/as_tokenizer.c49
1 files changed, 27 insertions, 22 deletions
diff --git a/src/as_tokenizer.c b/src/as_tokenizer.c
index 23c0ce1..4cb695e 100644
--- a/src/as_tokenizer.c
+++ b/src/as_tokenizer.c
@@ -3,8 +3,9 @@
#include <string.h>
#include "as_tokenizer.h"
+#include "utils.h"
-int InputStream_nextChar(InputStream *s) {
+int InputStream_nextChar(InputStream s) {
if (s->cursor == -1) {
return EOF;
}
@@ -28,7 +29,7 @@ int InputStream_nextChar(InputStream *s) {
return c;
}
-int InputStream_peekChar(InputStream *s) {
+int InputStream_peekChar(InputStream s) {
if (s->cursor == -1) {
return EOF;
}
@@ -79,7 +80,8 @@ int isPartOfIndentifier(int c) {
return 0;
}
-Token nextTokenImpl(InputStream *s) {
+Token nextTokenImpl(Allocator alct, InputStream s) {
+ Token t = allocate(alct, sizeof(struct token));
int c;
while (1) {
c = InputStream_peekChar(s);
@@ -88,12 +90,13 @@ Token nextTokenImpl(InputStream *s) {
}
if (c == '\n') {
InputStream_nextChar(s);
- Token t = (Token){.type = NEWLINE, .line = s->line, .col = s->col};
+ *t = (struct token){.type = NEWLINE, .line = s->line, .col = s->col};
return t;
}
if (c == ':') {
InputStream_nextChar(s);
- return (Token){.type = COLON, .line = s->line, .col = s->col};
+ *t = (struct token){.type = COLON, .line = s->line, .col = s->col};
+ return t;
}
if (c == ' ' || c == '\t') {
InputStream_nextChar(s);
@@ -109,12 +112,13 @@ Token nextTokenImpl(InputStream *s) {
break;
}
}
- return (Token){.type = ARG, .ival = ival, .line = s->line, .col = s->col};
+ *t = (struct token){.type = ARG, .ival = ival, .line = s->line, .col = s->col};
+ return t;
}
if (isStartOfIndentifier(c)) {
size_t line = s->line;
size_t col = s->col;
- char *sval = malloc(256);
+ char *sval = allocate(alct, 256);
size_t i = 0;
while(1) {
if (i >= 255) {
@@ -130,37 +134,38 @@ Token nextTokenImpl(InputStream *s) {
}
sval[i] = '\0';
if (isOp(sval)) {
- return (Token){.type = OP, .sval = sval, .line = line, .col = col};
+ *t = (struct token){.type = OP, .sval = sval, .line = line, .col = col};
+ return t;
}
- return (Token){.type = TAG, .sval = sval, .line = line, .col = col};
+ *t = (struct token){.type = 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
- return (Token){.type = ENDOFFILE};
+ *t = (struct token){.type = ENDOFFILE};
+ return t;
}
-Token *nextToken(TokenStream *ts) {
+Token nextToken(Allocator alct, TokenStream ts) {
if (ts->buf != NULL) {
- Token *t = ts->buf;
+ Token t = ts->buf;
ts->buf = NULL;
return t;
}
- Token *t = malloc(sizeof(Token));
- *t = nextTokenImpl(ts->s);
+ Token t = nextTokenImpl(alct, ts->s);
return t;
}
-Token *peekToken(TokenStream *ts) {
+Token peekToken(Allocator alct, TokenStream ts) {
if (ts->buf != NULL) {
return ts->buf;
}
- ts->buf = malloc(sizeof(Token));
- *(ts->buf) = nextTokenImpl(ts->s);
+ ts->buf = nextTokenImpl(alct, ts->s);
return ts->buf;
}
-void printToken(Token *t) {
+void printToken(Token t) {
switch (t->type) {
case OP:
printf("OP: %s, line: %d, col: %d\n", t->sval, t->line, t->col);
@@ -183,15 +188,15 @@ void printToken(Token *t) {
}
}
-TokenStream* makeTokenStream(FILE* fp) {
- InputStream *s = malloc(sizeof(InputStream));
+TokenStream makeTokenStream(Allocator alct, FILE* fp) {
+ InputStream s = allocate(alct, sizeof(struct inputStream));
s->fp = fp;
- s->buf = malloc(INPUT_STREAM_BUF_SIZE);
+ s->buf = allocate(alct, INPUT_STREAM_BUF_SIZE);
s->buf_pos = 0;
s->cursor = 0;
s->line = 1;
s->col = 1;
- TokenStream *ts = malloc(sizeof(TokenStream));
+ TokenStream ts = allocate(alct, sizeof(struct tokenStream));
ts->s = s;
ts->buf = NULL;
return ts;