-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.c
143 lines (114 loc) · 4.61 KB
/
lexer.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lexer.h"
char *lexer_getalphanum(buffer_t *buffer) {
char *result = malloc(LEXEM_SIZE + 1); // +1 for null character
if (!result) {
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE); // Exiting here might be harsh; consider another error handling strategy.
}
int i = 0;
buf_skipblank(buffer);
bool lockAcquired = false;
if (!buffer->islocked) {
buf_lock(buffer); // Lock the buffer if not already locked
lockAcquired = true;
}
char c = buf_getchar(buffer);
while (isalnum(c) && i < LEXEM_SIZE) {
result[i++] = c;
c = buf_getchar(buffer);
}
result[i] = '\0'; // Ensure string is null-terminated
if (c != '\0' && lockAcquired) {
buf_rollback_and_unlock(buffer, 1); // Rollback and unlock if we locked it here
} else if (lockAcquired) {
buf_unlock(buffer); // Ensure to unlock if we locked it and didn't rollback
}
if (i == 0) { // No alphanum sequence found, free memory and return NULL
free(result);
return NULL;
}
return result; // Return the found sequence
}
/*char *lexer_getalphanum_rollback(buffer_t *buffer) {
//size_t initialPosition = buffer->it; // Sauvegarde de la position initiale du curseur
buf_lock(buffer); // Verrouillage du buffer pour empêcher les modifications
// Appel de lexer_getalphanum pour obtenir une séquence alphanumérique, si disponible
char *result = lexer_getalphanum(buffer);
// Remet le curseur à sa position initiale, indépendamment du résultat de lexer_getalphanum
//buffer->it = initialPosition;
buf_rollback_and_unlock(buffer, buffer->it); // Remet le curseur à sa position initiale et déverrouille le buffer
return result; // Retourne le résultat obtenu par lexer_getalphanum
}*/
char *lexer_getalphanum_rollback(buffer_t *buffer) {
buf_lock(buffer); // Lock the buffer to prevent modifications
// Call lexer_getalphanum to get an alphanumeric sequence, if available
char *result = lexer_getalphanum(buffer);
if (result == NULL) {
buf_unlock(buffer); // Unlock the buffer if no sequence is found
return NULL; // Return NULL if no alphanumeric sequence is found
}
// Rollback the buffer to its initial position and unlock it
buf_rollback_and_unlock(buffer, buffer->it);
return result; // Return the found sequence
}
// Function to get the next operator
char *lexer_getop(buffer_t *buffer) {
bool lockAcquired = false;
if (!buffer->islocked) {
buf_lock(buffer); // Lock the buffer if not already locked
lockAcquired = true;
}
// This is a placeholder implementation
char *op = malloc(3);
if (!op) {
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
char c = buf_getchar_after_blank(buffer);
if (c == '+' || c == '-' || c == '*' || c == '/' || c == '!' || c == '=' || c == '<' || c == '>' || c == '&' || c == '|' || c == '^' || c == '~' || c == '%' || c == '?' || c == ':') {
op[0] = c;
if (c == '=' || c == '&' || c == '|' || c == '<' || c == '>' || c == '+' || c == '-' || c == '!') {
char next = buf_getchar(buffer);
if (next == '=' || next == '&' || next == '|' || next == '<' || next == '>' || next == '+' || next == '-' ) {
op[1] = next;
op[2] = '\0';
} else {
op[1] = '\0';
}
} else {
op[1] = '\0';}
if (lockAcquired) {
buf_unlock(buffer); // Rollback and unlock if we locked it here
}
return op;
} else {
free(op);
if (lockAcquired) {
buf_rollback_and_unlock(buffer, 1); // Rollback and unlock if we locked it here
}
return NULL; // No operator found
}
}
long lexer_getnumber(buffer_t *buffer) {
// Call lexer_getalphanum to get the alphanumeric sequence
char *alphanumStr = lexer_getalphanum(buffer);
// Check if the sequence is NULL
if (alphanumStr == NULL) {
return -1; // Indicates no alphanum sequence was found
}
// Check if the alphanumStr consists only of digits
for (int i = 0; alphanumStr[i] != '\0'; i++) {
if (!isdigit(alphanumStr[i])) {
free(alphanumStr); // Free the allocated memory
return -1; // Indicates that the sequence is not a valid number
}
}
// Convert string to long
long number = strtol(alphanumStr, NULL, 10);
free(alphanumStr); // Free the allocated memory
return number;
}