morph/src/token.c
2025-06-30 19:19:49 +03:00

128 lines
3.6 KiB
C

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <token.h>
// clang-format off
const char* OP_LIST[] = {
[OP_ADD] = "add",
[OP_SUB] = "sub",
[OP_MUL] = "mul",
[OP_DIV] = "div",
[OP_MOD] = "mod",
[OP_EQ] = "eq",
[OP_GT] = "gt",
[OP_LT] = "lt",
[OP_GE] = "ge",
[OP_LE] = "le",
[OP_NE] = "ne",
[OP_SHR] = "shr",
[OP_SHL] = "shl",
[OP_OR] = "or",
[OP_AND] = "and",
[OP_NOT] = "not",
[OP_DUP] = "dup",
[OP_SWAP] = "swap",
[OP_DROP] = "drop",
[OP_OVER] = "over",
[OP_ROT] = "rot",
[OP_LOAD8] = "load8",
[OP_STORE8] = "store8",
[OP_LOAD16] = "load16",
[OP_STORE16] = "store16",
[OP_LOAD32] = "load32",
[OP_STORE32] = "store32",
[OP_LOAD64] = "load64",
[OP_STORE64] = "store64",
[OP_SYSCALL0] = "syscall0",
[OP_SYSCALL1] = "syscall1",
[OP_SYSCALL2] = "syscall2",
[OP_SYSCALL3] = "syscall3",
[OP_SYSCALL4] = "syscall4",
[OP_SYSCALL5] = "syscall5",
[OP_SYSCALL6] = "syscall6",
[OP_ARGC] = "__argv__",
[OP_ARGV] = "__argv__",
[OP_CAST_PTR] = "cast(ptr)",
[OP_CAST_INT] = "cast(int)",
[OP_CAST_BOOL] = "cast(bool)",
[OP_HERE] = "__here__",
[OP_PRINT] = "__print__"
};
const char* KW_LIST[] = {
[KW_FN] = "fn",
[KW_DO] = "do",
[KW_END] = "end",
[KW_WITH] = "with",
[KW_RETURNS] = "returns",
[KW_STRUCT] = "struct",
[KW_ENUM] = "enum",
[KW_IF] = "if",
[KW_ELSE] = "else",
[KW_WHILE] = "while",
[KW_CONST] = "const",
[KW_MEMORY] = "memory",
[KW_INCLUDE] = "include",
};
const char* get_tok_type_str_dbg(token_type_t tok_t) {
switch (tok_t) {
case (TT_KW): return "Keyword";
case (TT_OP): return "Operator";
case (TT_IDENT): return "Identifier";
case (TT_PUSH_CHAR): return "Char Literal";
case (TT_PUSH_INT): return "Intager";
case (TT_PUSH_STR): return "String";
case (TT_PUSH_CSTR): return "CString";
case (TT_PUSH_MEM): return "Memory address";
case (TT_PUSH_FLOAT): return "Float";
case (TT_NONE): assert(true && "Invalid");
}
return "Unreachable";
}
const char* get_tok_str_dbg(token_t* tok) {
int buf_size = 1024*4;
char* buf = (char*)malloc(buf_size);
switch (tok->type) {
case (TT_KW): {
snprintf(buf, buf_size, "Keyword '%s'", KW_LIST[tok->kw_type]);
}; break;
case (TT_OP): {
snprintf(buf, buf_size, "Operator '%s'", OP_LIST[tok->op_type]);
}; break;
case (TT_IDENT): {
snprintf(buf, buf_size, "Identifier '%s'", tok->str_v);
}; break;
case (TT_PUSH_CHAR): {
snprintf(buf, buf_size, "'%c'", tok->char_v);
}; break;
case (TT_PUSH_INT): {
snprintf(buf, buf_size, "%zu", tok->int_v);
}; break;
case (TT_PUSH_STR): {
while (strlen(tok->str_v) > buf_size-2) {
buf = realloc(buf, buf_size*=2);
}
snprintf(buf, buf_size, "\"%s\"", tok->str_v);
}; break;
case (TT_PUSH_CSTR): {
while (strlen(tok->str_v) > buf_size-3) {
buf = realloc(buf, buf_size*=2);
}
snprintf(buf, buf_size, "c\"%s\"", tok->str_v);
}; break;
case (TT_PUSH_MEM): {
snprintf(buf, buf_size, "Memory address label '%s'", tok->str_v);
}; break;
case (TT_PUSH_FLOAT): {
snprintf(buf, buf_size, "%f", tok->float_v);
}; break;
case (TT_NONE): assert(true && "Invalid");
}
return buf;
}
// clang-format on