-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
76 lines (66 loc) · 1.5 KB
/
utils.cpp
File metadata and controls
76 lines (66 loc) · 1.5 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
#include "utils.h"
#include "parse/symbols.h"
/**
* Read at most "num" bytes of code from file "filename", to char buffer "code".
*
* @param filename The file name to read from.
* @param code The char buffer.
* @param num The maximal number of bytes read.
*/
void read_file(const char* filename, char* code, std::size_t num)
{
FILE* file = fopen(filename, "r");
if (file == nullptr)
{
fprintf(stderr, "Error reading file!\n");
exit(4);
}
for (std::size_t i = 0; i < num; i++)
{
code[i] = fgetc(file);
if (code[i] == EOF)
{
code[i] = 0;
break;
}
}
fclose(file);
}
std::string array_type_to_str(const std::vector<std::size_t>& sizes)
{
std::string base = "int";
for (auto& size: sizes)
{
base += ("[" + std::to_string(size) + "]");
}
return base;
}
void print_stack(const std::stack<int>& stack)
{
std::stack<int> stack_copy(stack);
while (!stack_copy.empty())
{
printf("%d ", stack_copy.top());
stack_copy.pop();
}
printf("\n");
}
void print_symbol_stack(const std::stack<void*>& stack)
{
printf("\n");
std::stack<void*> stack_copy(stack);
while (!stack_copy.empty())
{
printf("%s\n", ((Symbol*)stack_copy.top())->to_str().c_str());
stack_copy.pop();
}
printf("\n");
}
void print_set(const std::set<std::size_t>& set)
{
for (auto& elem: set)
{
printf("%lu ", elem);
}
printf("\n");
}