From 2defd751ecba51d7a8a3460134d412864f718944 Mon Sep 17 00:00:00 2001 From: timerix Date: Mon, 13 Mar 2023 18:01:48 +0600 Subject: [PATCH] updated kerep --- .gitignore | 10 ++- Makefile | 66 ++++++++++++----- cbuild | 2 +- default.config | 85 +++++++++++++++------ kerep | 2 +- src/cb2c/main.c | 10 +-- src/lexer/context.h | 32 ++++---- src/lexer/init.c | 6 +- src/lexer/lexer.c | 8 +- src/lexer/lexer.h | 2 +- src/lexer/tokens.c | 14 +++- src/lexer/tokens.h | 156 +++++++++++++++++++-------------------- tasks/clean_additions.sh | 4 + tasks/pre_build.sh | 14 ++-- tasks/rebuild_kerep.sh | 4 +- 15 files changed, 254 insertions(+), 161 deletions(-) create mode 100644 tasks/clean_additions.sh diff --git a/.gitignore b/.gitignore index cab1d1a..4be6400 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,16 @@ -# Build results +# build results bin/ obj/ +*.log +*.tmp -# user files -.old*/ +# IDE files .vs/ .vshistory/ .editorconfig *.user *.vcxproj.filters + +# other files +.old*/ current.config diff --git a/Makefile b/Makefile index 313e58a..03b6418 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,56 @@ -###### Build cbuild/default_tasks ####### +###################################### +###### Build tasks ####### +###################################### -all: build_exec +all: build_exec_dbg -rebuild_kerep: - @cbuild/call_task.sh rebuild_kerep +# generates different profile info +build_profile: + @cbuild/call_task.sh build_profile 2>&1 | tee make_raw.log -build_exec: - @cbuild/call_task.sh build_exec -build_exec_dbg: - @cbuild/call_task.sh build_exec_dbg +# creates executable using profile info generated by build_profile +build_exec: build_profile + @cbuild/call_task.sh build_exec 2>&1 | tee -a make_raw.log -build_shared_lib: - @cbuild/call_task.sh build_shared_lib -build_shared_lib_dbg: - @cbuild/call_task.sh build_shared_lib_dbg +# creates executable with debug info and no optimizations +build_exec_dbg: + @cbuild/call_task.sh build_exec_dbg 2>&1 | tee make_raw.log -build_static_lib: - @cbuild/call_task.sh build_static_lib -build_static_lib_dbg: - @cbuild/call_task.sh build_static_lib_dbg +###################################### +###### Launch tasks ####### +###################################### -###### Launch cbuild/default_tasks ####### +# executes $EXEC_FILE exec: build_exec - @cbuild/call_task.sh exec + @cbuild/call_task.sh exec 2>&1 | tee -a make_raw.log +# executes $EXEC_FILE +exec_dbg: build_exec_dbg + @cbuild/call_task.sh exec 2>&1 | tee -a make_raw.log + +# executes $EXEC_FILE with valgrind memory checker valgrind: build_exec_dbg - @cbuild/call_task.sh + @cbuild/call_task.sh valgrind 2>&1 | tee -a make_raw.log + +###################################### +###### Other tasks ####### +###################################### + +# deletes generated files +clean: + @cbuild/call_task.sh clean 2>&1 | tee make_raw.log + +# removes all unreadable characters copied from stdio +fix_log: + sed 's/[^[:blank:][:print:]]//g' make_raw.log \ + | sed 's/\[0;[0-9][0-9]m//g' \ + | sed 's/\[0;[0-9]m//g' \ + | sed 's/\[[0-9][0-9]m//g' \ + | sed 's/\[[0-9]m//g' \ + | sed 's/ H //g' \ + | sed 's/\[3gH //g' \ + > make_fixed.log + +# recompile kerep.a in the next build task +rebuild_kerep: + @cbuild/call_task.sh rebuild_kerep diff --git a/cbuild b/cbuild index 1b38b43..112fcc0 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit 1b38b43c547246ca358a676cee5eda85598ab949 +Subproject commit 112fcc04652d6ce65fbde215cd3d1f8935db2a7c diff --git a/default.config b/default.config index 54946ad..52dc182 100644 --- a/default.config +++ b/default.config @@ -1,54 +1,97 @@ #!/bin/bash -CBUILD_VERSION=4 -CONFIG_VERSION=5 +CBUILD_VERSION=5 +CONFIG_VERSION=6 PROJECT=cobek CMP_C=gcc CMP_CPP=g++ STD_C=c11 STD_CPP=c++17 -WARN_C="-Wall -Wno-discarded-qualifiers" +WARN_C="-Wall -Wno-discarded-qualifiers -ftrack-macro-expansion=0" WARN_CPP="-Wall" SRC_C="$( find src -name '*.c')" SRC_CPP="$( find src -name '*.cpp')" #TESTS_C="$( find tests -name '*.c')" #TESTS_CPP="$(find tests -name '*.cpp')" -OUTDIR=bin -OBJDIR=obj -EXEC_FILE=cb2c.com +# OBJDIR structure: +# ├── objects - dir where compiled *.o files are stored. cleans every call of build task +# ├── profile - dir where gcc *.gcda profiling info files stored +# ├── libs - there you can put static libs and linker will find them +# └── out - output files are created here and then copied to OUTDIR +OBJDIR="obj" +OUTDIR="bin" -case $TASK in - rebuild_kerep) - TASK_SCRIPT=tasks/rebuild_kerep.sh +# OS-specific options +case "$OS" in + WINDOWS) + EXEC_FILE="$PROJECT.exe" ;; - build_exec) - C_ARGS="-O2" + LINUX) + EXEC_FILE="$PROJECT" + ;; + *) + error "operating system $OS has no configuration variants" + ;; +esac + +# TASKS +case "$TASK" in + rebuild_kerep) + TASK_SCRIPT=tasks/rebuild_kerep.sh + ;; + # generates different profile info + build_profile) + OUTDIR="$OUTDIR/profile" + # -flto applies more optimizations across object files + # -flto=auto is needed to multithreaded copilation + # -fuse-linker-plugin is required to use static libs with lto, it strips away all + # -pg adds code to executable, that generates file containing function call info (gmon.out) + # -fprofile-generate + C_ARGS="-O2 -flto=auto -fuse-linker-plugin -pg -fprofile-generate -fprofile-prefix-path=$(realpath $OBJDIR)/objects" CPP_ARGS="$C_ARGS" - LINKER_ARGS="" - TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + LINKER_ARGS="$CPP_ARGS" PRE_TASK_SCRIPT=tasks/pre_build.sh - POST_TASK_SCRIPT="" + TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + POST_TASK_SCRIPT=cbuild/default_tasks/profile.sh KEREP_BUILD_TASK=build_static_lib ;; + # creates executable using profile info generated by build_profile + build_exec) + # -flto applies more optimizations across object files + # -flto=auto is needed to multithreaded copilation + # -fuse-linker-plugin is required to use static libs with lto, it strips away all + C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-use -fprofile-prefix-path=$(realpath $OBJDIR)/objects" + CPP_ARGS="$C_ARGS" + LINKER_ARGS="$CPP_ARGS" + PRE_TASK_SCRIPT=tasks/pre_build.sh + TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + KEREP_BUILD_TASK=build_static_lib + ;; + # creates executable with debug info and no optimizations build_exec_dbg) C_ARGS="-O0 -g" CPP_ARGS="$C_ARGS" - LINKER_ARGS="" - TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + LINKER_ARGS="$CPP_ARGS" PRE_TASK_SCRIPT=tasks/pre_build.sh - POST_TASK_SCRIPT="" + TASK_SCRIPT=cbuild/default_tasks/build_exec.sh KEREP_BUILD_TASK=build_static_lib_dbg ;; + # executes $EXEC_FILE exec) TASK_SCRIPT=cbuild/default_tasks/exec.sh - PRE_TASK_SCRIPT="" - POST_TASK_SCRIPT="" ;; + # executes $EXEC_FILE with valgrind memory checker valgrind) VALGRIND_ARGS="-s --log-file=valgrind.log --read-var-info=yes --track-origins=yes --fullpath-after=$PROJECT/ --leak-check=full --show-leak-kinds=all" TASK_SCRIPT=cbuild/default_tasks/valgrind.sh - PRE_TASK_SCRIPT="" - POST_TASK_SCRIPT="" ;; + # deletes generated files + clean) + TASK_SCRIPT=cbuild/default_tasks/clean.sh + ;; + # unknown task + *) + error "task <$TASK> not found" + ;; esac diff --git a/kerep b/kerep index 609dfcf..a0458f6 160000 --- a/kerep +++ b/kerep @@ -1 +1 @@ -Subproject commit 609dfcf3ed6d454599165e8dd443ba30b95771b2 +Subproject commit a0458f6d2ef04ce30f5c951281cd7de91bd48cff diff --git a/src/cb2c/main.c b/src/cb2c/main.c index 7b56df5..881f06f 100644 --- a/src/cb2c/main.c +++ b/src/cb2c/main.c @@ -8,7 +8,7 @@ Autoarr(Token)* parseFile(char* filename){ File* srcfile=m_srcfile.value.VoidPtr; char* src; tryLast(file_readAll(srcfile, &src), m_srcLen) - uint64 srcLen=m_srcLen.value.UInt64; + u64 srcLen=m_srcLen.value.UInt64; kprintf("srclen: %lu\n", srcLen); src[srcLen]='\0'; tryLast(lexan(src, filename), m_tokens) @@ -42,10 +42,10 @@ int main(const int argc, const char* const* argv){ } kprintf("\n"); return 0; // kerep type system - ktDescriptors_beginInit(); - ktDescriptors_initKerepTypes(); - ktDescriptors_initCbLexerTypes(); - ktDescriptors_endInit(); + kt_beginInit(); + kt_initKerepTypes(); + kt_initCbLexerTypes(); + kt_endInit(); // keywords search tree init_keywordsSearchTree(); kprint(kp_s|kp_fgGray, "keywordsSearchTree: ", kp_h|kp_pre, keywordsSearchTree, kp_c, '\n'); diff --git a/src/lexer/context.h b/src/lexer/context.h index 7fa5b7c..25239e8 100644 --- a/src/lexer/context.h +++ b/src/lexer/context.h @@ -3,35 +3,35 @@ #include "../../kerep/src/base/base.h" #include "tokens.h" -typedef struct ContextStruct Context; -PACK_ENUM(ContextType, +PACKED_ENUM(ContextType, CT_Namespace, - CT_Function, - CT_Class + CT_Class, + CT_Function ) -struct ContextStruct { +typedef struct Context Context; +STRUCT(Context, char* name; Context* parent; Autoarr(Token)* tokens; ContextType type; -}; +) -typedef struct NamespaceContext{ +STRUCT(NamespaceContext, Context base; -} NamespaceContext; +) -typedef struct FunctionContext { +STRUCT(ClassContext, + Context base; + Autoarr(Token)* attributes; + Token accessModifier; +) + +STRUCT(FunctionContext, Context base; Autoarr(Token)* arguments; Autoarr(Token)* attributes; Token accessModifier; Token returnType; -} FunctionContext; - -typedef struct ClassContext { - Context base; - Autoarr(Token)* attributes; - Token accessModifier; -} ClassContext; +) diff --git a/src/lexer/init.c b/src/lexer/init.c index 55105a1..7169cd1 100644 --- a/src/lexer/init.c +++ b/src/lexer/init.c @@ -1,6 +1,6 @@ #include "lexer.h" -void ktDescriptors_initCbLexerTypes(){ - kt_register(Token, NULL, NULL); - kt_register(Autoarr_Token, ____Autoarr_free_Token, NULL); +void kt_initCbLexerTypes(){ + kt_register(Token); + kt_register(Autoarr_Token); } diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index b629792..fdb2669 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -1,16 +1,16 @@ #include "lexer.h" #include "../../kerep/src/String/string.h" -typedef struct SharedLexerData{ +STRUCT(SharedLexerData, char* _source; char* _filename; Autoarr(Token)* _tokens; string _context; string _line; string _label; - uint32 _linenum; - uint32 _charnum; -} SharedLexerData; + u32 _linenum; + u32 _charnum; +) #define source sld->_source #define filename sld->_filename diff --git a/src/lexer/lexer.h b/src/lexer/lexer.h index aa55887..7058740 100644 --- a/src/lexer/lexer.h +++ b/src/lexer/lexer.h @@ -6,4 +6,4 @@ //Autoarr(Token)* Maybe lexan(char* source, char* filename); -void ktDescriptors_initCbLexerTypes(); \ No newline at end of file +void kt_initCbLexerTypes(); \ No newline at end of file diff --git a/src/lexer/tokens.c b/src/lexer/tokens.c index 92e2772..e803da1 100644 --- a/src/lexer/tokens.c +++ b/src/lexer/tokens.c @@ -1,7 +1,17 @@ #include "tokens.h" -ktid_define(Token); -Autoarr_define(Token) +void Token_freeMembers(void* _t){ + Token* t=(Token*)_t; + if(t->on_heap) + free(t->value); +} + +char* Token_toString(void* _t, u32 fmt){ + return cptr_copy(((Token*)_t)->value); +} + +kt_define(Token, Token_freeMembers, Token_toString); +Autoarr_define(Token, false) STNode* keywordsSearchTree=NULL; diff --git a/src/lexer/tokens.h b/src/lexer/tokens.h index 415e918..eb8bdc6 100644 --- a/src/lexer/tokens.h +++ b/src/lexer/tokens.h @@ -3,28 +3,28 @@ #include "../../kerep/src/Autoarr/Autoarr.h" #include "../../kerep/src/SearchTree/SearchTree.h" -PACK_ENUM(TokenId, - // base types +PACKED_ENUM(TokenId, + /* base types */ tok_void_t, - tok_int8_t, - tok_uint8_t, - tok_int16_t, - tok_uint16_t, - tok_int32_t, - tok_uint32_t, - tok_int64_t, - tok_uint64_t, - tok_char_t, //ascii - tok_char8_t, //utf8 - tok_char16_t, //utf16 + tok_i8_t, + tok_u8_t, + tok_i16_t, + tok_u16_t, + tok_i32_t, + tok_u32_t, + tok_i64_t, + tok_u64_t, + tok_char_t, /* ansi */ + tok_char8_t, /* utf8 *? */ + tok_char16_t, /* utf16 */ tok_bool_t, - tok_float32_t, - tok_float64_t, - // constant + tok_f32_t, + tok_f64_t, + /* constant */ tok_null, tok_true, tok_false, - // control flow operators + /* control flow operators */ tok_if, tok_else, tok_for, @@ -35,7 +35,7 @@ PACK_ENUM(TokenId, tok_braketo, tok_return, tok_goto, - // declaration keywords + /* declaration keywords */ tok_class, tok_struct, tok_enum, @@ -43,7 +43,7 @@ PACK_ENUM(TokenId, tok_event, tok_import, tok_alias, - // access modifiers + /* access modifiers */ tok_static, tok_const, tok_readonly, @@ -53,45 +53,45 @@ PACK_ENUM(TokenId, tok_virtual, tok_override, tok_partial, - // allocation keywords - tok_new, // allocates struct in heap - tok_sizeof, // size of variable value - tok_typeof, // type of variable - // special characters - tok_lbracket, // ( - tok_lbracket_fi, // { - tok_lbracket_sq, // [ - tok_rbracket, // ) - tok_rbracket_fi, // } - tok_rbracket_sq, // ] - tok_less, // < - tok_more, // > - tok_plus, // + - tok_minus, // - - tok_asterisk, // * - tok_slash, // / - tok_assign, // = - tok_not, // ! - tok_equals, // == - tok_not_equals, // != - tok_and, // & - tok_and_d, // && - tok_or, // | - tok_or_d, // || - tok_question, // ? - tok_question_d, // ?? - tok_colon, // : - tok_semicolon, // ; - tok_point, // . - tok_comma, // , - tok_tilda, // ~ - tok_backslash, // \ / - tok_percent, // % - tok_xor, // ^ - tok_lattice, // # - tok_dollar, // $ - tok_at, // @ - // user-defined + /* allocation keywords */ + tok_new, /* allocates struct in heap */ + tok_sizeof, /* size of variable value */ + tok_typeof, /* type of variable */ + /* special characters */ + tok_lbracket, /* ( */ + tok_lbracket_fi, /* { */ + tok_lbracket_sq, /* [ */ + tok_rbracket, /* ) */ + tok_rbracket_fi, /* } */ + tok_rbracket_sq, /* ] */ + tok_less, /* < */ + tok_more, /* > */ + tok_plus, /* + */ + tok_minus, /* - */ + tok_asterisk, /* * */ + tok_slash, /* / */ + tok_assign, /* = */ + tok_not, /* ! */ + tok_equals, /* == */ + tok_not_equals, /* != */ + tok_and, /* & */ + tok_and_d, /* && */ + tok_or, /* | */ + tok_or_d, /* || */ + tok_question, /* ? */ + tok_question_d, /* ?? */ + tok_colon, /* : */ + tok_semicolon, /* ; */ + tok_point, /* . */ + tok_comma, /* , */ + tok_tilda, /* ~ */ + tok_backslash, /* \ / */ + tok_percent, /* % */ + tok_xor, /* ^ */ + tok_lattice, /* # */ + tok_dollar, /* $ */ + tok_at, /* @ */ + /* user-defined */ tok_label, tok_number, tok_character, @@ -99,11 +99,11 @@ PACK_ENUM(TokenId, tok_comment ) -typedef struct Token{ +STRUCT(Token, char* value; TokenId id; -} Token; -ktid_declare(Token); + bool on_heap; // allocated on stack or heap +) Autoarr_declare(Token) extern STNode* keywordsSearchTree; @@ -112,27 +112,27 @@ void init_keywordsSearchTree(); static const Token default_tokens[]={ - // base types + /* base types */ {"void", tok_void_t}, - {"int8", tok_int8_t}, - {"uint8", tok_uint8_t}, - {"int16", tok_int16_t}, - {"uint16", tok_uint16_t}, - {"int32", tok_int32_t}, - {"uint32", tok_uint32_t}, - {"int64", tok_int64_t}, - {"uint64", tok_uint64_t}, + {"i8", tok_i8_t}, + {"u8", tok_u8_t}, + {"i16", tok_i16_t}, + {"u16", tok_u16_t}, + {"i32", tok_i32_t}, + {"u32", tok_u32_t}, + {"i64", tok_i64_t}, + {"u64", tok_u64_t}, {"char", tok_char_t}, {"char8", tok_char8_t}, {"char16", tok_char16_t}, {"bool", tok_bool_t}, - {"float32", tok_float32_t}, - {"float64", tok_float64_t}, - // constant + {"f32", tok_f32_t}, + {"f64", tok_f64_t}, + /* constant */ {"null", tok_null}, {"true", tok_true}, {"false", tok_false}, - // control flow operators + /* control flow operators */ {"if", tok_if}, {"else", tok_else}, {"for", tok_for}, @@ -143,7 +143,7 @@ static const Token default_tokens[]={ {"braketo", tok_braketo}, {"return", tok_return}, {"goto", tok_goto}, - // declaration keywords + /* declaration keywords */ {"class", tok_class}, {"struct", tok_struct}, {"enum", tok_enum}, @@ -151,7 +151,7 @@ static const Token default_tokens[]={ {"event", tok_event}, {"import", tok_import}, {"alias", tok_alias}, - // access modifiers + /* access modifiers */ {"static", tok_static}, {"const", tok_const}, {"readonly", tok_readonly}, @@ -161,11 +161,11 @@ static const Token default_tokens[]={ {"virtual", tok_virtual}, {"override", tok_override}, {"partial", tok_partial}, - // allocation keywords + /* allocation keywords */ {"new", tok_new}, {"sizeof", tok_sizeof}, {"typeof", tok_typeof}, - // special characters + /* special characters */ {"(", tok_lbracket}, {"{", tok_lbracket_fi}, {"[", tok_lbracket_sq}, diff --git a/tasks/clean_additions.sh b/tasks/clean_additions.sh new file mode 100644 index 0000000..85a607e --- /dev/null +++ b/tasks/clean_additions.sh @@ -0,0 +1,4 @@ +#!/usr/bin/bash + +myprint "${WHITE}deleting .kerep_rebuild.tmp" +rm -rf .kerep_rebuild.tmp diff --git a/tasks/pre_build.sh b/tasks/pre_build.sh index 4f3a298..126dffe 100755 --- a/tasks/pre_build.sh +++ b/tasks/pre_build.sh @@ -1,13 +1,17 @@ #!/bin/bash -if [ ! -f "kerep/bin/kerep.a" ] || [ -f .rebuild_kerep ] -then +# check if kerep static lib exists or kerep_rebuild task was executed +if [ ! -f "$OBJDIR/libs/kerep.a" ] || [ -f .rebuild_kerep.tmp ]; then + [[ -z "$KEREP_BUILD_TASK" ]] && error "KEREP_BUILD_TASK is empty" + myprint "${BLUE}making kerep task <$KEREP_BUILD_TASK>" + cd kerep if ! make "$KEREP_BUILD_TASK"; then exit 1 fi cd .. - rm -rf .rebuild_kerep + + cp kerep/bin/kerep.a $OBJDIR/libs/ + myprint "${GREEN}copied ${CYAN}kerep.a" + rm -f .rebuild_kerep.tmp fi -cp kerep/bin/kerep.a obj/ -printf "${GREEN}copied ${CYAN}kerep.a\n" diff --git a/tasks/rebuild_kerep.sh b/tasks/rebuild_kerep.sh index ea7e93b..7698a61 100644 --- a/tasks/rebuild_kerep.sh +++ b/tasks/rebuild_kerep.sh @@ -1,3 +1,3 @@ #!/bin/bash -touch .rebuild_kerep -printf "kerep.a will be rebuilt in the next build task" +touch .rebuild_kerep.tmp +myprint "${YELLOW}kerep.a will be rebuilt in the next build task"