From 4b2a22cc46b784b0cbbe0631af9020dc6416ab98 Mon Sep 17 00:00:00 2001 From: maix0 Date: Fri, 11 Oct 2024 11:32:02 +0200 Subject: [PATCH] updated code to now work --- Makefile | 3 +- includes/test | 1 + stdme/src/convert/numbers_to_str.c | 13 ++- test/.gitignore | 1 + test/Filelist.test.mk | 6 ++ test/Makefile | 145 +++++++++++++++++++++++++++++ test/Test.mk | 107 +++++++++++++++++++++ test/src/main.c | 41 ++++++++ 8 files changed, 309 insertions(+), 8 deletions(-) create mode 120000 includes/test create mode 100644 test/.gitignore create mode 100644 test/Filelist.test.mk create mode 100644 test/Makefile create mode 100644 test/Test.mk create mode 100644 test/src/main.c diff --git a/Makefile b/Makefile index 798a9428..83d2bc18 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2024/10/10 17:25:22 by maiboyer ### ########.fr # +# Updated: 2024/10/11 10:51:13 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -139,6 +139,7 @@ build_filelist: @$(MAKE) --no-print-directory -C ./line/ build_filelist @$(MAKE) --no-print-directory -C ./parser/ build_filelist @$(MAKE) --no-print-directory -f ./Minishell.mk build_filelist + @$(MAKE) --no-print-directory -C test ./Test.mk build_filelist # phony .PHONY: all bonus clean fclean re header footer build_filelist diff --git a/includes/test b/includes/test new file mode 120000 index 00000000..53104612 --- /dev/null +++ b/includes/test @@ -0,0 +1 @@ +../test/include/test \ No newline at end of file diff --git a/stdme/src/convert/numbers_to_str.c b/stdme/src/convert/numbers_to_str.c index 6b1590df..6e5bb105 100644 --- a/stdme/src/convert/numbers_to_str.c +++ b/stdme/src/convert/numbers_to_str.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/16 18:15:57 by maiboyer #+# #+# */ -/* Updated: 2024/10/10 18:49:27 by maiboyer ### ########.fr */ +/* Updated: 2024/10/11 11:30:00 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,12 +40,10 @@ static t_error _check_base(t_str base) static void _set_modulus(t_num_str_state *s) { - s->modulus = 0; + s->modulus = 1; s->base_len = str_len(s->base); - if (s->base_len == 10) - return ((void)(s->modulus = 10000000000000000000lu)); - while (UINT64_MAX - s->modulus > s->base_len) - s->modulus += s->base_len; + while (UINT64_MAX / s->modulus >= s->base_len) + s->modulus *= s->base_len; } static char _get_char(t_u64 value, t_num_str_state *s) @@ -66,7 +64,7 @@ static t_error _format_u64_base_innner(t_num_str_state *s) while (s->modulus) { c = _get_char(s->value / s->modulus, s); - if (s->zero || s->print) + if (!s->zero || s->print) { s->buffer[s->idx++] = c; s->print = true; @@ -89,6 +87,7 @@ t_error _format_u64(t_num_str args, t_str *out) mem_set_zero(&s, sizeof(s)); s.idx = 0; s.base = args.base; + s.value = args.value; if (args.is_nonnegative) s.buffer[s.idx++] = '-'; _set_modulus(&s); diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 00000000..ee4c9268 --- /dev/null +++ b/test/.gitignore @@ -0,0 +1 @@ +/test diff --git a/test/Filelist.test.mk b/test/Filelist.test.mk new file mode 100644 index 00000000..3bf076b3 --- /dev/null +++ b/test/Filelist.test.mk @@ -0,0 +1,6 @@ +SRC_FILES = \ +main \ + +GEN_FILES = \ + \ + diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 00000000..a5b60e98 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,145 @@ +# **************************************************************************** #make +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: rparodi +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # +# Updated: 2024/10/11 10:54:39 by maiboyer ### ########.fr # +# # +# **************************************************************************** # + +# Objdir +BUILD_DIR = $(shell realpath ../build/test) +BASE_PATH = $(shell pwd) + +# Colors +GREEN = \033[32m +CYAN = \033[36m +GREY = \033[0;90m +RED = \033[0;31m +GOLD = \033[38;5;220m +END = \033[0m +BOLD = \033[1m +ITALIC = \033[3m +UNDERLINE = \033[4m + +# Rules +SRC_DIR = ./sources +GEN_DIR = ./output +NAME = minishell + +# PMAKE_DISABLE = +PMAKE = +ifndef PMAKE_DISABLE +ifeq ($(shell uname), Linux) + PMAKE = -j$(shell grep -c ^processor /proc/cpuinfo) + #CFLAGS_ADDITIONAL += -DPRINT_BACKTRACE +endif +ifeq ($(shell uname), Darwin) + PMAKE = -j$(shell sysctl -n hw.ncpu) + #CFLAGS_ADDITIONAL += -DNVALGRIND +endif +endif + +# TODO: ADD THIS WHEN FINISHING THIS: +# CFLAGS_ADDITIONAL += -DNVALGRIND + +# TODO: REMOVE THIS WHEN FINISHING THIS: +# CFLAGS_ADDITIONAL += -fsanitize=memory -fno-omit-frame-pointer -fsanitize-memory-track-origins #-fuse-ld=lld -ffunction-sections -fdata-sections -Wl,--allow-multiple +# CFLAGS_ADDITIONAL += -O0 +# CFLAGS_ADDITIONAL += -Wno-cpp -Wno-type-limits -Wno-unused-command-line-argument + CFLAGS_ADDITIONAL += -gcolumn-info -g3 -fno-builtin + CFLAGS_ADDITIONAL += '-DERROR=((void)printf("ERROR HERE: " __FILE__ ":%d in %s\n", __LINE__, __func__), 1)' +CFLAGS_ADDITIONAL += -O2 +# CFLAGS_ADDITIONAL += -fuse-ld=gold -Wl,--print-symbol-counts -Wl,/tmp/symbols_count.log +# CFLAGS_ADDITIONAL += -fuse-ld=lld -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,-O3 +# CFLAGS_ADDITIONAL += -I$(shell realpath ./includes) -I$(shell realpath ./output/include) + +export CFLAGS_ADDITIONAL +export CC +export BASE_PATH +export BUILD_DIR + + +# All (make all) +all: + @$(MAKE) --no-print-directory header + @$(MAKE) --no-print-directory -f ./Test.mk $(PMAKE) + @$(MAKE) --no-print-directory footer + +bonus: + @$(MAKE) --no-print-directory header + @$(MAKE) --no-print-directory -f ./Test.mk $(PMAKE) bonus + @$(MAKE) --no-print-directory footer + +# Header +header: + @clear + @echo -e '' + @echo -e '$(GOLD) ******* ****** ******* $(END)' + @echo -e '$(GOLD) ****** *** ******* $(END)' + @echo -e '$(GOLD) ******* * ******* $(END)' + @echo -e '$(GOLD) ****** ******* $(END)' + @echo -e '$(GOLD) ******* ******* $(END)' + @echo -e '$(GOLD) ******************* ******* * $(END)' + @echo -e '$(GOLD) ******************* ******* *** $(END)' + @echo -e '$(GOLD) ****** ******* ****** $(END)' + @echo -e '$(GOLD) ****** $(END)' + @echo -e '$(GOLD) ****** $(END)' + @echo -e '$(GREY) Made by maiboyerlpb x rparodi$(END)' + +# Footer +footer: + @echo -e '$(GOLD) _ $(END)' + @echo -e '$(GOLD) | \ $(END)' + @echo -e '$(GOLD) | | $(END)' + @echo -e '$(GOLD) | | $(END)' + @echo -e '$(GOLD) |\ $(CYAN)$(BOLD)$(UNDERLINE)shcat$(END)$(GOLD) | | $(END)' + @echo -e '$(GOLD) /, ~\ / / $(END)' + @echo -e '$(GOLD) X `-.....-------./ / $(END)' + @echo -e '$(GOLD) ~-. ~ ~ | $(END)' + @echo -e '$(GOLD) \ / | $(END)' + @echo -e '$(GOLD) \ /_ ___\ / $(END)' + @echo -e '$(GOLD) | /\ ~~~~~ \ | $(END)' + @echo -e '$(GOLD) | | \ || | $(END)' + @echo -e '$(GOLD) | |\ \ || ) $(END)' + @echo -e '$(GOLD) (_/ (_/ ((_/ $(END)' + @echo -e ' $(GREY)The compilation is $(END)$(GOLD)finished$(END)' + @echo -e ' $(GREY)Have a good $(END)$(GOLD)correction$(END)' + + +# Clean (make clean) +clean: + @echo -e '$(GREY) Removing $(END)$(RED)Objects$(END)' + @echo -e '$(GREY) Removing $(END)$(RED)Objects Folder$(END)' + @$(RM) -r $(BUILD_DIR) + +# Clean (make fclean) +fclean: clean + @echo -e '$(GREY) Removing $(END)$(RED)Program$(END)' + @$(RM) $(NAME) + @echo "" + +# Restart (make re) +re: header + @$(MAKE) --no-print-directory fclean + @$(MAKE) --no-print-directory all + +tokei: + /bin/sh -c 'tokei -tC,C\ Header -e tree-sitter-sh' + + +build_filelist: + @$(MAKE) --no-print-directory -C ../stdme/ build_filelist + @$(MAKE) --no-print-directory -C ../allocator/ build_filelist + @$(MAKE) --no-print-directory -C ../ast/ build_filelist + @$(MAKE) --no-print-directory -C ../exec/ build_filelist + @$(MAKE) --no-print-directory -C ../line/ build_filelist + @$(MAKE) --no-print-directory -C ../parser/ build_filelist + @$(MAKE) --no-print-directory -C .. -f ./Minishell.mk build_filelist + @$(MAKE) --no-print-directory -f ./Test.mk build_filelist + +# phony +.PHONY: all bonus clean fclean re header footer build_filelist diff --git a/test/Test.mk b/test/Test.mk new file mode 100644 index 00000000..b2a7c94c --- /dev/null +++ b/test/Test.mk @@ -0,0 +1,107 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Test.mk :+: :+: :+: # +# +:+ +:+ +:+ # +# By: maiboyer +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/04/28 17:28:30 by maiboyer #+# #+# # +# Updated: 2024/10/11 10:55:07 by maiboyer ### ########.fr # +# # +# **************************************************************************** # + +# Functions + +link_group = -Wl,--start-group $(1) -Wl,--end-group + +# Variables +ANAME = test +BUILD_DIR ?= $(shell realpath ../build/test) +NAME = test + +CFLAGS_ADDITIONAL += -DBONUS=1 + +export CFLAGS_ADDITIONAL +export CC +export BASE_PATH +export BUILD_DIR + +# Flags +CFLAGS = -Werror -Wextra -Wall -Wno-unused-command-line-argument -MMD -I../includes -I./output/include -I./stdme/output/include -rdynamic -Wl,-E +CFLAGS += $(CFLAGS_ADDITIONAL) + + +SRC_DIR = src +GEN_DIR = output + +-include Filelist.$(ANAME).mk + +SRC = $(addsuffix .c,$(addprefix $(SRC_DIR)/,$(SRC_FILES)) $(addprefix $(GEN_DIR)/,$(GEN_FILES))) +OBJ = $(addsuffix .o,$(addprefix $(BUILD_DIR)/$(ANAME)/,$(SRC_FILES) $(GEN_FILES))) +DEP = $(addsuffix .d,$(addprefix $(BUILD_DIR)/$(ANAME)/,$(SRC_FILES) $(GEN_FILES))) + +# Commands +CC ?= clang +RM = rm -rf + +# Objects + +# Colors +GREEN = \033[32m +GREY = \033[0;90m +RED = \033[0;31m +GOLD = \033[38;5;220m +END = \033[0m + +.PHONY: all bonus build_filelist re clean fclean + +LIBS_NAMES = me aq ast parser line exec test +LIBS_FILES = $(addprefix $(BUILD_DIR)/, $(addsuffix .a, $(addprefix lib, $(LIBS_NAMES)))) +LIBS_FLAGS = $(addprefix -l, $(LIBS_NAMES)) + +all: + @$(MAKE) -C ../stdme/ "LIB_NAME=$(shell realpath ../stdme)/" libme.a + @$(MAKE) -C ../allocator/ "LIB_NAME=$(shell realpath ../allocator)/" libaq.a + @$(MAKE) -C ../ast/ "LIB_NAME=$(shell realpath ../ast)/" libast.a + @$(MAKE) -C ../exec/ "LIB_NAME=$(shell realpath ../exec)/" libexec.a + @$(MAKE) -C ../line/ "LIB_NAME=$(shell realpath ../line)/" libline.a + @$(MAKE) -C ../parser/ "LIB_NAME=$(shell realpath ../parser)/" libparser.a + @$(MAKE) -f ./Test.mk $(NAME) + +bonus: all + +# Dependences for all +$(NAME): $(LIBS_FILES) + @echo -e '$(GREY) Linking \t$(END)$(GOLD)$(NAME)$(END)' + @$(CC) $(CFLAGS) -o $(NAME) -L$(BUILD_DIR) $(call link_group,$(LIBS_FLAGS)) + +lib$(ANAME).a: $(BUILD_DIR)/lib$(ANAME).a + +$(BUILD_DIR)/lib$(ANAME).a: $(OBJ) + @ar rcs $(BUILD_DIR)/lib$(ANAME).a $(OBJ) + + +# Creating the objects +$(BUILD_DIR)/$(ANAME)/%.o: $(SRC_DIR)/%.c + @mkdir -p $(dir $@) + @echo -e '$(GREY) Building\t$(END)$(GREEN)$<$(END)' + @$(CC) $(CFLAGS) -o $@ -c $< + +$(BUILD_DIR)/$(ANAME)/%.o: $(GEN_DIR)/%.c + @mkdir -p $(dir $@) + @echo -e '$(GREY) Building\t$(END)$(GREEN)$<$(END)' + @$(CC) $(CFLAGS) -o $@ -c $< + +build_filelist: + @rm -f Filelist.$(ANAME).mk + @printf '%-78s\\\n' "SRC_FILES =" > Filelist.$(ANAME).mk + @tree $(SRC_DIR) -ifF | rg '$(SRC_DIR)/(.*)\.c$$' --replace '$$1' | sed -re 's/^(.*)_([0-9]+)$$/\1|\2/g' | sort -t'|' --key=1,1 --key=2,2n | sed -e's/|/_/' | xargs printf '%-78s\\\n' >> Filelist.$(ANAME).mk + @echo "" >> Filelist.$(ANAME).mk + @printf '%-78s\\\n' "GEN_FILES =" >> Filelist.$(ANAME).mk + @tree $(GEN_DIR) -ifF | rg '$(GEN_DIR)/(.*)\.c$$' --replace '$$1' | sed -re 's/^(.*)_([0-9]+)$$/\1|\2/g' | sort -t'|' --key=1,1 --key=2,2n | sed -e's/|/_/' | xargs printf '%-78s\\\n' >> Filelist.$(ANAME).mk + @echo "" >> Filelist.$(ANAME).mk + @echo -e '$(GREY) Populating $(GREEN) Filelist.$(ANAME).mk$(END)' + +%.h: ; + +-include $(DEP) diff --git a/test/src/main.c b/test/src/main.c new file mode 100644 index 00000000..d390610f --- /dev/null +++ b/test/src/main.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/11 10:56:02 by maiboyer #+# #+# */ +/* Updated: 2024/10/11 11:30:59 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/convert/numbers_to_str.h" +#include "me/mem/mem.h" +#include "me/types.h" +#include +#include + +t_error _format_u64(t_num_str args, t_str *out); +//if (_format_u64((t_num_str){.value = tmp, .is_nonnegative = false, .base = "0123456789ABCDEF", .prefix = ""}, &str)) +// continue; + +int main(int argc, char *argv[]) +{ + t_usize i; + t_u64 tmp; + t_str str; + + (void)(argc--); + (void)(argv++); + i = 0; + while (i < (t_usize)argc) + { + tmp = atoll(argv[i++]); + if (u64_to_str(tmp, &str)) + continue; + printf("%lu -> %s\n", tmp, str); + mem_free(str); + } + return (0); +}