Add KW's and OP's
This commit is contained in:
parent
7e95a9af34
commit
f6060fb142
|
@ -5,7 +5,6 @@
|
|||
#include <stddef.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
|
||||
typedef enum token_type_e {
|
||||
TT_NONE = 0,
|
||||
TT_KW,
|
||||
|
@ -16,6 +15,7 @@ typedef enum token_type_e {
|
|||
TT_PUSH_CHAR,
|
||||
TT_PUSH_INT,
|
||||
TT_PUSH_FLOAT,
|
||||
TT_IDENT
|
||||
} token_type_t;
|
||||
|
||||
typedef enum kw_type_e {
|
||||
|
@ -117,8 +117,6 @@ typedef struct token_s {
|
|||
};
|
||||
} token_t;
|
||||
|
||||
|
||||
|
||||
extern const char* OP_LIST[];
|
||||
extern const char* KW_LIST[];
|
||||
#endif // _H_MORPH_TOKEN
|
||||
|
|
29
src/main.c
29
src/main.c
|
@ -1,13 +1,36 @@
|
|||
#include "dynarray.h"
|
||||
#include "token.h"
|
||||
#include <util.h>
|
||||
#include <tokeniser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
int main(int argc, const char** argv) {
|
||||
if (argc < 2) {
|
||||
printf("Usage: %s [source]\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
char* str = read_to_string(argv[1]);
|
||||
tokenise_string(argv[1], str);
|
||||
return 0;
|
||||
token_t* tokens = tokenise_string((char*)argv[1], str);
|
||||
int token_count = dynarray_length(tokens);
|
||||
|
||||
for (int i = 0; i < token_count; i++) {
|
||||
token_t tok = tokens[i];
|
||||
switch (tok.type) {
|
||||
case (TT_NONE):
|
||||
break;
|
||||
case (TT_KW): {
|
||||
printf("TOK: KW %s\n", KW_LIST[tok.kw_type]);
|
||||
} break;
|
||||
case (TT_OP): {
|
||||
printf("TOK: OP %s\n", OP_LIST[tok.op_type]);
|
||||
} break;
|
||||
case (TT_IDENT): {
|
||||
printf("TOK: IDENT \"%s\"\n", tok.str_v);
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#include <token.h>
|
||||
|
||||
// clang-format off
|
||||
|
||||
const char* OP_LIST[] = {
|
||||
[OP_ADD] = "add",
|
||||
[OP_SUB] = "sub",
|
||||
|
@ -59,3 +61,5 @@ const char* KW_LIST[] = {
|
|||
[KW_CONST] = "const",
|
||||
[KW_MEMORY] = "memory"
|
||||
};
|
||||
|
||||
// clang-format on
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <token.h>
|
||||
|
@ -28,16 +29,58 @@ token_t* tokenise_string(char* file_path, char* str) {
|
|||
// TODO: manage memory better
|
||||
// INFO: If you make an ident longer than 4kb i will murder you
|
||||
char* buf = malloc(1024 * 4);
|
||||
int buf_counter = 1;
|
||||
*buf = str[i];
|
||||
while (i < str_len && (str[i + 1] != ' ' || str[i + 1] != '\t' || str[i + 1] != '\n' || str[i + 1] != '\r')) {
|
||||
i += 1;
|
||||
memset(buf, 0, 1024 * 4);
|
||||
int buf_counter = 0;
|
||||
while (i < str_len) {
|
||||
if (str[i] == ' ' || str[i] == '\t' || str[i] == '\r' || str[i] == '\n') {
|
||||
break;
|
||||
}
|
||||
buf[buf_counter++] = str[i];
|
||||
i += 1;
|
||||
}
|
||||
buf[buf_counter + 1] = '\0';
|
||||
printf("'%s'\n", buf);
|
||||
// PERF: I dont know if this makes it faster or slower, need 2 check
|
||||
buf = realloc(buf, strlen(buf) + 1);
|
||||
|
||||
|
||||
bool found = false;
|
||||
for (int i = 1; i < KW_COUNT__; i++) {
|
||||
if (strcmp(KW_LIST[i], buf) == 0) {
|
||||
token_t tok = {
|
||||
.type = TT_KW,
|
||||
.kw_type = (kw_type_t)i,
|
||||
};
|
||||
dynarray_push(tokens, tok);
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (found) {
|
||||
break;
|
||||
}
|
||||
for (int i = 1; i < OP_COUNT__; i++) {
|
||||
if (strcmp(OP_LIST[i], buf) == 0) {
|
||||
token_t tok = {
|
||||
.type = TT_OP,
|
||||
.op_type = (op_type_t)i,
|
||||
};
|
||||
dynarray_push(tokens, tok);
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (found) {
|
||||
break;
|
||||
}
|
||||
token_t tok = {
|
||||
.type = TT_IDENT,
|
||||
.str_v = strdup(buf),
|
||||
};
|
||||
dynarray_push(tokens, tok);
|
||||
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
return tokens;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user