4coder/4ed_metagen.cpp

508 lines
12 KiB
C++
Raw Normal View History

2016-02-25 23:52:11 +00:00
/*
* Mr. 4th Dimention - Allen Webster
*
* 25.02.2016
*
* File editing view for 4coder
*
*/
// TOP
2016-03-04 23:10:00 +00:00
#define FCPP_STRING_IMPLEMENTATION
#include "4coder_string.h"
2016-02-25 23:52:11 +00:00
2016-02-27 17:34:13 +00:00
struct Struct_Field{
char *type;
char *name;
};
void to_lower(char *src, char *dst){
char *c, ch;
2016-03-05 04:10:55 +00:00
for (c = src; *c != 0; ++c){
ch = char_to_lower(*c);
*dst++ = ch;
}
*dst = 0;
}
void to_upper(char *src, char *dst){
char *c, ch;
for (c = src; *c != 0; ++c){
ch = char_to_upper(*c);
*dst++ = ch;
}
*dst = 0;
}
void to_camel(char *src, char *dst){
char *c, ch;
int is_first = 1;
2016-02-27 17:34:13 +00:00
for (c = src; *c != 0; ++c){
ch = *c;
2016-03-05 04:10:55 +00:00
if (char_is_alpha_numeric_true(ch)){
if (is_first){
is_first = 0;
ch = char_to_upper(ch);
}
else{
ch = char_to_lower(ch);
}
}
else{
is_first = 1;
2016-02-27 17:34:13 +00:00
}
*dst++ = ch;
}
*dst = 0;
}
2016-02-25 23:52:11 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
2016-02-27 17:34:13 +00:00
void struct_begin(FILE *file, char *name){
fprintf(file, "struct %s{\n", name);
}
void struct_fields(FILE *file, Struct_Field *fields, int count){
int i;
for (i = 0; i < count; ++i){
fprintf(file, " %s %s;\n", fields[i].type, fields[i].name);
}
}
void struct_end(FILE *file){
fprintf(file, "};\n\n");
}
2016-03-04 23:10:00 +00:00
void enum_begin(FILE *file, char *name){
fprintf(file, "enum %s{\n", name);
}
2016-02-25 23:52:11 +00:00
char *keys_that_need_codes[] = {
"back",
"up",
"down",
"left",
"right",
"del",
"insert",
"home",
"end",
"page_up",
"page_down",
"esc",
"f1",
"f2",
"f3",
"f4",
"f5",
"f6",
"f7",
"f8",
"f9",
"f10",
"f11",
"f12",
"f13",
"f14",
"f15",
"f16",
};
2016-02-27 17:34:13 +00:00
char* generate_keycode_enum(){
2016-02-25 23:52:11 +00:00
FILE *file;
2016-02-27 17:34:13 +00:00
char *filename = "4coder_keycodes.h";
2016-02-25 23:52:11 +00:00
int i, count;
unsigned char code = 1;
file = fopen(filename, "wb");
fprintf(file, "enum Key_Code{\n");
count = ArrayCount(keys_that_need_codes);
for (i = 0; i < count;){
if (strcmp(keys_that_need_codes[i], "f1") == 0 && code < 0x7F){
code = 0x7F;
}
switch (code){
case '\n': code++; break;
case '\t': code++; break;
case 0x20: code = 0x7F; break;
default:
2016-02-27 17:34:13 +00:00
fprintf(file, " key_%s = %d,\n", keys_that_need_codes[i++], code++);
2016-02-25 23:52:11 +00:00
break;
}
}
fprintf(file, "};\n");
fclose(file);
2016-02-27 17:34:13 +00:00
return(filename);
}
2016-03-04 06:06:11 +00:00
//////////////////////////////////////////////////////////////////////////////////////////////////
2016-02-27 17:34:13 +00:00
char daction_enum_name[] = "Action_Type";
char *daction_enum[] = {
"OPEN",
2016-02-28 20:30:51 +00:00
"OPEN_BACKGROUND",
"SET_LINE",
2016-02-27 17:34:13 +00:00
"SAVE_AS",
"SAVE",
"NEW",
"SWITCH",
"TRY_KILL",
"KILL",
};
2016-02-28 20:30:51 +00:00
char str_alloc_copy[] =
"internal String\n"
"str_alloc_copy(General_Memory *general, String str){\n"
" String result;\n"
" result.memory_size = str.memory_size + 1;\n"
" result.size = str.size;\n"
" result.str = (char*)general_memory_allocate(general, result.memory_size, 0);\n"
" memcpy(result.str, str.str, str.size);\n"
" result.str[result.size] = 0;\n"
" return(result);"
"}\n\n";
2016-02-27 17:34:13 +00:00
char daction_name[] = "Delayed_Action";
Struct_Field daction_fields[] = {
{"Action_Type", "type"},
};
Struct_Field daction_fields_primary[] = {
{"String", "string"},
{"Panel*", "panel"},
{"Editing_File*", "file"},
2016-02-28 20:30:51 +00:00
{"i32", "integer"},
2016-02-27 17:34:13 +00:00
};
enum Daction_Field_Handle{
dfph_null,
dfph_string,
dfph_panel,
dfph_file,
2016-02-28 20:30:51 +00:00
dfph_integer,
2016-02-27 17:34:13 +00:00
};
Daction_Field_Handle dact_param_sets[] = {
dfph_panel, dfph_null,
2016-02-28 20:30:51 +00:00
dfph_string, dfph_null,
2016-02-27 17:34:13 +00:00
dfph_string, dfph_panel, dfph_null,
2016-02-28 20:30:51 +00:00
dfph_string, dfph_file, dfph_null,
dfph_panel, dfph_integer, dfph_null,
2016-02-27 17:34:13 +00:00
};
char delay_name[] = "Delay";
Struct_Field delay_fields[] = {
2016-02-28 20:30:51 +00:00
{"General_Memory*", "general"},
2016-02-27 17:34:13 +00:00
{"Delayed_Action*", "acts"},
{"i32", "count"},
{"i32", "max"},
};
char delayed_action_function_top[] =
"inline Delayed_Action*\n"
2016-02-27 19:33:42 +00:00
"delayed_action_(Delay *delay, Action_Type type";
2016-02-27 17:34:13 +00:00
char delayed_action_function_bottom[] =
"){\n"
" Delayed_Action *result;\n"
2016-02-28 20:30:51 +00:00
" if (delay->count == delay->max){\n"
" delay->max *= 2;\n"
" delay->acts = (Delayed_Action*)general_memory_reallocate("
"delay->general, delay->acts, delay->count*sizeof(Delayed_Action), delay->max*sizeof(Delayed_Action), 0);\n"
" }\n"
2016-02-27 17:34:13 +00:00
" result = delay->acts + delay->count++;\n"
" *result = {};\n"
" result->type = type;\n"
" return(result);\n"
"}\n\n";
char delayed_action_special_param[] = ", %s %s";
char delayed_action_specialized_middle[] =
"){\n"
" Delayed_Action *result;\n"
2016-02-27 19:33:42 +00:00
" result = delayed_action_(delay, type);\n";
2016-02-27 17:34:13 +00:00
char delayed_action_special_line[] =
" result->%s = %s;\n";
2016-02-28 20:30:51 +00:00
char delayed_action_special_string_line[] =
" result->%s = str_alloc_copy(delay->general, %s);\n";
2016-02-27 17:34:13 +00:00
char delayed_action_specialized_bottom[] =
" return(result);\n"
"}\n\n";
char delayed_action_macro[] =
2016-02-27 19:33:42 +00:00
"#define delayed_%s(delay, ...) delayed_action_(delay, DACT_%s, __VA_ARGS__)\n";
2016-02-27 17:34:13 +00:00
2016-02-28 20:30:51 +00:00
char delayed_action_repush_function[] =
"inline Delayed_Action*\n"
"delayed_action_repush(Delay *delay, Delayed_Action *act){\n"
" Delayed_Action *new_act = delayed_action_(delay, (Action_Type)0);\n"
" *new_act = *act;\n"
" if (act->string.str){\n"
" new_act->string = str_alloc_copy(delay->general, act->string);\n"
" }\n"
" return(new_act);\n"
"}\n\n";
2016-02-27 17:34:13 +00:00
char* generate_delayed_action(){
FILE *file;
char *filename = "4ed_delay.cpp";
char scratch[256];
int i,j;
file = fopen(filename, "wb");
fprintf(file, "enum %s{\n", daction_enum_name);
for (i = 0; i < ArrayCount(daction_enum); ++i){
fprintf(file, " DACT_%s,\n", daction_enum[i]);
}
fprintf(file, "};\n\n");
struct_begin(file, daction_name);
struct_fields(file, daction_fields, ArrayCount(daction_fields));
struct_fields(file, daction_fields_primary, ArrayCount(daction_fields_primary));
struct_end(file);
struct_begin(file, delay_name);
struct_fields(file, delay_fields, ArrayCount(delay_fields));
struct_end(file);
2016-02-28 20:30:51 +00:00
fprintf(file, "%s", str_alloc_copy);
2016-02-27 17:34:13 +00:00
fprintf(file, "%s%s", delayed_action_function_top, delayed_action_function_bottom);
for (i = 0; i < ArrayCount(dact_param_sets); ++i){
j = i;
fprintf(file, "%s", delayed_action_function_top);
for (; dact_param_sets[i] != dfph_null; ++i){
Struct_Field field = daction_fields_primary[dact_param_sets[i] - 1];
fprintf(file, delayed_action_special_param, field.type, field.name);
}
fprintf(file, "%s", delayed_action_specialized_middle);
for (; dact_param_sets[j] != dfph_null; ++j){
2016-02-28 20:30:51 +00:00
int handle = (int)(dact_param_sets[j]);
Struct_Field field = daction_fields_primary[handle - 1];
if (handle == dfph_string){
fprintf(file, delayed_action_special_string_line, field.name, field.name);
}
else{
fprintf(file, delayed_action_special_line, field.name, field.name);
}
2016-02-27 17:34:13 +00:00
}
fprintf(file, "%s", delayed_action_specialized_bottom);
}
2016-02-28 20:30:51 +00:00
fprintf(file, "%s", delayed_action_repush_function);
2016-02-27 17:34:13 +00:00
for (i = 0; i < ArrayCount(daction_enum); ++i){
to_lower(daction_enum[i], scratch);
fprintf(file, delayed_action_macro, scratch, daction_enum[i]);
}
return(filename);
2016-02-25 23:52:11 +00:00
}
2016-03-04 06:06:11 +00:00
//////////////////////////////////////////////////////////////////////////////////////////////////
#define MAJOR 3
#define MINOR 4
#define PATCH 5
#define VERS3__(a,b,c) #a"."#b"."#c
#define VERS3_(a,b,c) VERS3__(a,b,c)
#define VERS3 VERS3_(MAJOR, MINOR, PATCH)
#define VERSION_STRING VERS3
char version_header[] =
"#define MAJOR %d\n"
"#define MINOR %d\n"
"#define PATCH %d\n"
"#define VERSION_NUMBER \"alpha %s\"\n"
"#ifdef FRED_SUPER\n"
"#define VERSION_TYPE \" super!\"\n"
"#else\n"
"#define VERSION_TYPE \"\"\n"
"#endif\n"
"#define VERSION VERSION_NUMBER VERSION_TYPE\n";
char version_custom[] =
"#define MAJOR %d\n"
"#define MINOR %d\n"
"#define PATCH %d\n";
char* generate_version(){
char *filename = "4ed_version.h & 4coder_version.h";
char filename_header[] = "4ed_version.h";
char filename_custom[] = "4coder_version.h";
FILE *file;
file = fopen(filename_header, "wb");
fprintf(file, version_header, MAJOR, MINOR, PATCH, VERSION_STRING);
fclose(file);
file = fopen(filename_custom, "wb");
fprintf(file, version_custom, MAJOR, MINOR, PATCH);
fclose(file);
return(filename);
}
2016-03-04 23:10:00 +00:00
//////////////////////////////////////////////////////////////////////////////////////////////////
char* bar_style_fields[] = {
"bar",
"bar_active",
"base",
"pop1",
"pop2",
};
char* main_style_fields[] = {
"back",
"margin",
"margin_hover",
"margin_active",
"cursor",
"at_cursor",
"highlight",
"at_highlight",
"mark",
"default",
"comment",
"keyword",
"str_constant",
"char_constant",
"int_constant",
"float_constant",
"bool_constant",
"preproc",
"include",
"special_character",
"highlight_junk",
"highlight_white",
"paste",
"undo",
"next_undo",
};
2016-03-05 04:10:55 +00:00
static char*
make_style_tag(char *tag){
char *str;
int len;
2016-03-04 23:10:00 +00:00
2016-03-05 04:10:55 +00:00
len = (int)strlen(tag);
str = (char*)malloc(len + 1);
to_camel(tag, str);
str[len] = 0;
2016-03-04 23:10:00 +00:00
2016-03-05 04:10:55 +00:00
return(str);
2016-03-04 23:10:00 +00:00
}
2016-03-05 04:10:55 +00:00
char style_index_function_start[] =
"inline u32*\n"
"style_index_by_tag(Style_Main_Data *s, u32 tag){\n"
" u32 *result = 0;\n"
" switch (tag){\n";
char style_index_function_end[] =
" }\n"
" return(result);\n"
"}\n\n";
char style_case[] = " case Stag_%s: result = &s->%s_color; break;\n";
char style_info_case[] = " case Stag_%s: result = &s->file_info_style.%s_color; break;\n";
2016-03-04 23:10:00 +00:00
char* generate_style(){
char *filename = "4coder_style.h & 4ed_style.h";
char filename_4coder[] = "4coder_style.h";
char filename_4ed[] = "4ed_style.h";
FILE *file;
2016-03-05 04:10:55 +00:00
char *tag;
2016-03-04 23:10:00 +00:00
int count, i;
file = fopen(filename_4coder, "wb");
enum_begin(file, "Style_Tag");
{
count = ArrayCount(bar_style_fields);
for (i = 0; i < count; ++i){
2016-03-05 04:10:55 +00:00
tag = make_style_tag(bar_style_fields[i]);
fprintf(file, "Stag_%s,\n", tag);
free(tag);
2016-03-04 23:10:00 +00:00
}
count = ArrayCount(main_style_fields);
for (i = 0; i < count; ++i){
2016-03-05 04:10:55 +00:00
tag = make_style_tag(main_style_fields[i]);
fprintf(file, "Stag_%s,\n", tag);
free(tag);
2016-03-04 23:10:00 +00:00
}
}
struct_end(file);
fclose(file);
file = fopen(filename_4ed, "wb");
struct_begin(file, "Interactive_Style");
{
count = ArrayCount(bar_style_fields);
for (i = 0; i < count; ++i){
fprintf(file, "u32 %s_color;\n", bar_style_fields[i]);
}
}
struct_end(file);
struct_begin(file, "Style_Main_Data");
{
count = ArrayCount(main_style_fields);
for (i = 0; i < count; ++i){
fprintf(file, "u32 %s_color;\n", main_style_fields[i]);
}
fprintf(file, "Interactive_Style file_info_style;\n");
}
struct_end(file);
2016-03-05 04:10:55 +00:00
{
fprintf(file, "%s", style_index_function_start);
count = ArrayCount(bar_style_fields);
for (i = 0; i < count; ++i){
tag = make_style_tag(bar_style_fields[i]);
fprintf(file, style_info_case, tag, bar_style_fields[i]);
free(tag);
}
count = ArrayCount(main_style_fields);
for (i = 0; i < count; ++i){
tag = make_style_tag(main_style_fields[i]);
fprintf(file, style_case, tag, main_style_fields[i]);
free(tag);
}
fprintf(file, "%s", style_index_function_end);
}
2016-03-04 23:10:00 +00:00
fclose(file);
return(filename);
}
2016-03-04 06:06:11 +00:00
2016-02-25 23:52:11 +00:00
int main(){
2016-02-27 17:34:13 +00:00
char *filename;
filename = generate_keycode_enum();
printf("gen success: %s\n", filename);
filename = generate_delayed_action();
printf("gen success: %s\n", filename);
2016-03-04 06:06:11 +00:00
filename = generate_version();
printf("gen success: %s\n", filename);
2016-03-04 23:10:00 +00:00
filename = generate_style();
printf("gen success: %s\n", filename);
2016-02-25 23:52:11 +00:00
}
// BOTTOM