Compare commits

...

10 Commits

Author SHA1 Message Date
victor
b79c9584d5 bles 2025-10-15 23:18:21 +02:00
Victor Vobis
95d7ef4212 changed to file output 2025-05-14 17:53:43 +02:00
victor
390dd10dca added simple string_builder 2025-05-13 21:44:49 +02:00
Victor Vobis
30fb9231d8 failing to impl stringbuilder 2025-05-13 18:24:52 +02:00
victor
1df58ad7c3 simple function calls 2025-05-12 10:10:23 +02:00
Victor Vobis
f569dff167 add vec_pop 2025-05-03 20:40:38 +02:00
Victor Vobis
e5967f5dcc add vec_pop 2025-05-03 20:40:05 +02:00
Victor Vobis
c682f57427 simple vector type 2025-04-30 18:35:18 +02:00
Victor Vobis
7413694994 simple vector type 2025-04-30 18:33:49 +02:00
Victor Vobis
f3cb1d5858 simple vector type 2025-04-30 18:33:40 +02:00
107 changed files with 3740 additions and 367 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ vgcore*
debug
obj
*.o
.nfs*

View File

@ -1,3 +1,4 @@
[executable]
path=/home/victor/git/lang/debug
arguments=/home/victor/git/lang/test.lang
path=/sgoinfre/vvobis/git/personal/lang/debug
arguments=/sgoinfre/vvobis/git/personal/lang/test.lang
ask_directory=1

259
Makefile
View File

