wip lexer
This commit is contained in:
107
src/computorv1.c
107
src/computorv1.c
@@ -1,15 +1,112 @@
|
||||
|
||||
#include "computorv1.h"
|
||||
|
||||
int main(int ac, char **av)
|
||||
int stop_errors(int err)
|
||||
{
|
||||
switch (err)
|
||||
{
|
||||
case -ERROR_UNKNOWN_TOKEN:
|
||||
ft_putstr_fd("error: unknown token\n", STDERR_FILENO);
|
||||
break;
|
||||
|
||||
default:
|
||||
ft_putstr_fd("unknown error\n", STDERR_FILENO);
|
||||
break;
|
||||
}
|
||||
|
||||
exit(err);
|
||||
}
|
||||
|
||||
int skip_whitespace(const char *input, int input_pos)
|
||||
{
|
||||
while (ft_isspace(input[input_pos]))
|
||||
{
|
||||
input_pos++;
|
||||
}
|
||||
return input_pos;
|
||||
}
|
||||
|
||||
int token_is_plus(const char *input, int input_pos)
|
||||
{
|
||||
return (input[input_pos] == '+');
|
||||
}
|
||||
|
||||
int lexerize(const char *input)
|
||||
{
|
||||
int token_count = 0;
|
||||
int input_pos = 0;
|
||||
int token_size = 0;
|
||||
|
||||
while (input[input_pos])
|
||||
{
|
||||
input_pos = skip_whitespace(input, input_pos);
|
||||
|
||||
if (input[input_pos] == '\0')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
token_size = token_is_plus(input, input_pos);
|
||||
if (token_size)
|
||||
{
|
||||
tokens[token_count].type = TOKEN_PLUS;
|
||||
tokens[token_count].var_value = '+';
|
||||
}
|
||||
|
||||
if (token_size == 0)
|
||||
{
|
||||
stop_errors(-ERROR_UNKNOWN_TOKEN);
|
||||
}
|
||||
token_count++;
|
||||
input_pos += token_size;
|
||||
}
|
||||
|
||||
// Add end token
|
||||
tokens[token_count].type = TOKEN_END;
|
||||
tokens[token_count].var_value = '\0';
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int i;
|
||||
int ret;
|
||||
|
||||
if (ac < 2)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
i = 0;
|
||||
while(i < ac) {
|
||||
ft_putstr_fd(av[i], STDOUT_FILENO);
|
||||
ft_putchar_fd('\n', STDOUT_FILENO);
|
||||
while (i < ac)
|
||||
{
|
||||
ft_putnbr(i);
|
||||
ft_putstr(" : ");
|
||||
ft_putstr(av[i]);
|
||||
ft_putchar('\n');
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
|
||||
ret = lexerize(av[1]);
|
||||
if (ret <= 0)
|
||||
{
|
||||
stop_errors(ret);
|
||||
}
|
||||
|
||||
// tmp debug output
|
||||
i = 0;
|
||||
while (tokens[i].type != TOKEN_END)
|
||||
{
|
||||
ft_printf("token %i :\n type : %i\n value : ", i, tokens[i].type);
|
||||
if (tokens[i].type == TOKEN_NUMBER)
|
||||
{
|
||||
ft_printf("%d\n", i, tokens[i].num_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
ft_printf("%c\n", tokens[i].var_value);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user