lexer types equal and factor

This commit is contained in:
hugogogo
2026-04-30 22:48:51 +02:00
parent 26fa8025ef
commit 7a30dcc345
5 changed files with 37 additions and 58 deletions

View File

@@ -95,7 +95,7 @@ $(NAME): $(OBJS)
run: $(NAME)
@echo $(YELLOW)"run"$(RESET)
@./$(NAME) "3 * x^2 + 1 * x^1 - 2 * x^0"
@./$(NAME) "3 * x^2 + 1 * x^1 - 2 * x^0 = 5 * x^1"
clean:
$(RM_OBJS)

View File

@@ -1,6 +1,4 @@
#ifndef COMPUTORV1_H
#define COMPUTORV1_H
#include "libft.h"
#endif

View File

@@ -1,19 +1,16 @@
#ifndef LEXER_H
#define LEXER_H
#include "../libft/includes/libft.h"
typedef enum
{
TOKEN_VARIABLE, // x, y, etc.
TOKEN_NUMBER_INT, // int
TOKEN_NUMBER_DOUBLE, // double
TOKEN_POWER, // ^ or **
TOKEN_PLUS, // +
TOKEN_MINUS, // -
TOKEN_MULTIPLICATION, // *
TOKEN_DIVISION, // /
TOKEN_END // null (end of input)
TOKEN_VARIABLE, // x, y, etc.
TOKEN_NUMBER_INT, // int
TOKEN_NUMBER_DOUBLE, // double
TOKEN_POWER, // ^ or **
TOKEN_SIGN, // + or -
TOKEN_FACTOR, // * or /
TOKEN_EQUAL, // =
TOKEN_END // null (end of input)
} token_type;
typedef struct

View File

@@ -1,4 +1,5 @@
#include "computorv1.h"
#include "libft.h"
#include "lexer.h"
#include "errors.h"
#include <stdio.h> // tmp for float debug
@@ -79,14 +80,12 @@ int main(int ac, char **av)
ft_printf("%20s", "TOKEN_NUMBER_DOUBLE");
if (tokens[i].type == TOKEN_POWER)
ft_printf("%20s", "TOKEN_POWER");
if (tokens[i].type == TOKEN_PLUS)
ft_printf("%20s", "TOKEN_PLUS");
if (tokens[i].type == TOKEN_MINUS)
ft_printf("%20s", "TOKEN_MINUS");
if (tokens[i].type == TOKEN_MULTIPLICATION)
ft_printf("%20s", "TOKEN_MULTIPLICATION");
if (tokens[i].type == TOKEN_DIVISION)
ft_printf("%20s", "TOKEN_DIVISION");
if (tokens[i].type == TOKEN_SIGN)
ft_printf("%20s", "TOKEN_SIGN");
if (tokens[i].type == TOKEN_FACTOR)
ft_printf("%20s", "TOKEN_FACTOR");
if (tokens[i].type == TOKEN_EQUAL)
ft_printf("%20s", "TOKEN_EQUAL");
if (tokens[i].type == TOKEN_END)
ft_printf("%20s", "TOKEN_END");

View File

@@ -1,8 +1,9 @@
#include "lexer.h"
#include "libft.h"
#include "errors.h"
#include <stdbool.h>
// any single letter is a valid variable, like "x" or "y"
// any single letter is a valid variable, like 'x' or 'y'
static bool token_is_variable(const char *input, int input_pos, int *token_size)
{
if (ft_isalpha(input[input_pos]))
@@ -100,7 +101,7 @@ static bool token_is_number_double(const char *input, int input_pos, int *token_
return true;
}
// power can be "^" and "**"
// power can be '^' and "**"
static bool token_is_power(const char *input, int input_pos, int *token_size)
{
if (input[input_pos] == '^')
@@ -116,10 +117,10 @@ static bool token_is_power(const char *input, int input_pos, int *token_size)
return false;
}
// detect a single "+" is valid
static bool token_is_plus(const char *input, int input_pos, int *token_size)
// sign can be '+' or '-'
static bool token_is_sign(const char *input, int input_pos, int *token_size)
{
if (input[input_pos] == '+')
if (input[input_pos] == '+' || input[input_pos] == '-')
{
*token_size = 1;
return true;
@@ -127,10 +128,10 @@ static bool token_is_plus(const char *input, int input_pos, int *token_size)
return false;
}
// detect a single "-"
static bool token_is_minus(const char *input, int input_pos, int *token_size)
// factor can be '*' or '/' or ':'
static bool token_is_factor(const char *input, int input_pos, int *token_size)
{
if (input[input_pos] == '-')
if (input[input_pos] == '*' || input[input_pos] == '/' || input[input_pos] == ':')
{
*token_size = 1;
return true;
@@ -138,21 +139,10 @@ static bool token_is_minus(const char *input, int input_pos, int *token_size)
return false;
}
// detect a single "*"
static bool token_is_multiplication(const char *input, int input_pos, int *token_size)
// detect a single '='
static bool token_is_equal(const char *input, int input_pos, int *token_size)
{
if (input[input_pos] == '*')
{
*token_size = 1;
return true;
}
return false;
}
// detect a single "/"
static bool token_is_division(const char *input, int input_pos, int *token_size)
{
if (input[input_pos] == '+')
if (input[input_pos] == '=')
{
*token_size = 1;
return true;
@@ -200,25 +190,20 @@ void lexerize(const char *input, token *tokens)
tokens[token_count].type = TOKEN_POWER;
tokens[token_count].value_char = '^';
}
else if (token_is_plus(input, input_pos, &token_size))
else if (token_is_sign(input, input_pos, &token_size))
{
tokens[token_count].type = TOKEN_PLUS;
tokens[token_count].value_char = '+';
tokens[token_count].type = TOKEN_SIGN;
tokens[token_count].value_char = input[input_pos];
}
else if (token_is_minus(input, input_pos, &token_size))
else if (token_is_factor(input, input_pos, &token_size))
{
tokens[token_count].type = TOKEN_MINUS;
tokens[token_count].value_char = '-';
tokens[token_count].type = TOKEN_FACTOR;
tokens[token_count].value_char = input[input_pos];
}
else if (token_is_multiplication(input, input_pos, &token_size))
else if (token_is_equal(input, input_pos, &token_size))
{
tokens[token_count].type = TOKEN_MULTIPLICATION;
tokens[token_count].value_char = '*';
}
else if (token_is_division(input, input_pos, &token_size))
{
tokens[token_count].type = TOKEN_DIVISION;
tokens[token_count].value_char = '/';
tokens[token_count].type = TOKEN_EQUAL;
tokens[token_count].value_char = '=';
}
else
{