-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScopeStack.cpp
More file actions
125 lines (103 loc) · 3.47 KB
/
Copy pathScopeStack.cpp
File metadata and controls
125 lines (103 loc) · 3.47 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
///////////////////////////////////////////////////////////////////////////////
// ScopeStack.cpp - implements template stack holding specified element type //
// ver 2.2 //
// Language: Visual C++ 2010, SP1 //
// Platform: Dell Precision T7400, Win 7 Pro SP1 //
// Application: Code Analysis Research //
// Author: Jim Fawcett, CST 4-187, Syracuse University //
// (315) 443-3948, jfawcett@twcny.rr.com //
///////////////////////////////////////////////////////////////////////////////
//----< test stub >------------------------------------------------------
#ifdef TEST_SCOPESTACK
#include "ScopeStack.h"
#include "../Utilities/Utilities.h"
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
using namespace CodeAnalysis;
struct element
{
std::string type;
std::string name;
size_t startLineCount = 0;
size_t endLineCount = 0;
std::string path_;
std::string show()
{
std::ostringstream temp;
temp << "(";
temp << type;
temp << ", ";
temp << name;
temp << ", ";
temp << startLineCount;
temp << ", ";
temp << endLineCount;
temp << ")";
return temp.str();
}
std::vector<element*> children_;
};
int main()
{
ScopeStack<element*>::Rslt::attach(&std::cout);
ScopeStack<element*>::Rslt::start();
using Utils = Utilities::StringHelper;
Utils::Title("Testing Scope Stack");
Utils::title("pushing items onto ScopeStack instance after adding startLineCount");
ScopeStack<element*> testStack;
element* pItem = new element;
pItem->type = "function";
pItem->name = "fun1";
pItem->startLineCount = 33;
testStack.push(pItem);
pItem = new element;
pItem->type = "if";
pItem->name = "";
pItem->startLineCount = 38;
(testStack.top()->children_).push_back(pItem);
testStack.push(pItem);
pItem = new element;
pItem->type = "for";
pItem->name = "";
pItem->startLineCount = 40;
(testStack.top()->children_).push_back(pItem);
testStack.push(pItem);
showStack<element>(testStack, false);
std::cout << std::endl;
Utils::title("top and predecessor are:");
std::cout << "\n " << testStack.top()->show();
std::cout << "\n " << testStack.predOfTop()->show();
std::cout << "\n";
Utils::title("Popping two items off ScopeStack after adding endLineCount");
testStack.top()->endLineCount = 50;
std::cout << "\n " << testStack.pop()->show();
testStack.top()->endLineCount = 55;
std::cout << "\n " << testStack.pop()->show();
std::cout << "\n";
Utils::title("pushing another item onto ScopeStack after adding startLineElement");
pItem = new element;
pItem->type = "while";
pItem->name = "";
pItem->startLineCount = 60;
std::cout << "\n " << pItem->show();
(testStack.top()->children_).push_back(pItem);
testStack.push(pItem);
std::cout << "\n";
Utils::title("stack now contains:");
showStack<element>(testStack, false);
std::cout << "\n";
Utils::title("popping last elements off stack after adding endLineCount");
testStack.top()->endLineCount = 70;
element* pTop = testStack.pop();
std::cout << "\n " << pTop->show();
testStack.top()->endLineCount = 75;
pTop = testStack.pop();
std::cout << "\n " << pTop->show();
std::cout << "\n";
Utils::title("walking simulated AST tree:");
TreeWalk(pTop);
std::cout << "\n\n";
}
#endif