push
This commit is contained in:
83
srcs/srcs_asm/parser/ft_cal_progsize.c
Normal file
83
srcs/srcs_asm/parser/ft_cal_progsize.c
Normal file
@@ -0,0 +1,83 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_cal_progsize.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/07/02 15:20:20 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/13 15:26:28 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static unsigned char *ft_resize(unsigned char *str, int size)
|
||||
{
|
||||
int len;
|
||||
int i;
|
||||
int j;
|
||||
unsigned char *str2;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
len = ft_strlen((char*)str);
|
||||
if (!(str2 = malloc(sizeof(char) * (size * 2))))
|
||||
return (NULL);
|
||||
while (i < size * 2 - len)
|
||||
{
|
||||
str2[i] = '0';
|
||||
i++;
|
||||
}
|
||||
while (i < size * 2)
|
||||
{
|
||||
str2[i] = str[j];
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
free(str);
|
||||
return (str2);
|
||||
}
|
||||
|
||||
static unsigned char ft_atoi_hex(unsigned char *str)
|
||||
{
|
||||
unsigned char nb;
|
||||
|
||||
if (str[0] >= '0' && str[0] <= '9')
|
||||
str[0] = str[0] - 48;
|
||||
if (str[1] >= '0' && str[1] <= '9')
|
||||
str[1] = str[1] - 48;
|
||||
if (str[0] >= 'a' && str[0] <= 'f')
|
||||
str[0] = str[0] - 87;
|
||||
if (str[1] >= 'a' && str[1] <= 'f')
|
||||
str[1] = str[1] - 87;
|
||||
if (str[0] != 0)
|
||||
nb = (str[0] * 16) + str[1];
|
||||
else
|
||||
nb = str[1];
|
||||
return (nb);
|
||||
}
|
||||
|
||||
unsigned char *ft_conv_hexa(int nbr, int size)
|
||||
{
|
||||
unsigned char *str;
|
||||
unsigned char *final_str;
|
||||
int len;
|
||||
|
||||
if (!(final_str = (unsigned char*)ft_strnew(size)))
|
||||
return (NULL);
|
||||
if (size == 2)
|
||||
str = (unsigned char *)ft_itoa_base_short(nbr, 16);
|
||||
else
|
||||
str = (unsigned char *)ft_itoa_base_int(nbr, 16);
|
||||
len = ft_strlen((char*)str);
|
||||
if (len < size * 2)
|
||||
str = ft_resize(str, size);
|
||||
while (size * 2 > 0)
|
||||
{
|
||||
final_str[size - 1] = ft_atoi_hex(str + ((size * 2) - 2));
|
||||
size--;
|
||||
}
|
||||
ft_strdel((char **)&str);
|
||||
return (final_str);
|
||||
}
|
77
srcs/srcs_asm/parser/ft_parse_command.c
Normal file
77
srcs/srcs_asm/parser/ft_parse_command.c
Normal file
@@ -0,0 +1,77 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_parse_command.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/17 11:24:46 by mdchane #+# #+# */
|
||||
/* Updated: 2019/07/16 15:35:23 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
int ft_parse_name(t_token *tk, t_header *head)
|
||||
{
|
||||
if (!tk || !tk->contents)
|
||||
return (ft_printf("error: no champion name in the file\n") && 0);
|
||||
if (ft_strlen(tk->contents) > PROG_NAME_LENGTH + 2)
|
||||
{
|
||||
return (ft_printf("error: Champion name too long (Max length 128)\n")
|
||||
&& 0);
|
||||
}
|
||||
if (tk->next && tk->next->type != COMMENT)
|
||||
{
|
||||
return (ft_printf("error: syntax: Line %d: "
|
||||
"too mush arguments for champion name\n", tk->line) && 0);
|
||||
}
|
||||
ft_strcpy_1(head->prog_name, tk->contents);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_parse_champ_cmt(t_token *tk, t_header *head)
|
||||
{
|
||||
if (!tk || !tk->contents)
|
||||
return (ft_printf("error: no champion comment in the file\n") && 0);
|
||||
if (ft_strlen(tk->contents) > COMMENT_LENGTH + 2)
|
||||
{
|
||||
return (ft_printf("error: Champion comment too long "
|
||||
"(Max length 2048)\n")
|
||||
&& 0);
|
||||
}
|
||||
if (tk->next && tk->next->type != COMMENT)
|
||||
{
|
||||
return (ft_printf("error: syntax: Line %d: "
|
||||
"too mush arguments for champion comment\n", tk->line) && 0);
|
||||
}
|
||||
ft_strcpy_1(head->comment, tk->contents);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int parse_header(t_lst_tk **lst, t_header *head)
|
||||
{
|
||||
while ((*lst) && (*lst)->token && (*lst)->token->type == COMMENT)
|
||||
(*lst) = (*lst)->next;
|
||||
if ((*lst) && (*lst)->token && (*lst)->token->type == COMMAND
|
||||
&& ft_strcmp((*lst)->token->contents, NAME_CMD_STRING) == 0)
|
||||
{
|
||||
if (!ft_parse_name((*lst)->token->next, head))
|
||||
return (0);
|
||||
}
|
||||
else
|
||||
return (ft_printf("error: no name in the file\n") && 0);
|
||||
(*lst) = (*lst)->next;
|
||||
while ((*lst) && (*lst)->token && (*lst)->token->type == COMMENT)
|
||||
(*lst) = (*lst)->next;
|
||||
if ((*lst) && (*lst)->token && (*lst)->token->type == COMMAND
|
||||
&& ft_strcmp((*lst)->token->contents, COMMENT_CMD_STRING) == 0)
|
||||
{
|
||||
if (!ft_parse_champ_cmt((*lst)->token->next, head))
|
||||
return (0);
|
||||
}
|
||||
else
|
||||
return (ft_printf("error: no champion comment in the file\n") && 0);
|
||||
(*lst) = (*lst)->next;
|
||||
return (1);
|
||||
}
|
125
srcs/srcs_asm/parser/ft_parse_instruc.c
Normal file
125
srcs/srcs_asm/parser/ft_parse_instruc.c
Normal file
@@ -0,0 +1,125 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_parse_instruc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/17 16:17:56 by mdchane #+# #+# */
|
||||
/* Updated: 2019/07/16 15:36:42 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
int is_inst(char *name)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!name)
|
||||
return (-1);
|
||||
i = 0;
|
||||
while (i < 16 && ft_strcmp(name, g_tab[i].name) != 0)
|
||||
i++;
|
||||
if (i == 16)
|
||||
return (-1);
|
||||
else
|
||||
return (i);
|
||||
}
|
||||
|
||||
int ft_verif_param(t_token *tk, int inst, int num_param, t_label *label)
|
||||
{
|
||||
int type;
|
||||
|
||||
if (tk->type == INDIRECT
|
||||
|| (tk->type == INDIRECT_LABEL && get_label(tk, label)))
|
||||
type = T_IND;
|
||||
else if (tk->type == DIRECT
|
||||
|| (tk->type == DIRECT_LABEL && get_label(tk, label)))
|
||||
type = T_DIR;
|
||||
else if (tk->type == REGISTER && ft_atoi(tk->contents + 1) < 100)
|
||||
type = T_REG;
|
||||
else
|
||||
type = 0;
|
||||
if (g_tab[inst].poss[num_param] & type)
|
||||
return (1);
|
||||
else
|
||||
{
|
||||
ft_printf("error: l%d : Invalid parameter %d for instruction %s\n",
|
||||
tk->line, num_param, g_tab[inst].name);
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
|
||||
int ft_correct_instru(t_token *tk, int i, t_label *label, int line)
|
||||
{
|
||||
int num_param;
|
||||
|
||||
num_param = 0;
|
||||
while (tk && num_param < g_tab[i].nb_param)
|
||||
{
|
||||
if (ft_verif_param(tk, i, num_param, label))
|
||||
{
|
||||
tk = tk->next;
|
||||
if (!tk && num_param == g_tab[i].nb_param - 1)
|
||||
return (1);
|
||||
else if (tk && tk->type == COMMENT
|
||||
&& !tk->next && num_param == g_tab[i].nb_param - 1)
|
||||
return (1);
|
||||
else if (tk && ft_strcmp(tk->contents, ",") == 0)
|
||||
num_param++;
|
||||
else
|
||||
return (ft_printf("error: l%d: bad param %d for instru %s\n",
|
||||
line, num_param + 1, g_tab[i].name) && 0);
|
||||
}
|
||||
else
|
||||
return (0);
|
||||
tk = tk->next;
|
||||
}
|
||||
return (ft_printf("error: l%d: bad params for instru %s\n",
|
||||
line, g_tab[i].name) && 0);
|
||||
}
|
||||
|
||||
int ft_calc_size(t_token *tk, int inst)
|
||||
{
|
||||
int size;
|
||||
|
||||
size = 1 + g_tab[inst].byte;
|
||||
while (tk)
|
||||
{
|
||||
if (tk->type == INDIRECT || tk->type == INDIRECT_LABEL)
|
||||
size += 2;
|
||||
else if (tk->type == DIRECT || tk->type == DIRECT_LABEL)
|
||||
size += (g_tab[inst].size) ? 2 : 4;
|
||||
else if (tk->type == REGISTER)
|
||||
size += 1;
|
||||
tk = tk->next;
|
||||
}
|
||||
return (size);
|
||||
}
|
||||
|
||||
int ft_parse_instruct(t_token *tk, t_label *label, unsigned int *size)
|
||||
{
|
||||
int inst;
|
||||
|
||||
inst = is_inst(tk->contents);
|
||||
if (inst < 0)
|
||||
{
|
||||
return (ft_printf("error: l%d: invalid instruction '%s'\n",
|
||||
tk->line, tk->contents) && 0);
|
||||
}
|
||||
if (!tk->next)
|
||||
{
|
||||
return (ft_printf("error: l%d: no params for instru %s\n",
|
||||
tk->line, tk->contents) && 0);
|
||||
}
|
||||
if (ft_correct_instru(tk->next, inst, label, tk->line))
|
||||
{
|
||||
*size += ft_calc_size(tk->next, inst);
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
}
|
73
srcs/srcs_asm/parser/ft_parser.c
Normal file
73
srcs/srcs_asm/parser/ft_parser.c
Normal file
@@ -0,0 +1,73 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_parser.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/05/30 10:48:19 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/16 12:23:55 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
void ft_label_pos(t_label *label, char *tk_lab, int size)
|
||||
{
|
||||
while (label)
|
||||
{
|
||||
if (ft_strequ(label->contents, tk_lab))
|
||||
{
|
||||
label->place = size;
|
||||
}
|
||||
label = label->next;
|
||||
}
|
||||
}
|
||||
|
||||
void init_label(t_label *label)
|
||||
{
|
||||
while (label)
|
||||
{
|
||||
label->contents[ft_strlen(label->contents) - 1] = '\0';
|
||||
label = label->next;
|
||||
}
|
||||
}
|
||||
|
||||
int ft_parse_label(t_label *label, t_token *token, unsigned int *size)
|
||||
{
|
||||
ft_label_pos(label, token->contents, *size);
|
||||
if (!token->next || token->next->type == COMMENT)
|
||||
return (1);
|
||||
else if (!ft_parse_instruct(token->next, label, size))
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_parser(t_lst_tk *lst, t_label *label, t_header *head)
|
||||
{
|
||||
unsigned int size;
|
||||
|
||||
init_label(label);
|
||||
size = 0;
|
||||
if (!parse_header(&lst, head))
|
||||
return (0);
|
||||
while (lst)
|
||||
{
|
||||
if (lst->token->type == INSTRUCTION)
|
||||
{
|
||||
if (!ft_parse_instruct(lst->token, label, &size))
|
||||
return (0);
|
||||
}
|
||||
else if (lst->token->type == LABEL)
|
||||
{
|
||||
if (!(ft_parse_label(label, lst->token, &size)))
|
||||
return (0);
|
||||
}
|
||||
else if (lst->token->type != COMMENT)
|
||||
return (0);
|
||||
lst = lst->next;
|
||||
}
|
||||
if ((head->prog_size = size) == 0)
|
||||
return (ft_printf("error: no instruction in the file\n") && 0);
|
||||
return (1);
|
||||
}
|
95
srcs/srcs_asm/parser/parse_utils.c
Normal file
95
srcs/srcs_asm/parser/parse_utils.c
Normal file
@@ -0,0 +1,95 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parse_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/06 17:08:25 by mdchane #+# #+# */
|
||||
/* Updated: 2019/07/13 17:14:11 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
unsigned char *ft_strextend_nm(unsigned char *s1,
|
||||
char unsigned *s2, int n, int m)
|
||||
{
|
||||
unsigned char *str;
|
||||
int i;
|
||||
|
||||
if (s1 == NULL || s2 == NULL)
|
||||
return (NULL);
|
||||
i = n + m;
|
||||
if (!(str = (unsigned char *)malloc(sizeof(*str) * i + 1)))
|
||||
return (NULL);
|
||||
ft_strcpy_n(str, s1, 0, n);
|
||||
ft_strcpy_n(str, s2, n, m);
|
||||
ft_strdel((char **)&s1);
|
||||
ft_strdel((char **)&s2);
|
||||
return (str);
|
||||
}
|
||||
|
||||
int ft_tklen(t_token *tk)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (!tk)
|
||||
return (0);
|
||||
while (tk)
|
||||
{
|
||||
i++;
|
||||
tk = tk->next;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
unsigned char *ft_strcpy_n(unsigned char *dest,
|
||||
const unsigned char *src, int n, int m)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
while (i < n)
|
||||
i++;
|
||||
j = 0;
|
||||
while (j < m)
|
||||
{
|
||||
dest[i] = src[j];
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
return (dest);
|
||||
}
|
||||
|
||||
char *ft_strnew_1(size_t size)
|
||||
{
|
||||
char *str;
|
||||
|
||||
if (!(str = (char*)malloc(sizeof(char) * size + 1)))
|
||||
return (NULL);
|
||||
ft_memset(str, '0', size);
|
||||
str[size] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_strcpy_1(char *dest, const char *src)
|
||||
{
|
||||
unsigned int i;
|
||||
unsigned int j;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (src[j] != '\0')
|
||||
{
|
||||
if (src[j] != '"')
|
||||
{
|
||||
dest[i] = src[j];
|
||||
i++;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
return (dest);
|
||||
}
|
Reference in New Issue
Block a user