@ -1,87 +1,258 @@
# Basic configuration
AFLAGS = -felf64 -F dwarf -g
ifdef DEBUG
AFLAGS += -DDEBUG_BUILD
endif
# Directory structure
SRCDIR := src
OBJDIR := obj
LIBDIR := lib
BINDIR := bin
TARGET := $(BINDIR)/langc
# Core directories
COREDIR := $(SRCDIR)/core
MATHDIR := $(COREDIR)/math
VECDIR := $(COREDIR)/vector
STRDIR := $(COREDIR)/string
SBDIR := $(COREDIR)/string_builder
PRINTDIR := $(COREDIR)/print
MEMDIR := $(COREDIR)/mem
SYSCALLDIR := $(COREDIR)/syscall
FILEDIR := $(COREDIR)/file
PARSEDIR := $(SRCDIR)/parse
VARDIR := $(PARSEDIR)/vars
# Define source files
# Parser directories
PARSEDIR := $(SRCDIR)/parse
EXPRDIR := $(PARSEDIR)/expression
TOKDIR := $(PARSEDIR)/token
# Lexer directories
LEXDIR := $(SRCDIR)/lexer
VARDIR := $(LEXDIR)/vars
# Other directories
GLOBALDIR := $(SRCDIR)/global
# Object file directories (mirroring source structure)
OBJCOREDIR := $(OBJDIR)/core
OBJMATHDIR := $(OBJCOREDIR)/math
OBJVECDIR := $(OBJCOREDIR)/vector
OBJSTRDIR := $(OBJCOREDIR)/string
OBJSBDIR := $(OBJCOREDIR)/string_builder
OBJPRINTDIR := $(OBJCOREDIR)/print
OBJMEMDIR := $(OBJCOREDIR)/mem
OBJSYSCALLDIR := $(OBJCOREDIR)/syscall
OBJFILEDIR := $(OBJCOREDIR)/file
OBJPARSEDIR := $(OBJDIR)/parse
OBJEXPRDIR := $(OBJPARSEDIR)/expression
OBJTOKDIR := $(OBJPARSEDIR)/token
OBJLEXDIR := $(OBJDIR)/lexer
OBJVARDIR := $(OBJLEXDIR)/vars
OBJGLOBALDIR := $(OBJDIR)/global
# All object directories in dependency order
OBJDIRS := $(OBJDIR) \
$(OBJCOREDIR) \
$(OBJMATHDIR) \
$(OBJVECDIR) \
$(OBJSTRDIR) \
$(OBJSBDIR) \
$(OBJPRINTDIR) \
$(OBJMEMDIR) \
$(OBJSYSCALLDIR) \
$(OBJFILEDIR) \
$(OBJPARSEDIR) \
$(OBJEXPRDIR) \
$(OBJTOKDIR) \
$(OBJLEXDIR) \
$(OBJVARDIR) \
$(OBJGLOBALDIR)
# Source file definitions by module
MATHSRC := $(addprefix $(MATHDIR)/, $(addsuffix .s, \
operators \
))
STRSRC := $(addprefix $(STRDIR)/, $(addsuffix .s, \
strlen split strcpy substr is_num strcmp is_alpha \
strlen split strdup substr is_num strcmp is_alpha \
))
MEMSRC := $(addprefix $(MEMDIR)/, $(addsuffix .s, \
malloc memchr \
malloc memchr memcpy memset \
))
VECSRC := $(addprefix $(VECDIR)/, $(addsuffix .s, \
vec_create vec_push vec_get vec_pop \
))
PRINTSRC := $(addprefix $(PRINTDIR)/, $(addsuffix .s, \
print putnumber \
))
FILESRC := $(addprefix $(FILEDIR)/, $(addsuffix .s, \
read_file get_file_content \
))
SYSCALLSRC := $(addprefix $(SYSCALLDIR)/, $(addsuffix .s, \
exit file_ops syscall_err\
exit file_ops syscall_err fork\
))
PARSESRC := $(addprefix $(PARSEDIR)/, $(addsuffix .s, \
parse debug_token create_expressions debug_expression \
lexer lex_load lex_err \
TOKSRC := $(addprefix $(TOKDIR)/, $(addsuffix .s, \
parse_tokens debug_token \
))
EXPRSRC := $(addprefix $(EXPRDIR)/, $(addsuffix .s, \
create_expressions debug_expression \
))
LEXSRC := $(addprefix $(LEXDIR)/, $(addsuffix .s, \
lexer lex_err lex_load lex_func program_prologue \
func_boiler_plate \
))
VARSRC := $(addprefix $(VARDIR)/, $(addsuffix .s, \
get_vars insert_var \
get_vars insert_var \
))
# Collect all source files - now using the file variables, not directory variables
SRC := $(SRCDIR)/start.s $(MATHSRC) $(STRSRC) $(PRINTSRC) $(FILESRC) $(VARSRC) $(PARSESRC) $(SYSCALLSRC) $(MEMSRC)
GLOBALSRC := $(addprefix $(GLOBALDIR)/, $(addsuffix .s, \
function_table regs \
))
OBJDIR := obj
OBJ := $(patsubst %.s,$(OBJDIR)/%.o,$(notdir $(SRC)))
SBSRC := $(addprefix $(SBDIR)/, $(addsuffix .s, \
string_builder sb_append sb_dump_to_file \
))
all: debug
# Collect all sources and objects
MAIN_SRC := $(SRCDIR)/start.s
ALL_SRC := $(MAIN_SRC) $(MATHSRC) $(STRSRC) $(SBSRC) $(PRINTSRC) $(FILESRC) $(VARSRC) $(SYSCALLSRC) $(MEMSRC) $(TOKSRC) $(EXPRSRC) $(LEXSRC) $(GLOBALSRC) $(VECSRC)
debug: $(OBJDIR) $(OBJ)
ld -o $@ $(OBJ) -nostdlib -static
# Generate object file paths
ALL_OBJ := $(patsubst $(SRCDIR)/%.s,$(OBJDIR)/%.o,$(ALL_SRC))
# Pattern rules for object files - added the missing rules for string and print
$(OBJDIR)/%.o: $(SRCDIR)/%.s
nasm -felf64 -F dwarf -g $< -o $@
# Library settings
LIBNAME := $(LIBDIR)/core/core.a
LIBCORE_DIR := $(LIBDIR)/core
LIBFTDIR := $(LIBDIR)/libft
LIBFT := $(LIBFTDIR)/lib/libft.a
LIB_OBJ := $(filter-out $(OBJDIR)/start.o, $(ALL_OBJ))
$(OBJDIR)/%.o: $(MATHDIR)/%.s
nasm -felf64 -F dwarf -g $< -o $@
# Module-specific object files for staged compilation
MATH_OBJ := $(patsubst $(SRCDIR)/%.s,$(OBJDIR)/%.o,$(MATHSRC))
STR_OBJ := $(patsubst $(SRCDIR)/%.s,$(OBJDIR)/%.o,$(STRSRC))
MEM_OBJ := $(patsubst $(SRCDIR)/%.s,$(OBJDIR)/%.o,$(MEMSRC))
VEC_OBJ := $(patsubst $(SRCDIR)/%.s,$(OBJDIR)/%.o,$(VECSRC))
PRINT_OBJ := $(patsubst $(SRCDIR)/%.s,$(OBJDIR)/%.o,$(PRINTSRC))
FILE_OBJ := $(patsubst $(SRCDIR)/%.s,$(OBJDIR)/%.o,$(FILESRC))
SYSCALL_OBJ := $(patsubst $(SRCDIR)/%.s,$(OBJDIR)/%.o,$(SYSCALLSRC))
TOK_OBJ := $(patsubst $(SRCDIR)/%.s,$(OBJDIR)/%.o,$(TOKSRC))
EXPR_OBJ := $(patsubst $(SRCDIR)/%.s,$(OBJDIR)/%.o,$(EXPRSRC))
LEX_OBJ := $(patsubst $(SRCDIR)/%.s,$(OBJDIR)/%.o,$(LEXSRC))
VAR_OBJ := $(patsubst $(SRCDIR)/%.s,$(OBJDIR)/%.o,$(VARSRC))
GLOBAL_OBJ := $(patsubst $(SRCDIR)/%.s,$(OBJDIR)/%.o,$(GLOBALSRC))
SB_OBJ := $(patsubst $(SRCDIR)/%.s,$(OBJDIR)/%.o,$(SBSRC))
MAIN_OBJ := $(patsubst $(SRCDIR)/%.s,$(OBJDIR)/%.o,$(MAIN_SRC))
$(OBJDIR)/%.o: $(STRDIR)/%.s
nasm -felf64 -F dwarf -g $< -o $@
# Main targets
all: prepare-build build-core build-parser build-lexer build-global build-main link-executable create-library
$(OBJDIR)/%.o: $(MEMDIR)/%.s
nasm -felf64 -F dwarf -g $< -o $@
# Stage 1: Prepare build environment
prepare-build: create-directories
$(OBJDIR)/%.o: $(SYSCALLDIR)/%.s
nasm -felf64 -F dwarf -g $< -o $@
create-directories: | $(OBJDIRS) $(LIBDIR) $(LIBCORE_DIR) $(BINDIR)
$(OBJDIR)/%.o: $(PRINTDIR)/%.s
nasm -felf64 -F dwarf -g $< -o $@
$(OBJDIR)/%.o: $(FILEDIR)/%.s
nasm -felf64 -F dwarf -g $< -o $@
$(OBJDIR)/%.o: $(PARSEDIR)/%.s
nasm -felf64 -F dwarf -g $< -o $@
$(OBJDIR)/%.o: $(VARDIR)/%.s
nasm -felf64 -F dwarf -g $< -o $@
$(OBJDIR):
$(OBJDIRS):
mkdir -p $@
$(BINDIR):
mkdir -p $@
$(LIBDIR):
mkdir -p $@
$(LIBCORE_DIR):
mkdir -p $@
# Stage 2: Build core modules
build-core: build-math build-string build-memory build-vector build-print build-file build-syscall build-string-builder
build-math: $(MATH_OBJ)
build-string: $(STR_OBJ)
build-memory: $(MEM_OBJ)
build-vector: $(VEC_OBJ)
build-print: $(PRINT_OBJ)
build-file: $(FILE_OBJ)
build-syscall: $(SYSCALL_OBJ)
build-string-builder: $(SB_OBJ)
# Stage 3: Build parser modules
build-parser: build-tokens build-expressions
build-tokens: $(TOK_OBJ)
build-expressions: $(EXPR_OBJ)
# Stage 4: Build lexer modules
build-lexer: build-lex-core build-lex-vars
build-lex-core: $(LEX_OBJ)
build-lex-vars: $(VAR_OBJ)
# Stage 5: Build global modules
build-global: $(GLOBAL_OBJ)
# Stage 6: Build main entry point
build-main: $(MAIN_OBJ)
# Stage 7: Link executable
link-executable: $(TARGET)
$(TARGET): $(ALL_OBJ) $(LIBFT) $(LIBNAME)
ld -g -o $@ $(ALL_OBJ) $(LIBFT) $(LIBNAME) -nostdlib -static
# Stage 8: Create library
create-library: $(LIBNAME)
$(LIBNAME): $(LIB_OBJ) $(LIBDIR)
ar rcs $@ $(LIB_OBJ)
$(LIBFT):
make -C $(LIBFTDIR)
# Individual file compilation rule
$(OBJDIR)/%.o: $(SRCDIR)/%.s | $(OBJDIRS)
nasm $(AFLAGS) $< -o $@
# Utility targets
clean:
rm -rf $(OBJDIR) debug
rm -rf $(OBJDIR)
make clean -C $(LIBFTDIR)
re: clean all
clean-library:
rm -f $(LIBNAME)
clean-executable:
rm -f $(TARGET)
fclean: clean clean-library clean-executable
make fclean -C $(LIBFTDIR)
re: fclean all
# Debug target to show what would be built
show-config:
@echo "Target: $(TARGET)"
@echo "Flags: $(AFLAGS)"
@echo "Source files: $(words $(ALL_SRC)) files"
@echo "Object files: $(words $(ALL_OBJ)) files"
@echo "Object directories: $(OBJDIRS)"
.PHONY: all clean re

21
compile Executable file
View File

@ -0,0 +1,21 @@
#!/bin/sh
set -e
set -x
LD_ARGS="-nostdlib -static"
NASM_FLAG="-felf64 -g"
LIBS="./lib/core/core.a ./lib/libft/lib/libft.a"
COMPILER_PATH=./bin/langc
filename=$1
comp_out=${filename%.lang}.s
nasm_out=${comp_out%.s}.o
PROGNAME=${filename%.lang}
${COMPILER_PATH} ${filename}
nasm ${NASM_FLAG} ${comp_out} -o ${nasm_out}
ld ${nasm_out} ${LD_ARGS} ${LIBS} -o ${PROGNAME}
rm -rf ${comp_out} ${nasm_out}

7
examples/test.lang Normal file
View File

@ -0,0 +1,7 @@
a = 6
b = 5 - 2 + 6 + a
putnumber b
c = b + a
putnumber c
a = 0
exit 1

109
lib/gnl/get_next_line.c Normal file
View File

@ -0,0 +1,109 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 20:20:17 by vvobis #+# #+# */
/* Updated: 2024/05/27 17:44:59 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void *buffer_extend(void *ptr_old, size_t size_new, size_t size_old)
{
void *ptr_new;
void *ptr_back;
void *ptr_old_free;
ptr_new = g_calloc(size_new + 1, 1);
if (!ptr_new)
return (free(ptr_old), ptr_old = NULL, NULL);
ptr_back = ptr_new;
ptr_old_free = ptr_old;
while (size_old--)
*(char *)ptr_new++ = *(char *)ptr_old++;
return (free(ptr_old_free), ptr_old_free = NULL, ptr_back);
}
char *handle_no_nl(char **buf, char **left)
{
if (left)
{
free(*left);
*left = NULL;
}
if (!buf)
return (NULL);
if (g_strlen(*buf))
return (*buf);
free(*buf);
*buf = NULL;
return (NULL);
}
char *line_extract(char **buf_joined, char **left, size_t line_len)
{
char *line;
line = g_substr(*buf_joined, 0, line_len);
if (!line)
return (free(*buf_joined), *buf_joined = NULL, NULL);
*left = g_substr(*buf_joined, line_len, g_strlen(*buf_joined) - line_len);
free(*buf_joined);
*buf_joined = NULL;
if (!*left)
return (free(line), line = NULL, NULL);
return (line);
}
char *line_handle(char **buf_fetch)
{
char *buf_joined;
size_t line_len;
static char *left;
if (!buf_fetch)
return (free(left), NULL);
buf_joined = g_strjoin(left, *buf_fetch);
free(*buf_fetch);
*buf_fetch = NULL;
free(left);
left = NULL;
if (!buf_joined)
return (NULL);
line_len = find_newline(buf_joined);
if (line_len)
return (line_extract(&buf_joined, &left, line_len));
return (handle_no_nl(&buf_joined, &left));
}
char *get_next_line(int fd)
{
char *buf;
ssize_t bytes_read;
size_t buf_size_cur;
size_t buf_size_prev;
if (fd < 0)
return (line_handle(NULL), NULL);
buf = g_calloc(sizeof(*buf), BUFFER_SIZE + 1);
buf_size_prev = 0;
buf_size_cur = BUFFER_SIZE;
while (1)
{
if (!buf)
return (line_handle(NULL));
bytes_read = read(fd, buf + buf_size_prev, BUFFER_SIZE);
if (bytes_read < 0)
return (free(buf), buf = NULL, line_handle(NULL));
if (find_newline(buf) || bytes_read == 0)
break ;
buf_size_prev = buf_size_cur;
buf_size_cur += BUFFER_SIZE;
buf = buffer_extend(buf, buf_size_cur, buf_size_prev);
}
return (line_handle(&buf));
}

38
lib/gnl/get_next_line.h Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/15 14:07:36 by vvobis #+# #+# */
/* Updated: 2024/05/27 17:43:21 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 50
# endif
# define MAX_FD 1024
# include <stdlib.h>
# include <unistd.h>
# include <fcntl.h>
# include <stdint.h>
char *get_next_line(int fd);
char *g_strjoin(char const *s1, char const *s2);
size_t g_strlen(char const *str);
void *g_calloc(size_t n, size_t s);
char *g_substr(char const *s, unsigned int start, size_t len);
char *line_handle(char **buf_fetch);
size_t find_newline(char *buf);
#endif

View File

@ -0,0 +1,118 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/15 14:14:13 by vvobis #+# #+# */
/* Updated: 2024/05/20 11:38:03 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t g_strlen(char const *str)
{
size_t i;
if (!str)
return (0);
i = 0;
while (*str)
{
i++;
str++;
}
return (i);
}
size_t find_newline(char *buf)
{
size_t i;
if (!buf)
return (0);
i = 0;
while (*buf)
{
if (*buf == '\n')
return (i + 1);
i++;
buf++;
}
return (0);
}
char *g_strjoin(char const *s1, char const *s2)
{
char *tmp;
unsigned int i;
unsigned int j;
if (!s2)
return (NULL);
if (!s1)
return (g_substr(s2, 0, g_strlen(s2)));
tmp = g_calloc(g_strlen(s1) + g_strlen(s2) + 1, sizeof(*tmp));
if (!tmp)
return (NULL);
i = 0;
if (s1)
{
while (s1[i])
{
tmp[i] = s1[i];
i++;
}
}
j = 0;
if (s2)
while (s2[j])
tmp[i++] = s2[j++];
return (tmp);
}
char *g_substr(char const *s, unsigned int start, size_t len)
{
char *tmp;
unsigned int i;
i = 0;
if (!s || start >= g_strlen(s) || len <= 0)
{
tmp = malloc(1);
if (!tmp)
return (NULL);
tmp[i] = 0;
return (tmp);
}
if (len + start > g_strlen(s))
len = g_strlen(s) - start;
tmp = g_calloc(len + 1, sizeof(*tmp));
if (!tmp)
return (NULL);
while (i < len && s[i])
{
tmp[i] = s[i + start];
i++;
}
return (tmp);
}
void *g_calloc(size_t n, size_t s)
{
char *tmp;
unsigned long i;
i = 0;
if (n == 0)
return (malloc(0));
if (SIZE_MAX / n < s)
return (NULL);
tmp = malloc(n * s);
if (tmp)
while (i < n * s)
tmp[i++] = 0;
return ((void *)tmp);
}

78
lib/libft/Makefile Normal file
View File

@ -0,0 +1,78 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: vvobis <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/17 14:23:19 by vvobis #+# #+# #
# Updated: 2025/05/29 15:50:55 by victor ### ########.fr #
# #
# **************************************************************************** #
NAME := libft.a
CC := cc
CFLAGS := -Wall -Wextra -Werror -fstack-protector
ifdef DEBUG
CFLAGS += -g3
endif
SRC := ft_bzero.c ft_isalnum.c ft_isalpha.c ft_isdigit.c \
ft_isprint.c ft_memcpy.c ft_memmove.c ft_memset.c \
ft_strlcat.c ft_strlcpy.c ft_strlen.c \
ft_isascii.c ft_strchr.c ft_strrchr.c ft_strncmp.c \
ft_toupper.c ft_tolower.c ft_memchr.c ft_strnstr.c \
ft_atoi.c ft_atod.c ft_memcmp.c ft_calloc.c ft_strdup.c \
ft_substr.c ft_strjoin.c ft_strtrim.c ft_split.c \
ft_itoa.c ft_strmapi.c ft_striteri.c ft_putchar_fd.c \
ft_putstr_fd.c ft_putendl_fd.c ft_putnbr_fd.c \
ft_isspace.c ft_free.c ft_read.c
SRCGNL := ../gnl/get_next_line.c ../gnl/get_next_line_utils.c
SRCPRINT := printf/ft_fprintf.c printf/ft_printf.c printf/ft_putascii.c printf/ft_putptr.c printf/ft_puthex.c printf/ft_putfloat.c
SRCBON := ft_lstnew_bonus.c ft_lstadd_front_bonus.c ft_lstsize_bonus.c ft_lstlast_bonus.c ft_lstadd_back_bonus.c ft_lstdelone_bonus.c ft_lstclear_bonus.c ft_lstiter_bonus.c ft_lstmap_bonus.c
OBJBON := $(SRCBON:%.c=%.o)
OBJ := $(SRC:%.c=%.o)
OBJGNL := $(SRCGNL:%.c=%.o)
OBJPRINT := $(SRCPRINT:%.c=%.o)
LIBDIR := lib
LIBS := printf
all: $(NAME)
bonus: $(OBJBON) $(OBJ)
ar rcs libft.a $(OBJ) $(OBJBON)
$(NAME): $(LIBDIR) $(OBJ) $(OBJGNL) $(OBJPRINT) $(LIBS)
ar rcs $(LIBDIR)/$@ $(OBJ) $(OBJGNL) $(OBJPRINT)
$(LIBDIR):
mkdir -p $@
%.o: %.c
$(CC) -c $(CFLAGS) $^ -o $@
re: fclean all
so:
$(CC) -fPIC $(CFLAGS) $(SRC) test.c
gcc -shared -o libft.so $(OBJ) $(OBJBON)
fclean: clean
rm -rf $(LIBDIR)
make fclean -C $(LIBS)
clean:
rm -f $(OBJ) $(OBJBON) $(OBJGNL)
make clean -C $(LIBS)

57
lib/libft/ft_atod.c Normal file
View File

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atod.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: victor </var/spool/mail/victor> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/08 22:25:35 by victor #+# #+# */
/* Updated: 2024/12/05 20:08:42 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
double atod_collect_fraction(char *n, double d)
{
int period;
int mult;
mult = 1;
period = 0;
while (*n && !ft_isspace(*n))
{
if (*n == '.' && period == 0)
{
period = 1;
n++;
continue ;
}
else if (((*n == '.' && period == 1) || (*n != '.' && !ft_isdigit(*n))))
return (ft_putendl_fd("Invalid double format: Too many periods", \
2), exit(1), 0);
if (period)
mult *= 10;
d *= 10;
d += (*n - '0');
n++;
}
return (d / mult);
}
double ft_atod(char *n)
{
double d;
int sign;
if (!n)
exit(1);
sign = 1;
if (*n == '-')
{
sign = -1;
n++;
}
d = 0;
return (atod_collect_fraction(n, d) * sign);
}

60
lib/libft/ft_atoi.c Normal file
View File

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bszilas <bszilas@student.42vienna.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/10 10:49:04 by vvobis #+# #+# */
/* Updated: 2024/09/13 22:19:12 by bszilas ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int is_space(char const c)
{
if ((c >= 9 && c <= 13) || c == ' ')
return (1);
return (0);
}
int ft_atoi(char const *s)
{
int nb;
char const *tmp;
nb = 0;
while (is_space(*s))
s++;
tmp = s;
if (*tmp == '+' || *tmp == '-')
tmp++;
while (*tmp >= '0' && *tmp <= '9')
{
nb *= 10;
nb += (*tmp - '0');
tmp++;
}
if (*s == '-')
nb = -nb;
return (nb);
}
long ft_atol(const char *nptr)
{
int64_t n;
int sign;
n = 0;
sign = 1;
while (ft_isspace(*nptr))
nptr++;
if (*nptr == '-')
sign = -1;
if (sign == -1 || *nptr == '+')
nptr++;
while (*nptr >= '0' && *nptr <= '9')
n = n * 10 + (*nptr++ - '0');
return (n * sign);
}

19
lib/libft/ft_bzero.c Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 16:04:55 by vvobis #+# #+# */
/* Updated: 2024/04/09 20:54:53 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_bzero(void *s, size_t n)
{
while (n--)
*(char *)s++ = 0;
}

30
lib/libft/ft_calloc.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 13:44:00 by vvobis #+# #+# */
/* Updated: 2024/04/10 16:39:56 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_calloc(size_t n, size_t s)
{
char *tmp;
unsigned long i;
i = 0;
if (n == 0)
return (malloc(0));
if (SIZE_MAX / n < s)
return (NULL);
tmp = malloc(n * s);
if (tmp)
while (i < n * s)
tmp[i++] = 0;
return ((void *)tmp);
}

22
lib/libft/ft_free.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/10 12:16:39 by vvobis #+# #+# */
/* Updated: 2024/09/10 12:17:34 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_free(void *ptr_ptr)
{
void **ptr;
ptr = ptr_ptr;
free(*ptr);
*ptr = NULL;
}

20
lib/libft/ft_isalnum.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 15:59:18 by vvobis #+# #+# */
/* Updated: 2024/04/02 15:59:23 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalnum(int c)
{
if (ft_isdigit(c) || ft_isalpha(c))
return (1);
return (0);
}

20
lib/libft/ft_isalpha.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 15:59:49 by vvobis #+# #+# */
/* Updated: 2024/04/02 16:00:04 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalpha(int c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
return (1);
return (0);
}

20
lib/libft/ft_isascii.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 19:48:35 by vvobis #+# #+# */
/* Updated: 2024/04/02 19:49:36 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isascii(int c)
{
if (c >= 0 && c <= 127)
return (1);
return (0);
}

18
lib/libft/ft_isdigit.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 16:14:56 by vvobis #+# #+# */
/* Updated: 2024/04/02 19:38:45 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isdigit(int c)
{
if (c >= '0' && c <= '9')
return (1);
return (0);
}

18
lib/libft/ft_isprint.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 16:00:49 by vvobis #+# #+# */
/* Updated: 2024/04/02 16:00:52 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isprint(int c)
{
if (c >= 32 && c <= 126)
return (1);
return (0);
}

18
lib/libft/ft_isspace.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isspace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: victor </var/spool/mail/victor> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/08 18:27:49 by victor #+# #+# */
/* Updated: 2024/09/08 18:27:55 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isspace(int c)
{
return (c == ' ' || (c > 9 && c < 14));
}

75
lib/libft/ft_itoa.c Normal file
View File

@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/05 20:16:48 by vvobis #+# #+# */
/* Updated: 2024/04/11 14:48:59 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int get_len(int n)
{
int i;
if (n < 0)
n = -n;
i = 0;
while (n)
{
i++;
n /= 10;
}
return (i);
}
static char *is_negative(int n)
{
int i;
char *num;
i = get_len(n);
num = malloc(sizeof(*num) * i + 2);
if (!num)
return (NULL);
num[i + 1] = 0;
while (n)
{
num[i--] = -(n % 10) + 48;
n /= 10;
}
num[i] = 0x2d;
return (num);
}
static char *is_positive(int n)
{
int i;
char *num;
i = get_len(n);
num = malloc(sizeof(*num) * i + 1);
if (!num)
return (NULL);
num[i--] = 0;
while (n)
{
num[i--] = (n % 10) + 48;
n /= 10;
}
return (num);
}
char *ft_itoa(int n)
{
if (n == 0)
return (ft_strdup("0"));
else if (n < 0)
return (is_negative(n));
else
return (is_positive(n));
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 13:51:32 by vvobis #+# #+# */
/* Updated: 2024/04/09 18:17:12 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *tmp;
if (!new)
return ;
if (*lst)
{
tmp = *lst;
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
}
else
*lst = new;
}

View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 12:53:08 by vvobis #+# #+# */
/* Updated: 2024/04/09 18:17:50 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_front(t_list **lst, t_list *new)
{
if (!lst || !new)
return ;
if (!*lst)
*lst = new;
else
{
new->next = *lst;
*lst = new;
}
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 15:44:23 by vvobis #+# #+# */
/* Updated: 2024/04/09 22:35:41 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void *))
{
t_list *tmp;
if (!*lst || !del || !lst)
return ;
while (*lst)
{
tmp = (*lst)->next;
del((*lst)->content);
free(*lst);
*lst = tmp;
}
}

View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 14:24:03 by vvobis #+# #+# */
/* Updated: 2024/04/09 15:42:04 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
if (!lst || !del)
return ;
del(lst->content);
free(lst);
}

View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 15:57:51 by vvobis #+# #+# */
/* Updated: 2024/04/09 18:20:05 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(void *))
{
if (!lst || !f)
return ;
while (lst)
{
f(lst->content);
lst = lst->next;
}
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 13:47:08 by vvobis #+# #+# */
/* Updated: 2024/04/09 15:11:51 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstlast(t_list *lst)
{
if (!lst)
return (NULL);
while (lst->next)
lst = lst->next;
return (lst);
}

View File

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 16:18:03 by vvobis #+# #+# */
/* Updated: 2024/04/09 23:40:57 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void *free_list(t_list **head, void (*del)(void *), void *fcontent)
{
if (fcontent)
del(fcontent);
ft_lstclear(head, del);
return (NULL);
}
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *tmp;
t_list *head;
void *fcontent;
if (!lst || !f || !del)
return (NULL);
head = NULL;
while (lst)
{
fcontent = f(lst->content);
if (!fcontent)
return (free_list(&head, del, fcontent));
tmp = ft_lstnew(fcontent);
if (!tmp)
return (free_list(&head, del, fcontent));
ft_lstadd_back(&head, tmp);
lst = lst->next;
}
return (head);
}

View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 11:10:05 by vvobis #+# #+# */
/* Updated: 2024/04/09 18:59:48 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstnew(void *content)
{
t_list *tmp;
tmp = malloc(sizeof(*tmp));
if (!tmp)
return (NULL);
tmp->next = NULL;
tmp->content = content;
return (tmp);
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 13:43:08 by vvobis #+# #+# */
/* Updated: 2024/04/09 18:21:11 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_lstsize(t_list *lst)
{
int i;
if (!lst)
return (0);
i = 1;
while (lst->next)
{
lst = lst->next;
i++;
}
return (i);
}

31
lib/libft/ft_memchr.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 15:13:59 by vvobis #+# #+# */
/* Updated: 2024/04/09 20:55:24 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memchr(void *s, int c, size_t n)
{
unsigned char *p;
size_t i;
i = 0;
p = (unsigned char *)s;
while (i < n)
{
if (p[i] == (unsigned char)c)
return ((void *)&p[i]);
i++;
}
if (p[i - 1] == (unsigned char)c)
return ((void *)&p[i]);
return (NULL);
}

31
lib/libft/ft_memcmp.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 15:27:15 by vvobis #+# #+# */
/* Updated: 2024/04/09 19:21:02 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(void *s1, void const *s2, size_t n)
{
unsigned char *p1;
unsigned char *p2;
unsigned int i;
p1 = (unsigned char *)s1;
p2 = (unsigned char *)s2;
i = 0;
while (i < n)
{
if (p1[i] != p2[i])
return (p1[i] - p2[i]);
i++;
}
return (0);
}

32
lib/libft/ft_memcpy.c Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 17:13:18 by vvobis #+# #+# */
/* Updated: 2024/04/09 19:21:29 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memcpy(void *dest, void const *src, size_t n)
{
char *d;
char *s;
if (n == 0 || (dest == NULL && src == NULL))
return (dest);
if (dest == NULL || src == NULL)
{
*(char *)dest = 1;
*(char *)src = 1;
}
d = (char *) dest;
s = (char *) src;
while (n--)
*d++ = *s++;
return (dest);
}

30
lib/libft/ft_memmove.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 17:42:28 by vvobis #+# #+# */
/* Updated: 2024/04/09 19:21:54 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memmove(void *dest, void const *src, size_t n)
{
unsigned char *d;
unsigned char *s;
if (!dest && !src)
return (dest);
d = (unsigned char *)dest;
s = (unsigned char *)src;
if (s < d)
while (n--)
d[n] = s[n];
else
ft_memcpy(dest, (void *)src, n);
return (dest);
}

23
lib/libft/ft_memset.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 15:57:27 by vvobis #+# #+# */
/* Updated: 2024/04/03 11:18:10 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memset(void *s, int c, size_t n)
{
char *str;
str = (char *)s;
while (n--)
str[n] = c;
return (s);
}

18
lib/libft/ft_putchar_fd.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 17:34:30 by vvobis #+# #+# */
/* Updated: 2024/04/07 17:53:33 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}

21
lib/libft/ft_putendl_fd.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 17:57:33 by vvobis #+# #+# */
/* Updated: 2024/04/07 18:00:49 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putendl_fd(char *str, int fd)
{
if (!str)
return ;
ft_putstr_fd(str, fd);
ft_putchar_fd(0x0a, fd);
}

27
lib/libft/ft_putnbr_fd.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 18:02:14 by vvobis #+# #+# */
/* Updated: 2024/04/11 11:23:10 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbr_fd(int n, int fd)
{
if (n < 0)
ft_putchar_fd(0x2d, fd);
if (n <= -10)
ft_putnbr_fd(n / -10, fd);
if (n >= 10)
ft_putnbr_fd(n / 10, fd);
if (n >= 0)
ft_putchar_fd(n % 10 + 0x30, fd);
if (n < 0)
ft_putchar_fd(-(n % -10) + 0x30, fd);
}

21
lib/libft/ft_putstr_fd.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 17:48:41 by vvobis #+# #+# */
/* Updated: 2024/04/07 17:52:16 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putstr_fd(char *str, int fd)
{
if (!str)
return ;
while (*str)
ft_putchar_fd(*str++, fd);
}

25
lib/libft/ft_read.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_read.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/18 12:49:30 by vvobis #+# #+# */
/* Updated: 2024/12/10 21:18:38 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_read(int fd, char *character, unsigned int size_to_read)
{
int bytes_read;
bytes_read = read(fd, character, size_to_read);
if (bytes_read == -1)
{
ft_fprintf(STDERR_FILENO, "Read failed\n");
}
return (bytes_read);
}

84
lib/libft/ft_split.c Normal file
View File

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/05 20:14:20 by vvobis #+# #+# */
/* Updated: 2024/04/09 23:55:49 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int count(char const *s, char const c)
{
int n;
n = 0;
if (*s != c && *s)
n = 1;
while (*s)
{
if (*s == c && *(s + 1) != c && *(s + 1))
n++;
s++;
}
return (n);
}
static int count_sub(char const *s, char const c)
{
int i;
i = 0;
while (*s != c && *s)
{
i++;
s++;
}
return (i);
}
static char **free_all(char **back)
{
char **tmp;
tmp = back;
while (*back)
{
free(*back);
back++;
}
free(tmp);
return (NULL);
}
char **ft_split(char const *s, char const c)
{
char **tmp;
char **back;
if (!s)
return (NULL);
tmp = (char **)ft_calloc(sizeof(*tmp), count(s, c) + 1);
if (!tmp)
return (NULL);
back = tmp;
while (*s && tmp)
{
while (*s == c && *s)
s++;
if (*s)
{
*tmp = ft_substr(s, 0, count_sub(s, c));
if (!*tmp)
return (free_all(back));
}
tmp++;
while (*s != c && *s)
s++;
}
return (back);
}

29
lib/libft/ft_strchr.c Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 12:20:50 by vvobis #+# #+# */
/* Updated: 2024/04/09 20:54:14 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strchr(char const *s, int c)
{
int i;
i = 0;
while (s[i])
{
if (s[i] == (char)c)
return ((char *)&s[i]);
i++;
}
if ((char)c == 0)
return ((char *)&s[i]);
return (NULL);
}

31
lib/libft/ft_strdup.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 18:44:05 by vvobis #+# #+# */
/* Updated: 2024/04/12 10:44:23 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(char const *s)
{
char *tmp;
int i;
i = 0;
tmp = ft_calloc(ft_strlen(s) + 1, sizeof(*tmp));
if (!tmp)
return (NULL);
while (s[i])
{
tmp[i] = s[i];
i++;
}
tmp[i] = 0;
return (tmp);
}

27
lib/libft/ft_striteri.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 17:24:08 by vvobis #+# #+# */
/* Updated: 2024/04/09 19:27:07 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_striteri(char const *s, void (*f)(unsigned int, char *))
{
unsigned int i;
if (!s || !f)
return ;
i = 0;
while (s[i])
{
f(i, (char *)&s[i]);
i++;
}
}

41
lib/libft/ft_strjoin.c Normal file
View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 19:08:03 by vvobis #+# #+# */
/* Updated: 2024/04/12 10:07:26 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
char *tmp;
unsigned int i;
unsigned int j;
if (!s1 || !s2)
return (NULL);
tmp = ft_calloc(ft_strlen(s1) + ft_strlen(s2) + 1, sizeof(*tmp));
if (!tmp)
return (NULL);
i = 0;
while (s1[i])
{
tmp[i] = s1[i];
i++;
}
j = 0;
while (s2[j])
{
tmp[i] = s2[j];
i++;
j++;
}
tmp[i] = 0;
return (tmp);
}

37
lib/libft/ft_strlcat.c Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 16:10:59 by vvobis #+# #+# */
/* Updated: 2024/04/10 16:41:14 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcat(char *dest, char const *src, size_t size)
{
size_t dlen;
size_t slen;
if (size == 0)
return (ft_strlen(src));
dlen = ft_strlen(dest);
slen = ft_strlen(src);
if (size <= dlen)
return (slen + size);
if (dlen + slen < size)
{
ft_memcpy(&dest[dlen], src, slen);
dest[dlen + slen] = 0;
}
else
{
ft_memcpy(&dest[dlen], src, size - dlen);
dest[size - 1] = 0;
}
return (dlen + slen);
}

32
lib/libft/ft_strlcpy.c Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 19:03:18 by vvobis #+# #+# */
/* Updated: 2024/04/09 19:28:44 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcpy(char *dest, char const *src, size_t size)
{
size_t i;
i = 0;
if (size != 0)
{
while (src[i] != '\0' && i < size - 1)
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
}
while (src[i] != '\0')
i++;
return (i);
}

27
lib/libft/ft_strlen.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 16:01:03 by vvobis #+# #+# */
/* Updated: 2024/04/12 10:40:59 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlen(char const *str)
{
size_t i;
i = 0;
while (*str++)
{
i++;
if (i == SIZE_MAX)
break ;
}
return (i);
}

33
lib/libft/ft_strmapi.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 17:01:35 by vvobis #+# #+# */
/* Updated: 2024/04/12 10:13:57 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
char *tmp;
unsigned int i;
if (!s || !f)
return (NULL);
tmp = ft_calloc(ft_strlen(s) + 1, sizeof(*tmp));
if (!tmp)
return (NULL);
i = 0;
while (s[i])
{
tmp[i] = f(i, s[i]);
i++;
}
tmp[i] = 0;
return (tmp);
}

31
lib/libft/ft_strncmp.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 13:33:22 by vvobis #+# #+# */
/* Updated: 2024/04/12 10:29:59 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strncmp(char const *s1, char const *s2, size_t n)
{
size_t i;
if (n == 0)
return (0);
i = 0;
while ((s1[i] || s2[i]) && i < n)
{
if (s1[i] < s2[i])
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
else if (s1[i] > s2[i])
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
i++;
}
return (0);
}

36
lib/libft/ft_strnstr.c Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 15:36:41 by vvobis #+# #+# */
/* Updated: 2024/04/10 16:42:02 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(char const *s1, char const *s2, size_t n)
{
size_t i;
size_t j;
if (!*s2)
return ((char *)s1);
i = 0;
while (i < n && s1[i])
{
j = 0;
if (s1[i + j] == s2[j])
{
while (s1[i + j] == s2[j] && s1[i + j] && i + j < n)
j++;
if (!s2[j])
return ((char *)&s1[i]);
}
i++;
}
return (NULL);
}

33
lib/libft/ft_strrchr.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 12:20:50 by vvobis #+# #+# */
/* Updated: 2024/04/09 20:54:22 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strrchr(char const *s, int c)
{
char *n;
int i;
i = 0;
n = NULL;
while (s[i] != 0)
{
if (s[i] == (char)c)
n = (char *)&s[i];
i++;
}
if ((char)c == 0)
return ((char *)&s[i]);
if (n)
return (n);
return (NULL);
}

47
lib/libft/ft_strtrim.c Normal file
View File

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 19:30:35 by vvobis #+# #+# */
/* Updated: 2024/04/15 13:07:55 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_set(char c, char const *s)
{
while (*s)
if (c == *s++)
return (1);
return (0);
}
static int find_end(char const *s1, char const *set)
{
int i;
i = 0;
while (*s1)
s1++;
while (is_set(*--s1, set))
i++;
return (i);
}
char *ft_strtrim(char const *s1, char const *set)
{
if (!s1)
return (NULL);
if (!set)
return ((char *)s1);
while (is_set(*s1, set) && *s1)
s1++;
if ((int)ft_strlen(s1) > find_end(s1, set))
return (ft_substr(s1, 0, ft_strlen(s1) - find_end(s1, set)));
else
return (ft_calloc(1, 1));
}

41
lib/libft/ft_substr.c Normal file
View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 18:58:25 by vvobis #+# #+# */
/* Updated: 2024/04/09 21:50:44 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *tmp;
unsigned int i;
i = 0;
if (!s || start >= ft_strlen(s) || len <= 0)
{
tmp = malloc(1);
if (!tmp)
return (NULL);
tmp[i] = 0;
return (tmp);
}
if (len + start > ft_strlen(s))
len = ft_strlen(s) - start;
tmp = malloc(sizeof(*tmp) * len + 1);
if (!tmp)
return (NULL);
while (i < len && s[i])
{
tmp[i] = s[i + start];
i++;
}
tmp[i] = 0;
return (tmp);
}

20
lib/libft/ft_tolower.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 12:01:54 by vvobis #+# #+# */
/* Updated: 2024/04/03 12:15:18 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_tolower(int c)
{
if (c >= 65 && c <= 90)
c += 32;
return (c);
}

20
lib/libft/ft_toupper.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 12:18:44 by vvobis #+# #+# */
/* Updated: 2024/04/03 12:19:05 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_toupper(int c)
{
if (c >= 97 && c <= 122)
c -= 32;
return (c);
}

92
lib/libft/libft.h Normal file
View File

@ -0,0 +1,92 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bszilas <bszilas@student.42vienna.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 23:43:36 by vvobis #+# #+# */
/* Updated: 2024/12/10 21:12:01 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include "../gnl/get_next_line.h"
# include "./printf/ft_printf.h"
# include <stdlib.h>
# include <unistd.h>
# include <stddef.h>
# include <stdint.h>
# include <limits.h>
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
int ft_isalpha(int c);
int ft_isdigit(int c);
int ft_isalnum(int c);
int ft_isprint(int c);
int ft_isascii(int c);
int ft_isspace(int c);
/*Memory Management*/
int ft_memcmp(void *s1, void const *s2, size_t n);
void *ft_memset(void *s, int c, size_t n);
void ft_bzero(void *s, size_t n);
void *ft_memcpy(void *dest, void const *src, size_t n);
void *ft_memmove(void *dest, void const *src, size_t n);
void *ft_memchr(void *s, int c, size_t n);
void *ft_calloc(size_t n, size_t s);
/*Info Conversion*/
int ft_atoi(char const *s);
double ft_atod(char *s);
char *ft_itoa(int n);
char **ft_split(char const *s, char c);
void ft_free(void *ptr_ptr);
long ft_atol(const char *nptr);
/*String Manip*/
int ft_toupper(int c);
int ft_tolower(int c);
int ft_strncmp(char const *s1, char const *s2, size_t n);
char *ft_strnstr(char const *s1, char const *s2, size_t n);
char *ft_strchr(char const *s, int c);
char *ft_strrchr(char const *s, int c);
char *ft_strdup(char const *s);
char *ft_substr(char const *s, unsigned int start, size_t len);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_strtrim(char const *s1, char const *set);
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
void ft_striteri(char const *s, void (*f)(unsigned int, char *));
size_t ft_strlcpy(char *dest, char const *src, size_t size);
size_t ft_strlcat(char *dest, char const *src, size_t size);
size_t ft_strlen(char const *str);
/*List manip*/
int ft_lstsize(t_list *lst);
void ft_lstdelone(t_list *lst, void (*del)(void*));
void ft_lstclear(t_list **lst, void (*del)(void*));
void ft_lstiter(t_list *lst, void (*f)(void *));
void ft_lstadd_front(t_list **lst, t_list *node_new);
void ft_lstadd_back(t_list **lst, t_list *node_new);
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
t_list *ft_lstnew(void *content);
t_list *ft_lstlast(t_list *lst);
/*Output*/
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *str, int fd);
void ft_putendl_fd(char *str, int fd);
void ft_putnbr_fd(int n, int fd);
/* Get Next Line */
char *get_next_line(int fd);
#endif

50
lib/libft/printf/Makefile Normal file
View File

@ -0,0 +1,50 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: victor </var/spool/mail/victor> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/14 09:21:46 by victor #+# #+# #
# Updated: 2024/11/11 11:52:05 by marvin ### ########.fr #
# #
# **************************************************************************** #
LIBDIR := lib
NAME := $(LIBDIR)/libftprintf.a
CC := gcc
CFLAGS = -Wall -Werror -Wextra -g3
SRC := ft_fprintf.c ft_printf.c ft_putascii.c ft_puthex.c ft_putptr.c ft_strlen.c ft_putfloat.c
OBJ := $(SRC:.c=.o)
ifdef RELEASE
CFLAGS -= -g3
endif
MKDIR = mkdir
ifeq ($(OS), Windows_NT)
RM = del /Q
else
RM = rm -rf
MKDIR += -p
endif
all: $(NAME)
$(NAME): $(OBJ)
ar rsc $@ $(OBJ)
$(OBJ): $(SRC) $(LIBDIR)
$(CC) $(CFLAGS) -c $(SRC)
$(LIBDIR):
$(MKDIR) $@
clean:
$(RM) $(OBJ)
fclean: clean
$(RM) $(LIBDIR)
re: fclean all

View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_fprintf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/17 12:04:30 by vvobis #+# #+# */
/* Updated: 2024/09/17 12:09:29 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "./ft_printf.h"
int ft_fprintf(int fd, const char *format, ...)
{
va_list args;
int count;
if (!format || fd < 0)
return (-1);
va_start(args, format);
count = 0;
while (1)
{
while (*format != '%' && *format)
ft_putchar(*format++, &count, fd);
if (!*format)
break ;
else
format++;
if (!*format || !handle_arg(args, *format, &count, fd))
return (-1);
format++;
}
va_end(args);
return (count);
}

View File

@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 17:50:35 by vvobis #+# #+# */
/* Updated: 2024/09/17 14:11:57 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int handle_arg(va_list args, char format, int *count, int fd)
{
if (format == 'd' || format == 'i')
ft_putnbr(va_arg(args, int), count, fd);
else if (format == 'u')
ft_putnbr(va_arg(args, unsigned int), count, fd);
else if (format == 's')
ft_putstr(va_arg(args, char *), count, fd);
else if (format == 'X')
ft_puthex_upper(va_arg(args, unsigned int), count, fd);
else if (format == 'x')
ft_puthex_lower(va_arg(args, unsigned int), count, fd);
else if (format == 'p')
ft_putptr(va_arg(args, void *), count, fd);
else if (format == 'c')
ft_putchar(va_arg(args, int), count, fd);
else if (format == '%')
ft_putchar('%', count, fd);
else if (format == 'f')
ft_putnbrf(va_arg(args, double), count, 5, fd);
else
return (0);
return (1);
}
int ft_printf(const char *format, ...)
{
va_list args;
int count;
if (!format)
return (-1);
va_start(args, format);
count = 0;
while (1)
{
while (*format != '%' && *format)
ft_putchar(*format++, &count, STDOUT_FILENO);
if (!*format)
break ;
else
format++;
if (!*format || !handle_arg(args, *format, &count, STDOUT_FILENO))
return (-1);
format++;
}
va_end(args);
return (count);
}

View File

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 12:50:35 by vvobis #+# #+# */
/* Updated: 2024/11/11 11:42:50 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdarg.h>
# include <stdlib.h>
# include <stdio.h>
# ifdef _WIN32
# include <io.h>
# undef STDIN_FILENO
# define STDIN_FILENO _fileno(stdin)
# undef STDOUT_FILENO
# define STDOUT_FILENO _fileno(stdout)
# define write _write
# else
# include <unistd.h>
# endif
int ft_fprintf(int fd, const char *format, ...);
int ft_printf(const char *format, ...);
int handle_arg(va_list args, char format, int *count, int fd);
void ft_puthex_lower(unsigned long nbr, int *count, int fd);
void ft_puthex_upper(unsigned long nbr, int *count, int fd);
void ft_putchar(int c, int *count, int fd);
void ft_putnbr(long n, int *count, int fd);
void ft_putnbrf(double f, int *count, int precision, int fd);
void ft_putstr(const char *str, int *count, int fd);
void ft_putptr(void *ptr, int *count, int fd);
size_t ft_strlen(const char *s);
#endif

View File

@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 17:54:53 by vvobis #+# #+# */
/* Updated: 2025/05/14 17:11:37 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putchar(int c, int *count, int fd)
{
write(fd, &c, 1);
*count += 1;
}
void ft_putnbr(long n, int *count, int fd)
{
if (n < 0)
ft_putchar(0x2d, count, fd);
if (n <= -10)
ft_putnbr(n / -10, count, fd);
if (n >= 10)
ft_putnbr(n / 10, count, fd);
if (n >= 0)
ft_putchar(n % 10 + 0x30, count, fd);
if (n < 0)
ft_putchar(-(n % -10) + 0x30, count, fd);
}
int putstring(char *str, int fd) {
return (write(fd, str, ft_strlen(str)));
}
void ft_putstr(const char *str, int *count, int fd)
{
if (!str)
{
*count += putstring("(null)", fd);
return ;
}
*count += putstring((char *)str, fd);
}

View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putfloat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/17 11:50:45 by vvobis #+# #+# */
/* Updated: 2024/09/17 12:07:47 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putnbrf(double f, int *count, int precision, int fd)
{
int number;
double fraction;
unsigned int mult;
mult = 1;
while (precision-- > 0)
{
mult *= 10;
}
number = (int)f;
fraction = f - (double)number;
if (fraction < 0)
fraction = -fraction;
ft_putnbr(number, count, fd);
ft_putchar('.', count, fd);
number = fraction * mult;
ft_putnbr(number, count, fd);
}

View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_puthex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 17:52:38 by vvobis #+# #+# */
/* Updated: 2024/09/17 12:09:29 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_puthex_upper(unsigned long nbr, int *count, int fd)
{
char *base_str;
base_str = "0123456789ABCDEF";
if (nbr >= 16)
ft_puthex_upper(nbr / 16, count, fd);
ft_putchar(base_str[(nbr % 16)], count, fd);
}
void ft_puthex_lower(unsigned long nbr, int *count, int fd)
{
char *base_str;
base_str = "0123456789abcdef";
if (nbr >= 16)
ft_puthex_lower(nbr / 16, count, fd);
ft_putchar(base_str[(nbr % 16)], count, fd);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putptr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 17:53:53 by vvobis #+# #+# */
/* Updated: 2025/05/14 17:13:56 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putptr(void *ptr, int *count, int fd)
{
void **to_print;
if (!ptr)
{
ft_putstr("(nil)", count, fd);
return ;
}
to_print = &ptr;
*count += write(fd, "0x", 2);
ft_puthex_lower((unsigned long long)*to_print, count, fd);
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 16:01:03 by vvobis #+# #+# */
/* Updated: 2024/09/14 09:24:02 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include "limits.h"
size_t ft_strlen(char const *str)
{
size_t i;
i = 0;
while (*str++)
{
i++;
if (i == ULONG_MAX)
break ;
}
return (i);
}

View File

@ -50,6 +50,7 @@ malloc: ; RAX: long basic_malloc(RDI: size_t n)
mov r8, rdi
push rdi
%ifdef DEBUG_BUILD
mov rdi, success
call putstr
mov rdi, r8
@ -63,6 +64,7 @@ malloc: ; RAX: long basic_malloc(RDI: size_t n)
mov rdi, str_bytecnt
call putendl
%endif
pop rax
sub rax, rbx
pop rbx

18
src/core/mem/memcpy.s Normal file
View File

@ -0,0 +1,18 @@
section .text
global memcpy
memcpy: ; (rdi: dst*, rsi: src*, rdx: len)
xor rcx, rcx
push rbx
.copy:
cmp rcx, rdx
je .done
mov bl, byte [rsi + rcx]
mov byte [rdi + rcx], bl
inc rcx
jmp .copy
.done:
pop rbx
ret

14
src/core/mem/memset.s Normal file
View File

@ -0,0 +1,14 @@
section .text
global memset
memset: ; (rdi: mem*, rsi: c, rdx: n)
xor rcx, rcx
.loop:
cmp rcx, rdx
je .done
mov byte [rdi + rcx], sil
inc rcx
jmp .loop
.done:
ret

15
src/core/string/atoi.s Normal file
View File

@ -0,0 +1,15 @@
section .text
extern strlen
atoi: ; rax: int (rdi: str*)
push rbp
mov rbp, rsp
sub rsp, 16
xor rax, rax
xor rcx, rcx
push rdi
call strlen
mov [rbp - 8], rax
.loop:
.done:

View File

@ -4,7 +4,7 @@ section .text
extern malloc
extern putstr
extern putnumber
extern strcpy
extern strdup
extern memchr
extern substr
extern strlen
@ -121,7 +121,7 @@ split: ; RAX: char ** split(RDI: char *, RSI: int)
jmp .cleanup
.no_match:
push rcx
call strcpy
call strdup
pop rcx
mov [rcx], rax
.done:

View File

@ -1,10 +1,10 @@
global strcpy
global strdup
section .text
extern malloc
extern strlen
strcpy: ; rax: char *(rdi: char *)
strdup: ; rax: char *(rdi: char *)
push rbx
push rcx
call strlen

View File

@ -0,0 +1,12 @@
%define STRING_INIT_CAP 1024
%define STR_SIZE 16
%define STR_LEN 0
%define STR_CAP 4
%define STR_DATA 8
; struct string:
; .len 0 uint
; .cap 4 uint
; .data 8 char*

View File

@ -0,0 +1,84 @@
%include "./src/core/string_builder/sb.s"
section .text
extern strlen
extern memcpy
extern malloc
extern err_malloc
extern ft_strlcpy
extern ft_strlcat
%define SB [rbp - 16]
%define SB_LEN dword [rbp - 4]
%define SB_CAP dword [rbp - 8]
%define APPENDIX [rbp - 24]
%define APP_LEN dword [rbp - 28]
global sb_append
sb_append: ; (rdi: *sb, rsi: char*)
push rbp
mov rbp, rsp
sub rsp, 32
test rsi, rsi
jz .done
; store sb on stack
mov SB, rdi
; store new str on stack
mov APPENDIX, rsi
; get sb len
mov eax, dword [rdi + STR_LEN]
mov SB_LEN, eax
mov eax, dword [rdi + STR_CAP]
mov SB_CAP, eax
push rdi
mov rdi, rsi
call strlen
mov APP_LEN, eax
add eax, SB_LEN
cmp eax, SB_CAP
jl .copy_string
mov r9d, SB_CAP
; new string will be to large for current cap, need to realloc
.get_new_len:
imul r9, 2
cmp r9, rax
jl .get_new_len
mov SB_CAP, r9d
push r9
push rax
mov rdi, r9
call malloc
test rax, rax
jz err_malloc
mov rdi, rax
mov rsi, SB
mov rsi, [rsi + STR_DATA]
mov edx, SB_LEN
call ft_strlcpy
pop rax
mov rsi, SB
pop r9
mov [rsi + STR_CAP], r9
.copy_string:
pop rdi
mov r9, SB
mov dword [r9 + STR_LEN], eax
mov rdi, [r9 + STR_DATA]
mov eax, dword [rbp - 4]
mov rsi, APPENDIX
mov edx, SB_CAP
call ft_strlcat
.done:
mov rsp, rbp
pop rbp
ret

View File

@ -0,0 +1,25 @@
%include "./src/core/string_builder/sb.s"
%define FD dword [rbp - 4]
section .text
extern write
global sb_dump_to_file
sb_dump_to_file: ; (rdi: *sb, rsi: int fd)
push rbp
mov rbp, rsp
sub rsp, 16
; load data, len and fd into regs to prepare for write syscall
mov FD, esi
mov rsi, [rdi + STR_DATA]
mov rdx, [rdi + STR_LEN]
mov edi, FD
; dump to file
call write
mov rsp, rbp
pop rbp
ret

View File

@ -0,0 +1,55 @@
%include "./src/core/string_builder/sb.s"
section .text
extern malloc
extern err_malloc
extern memcpy
extern strlen
global sb_new
sb_new: ; rax: str*(rdi: char* || NULL, rsi: *hidden_copy_ptr)
push rbx
push rsi
mov rbx, STRING_INIT_CAP
xor r9, r9
test rdi, rdi
jz .alloc_string
push rdi
call strlen
pop rdi
mov r9, rax
cmp r9, STRING_INIT_CAP
jl .alloc_string
.calc_init_len:
add rbx, STRING_INIT_CAP
cmp r9, rbx
jg .calc_init_len
.alloc_string:
push rdi
mov rdi, rbx
call malloc
test rax, rax
jz err_malloc
pop rdi
pop rsi
mov dword [rsi + STR_CAP], ebx
mov dword [rsi + STR_LEN], r9d
mov [rsi + STR_DATA], rax
test r9, r9
jz .done
push rdi
mov rdi, rax
pop rsi
mov rdx, r9
call memcpy
.done:
pop rbx
ret

View File

@ -5,18 +5,24 @@ section .text
global read
global get_file_size
global lseek
global write
open: ; rax: int fd (rdi: char *name, rsi: int flags)
mov rax, 2 ; sys_open
write: ; write(RDI: int fd, RSI: char *buffer, RDX: size_t size)
mov rax, 1
syscall
ret
open: ; rax: int fd (rdi: char *name, rsi: int flags)
mov rax, 2 ; sys_open
syscall
ret
close:
mov rax, 3 ; sys_close
mov rax, 3 ; sys_close
syscall
ret
read: ; read(RDI: int fd, RSI: char *buffer, RDX: size_t size)
read: ; read(RDI: int fd, RSI: char *buffer, RDX: size_t size)
mov rax, 0 ; sys_read
syscall ;
ret

9
src/core/syscall/fork.s Normal file
View File

@ -0,0 +1,9 @@
%define SYS_FORK 57
section .text
global fork
fork: ; rax: pid()
mov rdi, SYS_FORK
syscall
ret

View File

@ -1,7 +1,7 @@
section .data
EARGCNT: db "[ERROR] Invalid arg count: expected 1", 0xa, 0
EMALLOC: db "[ERROR] Malloc fialed!", 0xa, 0
ELSEEK: db "[ERROR] lseek failed!", 0xa, 0
EARGCNT: db 0xa, "[ERROR] Invalid arg count: expected 1", 0xa, 0
EMALLOC: db 0xa, "[ERROR] Malloc failed!", 0xa, 0
ELSEEK: db 0xa, "[ERROR] lseek failed!", 0xa, 0
section .text
global err_args

View File

@ -0,0 +1,54 @@
%include "./src/core/vector/vector.inc"
section .text
extern malloc
extern err_malloc
extern memcpy
extern memset
global vec_create
vec_create: ; rax: vec* (rdi: member_size)
push rbp
mov rbp, rsp
sub rsp, 16
; allocate vector ptr
push rdi
mov rdi, VEC_SIZE
call malloc
test rax, rax
jz err_malloc
mov rdi, rax
mov rsi, 0
mov rdx, VEC_SIZE
call memset
pop rdi
mov [rbp - 8], rax ; store vec
mov dword [rax + VEC_COUNT], 0
mov dword [rax + VEC_CAP], VEC_DEFAULT_CAP
mov dword [rax + VEC_MEMBER_SIZE], edi
; allocate initial vector capacity
imul rdi, VEC_DEFAULT_CAP
push rdi
call malloc
test rax, rax
jz err_malloc
pop rdi
mov rdx, rdi
mov rdi, rax
mov rsi, 0
call memset
mov rdi, [rbp - 8]
mov [rdi + VEC_MEM], rax
mov rax, rdi
mov rsp, rbp
pop rbp
ret

36
src/core/vector/vec_get.s Normal file
View File

@ -0,0 +1,36 @@
%include "./src/core/vector/vector.inc"
section .text
extern memcpy
global vec_get
vec_get: ; rax: bool (rdi: vec*, rsi: dest*, rdx: idx)
push rbp
mov rbp, rsp
sub rsp, 16
mov eax, dword [rdi + VEC_COUNT]
cmp eax, edx
jl .not_found
push rsi
mov rax, rdx
mov rsi, [rdi + VEC_MEM]
imul eax, dword [rdi + VEC_MEMBER_SIZE]
add rsi, rax
mov edx, dword [rdi + VEC_MEMBER_SIZE]
pop rdi
call memcpy
mov rax, 1
jmp .done
.not_found:
xor rax, rax
.done:
mov rsp, rbp
pop rbp
ret

47
src/core/vector/vec_pop.s Normal file
View File

@ -0,0 +1,47 @@
%include "./src/core/vector/vector.inc"
section .text
extern memset
%define vec_count dword [rbp - 4]
%define vec_cap dword [rbp - 8]
%define vec_member_size dword [rbp - 12]
%define vec_mem [rbp - 20]
global vec_pop
vec_pop: ; rax: bool (rdi: vec*)
push rbp
mov rbp, rsp
sub rsp, 32
push rbx
mov eax, dword [rdi + VEC_COUNT]
test eax, eax
jz .done
mov vec_count, eax
mov eax, dword [rdi + VEC_MEMBER_SIZE]
mov vec_member_size, eax
mov rax, qword [rdi + VEC_MEM]
mov vec_mem, rax
push rdi
mov ebx, vec_member_size
imul ebx, vec_count
sub ebx, vec_member_size
mov rdi, vec_mem
add rdi, rbx
mov rsi, 0
mov edx, vec_member_size
call memset
pop rdi
dec dword [rdi + VEC_COUNT]
.done:
pop rbx
mov rsp, rbp
pop rbp
ret

View File

@ -0,0 +1,69 @@
%include "./src/core/vector/vector.inc"
section .text
extern malloc
extern err_malloc
extern memcpy
%define vec_count dword [rbp - 4]
%define vec_cap dword [rbp - 8]
%define vec_member_size dword [rbp - 12]
%define vec_mem [rbp - 20]
global vec_push
vec_push: ; (rdi: vec*, rsi: mem*)
push rbp
mov rbp, rsp
sub rsp, 48
mov eax, [rdi + VEC_COUNT]
mov vec_count, eax
mov eax, [rdi + VEC_CAP]
mov vec_cap, eax
mov eax, [rdi + VEC_MEMBER_SIZE]
mov vec_member_size, eax
mov rax, [rdi + VEC_MEM]
mov vec_mem, rax
mov [rbp - 32], rdi ; store vec
mov [rbp - 40], rsi
mov eax, vec_count
cmp eax, vec_cap
je .reallocate
.push:
mov rsi, [rbp - 40]
mov rdi, [rbp - 32]
mov eax, dword [rdi + VEC_MEMBER_SIZE]
imul eax, vec_count
mov rdi, [rdi + VEC_MEM]
add rdi, rax
mov edx, vec_member_size
call memcpy
mov rdi, [rbp - 32]
inc dword [rdi + VEC_COUNT]
.done:
mov rsp, rbp
pop rbp
ret
.reallocate:
imul eax, 2
mov edi, eax
push rax
call malloc
test rax, rax
jz err_malloc
mov rdi, rax
mov rsi, vec_mem
mov edx, vec_count
call memcpy
pop rax
mov rdx, [rbp - 32]
mov [rdx + VEC_MEM], rdi
mov dword [rdx + VEC_CAP], eax
jmp .push

View File

@ -0,0 +1,43 @@
%define VEC_SIZE 32
%define VEC_DEFAULT_CAP 64
%define VEC_COUNT 0
%define VEC_CAP 4
%define VEC_MEMBER_SIZE 8
%define VEC_MEM 16
; for all macros,
; arg1 should be the target reg,
; arg2 is the offset on the stack for vec_count
%macro vec_cap 2
mov %1, [rbp - (%2 + 8)]
%endmacro
%macro vec_member_size 1
[rbp - (%1 + 12)]
%endmacro
%macro vec_mem 2
mov %1, [rbp - (%2 + 20)]
%endmacro
%macro vec_onto_stack 2
mov eax, dword [%1 + VEC_COUNT]
mov dword [rbp - (%2 + 4)], eax
mov eax, dword [%1 + VEC_CAP]
mov dword [rbp - (%2 + 8)], eax
mov eax, dword [%1 + VEC_MEMBER_SIZE]
mov dword [rbp - (%2 + 12)], eax
mov rax, [%1 + VEC_MEM]
mov [rbp - (%2 + 20)], rax
%endmacro
; struct vec
; .count 0
; .cap 4
; .member_size 8
; .mem 16

View File

@ -0,0 +1,17 @@
section .data
putnumber_func: db "putnumber", 0
exit_func: db "exit", 0
global ftable
ftable: dq 2, \
putnumber_func, \
exit_func
global func_call_prologue
func_call_prologue: db " push rdi", 0x0a, 0
global func_call
func_call: db " call %s", 0x0a, 0
global func_call_epilogue
func_call_epilogue: db " pop rdi", 0x0a, 0

6
src/global/regs.s Normal file
View File

@ -0,0 +1,6 @@
section .data
global REG_RDI
REG_RDI: db "rdi", 0
global REG_RAX
REG_RAX: db "rax", 0

39
src/inc/c_alignment.s Normal file
View File

@ -0,0 +1,39 @@
%macro c_call 1-*
push r12
; Save current stack pointer in a scratch register
mov r12, rsp
mov al, 0
; Align stack to 16 bytes
and rsp, -16
; Set up arguments if provided
%if %0 > 1
mov rdi, %2
%endif
%if %0 > 2
mov rsi, %3
%endif
%if %0 > 3
mov rdx, %4
%endif
%if %0 > 4
mov rcx, %5
%endif
%if %0 > 5
mov r8, %6
%endif
%if %0 > 6
mov r9, %7
%endif
; Set al to 0 for variadic functions
mov al, 0
; Make the call
call %1
; Restore original stack pointer
mov rsp, r12
pop r12
%endmacro

View File

@ -4,8 +4,8 @@
%define EXPR_TOK_CNT 8
%define EXPR_TOK 16
; struct expression size = 32
; .type
; .tok_count + 8
; .tokens + 16

7
src/inc/function_table.s Normal file
View File

@ -0,0 +1,7 @@
%define FTABLE_SIZE 16
%define FTABLE_COUNT 2
%define FTABLE_SYM 8
; struct func_table
; .count 0
; .symbols 8

View File

@ -1,3 +0,0 @@
section .data
REG_RAX: db "rax", 0

View File

@ -0,0 +1,35 @@
%include "./src/inc/c_alignment.s"
section .text
extern putstr
extern putnumberendl
extern fd_out
extern ft_fprintf
func_stack_alloc: db " push rbp", 0xa, " mov rbp, rsp", 0xa, " sub rsp, %d", 0x0a, 0
func_stack_dealloc: db " mov rsp, rbp", 0xa, " pop rbp", 0xa, 0
ret_inst: db " ret", 0xa, 0
global func_prologue
func_prologue: ; (rdi: var_count)
; calculate var offsets
mov rax, 8
imul rax, rdi
; prepare func call
mov rdi, [fd_out]
mov rsi, func_stack_alloc
mov rdx, rax
c_call ft_fprintf
ret
global func_epilogue
func_epilogue: ;
mov rdi, [fd_out]
mov rsi, func_stack_dealloc
c_call ft_fprintf
ret

196
src/lexer/lex_func.s Normal file
View File

@ -0,0 +1,196 @@
%include "./src/inc/lexer.s"
%include "./src/inc/expression.s"
%include "./src/inc/function_table.s"
%include "./src/inc/token.s"
%include "./src/inc/c_alignment.s"
%define FUNC_ERR "[FUNC_ERROR]"
section .data
err_func_args_message: db FUNC_ERR, "invalid argument", 0
extern func_call
extern func_call_prologue
extern func_call_epilogue
extern REG_RDI
extern REG_RAX
extern fd_out
section .text
global lex_func_call
extern ftable
extern strcmp
extern lex_eundefined
extern putstr
extern putendl
extern putnumberendl
extern exit
extern look_up_var
extern insert_mov
extern load_var_reg
extern load_const_reg
extern ft_fprintf
insert_func_with_const: ; (rdi: name*, rsi: arg*)
push rbp
mov rbp, rsp
sub rsp, 16
mov [rbp - 8], rdi ; store name
mov [rbp - 16], rsi ; store arg
mov rdi, [fd_out]
mov rsi, func_call_prologue
call ft_fprintf
mov rdi, [rbp - 16]
mov rsi, REG_RDI
mov rdx, REG_RAX
call load_const_reg
mov rdi, [fd_out]
mov rsi, func_call
mov rdx, [rbp - 8]
c_call ft_fprintf
mov rdi, [fd_out]
mov rdi, func_call_epilogue
c_call ft_fprintf
mov rsp, rbp
pop rbp
ret
insert_func_with_var: ; (rdi: name*, rsi: arg*)
push rbp
mov rbp, rsp
sub rsp, 16
mov [rbp - 8], rdi ; store name
mov [rbp - 16], rsi ; store arg
mov rdi, [fd_out]
mov rsi, func_call_prologue
call ft_fprintf
mov rdi, [rbp - 16]
mov rsi, REG_RDI
call load_var_reg
mov rdi, [fd_out]
mov rsi, func_call
mov rdx, [rbp - 8]
c_call ft_fprintf
mov rdi, [fd_out]
mov rdi, func_call_epilogue
c_call ft_fprintf
mov rsp, rbp
pop rbp
ret
err_func_args:
mov rdi, err_func_args_message
call putendl
mov rdi, 1
call exit
; does not change rdi
global look_up_func
look_up_func: ; (rdi: name*)
push rbx
mov rbx, FTABLE_COUNT
xor rcx, rcx
.search_func:
cmp rcx, rbx
je .done_false
; rdi still contains name
push rcx
mov rax, FTABLE_SYM
inc rcx
imul rax, rcx
mov rsi, [ftable + rax]
call strcmp
test rax, rax
pop rcx
jz .done_true
inc rcx
jmp .search_func
.done_true:
mov rax, 1
jmp .done
.done_false:
xor rax, rax
.done:
pop rbx
ret
lex_func_call: ; rax: bool (rdi :lex *)
push rbp
mov rbp, rsp
sub rsp, 32
mov [rbp - 8], rdi ; store lex
; store expression based on lexer expression index
mov rsi, [rdi + LEX_EXPR]
mov rax, [rdi + LEX_EXPR_IDX]
imul rax, EXPR_SIZE
lea rsi, [rsi + rax]
mov [rbp - 16], rsi
mov rdi, [rsi + EXPR_TOK_CNT]
mov rax, [rsi + EXPR_TOK]
cmp qword [rax + TOK_TYPE], TOK_FUNC
jne .done
cmp rdi, 2
jne err_func_args
mov rdi, [rax + TOK_VALUE]
push rdi
call look_up_func
test rax, rax
pop rdi
jz lex_eundefined
mov [rbp - 24], rdi
mov rdi, [rbp - 16]
mov rsi, [rdi + EXPR_TOK]
lea rsi, [rsi + SIZE_TOK]
cmp qword [rsi + TOK_TYPE], TOK_CONST
je .arg_const
cmp qword [rsi + TOK_TYPE], TOK_VAR
je .arg_var
call err_func_args
.arg_const:
mov rdi, [rbp - 24]
mov rsi, [rsi + TOK_VALUE]
call insert_func_with_const
jmp .done
.arg_var:
mov rdi, [rbp - 8]
mov rsi, [rsi + TOK_VALUE]
call look_up_var
mov rsi, rax
mov rdi, [rbp - 24]
call insert_func_with_var
.done:
mov rsp, rbp
pop rbp
ret

View File

@ -1,7 +1,6 @@
%include "./src/inc/lexer.s"
%include "./src/inc/expression.s"
%include "./src/inc/token.s"
%include "./src/inc/regs.s"
section .text
extern malloc
@ -21,12 +20,15 @@ section .text
extern insert_var
extern insert_mov
extern load_var_rax
extern load_rax_var
extern op_const_rax
extern op_var_rax
extern load_reg_var
extern op_const_reg
extern op_var_reg
extern xor_reg
extern look_up_var
extern REG_RAX
extern lex_eexpect
extern lex_eundefined
@ -49,7 +51,8 @@ process_var: ; (rdi: lex*, rsi: name*, rdx: tok_op)
mov rdi, rax
mov rsi, rdx
call op_var_rax
mov rdx, REG_RAX
call op_var_reg
mov rsp, rbp
pop rbp
@ -80,9 +83,15 @@ process_token: ; rax: new_last_tok_type (rdi: lex*, rsi: *tok, rdx: last_to
jmp .done
.process_const:
mov rsi, [rsi + TOK_VALUE]
push rdi
mov rdi, [rsi + TOK_VALUE]
mov rsi, rdx
call op_const_rax
mov rdx, REG_RAX
call op_const_reg
pop rdi
mov rax, TOK_CONST
jmp .done
@ -103,7 +112,7 @@ process_token: ; rax: new_last_tok_type (rdi: lex*, rsi: *tok, rdx: last_to
.done:
ret
is_assignment: ; rax: OFF_S (rdi: expr*)
is_load: ; rax: OFF_S (rdi: expr*)
xor rax, rax
mov rcx, [rdi + EXPR_TOK_CNT]
@ -120,7 +129,7 @@ is_assignment: ; rax: OFF_S (rdi: expr*)
.done:
ret
lex_load: ; (rdi: lex*)
lex_load: ; rax: bool (rdi: lex*)
push rbp
mov rbp, rsp
sub rsp, 48
@ -144,7 +153,7 @@ lex_load: ; (rdi: lex*)
mov rdi, [rbp - 16]
push rdi
call is_assignment
call is_load
test rax, rax
jz .done_false
@ -166,6 +175,7 @@ lex_load: ; (rdi: lex*)
pop rdi
; advance token ptr
mov rdi, [rbp - 16]
mov rsi, [rdi + EXPR_TOK]
xor rcx, rcx
@ -193,9 +203,14 @@ lex_load: ; (rdi: lex*)
.done_true:
mov rdi, [rbp - 40]
call load_rax_var
mov rsi, REG_RAX
call load_reg_var
mov rax, 1
.done_false:
mov rsp, rbp
pop rbp
ret
.done_false:
mov rax, 0
mov rsp, rbp
pop rbp
ret

View File

@ -2,35 +2,27 @@
%include "./src/inc/lexer.s"
%include "./src/inc/expression.s"
%include "./src/inc/asm_output.s"
%include "./src/inc/regs.s"
section .text
extern malloc
extern err_malloc
extern exit
extern putstr
extern putendl
extern create_expressions
extern strcmp
extern VAL_OP_LOAD
extern VAL_OP_ADD
extern VAL_VAR
extern VAL_CONST
extern putchar
extern putnumber
extern putendl
extern get_vars
extern insert_var
extern insert_mov
extern load_var_rax
extern load_rax_var
extern add_const_rax
extern add_var_rax
extern sub_const_rax
extern sub_var_rax
extern xor_reg
extern lex_load
extern lex_func_call
extern lex_eundefined
extern func_prologue
extern func_epilogue
extern memset
extern sb_new
extern program_prologue
global lex
lex: ; rax: lex* (rdi: char *file_content)
@ -47,6 +39,18 @@ lex: ; rax: lex* (rdi: char *file_content)
cmp rax, 0
je err_malloc
; zero out lexer
push rax
mov rdi, rax
mov rsi, 0
mov rdx, LEX_SIZE
call memset
mov rdi, 0
lea rsi, [rax + LEX_OUT]
call sb_new
pop rax
mov [rbp - 24], rax ; store lex on stack
lea rsi, [rbp - 16] ; int* expr_cnt
@ -56,11 +60,19 @@ lex: ; rax: lex* (rdi: char *file_content)
mov rdi, [rbp - 24]
mov [rdi + LEX_EXPR], rax
mov rax, [rbp - 16]
mov [rdi + LEX_EXPR_CNT], rax
mov eax, dword [rbp - 16]
mov dword [rdi + LEX_EXPR_CNT], eax
call get_vars
mov rdi, [rbp - 24]
call program_prologue
mov rdi, [rbp - 24]
mov edi, dword [rdi + LEX_VAR_CNT]
call func_prologue
mov rax, [rbp - 24]
lea rcx, [rax + LEX_EXPR_IDX]
@ -73,20 +85,24 @@ lex: ; rax: lex* (rdi: char *file_content)
push rcx
call lex_load
test rax, rax
jnz .loop_epilog
mov rdi, [rbp - 24]
call lex_func_call
jnz .loop_epilog
.loop_epilog:
pop rcx
inc dword [rcx]
jmp .process_expressions
.done:
call func_epilogue
pop rbx
mov rsp, rbp
pop rbp
ret
global look_up_var
look_up_var: ; rax: bool (rdi: lex*, rsi: name*)
push rbp

View File

@ -0,0 +1,100 @@
%include "./src/inc/c_alignment.s"
%include "./src/inc/lexer.s"
%include "./src/inc/token.s"
%include "./src/inc/expression.s"
section .data
prologue: db "section .text", 0xa, 0
program_entry: db "global _start", 0xa, "_start:", 0xa, 0
extern_str: db "extern %s", 0x0a, 0
section .text
extern putendl
extern putstr
extern fd_out
extern ft_fprintf
global program_prologue
print_program_entry:
mov rdi, [fd_out]
mov rsi, program_entry
c_call ft_fprintf
ret
print_section_text:
mov rdi, [fd_out]
mov rsi, prologue
c_call ft_fprintf
ret
declare_extern: ; (rdi: func_name*)
push rdi
mov rdi, [fd_out]
mov rsi, extern_str
pop rdx
c_call ft_fprintf
ret
declare_extern_func: ;(rdi: lex*)
push rbp
mov rbp, rsp
sub rsp, 16
push rbx
xor rcx, rcx
mov rax, [rdi + LEX_EXPR]
mov [rbp - 8], rax
mov eax, dword [rdi + LEX_EXPR_CNT]
mov [rbp - 12], eax
.loop:
cmp ecx, [rbp - 12]
je .loop_end
mov rbx, [rbp - 8]
mov rax, EXPR_SIZE
imul rax, rcx
lea rbx, [rbx + rax]
mov r8, [rbx + EXPR_TOK_CNT]
mov rbx, [rbx + EXPR_TOK]
push rcx
xor rcx, rcx
.loop_tokens:
cmp rcx, r8
je .loop_tokens_end
mov rax, SIZE_TOK
imul rax, rcx
lea rax, [rbx + rax]
mov rdx, TOK_FUNC
cmp rdx, qword [rax + TOK_TYPE]
jne .dont_print
mov rdi, [rax + TOK_VALUE]
push rcx
push r8
call declare_extern
pop r8
pop rcx
.dont_print:
inc rcx
jmp .loop_tokens
.loop_tokens_end:
pop rcx
inc rcx
jmp .loop
.loop_end:
pop rbx
mov rsp, rbp
pop rbp
ret
program_prologue: ;(rdi: lex*)
push rdi
call print_section_text
pop rdi
call declare_extern_func
call print_program_entry
ret

View File

@ -118,13 +118,14 @@ get_vars: ; (rdi: lex*)
mov eax, dword [rdi + LEX_EXPR_CNT]
mov [rbp - 8], eax
mov rax, [rdi + LEX_EXPR]
mov [rbp - 16], rax
mov [rbp - 16], rax ; store expr*
xor rcx, rcx
push rcx
.loop_expr:
pop rcx
cmp ecx, dword [rdi + LEX_EXPR_CNT]
mov ebx, dword [rbp - 8]
cmp ecx, ebx
je .done
mov rax, EXPR_SIZE
mul rcx
@ -156,7 +157,9 @@ get_vars: ; (rdi: lex*)
pop rcx
pop rdi
cmp rax, 1
pop rax
je .skip_alloc
push rax
mov rax, VAR_SIZE
mul r9
lea rbx, [rbx + rax]

158
src/lexer/vars/insert_var.s Normal file
View File

@ -0,0 +1,158 @@
%include "./src/inc/token.s"
%include "./src/inc/c_alignment.s"
section .data
INST_MOV: db " mov ", 0
INST_ADD: db " add ", 0
INST_SUB: db " sub ", 0
INST_XOR: db " xor %s, %s", 0x0a, 0
OPERAND_PAIR_CONST_REG: db "%s, %s", 0x0a, 0
OPERAND_PAIR_CONST: db "%s, %s", 0x0a, 0
OPERAND_PAIR_REG_VAR: db "[rbp-%d], %s", 0x0a, 0
OPERAND_PAIR_VAR_REG: db "%s, [rbp-%d]", 0x0a, 0
LOAD_REG_VAR: db " mov [rbp-%d], %s", 0x0a, 0
LOAD_VAR_REG: db " mov %s, [rbp-%d]", 0x0a, 0
LOAD_CONST_REG: db " mov %s, %s", 0x0a, 0
section .text
extern putstr
extern putnumber
extern putnumberendl
extern putchar
extern putendl
extern VAL_CONST
extern VAL_VAR
extern VAL_OP_ADD
extern VAL_OP_SUB
extern VAL_OP_LOAD
extern VAL_FUNC
extern REG_RAX
extern REG_RDI
extern ft_fprintf
extern fd_out
global insert_add
insert_add:
mov rdi, [fd_out]
mov rsi, INST_ADD
c_call ft_fprintf
ret
global insert_sub
insert_sub:
mov rdi, [fd_out]
mov rsi, INST_SUB
c_call ft_fprintf
ret
global xor_reg
xor_reg: ; rdi: char*
push rbx
mov rbx, rdi
mov rdi, [fd_out]
mov rsi, INST_XOR
mov rdx, rbx
mov rcx, rbx
c_call ft_fprintf
pop rbx
ret
global load_var_reg
load_var_reg: ; (rdi: OFF_S, rsi: REG*)
push rsi
push rdi
mov rdi, [fd_out]
mov rsi, LOAD_VAR_REG
pop rcx
pop rdx
c_call ft_fprintf
ret
global load_reg_var
load_reg_var: ; (rdi: OFF_S, rsi: REG*)
push rsi
push rdi
mov rdi, [fd_out]
mov rsi, LOAD_REG_VAR
pop rdx
pop rcx
c_call ft_fprintf
ret
global load_const_reg
load_const_reg: ; (rdi: const*, rsi: REG*)
push rdi
push rsi
mov rdi, [fd_out]
mov rsi, LOAD_CONST_REG
pop rdx
pop rcx
c_call ft_fprintf
ret
global op_const_reg
op_const_reg: ; (rdi: char *, rsi: op, rdx: reg*)
push rdi
push rdx
cmp rsi, TOK_SUB
je .sub
jmp .add
.sub:
call insert_sub
jmp .done
.add:
call insert_add
.done:
pop rdx
pop rcx
mov rdi, [fd_out]
mov rsi, OPERAND_PAIR_CONST_REG
c_call ft_fprintf
ret
global op_var_reg
op_var_reg: ; (rdi: OFF_S, rsi: op, rdx: reg)
push rdi
push rdx
cmp rsi, TOK_SUB
je .sub
jmp .add
.sub:
call insert_sub
jmp .done
.add:
call insert_add
.done:
pop rdx
pop rcx
mov rdi, [fd_out]
mov rsi, OPERAND_PAIR_VAR_REG
c_call ft_fprintf
ret

View File

@ -107,9 +107,11 @@ create_expressions: ; rax: exp* (rdi: char *filecontent, rsi: *cnt)
mov rax, EXPR_SIZE
mul rcx
lea rdi, [rbx + rax]
%ifdef DEBUG_BUILD
push rcx
call print_expression
pop rcx
%endif
inc rcx
jmp .expr_loop_print

Some files were not shown because too many files have changed in this diff Show More