This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
string_stack.hpp
189 lines (138 loc) · 3.97 KB
/
string_stack.hpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#ifndef STRING_STACK_H
#define STRING_STACK_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
/// NOTE: String-Stack = Block = Executable array = Macro
/// there were a variety of names for this type before I decided on calling them macros
class StrStack {
public:
// how many times to double the number of lines
uint8_t sizeFactor;
// points to element above top of stack
char** buffer;
// number of elements in the stack
size_t stackDepth;
// the first elements in the stack
char** stackHead;
StrStack():
sizeFactor(0),
buffer((char**) calloc(256, sizeof(char*))),
stackDepth(0),
stackHead(buffer)
{
*buffer = NULL;
}
// copy constructor
StrStack(const StrStack& cpy):
sizeFactor(cpy.sizeFactor),
buffer((char**) calloc((1 << cpy.sizeFactor) * 256, sizeof(char*))),
stackDepth(cpy.stackDepth),
stackHead(buffer)
{
*buffer = NULL;
char** sh = cpy.stackHead;
while (sh != cpy.buffer) {
*buffer = (char*) calloc ( strlen(*sh) + 1, sizeof(char) );
strcpy(*buffer++, *sh++);
}
}
~StrStack(){
for (; stackDepth > 0; stackDepth--)
free(*(--buffer));
free(stackHead);
}
// copy
StrStack& operator=(const StrStack& cpy){
sizeFactor = cpy.sizeFactor;
buffer = (char**) malloc((1 << cpy.sizeFactor) * 256 * sizeof(char*));
stackDepth = cpy.stackDepth;
stackHead = buffer;
char** sh = cpy.stackHead;
while (sh != cpy.buffer) {
*buffer = (char*) malloc ( strlen(*sh) + 1 );
strcpy(*buffer++, *sh++);
}
return *this;
}
// 0 = bottom of stack
char*& at(const ssize_t index) {
if ((size_t)abs(index) >= stackDepth)
throw "StrStack[] index out of bounds";
return index >= 0 ? *(stackHead + index) : *(buffer + index - 1);
}
char*& operator[](const ssize_t index) {
return at(index);
}
void setAt(const ssize_t index, char* c) {
if ((size_t)abs(index) >= stackDepth)
throw "StrStack[] index out of bounds";
(index >= 0 ? *(stackHead + index) : *(buffer + index - 1)) = c;
}
void edit(const ssize_t index, const char* value) {
char*& ref = at(index);
ref = (char*) realloc(ref, strlen(value) + 1);
strcpy(ref, value);
setAt(index, ref);
//std::cout <<"ref='" <<ref <<"'\nline='" <<at(index) <<"'\n";
}
// deletes all strings
void clear();
/// doubles the size of the buffer
void grow();
/// pushes a line to the top of the stack
void push(const char* str);
/// deletes the string at the top of the stack.
void pop();
/// the string at the top of the stack
char* top()
{ return stackDepth ? *(buffer - 1) : NULL; }
/// the number of strings being stored
size_t size()
{ return stackDepth; }
// modify the top element
void changeTop(const char* str);
void top(const char* str)
{ return changeTop(str);}
/// the number of characters in all the strings stored in the stack
size_t totalLength(){
char** buff = buffer;
// start with one to count the '\0' char at the end
size_t ret = 1;
while (buff-- > stackHead)
ret += strlen(*buff) + 2;
return ret;
}
// converts the block to a string
void toString(char** dest, size_t* size);
// prints the contents of a string stack
static inline void printStrStack(const StrStack& stack){
// start from top element (this one is empty)
char** buff = stack.buffer;
unsigned num = 0;
// skip the first element (empty) and print the rest of the stack
while (buff-- > stack.stackHead)
printf("%d: %s\n", num++, *buff);
}
// concatenates 2 stacks together
static inline void appendToStack(StrStack& out, const StrStack& in){
// bottom of stack1 -> top of stack1 -> bottom of stack2 -> top of stack2
char** head = in.stackHead;
out.push(*head);
while (head++ < in.buffer)
out.push(*head);
}
};
// associated stuffz
namespace macro {
// gets a string stack from a file
StrStack* getMacro(char*& str, FILE* codeFeed, char*& codeLine);
typedef enum {
MACRO, LAMBDA, OTHER
} exec_t;
typedef enum {
SUCCESS, RETURN, ERROR, BREAK
} ret_t;
}
#endif