lexer types equal and factor
This commit is contained in:
@@ -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");
|
||||
|
||||
|
||||
57
src/lexer.c
57
src/lexer.c
@@ -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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user