lexer types equal and factor
This commit is contained in:
2
Makefile
2
Makefile
@@ -95,7 +95,7 @@ $(NAME): $(OBJS)
|
|||||||
|
|
||||||
run: $(NAME)
|
run: $(NAME)
|
||||||
@echo $(YELLOW)"run"$(RESET)
|
@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:
|
clean:
|
||||||
$(RM_OBJS)
|
$(RM_OBJS)
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
#ifndef COMPUTORV1_H
|
#ifndef COMPUTORV1_H
|
||||||
#define COMPUTORV1_H
|
#define COMPUTORV1_H
|
||||||
|
|
||||||
#include "libft.h"
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,18 +1,15 @@
|
|||||||
#ifndef LEXER_H
|
#ifndef LEXER_H
|
||||||
#define LEXER_H
|
#define LEXER_H
|
||||||
|
|
||||||
#include "../libft/includes/libft.h"
|
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
TOKEN_VARIABLE, // x, y, etc.
|
TOKEN_VARIABLE, // x, y, etc.
|
||||||
TOKEN_NUMBER_INT, // int
|
TOKEN_NUMBER_INT, // int
|
||||||
TOKEN_NUMBER_DOUBLE, // double
|
TOKEN_NUMBER_DOUBLE, // double
|
||||||
TOKEN_POWER, // ^ or **
|
TOKEN_POWER, // ^ or **
|
||||||
TOKEN_PLUS, // +
|
TOKEN_SIGN, // + or -
|
||||||
TOKEN_MINUS, // -
|
TOKEN_FACTOR, // * or /
|
||||||
TOKEN_MULTIPLICATION, // *
|
TOKEN_EQUAL, // =
|
||||||
TOKEN_DIVISION, // /
|
|
||||||
TOKEN_END // null (end of input)
|
TOKEN_END // null (end of input)
|
||||||
} token_type;
|
} token_type;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "computorv1.h"
|
#include "computorv1.h"
|
||||||
|
#include "libft.h"
|
||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
#include "errors.h"
|
#include "errors.h"
|
||||||
#include <stdio.h> // tmp for float debug
|
#include <stdio.h> // tmp for float debug
|
||||||
@@ -79,14 +80,12 @@ int main(int ac, char **av)
|
|||||||
ft_printf("%20s", "TOKEN_NUMBER_DOUBLE");
|
ft_printf("%20s", "TOKEN_NUMBER_DOUBLE");
|
||||||
if (tokens[i].type == TOKEN_POWER)
|
if (tokens[i].type == TOKEN_POWER)
|
||||||
ft_printf("%20s", "TOKEN_POWER");
|
ft_printf("%20s", "TOKEN_POWER");
|
||||||
if (tokens[i].type == TOKEN_PLUS)
|
if (tokens[i].type == TOKEN_SIGN)
|
||||||
ft_printf("%20s", "TOKEN_PLUS");
|
ft_printf("%20s", "TOKEN_SIGN");
|
||||||
if (tokens[i].type == TOKEN_MINUS)
|
if (tokens[i].type == TOKEN_FACTOR)
|
||||||
ft_printf("%20s", "TOKEN_MINUS");
|
ft_printf("%20s", "TOKEN_FACTOR");
|
||||||
if (tokens[i].type == TOKEN_MULTIPLICATION)
|
if (tokens[i].type == TOKEN_EQUAL)
|
||||||
ft_printf("%20s", "TOKEN_MULTIPLICATION");
|
ft_printf("%20s", "TOKEN_EQUAL");
|
||||||
if (tokens[i].type == TOKEN_DIVISION)
|
|
||||||
ft_printf("%20s", "TOKEN_DIVISION");
|
|
||||||
if (tokens[i].type == TOKEN_END)
|
if (tokens[i].type == TOKEN_END)
|
||||||
ft_printf("%20s", "TOKEN_END");
|
ft_printf("%20s", "TOKEN_END");
|
||||||
|
|
||||||
|
|||||||
57
src/lexer.c
57
src/lexer.c
@@ -1,8 +1,9 @@
|
|||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
|
#include "libft.h"
|
||||||
#include "errors.h"
|
#include "errors.h"
|
||||||
#include <stdbool.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)
|
static bool token_is_variable(const char *input, int input_pos, int *token_size)
|
||||||
{
|
{
|
||||||
if (ft_isalpha(input[input_pos]))
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// power can be "^" and "**"
|
// power can be '^' and "**"
|
||||||
static bool token_is_power(const char *input, int input_pos, int *token_size)
|
static bool token_is_power(const char *input, int input_pos, int *token_size)
|
||||||
{
|
{
|
||||||
if (input[input_pos] == '^')
|
if (input[input_pos] == '^')
|
||||||
@@ -116,10 +117,10 @@ static bool token_is_power(const char *input, int input_pos, int *token_size)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// detect a single "+" is valid
|
// sign can be '+' or '-'
|
||||||
static bool token_is_plus(const char *input, int input_pos, int *token_size)
|
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;
|
*token_size = 1;
|
||||||
return true;
|
return true;
|
||||||
@@ -127,10 +128,10 @@ static bool token_is_plus(const char *input, int input_pos, int *token_size)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// detect a single "-"
|
// factor can be '*' or '/' or ':'
|
||||||
static bool token_is_minus(const char *input, int input_pos, int *token_size)
|
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;
|
*token_size = 1;
|
||||||
return true;
|
return true;
|
||||||
@@ -138,21 +139,10 @@ static bool token_is_minus(const char *input, int input_pos, int *token_size)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// detect a single "*"
|
// detect a single '='
|
||||||
static bool token_is_multiplication(const char *input, int input_pos, int *token_size)
|
static bool token_is_equal(const char *input, int input_pos, int *token_size)
|
||||||
{
|
{
|
||||||
if (input[input_pos] == '*')
|
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] == '+')
|
|
||||||
{
|
{
|
||||||
*token_size = 1;
|
*token_size = 1;
|
||||||
return true;
|
return true;
|
||||||
@@ -200,25 +190,20 @@ void lexerize(const char *input, token *tokens)
|
|||||||
tokens[token_count].type = TOKEN_POWER;
|
tokens[token_count].type = TOKEN_POWER;
|
||||||
tokens[token_count].value_char = '^';
|
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].type = TOKEN_SIGN;
|
||||||
tokens[token_count].value_char = '+';
|
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].type = TOKEN_FACTOR;
|
||||||
tokens[token_count].value_char = '-';
|
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].type = TOKEN_EQUAL;
|
||||||
tokens[token_count].value_char = '*';
|
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 = '/';
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user