-
Notifications
You must be signed in to change notification settings - Fork 2
/
stack.bro
169 lines (144 loc) · 3.87 KB
/
stack.bro
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
##! A LIFO stack.
module Stack;
export {
## Settings for initializing the stack.
type Settings: record {
## If a maximum length is set for the stack
## it will maintain itself at that
## maximum length automatically.
max_len: count &optional;
};
## The internal data structure for the stack.
type Stack: record {};
## Initialize a stack record structure.
##
## s: A :bro:record:`Settings` record configuring the stack.
##
## Returns: An opaque stack record.
global init: function(st: Settings): Stack;
## Push a string to the top of a stack.
##
## s: The stack to push the string into.
##
## val: The string to push
global push: function(s: Stack, val: any);
## Pop a string from the top of a stack.
##
## s: The stack to pop the string from.
##
## Returns: The string popped from the stack.
global pop: function(s: Stack): any;
## :Not Implemented:
## Merge two stack's together. If any settings are applied
## to the stacks, the settings from q1 are used for the new
## merged stack.
##
## s1: The first stack. Settings are taken from here.
##
## s2: The second stack.
##
## Returns: A new stack from merging the other two together.
#global merge: function(s1: Stack, s2: Stack): stack;
## Get the number of items in a stack.
##
## s: The stack.
##
## Returns: The length of the stack.
global len: function(s: Stack): count;
## Get the top element of a stack.
##
## s: The stack.
##
## Returns: Top element of the stack.
global peek: function(s: Stack): count;
## Copy the contents of the stack to a vector provided in
## function arguments starting at :idx:.
##
## s: The stack.
##
## v: A vector supplied by user in function arguments. The stack
## contents will be added to it
global get_stack_as_vector: function(s: Stack, v: vector of any, idx: int);
global get_stack_as_vector_till_op: function(s: Stack, v: vector of any, idx: int);
global get_stack_as_vector_till_lparanthesis: function(s: Stack, v: vector of any, idx: int);
## :Not Implemented:
## Get the contents of the stack as a count vector. Use care
## with this function. If the data put into the stack wasn't
## integers you will get conversion errors.
##
## s: The stack.
##
## Returns: A :bro:type:`vector of count` containing the
## current contents of s.
global get_cnt_vector: function(s: Stack): vector of count;
}
redef record Stack += {
# Indicator for if the stack was appropriately initialized.
initialized: bool &default=F;
# The values are stored here.
vals: table[count] of any &optional;
# Settings for the stack.
settings: Settings &optional;
# The top value in the vals table.
top: count &default=0;
# The bottom value in the vals table.
bottom: count &default=0;
# The number of bytes in the stack.
size: count &default=0;
};
function init(st: Settings): Stack
{
local s: Stack;
s$vals=table();
s$settings = copy(st);
s$initialized=T;
return s;
}
function push(s: Stack, val: any)
{
if ( s$settings?$max_len && len(s) >= s$settings$max_len )
pop(s);
s$vals[s$top] = val;
++s$top;
#print s$vals;
}
function pop(s: Stack): any
{
if ( |s$vals| == 0 )
{
#print "Stack Error: Cannot pop an empty stack.";
# dummy stmt to silence the above error
local a = T;
}
else
{
if ( s$top > s$bottom )
--s$top;
local ret = s$vals[s$top];
delete s$vals[s$top];
return ret;
}
return F;
}
function len(s: Stack): count
{
return |s$vals|;
}
function peek(s: Stack): any
{
if ( |s$vals| == 0 )
return F;
return s$vals[(s$top)-1];
}
function get_stack_as_vector( s: Stack, v: vector of any, idx: int )
{
local ret = Stack::pop(s);
if (type_name(ret)=="bool" )
return;
if (idx >= 0)
{
v[idx] = ret;
++idx;
get_stack_as_vector(s,v,idx);
}
}