code ordering

This commit is contained in:
anon 2023-08-21 16:13:24 +02:00
parent 966b90056e
commit 8bc5c7e137
3 changed files with 59 additions and 17 deletions

View File

@ -8,6 +8,7 @@ CHAD_DEBUG:=-Og -ggdb -pg -fno-inline
# Programs to check warnings for as defined by the Chad standard # Programs to check warnings for as defined by the Chad standard
GCC:=gcc GCC:=gcc
D.versions:=-D_XOPEN_SOURCE=700
GCC.warnings:=-Wall -Wextra -Wpedantic -Wvla -Wshadow -Wundef GCC.warnings:=-Wall -Wextra -Wpedantic -Wvla -Wshadow -Wundef
CLANG:=clang CLANG:=clang
CLANG.warnings:=-Weverything CLANG.warnings:=-Weverything
@ -15,8 +16,8 @@ VALGRIND:=valgrind
VALGRIND.flags:=--track-origins=yes --leak-check=full --show-leak-kinds=all VALGRIND.flags:=--track-origins=yes --leak-check=full --show-leak-kinds=all
chad_test: chad_test:
${GCC} ${GCC.warnings} ${SRC} -o ${OUT} ${GCC} ${D.versions} ${GCC.warnings} ${SRC} -o ${OUT}
${CLANG} ${GCC.warnings} ${SRC} -o ${OUT} ${CLANG} ${D.versions} ${GCC.warnings} ${SRC} -o ${OUT}
${VALGRIND} ${VALGRIND.flags} ${OUT} ${OUTARGS} ${VALGRIND} ${VALGRIND.flags} ${OUT} ${OUTARGS}
.DEFAULT_GOAL:=main .DEFAULT_GOAL:=main

View File

@ -5,6 +5,9 @@
#include "chad.h" #include "chad.h"
#include "regex.h" #include "regex.h"
// -------------------
// ### Definitions ###
// -------------------
typedef void (*attribute_callback_t)(const char * const string, typedef void (*attribute_callback_t)(const char * const string,
const int length, const int length,
void * const attributes); void * const attributes);
@ -40,6 +43,24 @@ typedef struct {
token_t * token_table[1000]; token_t * token_table[1000];
int token_table_top = 0; int token_table_top = 0;
// --------------------------------
// ### Constructors/Destructors ###
// --------------------------------
void new_display_mode(display_t * mode) {
HASH_ADD_STR(display_table,
key,
mode);
}
int free_token(token_t * token){
free(token->hl);
free(token->syntax);
return 0;
}
int append_token(token_t * token){ int append_token(token_t * token){
token_table[token_table_top++] = token; token_table[token_table_top++] = token;
return 0; return 0;
@ -104,6 +125,19 @@ token_t * new_keyword_token(const char * const word,
return mt; return mt;
} }
int new_keyword_tokens(const char * const * words,
hl_group_t * const g) {
int i = 0;
while (*words) {
if(new_keyword_token(*words, g)){
++i;
}
++words;
}
return i;
}
token_t * new_token(const char * const word, token_t * new_token(const char * const word,
const token_type_t t, const token_type_t t,
hl_group_t * const g) { hl_group_t * const g) {
@ -120,20 +154,14 @@ token_t * new_token(const char * const word,
} break; } break;
} }
// XXX: implement the rest // XXX: implement the rest
return NULL;
} }
int new_keyword_tokens(const char * const * words,
hl_group_t * const g) {
int i = 0;
while (*words) {
if(new_keyword_token(*words, g)){
++i;
}
++words;
}
return i;
} // --------------------
// ### Highlighting ###
// --------------------
int token_fits(const token_t* const token, int token_fits(const token_t* const token,
const char* const to) { const char* const to) {
@ -176,8 +204,18 @@ void render_string(const char * const string,
} }
} }
void new_display_mode(display_t * mode) {
HASH_ADD_STR(display_table,
key, // -------------------------
mode); // ### Library Mangement ###
// -------------------------
int hl_init(void) {
return 0;
}
int hl_deinit(void) {
for(int i = 0; i < token_table_top; i++){
free_token(token_table[i]);
}
return 0;
} }

View File

@ -59,6 +59,7 @@ int main(int argc,
buffer[buffer_size - 1] = '\0'; buffer[buffer_size - 1] = '\0';
// Highlight init // Highlight init
hl_init();
const char * c_keywords[] = { const char * c_keywords[] = {
"register", "volatile", "auto", "const", "static", "extern", "if", "else", "register", "volatile", "auto", "const", "static", "extern", "if", "else",
"do", "while", "for", "continue", "switch", "case", "default", "break", "do", "while", "for", "continue", "switch", "case", "default", "break",
@ -73,6 +74,7 @@ int main(int argc,
NULL NULL
}; };
// //
display_t * cterm = &(display_t) { display_t * cterm = &(display_t) {
.key = "cterm", .key = "cterm",
@ -116,6 +118,7 @@ int main(int argc,
// //
render_string(buffer, "cterm"); render_string(buffer, "cterm");
putchar('\n'); putchar('\n');
hl_deinit();
free(buffer); free(buffer);
return 0; return 0;