5
5
#define JIM_SCOPES_CAPACITY 128
6
6
#endif // JIM_SCOPES_CAPACITY
7
7
8
- #include <stddef.h>
8
+ #include <assert.h>
9
+ #include <stdlib.h>
9
10
10
11
typedef void * Jim_Sink ;
11
12
typedef size_t (* Jim_Write )(const void * ptr , size_t size , size_t nmemb , Jim_Sink sink );
12
13
13
14
typedef enum {
14
15
JIM_OK = 0 ,
15
16
JIM_WRITE_ERROR ,
16
- JIM_SCOPES_OVERFLOW ,
17
17
JIM_SCOPES_UNDERFLOW ,
18
18
JIM_OUT_OF_SCOPE_KEY ,
19
19
JIM_DOUBLE_KEY
@@ -36,8 +36,9 @@ typedef struct {
36
36
Jim_Sink sink ;
37
37
Jim_Write write ;
38
38
Jim_Error error ;
39
- Jim_Scope scopes [JIM_SCOPES_CAPACITY ];
40
- size_t scopes_size ;
39
+ Jim_Scope * scopes ;
40
+ size_t scopes_count ;
41
+ size_t scopes_capacity ;
41
42
} Jim ;
42
43
43
44
void jim_null (Jim * jim );
@@ -74,22 +75,24 @@ static size_t jim_strlen(const char *s)
74
75
static void jim_scope_push (Jim * jim , Jim_Scope_Kind kind )
75
76
{
76
77
if (jim -> error == JIM_OK ) {
77
- if (jim -> scopes_size < JIM_SCOPES_CAPACITY ) {
78
- jim -> scopes [jim -> scopes_size ].kind = kind ;
79
- jim -> scopes [jim -> scopes_size ].tail = 0 ;
80
- jim -> scopes [jim -> scopes_size ].key = 0 ;
81
- jim -> scopes_size += 1 ;
82
- } else {
83
- jim -> error = JIM_SCOPES_OVERFLOW ;
78
+ if (jim -> scopes_count >= jim -> scopes_capacity ) {
79
+ if (jim -> scopes_capacity == 0 ) jim -> scopes_capacity = JIM_SCOPES_CAPACITY ;
80
+ else jim -> scopes_capacity *= 2 ;
81
+ jim -> scopes = realloc (jim -> scopes , sizeof (* jim -> scopes )* jim -> scopes_capacity );
82
+ assert (jim -> scopes );
84
83
}
84
+ jim -> scopes [jim -> scopes_count ].kind = kind ;
85
+ jim -> scopes [jim -> scopes_count ].tail = 0 ;
86
+ jim -> scopes [jim -> scopes_count ].key = 0 ;
87
+ jim -> scopes_count += 1 ;
85
88
}
86
89
}
87
90
88
91
static void jim_scope_pop (Jim * jim )
89
92
{
90
93
if (jim -> error == JIM_OK ) {
91
- if (jim -> scopes_size > 0 ) {
92
- jim -> scopes_size -- ;
94
+ if (jim -> scopes_count > 0 ) {
95
+ jim -> scopes_count -- ;
93
96
} else {
94
97
jim -> error = JIM_SCOPES_UNDERFLOW ;
95
98
}
@@ -99,8 +102,8 @@ static void jim_scope_pop(Jim *jim)
99
102
static Jim_Scope * jim_current_scope (Jim * jim )
100
103
{
101
104
if (jim -> error == JIM_OK ) {
102
- if (jim -> scopes_size > 0 ) {
103
- return & jim -> scopes [jim -> scopes_size - 1 ];
105
+ if (jim -> scopes_count > 0 ) {
106
+ return & jim -> scopes [jim -> scopes_count - 1 ];
104
107
}
105
108
}
106
109
@@ -165,8 +168,6 @@ const char *jim_error_string(Jim_Error error)
165
168
return "There is no error. The developer of this software just had a case of \"Task failed successfully\" https://i.imgur.com/Bdb3rkq.jpg - Please contact the developer and tell them that they are very lazy for not checking errors properly." ;
166
169
case JIM_WRITE_ERROR :
167
170
return "Write error" ;
168
- case JIM_SCOPES_OVERFLOW :
169
- return "Stack of Scopes Overflow" ;
170
171
case JIM_SCOPES_UNDERFLOW :
171
172
return "Stack of Scopes Underflow" ;
172
173
case JIM_OUT_OF_SCOPE_KEY :
0 commit comments