split files
This commit is contained in:
12
Makefile
12
Makefile
@@ -33,10 +33,14 @@ RESET = "\e[0m"
|
|||||||
# FILES :
|
# FILES :
|
||||||
NAME = computorv1
|
NAME = computorv1
|
||||||
D_LIB = ./libft
|
D_LIB = ./libft
|
||||||
D_SRCS = ./src
|
|
||||||
D_HEADERS = ./headers
|
D_HEADERS = ./headers
|
||||||
SRCS = computorv1.c
|
HEADERS = computorv1.h \
|
||||||
HEADERS = computorv1.h
|
errors.h \
|
||||||
|
lexer.h
|
||||||
|
D_SRCS = ./src
|
||||||
|
SRCS = computorv1.c \
|
||||||
|
errors.c \
|
||||||
|
lexer.c
|
||||||
|
|
||||||
# COMPILATION CONFIG :
|
# COMPILATION CONFIG :
|
||||||
CC = gcc
|
CC = gcc
|
||||||
@@ -49,7 +53,7 @@ D_OBJS = builds
|
|||||||
OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o)
|
OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o)
|
||||||
VPATH = $(D_SRCS)
|
VPATH = $(D_SRCS)
|
||||||
F_INCLUDES = $(HEADERS:%=$(D_HEADERS)/%)
|
F_INCLUDES = $(HEADERS:%=$(D_HEADERS)/%)
|
||||||
INCLUDES = -I$(D_HEADERS)
|
INCLUDES = -I$(D_HEADERS) -I$(D_LIB)/includes
|
||||||
ifeq "$(D_OBJS)" "."
|
ifeq "$(D_OBJS)" "."
|
||||||
RM_OBJS = rm -f $(OBJS)
|
RM_OBJS = rm -f $(OBJS)
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -1,37 +1,6 @@
|
|||||||
#ifndef COMPUTORV1_H
|
#ifndef COMPUTORV1_H
|
||||||
#define COMPUTORV1_H
|
#define COMPUTORV1_H
|
||||||
|
|
||||||
#include "../libft/includes/libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
typedef enum
|
|
||||||
{
|
|
||||||
ERROR_BASIC, // 0
|
|
||||||
ERROR_UNKNOWN_TOKEN, // 1
|
|
||||||
} program_error;
|
|
||||||
|
|
||||||
typedef enum
|
|
||||||
{
|
|
||||||
TOKEN_PLUS, // +
|
|
||||||
TOKEN_MINUS, // -
|
|
||||||
TOKEN_VARIABLE, // x, y, etc.
|
|
||||||
TOKEN_NUMBER, // int or double
|
|
||||||
TOKEN_POWER, // ^ or **
|
|
||||||
TOKEN_MULTIPLICATION, // *
|
|
||||||
TOKEN_DIVISION, // /
|
|
||||||
TOKEN_END // null (end of input)
|
|
||||||
} TokenType;
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
TokenType type;
|
|
||||||
union
|
|
||||||
{
|
|
||||||
double num_value; // For NUMBER
|
|
||||||
char var_value; // For VARIABLE (single char, e.g., 'x')
|
|
||||||
};
|
|
||||||
} t_token;
|
|
||||||
|
|
||||||
#define MAX_TOKENS 100
|
|
||||||
t_token tokens[MAX_TOKENS];
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
14
headers/errors.h
Normal file
14
headers/errors.h
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef ERRORS_H
|
||||||
|
#define ERRORS_H
|
||||||
|
|
||||||
|
#include "../libft/includes/libft.h"
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
ERROR_BASIC = 0,
|
||||||
|
ERROR_UNKNOWN_TOKEN = -1,
|
||||||
|
} program_error;
|
||||||
|
|
||||||
|
int stop_errors(int err);
|
||||||
|
|
||||||
|
#endif
|
||||||
31
headers/lexer.h
Normal file
31
headers/lexer.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#ifndef LEXER_H
|
||||||
|
#define LEXER_H
|
||||||
|
|
||||||
|
#include "../libft/includes/libft.h"
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
TOKEN_PLUS, // +
|
||||||
|
TOKEN_MINUS, // -
|
||||||
|
TOKEN_VARIABLE, // x, y, etc.
|
||||||
|
TOKEN_NUMBER, // int or double
|
||||||
|
TOKEN_POWER, // ^ or **
|
||||||
|
TOKEN_MULTIPLICATION, // *
|
||||||
|
TOKEN_DIVISION, // /
|
||||||
|
TOKEN_END // null (end of input)
|
||||||
|
} token_type;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
token_type type;
|
||||||
|
union
|
||||||
|
{
|
||||||
|
double num_value; // For NUMBER
|
||||||
|
char var_value; // For VARIABLE (single char, e.g., 'x')
|
||||||
|
};
|
||||||
|
} token;
|
||||||
|
|
||||||
|
#define MAX_TOKENS 100
|
||||||
|
int lexerize(const char *input, token tokens[MAX_TOKENS]);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,71 +1,6 @@
|
|||||||
|
|
||||||
#include "computorv1.h"
|
#include "computorv1.h"
|
||||||
|
#include "lexer.h"
|
||||||
int stop_errors(int err)
|
#include "errors.h"
|
||||||
{
|
|
||||||
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 main(int ac, char **av)
|
||||||
{
|
{
|
||||||
@@ -86,7 +21,8 @@ int main(int ac, char **av)
|
|||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = lexerize(av[1]);
|
token tokens[MAX_TOKENS];
|
||||||
|
ret = lexerize(av[1], tokens);
|
||||||
if (ret <= 0)
|
if (ret <= 0)
|
||||||
{
|
{
|
||||||
stop_errors(ret);
|
stop_errors(ret);
|
||||||
|
|||||||
17
src/errors.c
Normal file
17
src/errors.c
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#include "errors.h"
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
52
src/lexer.c
Normal file
52
src/lexer.c
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#include "lexer.h"
|
||||||
|
#include "errors.h"
|
||||||
|
|
||||||
|
static int skip_whitespace(const char *input, int input_pos)
|
||||||
|
{
|
||||||
|
while (ft_isspace(input[input_pos]))
|
||||||
|
{
|
||||||
|
input_pos++;
|
||||||
|
}
|
||||||
|
return input_pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int token_is_plus(const char *input, int input_pos)
|
||||||
|
{
|
||||||
|
return (input[input_pos] == '+');
|
||||||
|
}
|
||||||
|
|
||||||
|
int lexerize(const char *input, token tokens[MAX_TOKENS])
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
tokens[token_count].type = TOKEN_END;
|
||||||
|
tokens[token_count].var_value = '\0';
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user