#include #include #include #include #include #include "base.c" int main() { typedef struct { char *name; char *serialized_operator; } Task; Task kinds[] = { {"EOF", 0}, {"ERROR", 0}, {"IDENT", 0}, {"KEYWORD", 0}, {"INT", 0}, {"FLOAT", 0}, {"CHAR", 0}, {"STRING", 0}, {"LPAREN", "("}, {"RPAREN", ")"}, {"LBRACKET", "["}, {"RBRACKET", "]"}, {"LBRACE", "{"}, {"RBRACE", "}"}, {"COMMA", ","}, {"DOT", "."}, {"ARROW", "->"}, {"ELLIPSIS", "..."}, {"COLON", ":"}, {"SEMICOLON", ";"}, {"QUESTION", "?"}, {"HASH", "#"}, {"HASHHASH", "##"}, {"PLUS", "+"}, {"MINUS", "-"}, {"STAR", "*"}, {"SLASH", "/"}, {"PERCENT", "%"}, {"INC", "++"}, {"DEC", "--"}, {"ASSIGN", "="}, {"PLUS_ASSIGN", "+="}, {"MINUS_ASSIGN", "-="}, {"MUL_ASSIGN", "*="}, {"DIV_ASSIGN", "/="}, {"MOD_ASSIGN", "%="}, {"LSHIFT_ASSIGN", "<<="}, {"RSHIFT_ASSIGN", ">>="}, {"AND_ASSIGN", "&="}, {"XOR_ASSIGN", "^="}, {"OR_ASSIGN", "|="}, {"EQ", "=="}, {"NEQ", "!="}, {"LT", "<"}, {"LEQ", "<="}, {"GT", ">"}, {"GEQ", ">="}, {"NOT", "!"}, {"BITNOT", "~"}, {"BITAND", "&"}, {"BITOR", "|"}, {"BITXOR", "^"}, {"AND", "&&"}, {"OR", "||"}, {"LSHIFT", "<<"}, {"RSHIFT", ">>"}, }; printf("// auto generated by meta.c\n"); printf("typedef enum {\n"); for (int i = 0; i < ilen(kinds); i += 1) { printf(" TOK_%s,\n", kinds[i].name); } printf("} Token_Kind;\n"); printf("char *token_to_op(Token_Kind kind) {\n"); printf(" switch (kind) {\n"); for (int i = 0; i < ilen(kinds); i += 1) { if (kinds[i].serialized_operator) { printf(" case TOK_%s: return \"%s\";\n", kinds[i].name, kinds[i].serialized_operator); } } printf(" default: return 0;\n"); printf(" }\n"); printf("}\n"); printf("char *token_to_name(Token_Kind kind) {\n"); printf(" switch (kind) {\n"); for (int i = 0; i < ilen(kinds); i += 1) { if (kinds[i].name) { printf(" case TOK_%s: return \"%s\";\n", kinds[i].name, kinds[i].name); } } printf(" default: return \"\";\n"); printf(" }\n"); printf("}\n"); }