ioiasndoipnasd
This commit is contained in:
parent
f6060fb142
commit
a9b8c9dd5a
|
@ -27,6 +27,9 @@ int main(int argc, const char** argv) {
|
||||||
case (TT_IDENT): {
|
case (TT_IDENT): {
|
||||||
printf("TOK: IDENT \"%s\"\n", tok.str_v);
|
printf("TOK: IDENT \"%s\"\n", tok.str_v);
|
||||||
} break;
|
} break;
|
||||||
|
case (TT_PUSH_INT): {
|
||||||
|
printf("TOK: PUSH_INT \"%zu\"\n", tok.int_v);
|
||||||
|
} break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
100
src/tokeniser.c
100
src/tokeniser.c
|
@ -1,3 +1,4 @@
|
||||||
|
#include <limits.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -5,6 +6,58 @@
|
||||||
#include <tokeniser.h>
|
#include <tokeniser.h>
|
||||||
#include <dynarray.h>
|
#include <dynarray.h>
|
||||||
|
|
||||||
|
int parse_int(const char* s, int* out) {
|
||||||
|
|
||||||
|
/* ----- handle optional sign ----- */
|
||||||
|
long sign = 1;
|
||||||
|
if (*s == '+' || *s == '-') {
|
||||||
|
if (*s == '-') {
|
||||||
|
sign = -1;
|
||||||
|
}
|
||||||
|
++s;
|
||||||
|
}
|
||||||
|
if (*s == '\0') {
|
||||||
|
return -1;
|
||||||
|
} /* sign only? :3 */
|
||||||
|
|
||||||
|
/* ----- binary special‑case ----- */
|
||||||
|
if (s[0] == '0' && (s[1] == 'b' || s[1] == 'B')) {
|
||||||
|
const char* p = s + 2;
|
||||||
|
if (*p == '\0') {
|
||||||
|
return -1;
|
||||||
|
} /* “0b” but no digits */
|
||||||
|
|
||||||
|
long val = 0;
|
||||||
|
for (; *p; ++p) {
|
||||||
|
if (*p == '0' || *p == '1') {
|
||||||
|
val = (val << 1) | (*p - '0');
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
} /* stray char => error */
|
||||||
|
}
|
||||||
|
val *= sign;
|
||||||
|
|
||||||
|
if (val < INT_MIN || val > INT_MAX) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*out = (int)val;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ----- all other radices via strtol ----- */
|
||||||
|
char* end;
|
||||||
|
long val = strtol((sign == -1) ? (s - 1) : s, &end, 0); /* feed original string */
|
||||||
|
if (*end != '\0') {
|
||||||
|
return -1;
|
||||||
|
} /* trailing junk */
|
||||||
|
if (val < INT_MIN || val > INT_MAX) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (int)val;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
token_t* tokenise_string(char* file_path, char* str) {
|
token_t* tokenise_string(char* file_path, char* str) {
|
||||||
const int str_len = strlen(str);
|
const int str_len = strlen(str);
|
||||||
loc_t loc = {0};
|
loc_t loc = {0};
|
||||||
|
@ -25,6 +78,32 @@ token_t* tokenise_string(char* file_path, char* str) {
|
||||||
loc.line += 1;
|
loc.line += 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
case ('c'):
|
||||||
|
case ('"'): {
|
||||||
|
if (str[i] == 'c') {
|
||||||
|
}
|
||||||
|
char* buf = malloc(1024 * 4);
|
||||||
|
bool escaped = false;
|
||||||
|
for (int y = 0;;) {
|
||||||
|
if (str[i] == '"' && !escaped) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (str[i] == '\\' && !escaped) {
|
||||||
|
escaped = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (str[i] == '\\' && escaped) {
|
||||||
|
escaped = false;
|
||||||
|
}
|
||||||
|
buf[y++] = str[i++];
|
||||||
|
}
|
||||||
|
|
||||||
|
token_t tok = {
|
||||||
|
.type = TT_PUSH_STR,
|
||||||
|
.str_v = buf,
|
||||||
|
};
|
||||||
|
dynarray_push(tokens, tok);
|
||||||
|
} break;
|
||||||
default: {
|
default: {
|
||||||
// TODO: manage memory better
|
// TODO: manage memory better
|
||||||
// INFO: If you make an ident longer than 4kb i will murder you
|
// INFO: If you make an ident longer than 4kb i will murder you
|
||||||
|
@ -44,6 +123,15 @@ token_t* tokenise_string(char* file_path, char* str) {
|
||||||
buf = realloc(buf, strlen(buf) + 1);
|
buf = realloc(buf, strlen(buf) + 1);
|
||||||
|
|
||||||
bool found = false;
|
bool found = false;
|
||||||
|
int num;
|
||||||
|
if (parse_int(buf, &num) == 0) {
|
||||||
|
token_t tok = {
|
||||||
|
.type = TT_PUSH_INT,
|
||||||
|
.int_v = num,
|
||||||
|
};
|
||||||
|
dynarray_push(tokens, tok);
|
||||||
|
goto loop_end;
|
||||||
|
}
|
||||||
for (int i = 1; i < KW_COUNT__; i++) {
|
for (int i = 1; i < KW_COUNT__; i++) {
|
||||||
if (strcmp(KW_LIST[i], buf) == 0) {
|
if (strcmp(KW_LIST[i], buf) == 0) {
|
||||||
token_t tok = {
|
token_t tok = {
|
||||||
|
@ -51,13 +139,10 @@ token_t* tokenise_string(char* file_path, char* str) {
|
||||||
.kw_type = (kw_type_t)i,
|
.kw_type = (kw_type_t)i,
|
||||||
};
|
};
|
||||||
dynarray_push(tokens, tok);
|
dynarray_push(tokens, tok);
|
||||||
found = true;
|
goto loop_end;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (found) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
for (int i = 1; i < OP_COUNT__; i++) {
|
for (int i = 1; i < OP_COUNT__; i++) {
|
||||||
if (strcmp(OP_LIST[i], buf) == 0) {
|
if (strcmp(OP_LIST[i], buf) == 0) {
|
||||||
token_t tok = {
|
token_t tok = {
|
||||||
|
@ -65,19 +150,16 @@ token_t* tokenise_string(char* file_path, char* str) {
|
||||||
.op_type = (op_type_t)i,
|
.op_type = (op_type_t)i,
|
||||||
};
|
};
|
||||||
dynarray_push(tokens, tok);
|
dynarray_push(tokens, tok);
|
||||||
found = true;
|
goto loop_end;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (found) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
token_t tok = {
|
token_t tok = {
|
||||||
.type = TT_IDENT,
|
.type = TT_IDENT,
|
||||||
.str_v = strdup(buf),
|
.str_v = strdup(buf),
|
||||||
};
|
};
|
||||||
dynarray_push(tokens, tok);
|
dynarray_push(tokens, tok);
|
||||||
|
loop_end:
|
||||||
free(buf);
|
free(buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user