init parser
This commit is contained in:
8
Makefile
8
Makefile
@@ -36,11 +36,13 @@ D_LIB = ./libft
|
|||||||
D_HEADERS = ./headers
|
D_HEADERS = ./headers
|
||||||
HEADERS = computorv1.h \
|
HEADERS = computorv1.h \
|
||||||
errors.h \
|
errors.h \
|
||||||
lexer.h
|
lexer.h \
|
||||||
|
parser.h
|
||||||
D_SRCS = ./src
|
D_SRCS = ./src
|
||||||
SRCS = computorv1.c \
|
SRCS = computorv1.c \
|
||||||
errors.c \
|
errors.c \
|
||||||
lexer.c
|
lexer.c \
|
||||||
|
parser.c
|
||||||
|
|
||||||
# COMPILATION CONFIG :
|
# COMPILATION CONFIG :
|
||||||
CC = gcc
|
CC = gcc
|
||||||
@@ -95,7 +97,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 = 5 * x^1"
|
@./$(NAME) "3.4 * x^2 + 1 * x^1 - 2 * x^0 = 5.123 * x^1"
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(RM_OBJS)
|
$(RM_OBJS)
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
#ifndef COMPUTORV1_H
|
#ifndef COMPUTORV1_H
|
||||||
#define COMPUTORV1_H
|
#define COMPUTORV1_H
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
#include "lexer.h"
|
||||||
|
#include "parser.h"
|
||||||
|
#include "errors.h"
|
||||||
|
#include <stdio.h> // tmp for printf, for float debug
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,6 +1,10 @@
|
|||||||
#ifndef LEXER_H
|
#ifndef LEXER_H
|
||||||
#define LEXER_H
|
#define LEXER_H
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
#include "errors.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
TOKEN_VARIABLE, // x, y, etc.
|
TOKEN_VARIABLE, // x, y, etc.
|
||||||
@@ -24,6 +28,6 @@ typedef struct
|
|||||||
};
|
};
|
||||||
} token;
|
} token;
|
||||||
|
|
||||||
void lexerize(const char *input, token *tokens);
|
int lexerize(const char *input, token *tokens);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
27
headers/parser.h
Normal file
27
headers/parser.h
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#ifndef PARSER_H
|
||||||
|
#define PARSER_H
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
#include "lexer.h"
|
||||||
|
#include "errors.h"
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
TERM_LEFT, // a in "a = b"
|
||||||
|
TERM_RIGHT, // b in "a = b"
|
||||||
|
} term_type;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
term_type type;
|
||||||
|
union
|
||||||
|
{
|
||||||
|
char value_char;
|
||||||
|
int value_int;
|
||||||
|
double value_double;
|
||||||
|
};
|
||||||
|
} term;
|
||||||
|
|
||||||
|
int parse(token *tokens, term *terms);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,8 +1,4 @@
|
|||||||
#include "computorv1.h"
|
#include "computorv1.h"
|
||||||
#include "libft.h"
|
|
||||||
#include "lexer.h"
|
|
||||||
#include "errors.h"
|
|
||||||
#include <stdio.h> // tmp for float debug
|
|
||||||
|
|
||||||
void remove_spaces(char *s)
|
void remove_spaces(char *s)
|
||||||
{
|
{
|
||||||
@@ -63,7 +59,11 @@ int main(int ac, char **av)
|
|||||||
ft_putchar('\n'); // debug
|
ft_putchar('\n'); // debug
|
||||||
|
|
||||||
token tokens[arg_len];
|
token tokens[arg_len];
|
||||||
lexerize(input, tokens);
|
int token_count = lexerize(input, tokens);
|
||||||
|
|
||||||
|
ft_putstr("-> token_count : "); // debug
|
||||||
|
ft_putnbr(token_count); // debug
|
||||||
|
ft_putchar('\n'); // debug
|
||||||
|
|
||||||
// tmp debug output
|
// tmp debug output
|
||||||
ft_putchar('\n'); // debug
|
ft_putchar('\n'); // debug
|
||||||
@@ -105,6 +105,15 @@ int main(int ac, char **av)
|
|||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
ft_putchar('\n'); // debug
|
||||||
|
// END tmp debug output
|
||||||
|
|
||||||
|
term terms[token_count / 2];
|
||||||
|
int term_count = parse(tokens, terms);
|
||||||
|
|
||||||
|
ft_putstr("-> term_count : "); // debug
|
||||||
|
ft_putnbr(term_count); // debug
|
||||||
|
ft_putchar('\n'); // debug
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
#include "lexer.h"
|
#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)
|
static bool token_is_variable(const char *input, int input_pos, int *token_size)
|
||||||
@@ -153,7 +150,7 @@ static bool token_is_equal(const char *input, int input_pos, int *token_size)
|
|||||||
/**
|
/**
|
||||||
* LEXER
|
* LEXER
|
||||||
*/
|
*/
|
||||||
void lexerize(const char *input, token *tokens)
|
int lexerize(const char *input, token *tokens)
|
||||||
{
|
{
|
||||||
int token_count;
|
int token_count;
|
||||||
int input_pos;
|
int input_pos;
|
||||||
@@ -222,4 +219,6 @@ void lexerize(const char *input, token *tokens)
|
|||||||
|
|
||||||
tokens[token_count].type = TOKEN_END;
|
tokens[token_count].type = TOKEN_END;
|
||||||
tokens[token_count].value_char = '\0';
|
tokens[token_count].value_char = '\0';
|
||||||
|
|
||||||
|
return token_count;
|
||||||
}
|
}
|
||||||
|
|||||||
10
src/parser.c
Normal file
10
src/parser.c
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#include "parser.h"
|
||||||
|
|
||||||
|
int parse(token *tokens, term *terms)
|
||||||
|
{
|
||||||
|
if (tokens)
|
||||||
|
{
|
||||||
|
terms[0].type = 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user