-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi_token.c
More file actions
138 lines (114 loc) · 3.61 KB
/
Copy pathpi_token.c
File metadata and controls
138 lines (114 loc) · 3.61 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "pi_token.h"
token_t create_token(tk_type type, char *start, int length, int line, int column)
{
token_t token;
// Allocate memory for the token's lexeme and copy it
token.start = (char *)malloc(length + 1);
if (!token.start)
{
perror("Failed to allocate memory for token lexeme");
exit(EXIT_FAILURE);
}
memcpy(token.start, start, length);
token.start[length] = '\0'; // Ensure null termination
// Set the other token properties
token.type = type;
token.length = length;
token.line = line;
token.column = column;
token.is_negative = false;
token.skip = false;
return token;
}
tk_type token_type(token_t token)
{
return token.type;
}
// Function to get the value of the token as a string
char *token_value(token_t token)
{
int length = token.length;
char *start = token.start;
// Check if the token is negative and adjust memory allocation
int _length = token.is_negative ? 1 : 0; // Add 1 for '-' if negative
char *new_str = malloc(length + _length + 1); // +1 for null terminator
if (new_str)
{
// If negative, add the minus sign to the beginning
if (token.is_negative)
{
new_str[0] = '-';
strncpy(new_str + 1, start, length); // Copy the original token after '-'
}
else
strncpy(new_str, start, length); // Copy the original token
new_str[length + _length] = '\0'; // Set null terminator
}
return new_str;
}
int token_line(token_t token)
{
return token.line;
}
int token_column(token_t token)
{
return token.column;
}
// Function to convert token to string (simple implementation)
const char *token_toString(token_t token)
{
// Allocate enough space to hold the type, token value, and additional characters
// Adjust the buffer size as needed to fit your largest expected token value
int buffer_size = 256;
char *result = (char *)malloc(buffer_size);
if (token.length == 0)
snprintf(result, buffer_size, "<%s>", token_names[token.type]);
else
snprintf(result, buffer_size, "<%s, %.*s>", token_names[token.type], token.length, token.start);
return result;
}
tk_type find_kw(const char *name)
{
for (int i = 0; i < KW_NUM; ++i)
if (strcmp(keywords[i].name, name) == 0)
return keywords[i].type;
return TK_INVALID;
}
double tk_double(const token_t token)
{
// Numeric separators are syntax only; omit them before conversion.
char *buffer = malloc(token.length + 1);
if (!buffer)
{
perror("Failed to allocate numeric token buffer");
exit(EXIT_FAILURE);
}
int length = 0;
for (int i = 0; i < token.length; i++)
if (token.start[i] != '_')
buffer[length++] = token.start[i];
buffer[length] = '\0';
// Convert the string to a double.
double result = strtod(buffer, NULL);
// Clean up the temporary buffer.
free(buffer);
return token.is_negative ? -result : result;
}
char *tk_string(const token_t token)
{
// char *buffer = (char *)malloc(token.length + 1);
// snprintf(buffer, token.length + 1, "%.*s", token.length, token.start);
// return buffer; // The caller is responsible for freeing this memory
return token_value(token);
}
bool tk_bool(const token_t token)
{
char *str = token_value(token);
if (strcmp(str, "true") == 0)
return true;
else
return false;
}