This repository has been archived by the owner on Oct 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arith_expr_test_2.c
94 lines (83 loc) · 2.39 KB
/
arith_expr_test_2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* arith_expr_test_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mgautier <mgautier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/01/22 13:15:05 by mgautier #+# #+# */
/* Updated: 2018/01/22 13:15:05 by mgautier ### ########.fr */
/* */
/* ************************************************************************** */
#include "test_interface.h"
#include <stdlib.h>
t_bool give_factor(void *v_factor, void *to_give)
{
int *factor;
int *expr_or_integer;
factor = v_factor;
expr_or_integer = to_give;
*factor = *expr_or_integer;
free(expr_or_integer);
return (TRUE);
}
void *create_integer(void const *v_integer)
{
int *integer;
t_token const *integer_tok;
integer_tok = v_integer;
integer = malloc(sizeof(*integer));
if (integer != NULL)
*integer = ft_atoi(integer_tok->start);
return (integer);
}
void destroy_token(t_token **token)
{
t_token *to_destroy;
to_destroy = *token;
if (to_destroy != NULL)
{
to_destroy->type = 0;
to_destroy->start = NULL;
free(to_destroy);
*token = NULL;
}
}
void *get_token(void *input)
{
char **str;
t_token *new_token;
str = input;
new_token = malloc(sizeof(*new_token));
if (new_token != NULL)
{
while (**str == ' ')
(*str)++;
new_token->start = *str;
while (ft_isdigit(**str))
(*str)++;
if (*str != new_token->start)
new_token->type = INTEGER;
else if ((**str) == '(')
new_token->type = LEFT_PAR;
else if ((**str) == ')')
new_token->type = RIGHT_PAR;
else if ((**str) == '+')
new_token->type = PLUS;
else if ((**str) == '*')
new_token->type = MULT;
else if ((**str) == '\0')
new_token->type = END_OF_INPUT;
else
destroy_token(&new_token);
if (new_token != NULL && new_token->type != INTEGER)
(*str)++;
}
return (new_token);
}
size_t get_token_index(void const *v_token)
{
t_token const *token;
token = v_token;
return (token->type);
}