Skip to content

Commit

Permalink
implementing lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
ys-zm committed Jan 31, 2024
1 parent 44486cf commit 3b6c2d8
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 161 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ SRCS = parser/check_elements.c \
game/sprite.c \
game/sprite_utils.c \
parser/lexer.c \
parser/lexer_utils.c
parser/lexer_utils.c \
parser/map_lexer.c \
parser/flag_lexer.c \
parser/extra_lexer.c

HEADER_DIR := include
HEADERS := meta.h \
Expand Down
26 changes: 23 additions & 3 deletions include/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,36 @@ void sprite_sort(double *sprite_dist, int32_t *sprite_order);


// lexer.c

char *extract_file(char *map_file);
bool mandatory_elements(t_lex *lex);
int lex(char *file, t_lex *lexer, t_map *map, t_flag **extras);
int lexer(t_meta *meta, char *map_file);

// lexer_utils.c

void print_lexer_mandatory(t_lex *lexer);
void print_lexer_map(t_map *map);
void print_lexer_extras(t_flag *extras);
void print_lexer_extras(t_flag **extras);

// map_lexer.c

bool nl_only_spaces(char *file);
int end_of_map(char *file);
void skip_map_element(char **file, int *skip);
int input_map_lexer(char *file, t_map *map);
int map_lex(char **file, t_map *map, t_lex *lexer, int *skip);

// extra_lexer.c

char *get_title_val(char *file);
int save_extra_title(t_flag **extras, char **file);
bool is_valid_extra(char *file);
int lexer_input_extra(t_flag **extras, char *file, int *skip);

//flag_lexer.c

// parser.c
char *get_val(char *file);
int input_lexer(t_lex *lex, char *file, int *skip);


#endif
2 changes: 1 addition & 1 deletion src/cub3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ int cub3d(int argc, char **argv)
return (EXIT_FAILURE);
print_lexer_mandatory(&meta.lexer);
print_lexer_map(&meta.map);
print_lexer_extras(meta.extras);
print_lexer_extras(&meta.extras);
return 0;
if (parser(&meta, argv[1]))
return(meta_free(&meta), EXIT_FAILURE);
Expand Down
79 changes: 79 additions & 0 deletions src/parser/extra_lexer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* extra_lexer.c :+: :+: */
/* +:+ */
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/08 15:30:18 by yzaim #+# #+# */
/* Updated: 2024/01/24 11:18:50 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

#include "meta.h"
#include "parser.h"

char *get_title_val(char *file)
{
int i;
char *val;

i = 0;
skip_spaces(&file);
while (file[i] && file[i] != ' ')
i++;
if (i)
{
val = ft_substr(file, 0, i);
if (!val)
return (NULL);
return (val);
}
return (ft_strdup(""));
}

// saves the flag of the extra (for now only sprites called "OBJ")
int save_extra_title(t_flag **extras, char **file)
{
t_flag *new_node;
int i;

i = 0;
new_node = malloc(sizeof(t_flag) * 1);
if (!new_node)
return (pr_err(MALL_ERR), EXIT_FAILURE);
new_node->next = NULL;
new_node->flag = get_title_val(*file);
if (*extras == NULL)
*extras = new_node;
else
{
while ((*extras)->next == NULL)
*extras = (*extras)->next;
*extras = new_node;
}
while (new_node->flag[i])
{
i++;
(*file)++;
}
return (EXIT_SUCCESS);
}

bool is_valid_extra(char *file)
{
if (*file && !ft_strncmp(file, "OBJ", 3))
return (true);
return (false);
}

// inputs extras into the extras linked list (for now only sprites)
int lexer_input_extra(t_flag **extras, char *file, int *skip)
{
while (*extras && (*extras)->next != NULL)
*extras = (*extras)->next;
(*extras)->content = get_val(file);
(*extras)->next = NULL;
*skip = 1;
return (EXIT_SUCCESS);
}
59 changes: 59 additions & 0 deletions src/parser/flag_lexer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* flag_lexer.c :+: :+: */
/* +:+ */
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/08 15:30:18 by yzaim #+# #+# */
/* Updated: 2024/01/24 11:18:50 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

#include "meta.h"
#include "parser.h"

char *get_val(char *file)
{
int i;
char *val;

i = 0;
skip_spaces(&file);
while (file[i] && file[i] != '\n')
i++;
if (i)
{
val = ft_substr(file, 0, i);
if (!val)
return (NULL);
return (val);
}
return (ft_strdup(""));
}

int input_lexer(t_lex *lex, char *file, int *skip)
{
char element[6] = {'N', 'S', 'W', 'E', 'F', 'C'};
char** content[6] = {&lex->n.content, &lex->s.content, &lex->w.content, &lex->e.content, &lex->c.content, &lex->f.content};
int i;

i = 0;
*skip = 1;
skip_spaces(&file);
while (i < 6)
{
if (*file && *file == element[i])
{
if (*content[i])
return (pr_err(DUP_ELEMENTS));
*content[i] = get_val(file + 2);
if (!(content[i]))
return (pr_err(MALL_ERR), EXIT_FAILURE);
if (!ft_strncmp(*content[i], "", 1))
return (pr_err(M_PATH), EXIT_FAILURE);
}
i++;
}
return (EXIT_SUCCESS);
}
Loading

0 comments on commit 3b6c2d8

Please sign in to comment.