update file structure

This commit is contained in:
hugogogo
2026-05-06 17:51:07 +02:00
parent 0046409c89
commit 4b6ad34720
5 changed files with 172 additions and 162 deletions

146
src/launcher.c Normal file
View File

@@ -0,0 +1,146 @@
/* computorv1.c */
#include "computorv1.h"
static 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';
}
}
static size_t count_any_of(const char *s, const char *set)
{
size_t count = 0;
for (; *s != '\0'; s++)
{
if (ft_strchr(set, *s) != NULL)
{
count++;
}
}
return count;
}
static void tokens_fill_null(s_token *tokens, size_t arg_len)
{
size_t i;
i = 0;
while (i < arg_len)
{
tokens[i].type = TOKEN_END;
tokens[i].tag = TOKEN_NO_TAG;
tokens[i].value_char = '\0';
i++;
}
}
static void terms_fill_null(s_term *terms, size_t terms_count_prediction)
{
size_t i;
i = 0;
while (i < terms_count_prediction)
{
terms[i].coefficient = 0.0;
terms[i].exponent = 0;
terms[i].position = TERM_END;
terms[i].sign = TERM_NULL;
i++;
}
}
static int get_max_exponent(s_term *terms)
{
int i;
int max_exponent;
i = 0;
max_exponent = 0;
while (terms[i].position != TERM_END)
{
if (terms[i].exponent > max_exponent)
{
max_exponent = terms[i].exponent;
}
i++;
}
return max_exponent;
}
static void polynom_fill_null(double *polynom, int len)
{
int i;
i = 0;
while (i < len)
{
polynom[i] = 0.0;
i++;
}
}
void launch_computorv1(char *input)
{
int ret;
int max_exponent;
size_t arg_len;
size_t terms_count_prediction;
// init
input_g_err = input;
remove_spaces(input);
// lexerize
arg_len = ft_strlen(input) + 1; // +1 for last END token
// ft_printf("-> tokens[%i]\n", arg_len); // debug
s_token tokens[arg_len];
tokens_g_err = tokens;
tokens_fill_null(tokens, arg_len);
ret = lexerize(input, tokens);
if (ret == 0)
{
stop_errors("lexer returned 0 token");
}
// parse
terms_count_prediction = count_any_of(input, "-+=") + 2; // +1 for first term that can have no leading '+', +1 for last term == NULL
// ft_printf("-> terms[%i]\n", terms_count_prediction); // debug
s_term terms[terms_count_prediction];
terms_g_err = terms;
terms_fill_null(terms, terms_count_prediction);
ret = parse(tokens, terms, terms_count_prediction);
if (ret == 0)
{
stop_errors("parser returned 0 term");
}
// reduce
max_exponent = get_max_exponent(terms) + 1;
double polynom[max_exponent];
polynom_g_err = polynom;
polynom_len_g_err = max_exponent;
polynom_fill_null(polynom, max_exponent);
reduce(terms, polynom);
// debug
print_state();
}