aboutsummaryrefslogtreecommitdiff
path: root/src/as_tokenizer.c
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-03-23 23:37:25 +0800
committerMistivia <i@mistivia.com>2025-03-23 23:37:25 +0800
commit4f7f0aa49844756dbf430f35600d7c88e1a6a730 (patch)
tree7b3fcc235f33ebec5c7623b6cc3946a138162682 /src/as_tokenizer.c
parent45e53e55aa555d5a53d9d489e8447e9112eac1f0 (diff)
refactor names
Diffstat (limited to 'src/as_tokenizer.c')
-rw-r--r--src/as_tokenizer.c76
1 files changed, 38 insertions, 38 deletions
diff --git a/src/as_tokenizer.c b/src/as_tokenizer.c
index 4a5353a..54e0969 100644
--- a/src/as_tokenizer.c
+++ b/src/as_tokenizer.c
@@ -6,7 +6,7 @@
#include "as_op.h"
#include "utils.h"
-int InputStream_nextChar(InputStream s) {
+int input_stream_next_char(input_stream_t s) {
if (s->cursor == -1) {
return EOF;
}
@@ -30,7 +30,7 @@ int InputStream_nextChar(InputStream s) {
return c;
}
-int InputStream_peekChar(InputStream s) {
+int input_stream_peek_char(input_stream_t s) {
if (s->cursor == -1) {
return EOF;
}
@@ -45,7 +45,7 @@ int InputStream_peekChar(InputStream s) {
return s->buf[s->cursor];
}
-int isStartOfIndentifier(int c) {
+int is_start_of_identifier(int c) {
if (c >= 'a' && c <= 'z') {
return 1;
}
@@ -58,8 +58,8 @@ int isStartOfIndentifier(int c) {
return 0;
}
-int isPartOfIndentifier(int c) {
- if (isStartOfIndentifier(c)) {
+int is_part_of_identifier(int c) {
+ if (is_start_of_identifier(c)) {
return 1;
}
if (c >= '0' && c <= '9') {
@@ -68,42 +68,42 @@ int isPartOfIndentifier(int c) {
return 0;
}
-Token nextTokenImpl(Allocator alct, InputStream s) {
- Token t = allocate(alct, sizeof(struct token));
+token_t next_token_impl(allocator_t alct, input_stream_t s) {
+ token_t t = allocate(alct, sizeof(struct token));
int c;
while (1) {
- c = InputStream_peekChar(s);
+ c = input_stream_peek_char(s);
if (c == EOF) {
break;
}
if (c == '\n') {
- InputStream_nextChar(s);
- *t = (struct token){.type = NEWLINE, .line = s->line, .col = s->col};
+ input_stream_next_char(s);
+ *t = (struct token){.type = TK_NEWLINE, .line = s->line, .col = s->col};
return t;
}
if (c == ':') {
- InputStream_nextChar(s);
- *t = (struct token){.type = COLON, .line = s->line, .col = s->col};
+ input_stream_next_char(s);
+ *t = (struct token){.type = TK_COLON, .line = s->line, .col = s->col};
return t;
}
if (c == ' ' || c == '\t') {
- InputStream_nextChar(s);
+ input_stream_next_char(s);
continue;
}
if (c >= '0' && c <= '9') {
int64_t ival = 0;
while (1) {
- InputStream_nextChar(s);
+ input_stream_next_char(s);
ival = ival * 10 + (c - '0');
- c = InputStream_peekChar(s);
+ c = input_stream_peek_char(s);
if (c < '0' || c > '9') {
break;
}
}
- *t = (struct token){.type = ARG, .ival = ival, .line = s->line, .col = s->col};
+ *t = (struct token){.type = TK_ARG, .ival = ival, .line = s->line, .col = s->col};
return t;
}
- if (isStartOfIndentifier(c)) {
+ if (is_start_of_identifier(c)) {
size_t line = s->line;
size_t col = s->col;
char *sval = allocate(alct, 256);
@@ -113,78 +113,78 @@ Token nextTokenImpl(Allocator alct, InputStream s) {
fprintf(stderr, "error: identifier too long\n");
exit(1);
}
- InputStream_nextChar(s);
+ input_stream_next_char(s);
sval[i++] = c;
- c = InputStream_peekChar(s);
- if (!isPartOfIndentifier(c)) {
+ c = input_stream_peek_char(s);
+ if (!is_part_of_identifier(c)) {
break;
}
}
sval[i] = '\0';
if (isOp(sval)) {
- *t = (struct token){.type = OP, .sval = sval, .line = line, .col = col};
+ *t = (struct token){.type = TK_OP, .sval = sval, .line = line, .col = col};
return t;
}
- *t = (struct token){.type = TAG, .sval = sval, .line = line, .col = col};
+ *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 = ENDOFFILE};
+ *t = (struct token){.type = TK_ENDOFFILE};
return t;
}
-Token nextToken(Allocator alct, TokenStream ts) {
+token_t next_token(allocator_t alct, token_stream_t ts) {
if (ts->buf != NULL) {
- Token t = ts->buf;
+ token_t t = ts->buf;
ts->buf = NULL;
return t;
}
- Token t = nextTokenImpl(alct, ts->s);
+ token_t t = next_token_impl(alct, ts->s);
return t;
}
-Token peekToken(Allocator alct, TokenStream ts) {
+token_t peek_token(allocator_t alct, token_stream_t ts) {
if (ts->buf != NULL) {
return ts->buf;
}
- ts->buf = nextTokenImpl(alct, ts->s);
+ ts->buf = next_token_impl(alct, ts->s);
return ts->buf;
}
-void printToken(Token t) {
+void print_token(token_t t) {
switch (t->type) {
- case OP:
+ case TK_OP:
printf("OP: %s, line: %d, col: %d\n", t->sval, t->line, t->col);
break;
- case ARG:
+ case TK_ARG:
printf("ARG: %ld, line: %d, col: %d\n", t->ival, t->line, t->col);
break;
- case TAG:
+ case TK_TAG:
printf("LABEL: %s, line: %d, col: %d\n", t->sval, t->line, t->col);
break;
- case COLON:
+ case TK_COLON:
printf("COLON\n");
break;
- case NEWLINE:
+ case TK_NEWLINE:
printf("NEWLINE\n");
break;
- case ENDOFFILE:
+ case TK_ENDOFFILE:
printf("ENDOFFILE\n");
break;
}
}
-TokenStream makeTokenStream(Allocator alct, FILE* fp) {
- InputStream s = allocate(alct, sizeof(struct inputStream));
+token_stream_t new_token_stream(allocator_t alct, FILE* fp) {
+ input_stream_t 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;
- TokenStream ts = allocate(alct, sizeof(struct tokenStream));
+ token_stream_t ts = allocate(alct, sizeof(struct token_stream));
ts->s = s;
ts->buf = NULL;
return ts;