lexer complete with luke memory solution
This commit is contained in:
@@ -3,18 +3,46 @@
|
||||
#include "errors.h"
|
||||
#include <stdio.h> // tmp for float debug
|
||||
|
||||
void remove_spaces(char *s)
|
||||
{
|
||||
char *read = s;
|
||||
char *write = s;
|
||||
|
||||
// copy all non-space chars
|
||||
while (*read)
|
||||
{
|
||||
if (!ft_isspace(*read))
|
||||
{
|
||||
*write++ = *read;
|
||||
}
|
||||
read++;
|
||||
}
|
||||
*write = '\0';
|
||||
|
||||
// zero the rest of the buffer
|
||||
while (write != read)
|
||||
{
|
||||
*write++ = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int i;
|
||||
int ret;
|
||||
int arg_len;
|
||||
char *input;
|
||||
|
||||
if (ac < 2)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// tmp debug output
|
||||
ft_putstr("-> received args :\n"); // debug
|
||||
i = 0;
|
||||
while (i < ac)
|
||||
{
|
||||
ft_putstr(" ");
|
||||
ft_putnbr(i);
|
||||
ft_putstr(" : ");
|
||||
ft_putstr(av[i]);
|
||||
@@ -22,14 +50,22 @@ int main(int ac, char **av)
|
||||
i++;
|
||||
}
|
||||
|
||||
token tokens[MAX_TOKENS];
|
||||
ret = lexerize(av[1], tokens);
|
||||
if (ret <= 0)
|
||||
{
|
||||
stop_errors(ret);
|
||||
}
|
||||
input = av[1];
|
||||
remove_spaces(input);
|
||||
arg_len = ft_strlen(input);
|
||||
|
||||
ft_putstr("-> input without space : "); // debug
|
||||
ft_putstr(input); // debug
|
||||
ft_putchar('\n'); // debug
|
||||
ft_putstr("-> arg_len : "); // debug
|
||||
ft_putnbr(arg_len); // debug
|
||||
ft_putchar('\n'); // debug
|
||||
|
||||
token tokens[arg_len];
|
||||
lexerize(input, tokens);
|
||||
|
||||
// tmp debug output
|
||||
ft_putchar('\n'); // debug
|
||||
i = 0;
|
||||
while (tokens[i].type != TOKEN_END)
|
||||
{
|
||||
@@ -37,8 +73,10 @@ int main(int ac, char **av)
|
||||
|
||||
if (tokens[i].type == TOKEN_VARIABLE)
|
||||
ft_printf("%20s", "TOKEN_VARIABLE");
|
||||
if (tokens[i].type == TOKEN_NUMBER)
|
||||
ft_printf("%20s", "TOKEN_NUMBER");
|
||||
if (tokens[i].type == TOKEN_NUMBER_INT)
|
||||
ft_printf("%20s", "TOKEN_NUMBER_INT");
|
||||
if (tokens[i].type == TOKEN_NUMBER_DOUBLE)
|
||||
ft_printf("%20s", "TOKEN_NUMBER_DOUBLE");
|
||||
if (tokens[i].type == TOKEN_POWER)
|
||||
ft_printf("%20s", "TOKEN_POWER");
|
||||
if (tokens[i].type == TOKEN_PLUS)
|
||||
@@ -54,13 +92,17 @@ int main(int ac, char **av)
|
||||
|
||||
ft_putstr(" - value : ");
|
||||
|
||||
if (tokens[i].type == TOKEN_NUMBER)
|
||||
if (tokens[i].type == TOKEN_NUMBER_INT)
|
||||
{
|
||||
printf("%f\n", tokens[i].num_value);
|
||||
printf("%i\n", tokens[i].value_int);
|
||||
}
|
||||
else if (tokens[i].type == TOKEN_NUMBER_DOUBLE)
|
||||
{
|
||||
printf("%f\n", tokens[i].value_double);
|
||||
}
|
||||
else
|
||||
{
|
||||
ft_printf("%c\n", tokens[i].var_value);
|
||||
ft_printf("%c\n", tokens[i].value_char);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user