-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebug.cpp
More file actions
173 lines (167 loc) · 7.82 KB
/
Debug.cpp
File metadata and controls
173 lines (167 loc) · 7.82 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
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
#include "Debug.h"
#include "NobleCore/Debug.h"
#include <iomanip>
namespace Noble::Compiler
{
std::string Debug::TokenToString(const Token& token)
{
std::stringstream ss;
switch (token.type)
{
case Token::And: ss << "And"; break;
case Token::Bang: ss << "Bang"; break;
case Token::Class: ss << "Class"; break;
case Token::Comma: ss << "Comma"; break;
case Token::Dot: ss << "Dot"; break;
case Token::Else: ss << "Else"; break;
case Token::Equal: ss << "Equal"; break;
case Token::Error: ss << "Error"; break;
case Token::False: ss << "False"; break;
case Token::For: ss << "For"; break;
case Token::Function: ss << "Function"; break;
case Token::Greater: ss << "Greater"; break;
case Token::Identifier: ss << "Identifier"; break;
case Token::If: ss << "If"; break;
case Token::Less: ss << "Less"; break;
case Token::Minus: ss << "Minus"; break;
case Token::None: ss << "None"; break;
case Token::Null: ss << "Null"; break;
case Token::Number: ss << "Number"; break;
case Token::Or: ss << "Or"; break;
case Token::Plus: ss << "Plus"; break;
case Token::Print: ss << "Print"; break;
case Token::Return: ss << "Return"; break;
case Token::Semicolon: ss << "Semicolon"; break;
case Token::Slash: ss << "Slash"; break;
case Token::Star: ss << "Star"; break;
case Token::String: ss << "String"; break;
case Token::Super: ss << "Super"; break;
case Token::This: ss << "This"; break;
case Token::True: ss << "True"; break;
case Token::Variable: ss << "Variable"; break;
case Token::While: ss << "While"; break;
case Token::BangEqual: ss << "BangEqual"; break;
case Token::EqualEqual: ss << "EqualEqual"; break;
case Token::GreaterEqual: ss << "GreaterEqual"; break;
case Token::LeftBrace: ss << "LeftBrace"; break;
case Token::LeftParen: ss << "LeftParen"; break;
case Token::LessEqual: ss << "LessEqual"; break;
case Token::RightBrace: ss << "RightBrace"; break;
case Token::RightParen: ss << "RightParen"; break;
case Token::EndOfFile: ss << "EndOfFile"; break;
default: ss << "Unknown token '" << token.type << "'\n"; break;
}
return ss.str();
}
bool Debug::MakeDebugFile(const Frame &frame, const std::string& name)
{
std::ofstream debugFile(name + ".debug", std::ios::trunc | std::ios::binary);
if (!debugFile.is_open())
{
std::cerr << "Error: Unable to create debug file '" << name << ".debug'\n";
return false;
}
debugFile << "------------------- Debug Info -------------------\n";
debugFile << "Op Index | Op Name | Address | Literal Value\n";
debugFile << "--------------------------------------------------\n";
for (unsigned i = 0; i < frame.GetOps().Count(); ++i)
{
debugFile << std::setw(8) << std::setfill('0') << i << " ";
debugFile << Core::Debug::OpToString(static_cast<Op::Code>(frame.GetOps()[i])) << " ";
switch (frame.GetOps()[i])
{
case Op::Code::Constant:
{
const Address::Single constantAddress = frame.ReadAddress(i + 1);
debugFile << std::setw(7) << std::setfill('0') << constantAddress << " ";
debugFile << frame.ReadConstant(constantAddress);
i += sizeof(Address::Single);
break;
}
case Op::Code::GetGlobal:
{
const Address::Single globalVarAddress = frame.ReadAddress(i + 1);
debugFile << std::setw(7) << std::setfill('0') << globalVarAddress << " ";
i += sizeof(Address::Single);
break;
}
case Op::Code::SetGlobal:
{
const Address::Single globalVarAddress = frame.ReadAddress(i + 1);
debugFile << std::setw(7) << std::setfill('0') << globalVarAddress << " ";
i += sizeof(Address::Single);
break;
}
case Op::Code::SetLocal:
{
const Address::Single globalVarAddress = frame.ReadAddress(i + 1);
debugFile << std::setw(7) << std::setfill('0') << globalVarAddress << " ";
i += sizeof(Address::Single);
break;
}
case Op::Code::GetLocal:
{
const Address::Single globalVarAddress = frame.ReadAddress(i + 1);
debugFile << std::setw(7) << std::setfill('0') << globalVarAddress << " ";
i += sizeof(Address::Single);
break;
}
case Op::Code::PopN:
{
const Address::Single globalVarAddress = frame.ReadAddress(i + 1);
debugFile << std::setw(7) << std::setfill('0') << globalVarAddress << " ";
i += sizeof(Address::Single);
break;
}
case Op::Code::Jump:
{
const Address::Single globalVarAddress = frame.ReadAddress(i + 1);
debugFile << std::setw(7) << std::setfill('0') << globalVarAddress << " ";
i += sizeof(Address::Single);
break;
}
case Op::Code::JumpIfFalse:
{
const Address::Single globalVarAddress = frame.ReadAddress(i + 1);
debugFile << std::setw(7) << std::setfill('0') << globalVarAddress << " ";
i += sizeof(Address::Single);
break;
}
case Op::Code::JumpIfTrue:
{
const Address::Single globalVarAddress = frame.ReadAddress(i + 1);
debugFile << std::setw(7) << std::setfill('0') << globalVarAddress << " ";
i += sizeof(Address::Single);
break;
}
case Op::Code::Loop:
{
const Address::Single globalVarAddress = frame.ReadAddress(i + 1);
debugFile << std::setw(7) << std::setfill('0') << globalVarAddress << " ";
i += sizeof(Address::Single);
break;
}
default: break;
}
debugFile << "\n";
}
return true;
}
bool Debug::MakeTokenFile(const std::vector<Token> &tokens, const std::string &name)
{
std::ofstream tokenFile(name + ".token", std::ios::trunc | std::ios::binary);
if (!tokenFile.is_open())
{
std::cerr << "Error: Unable to create token file '" << name << ".token'\n";
return false;
}
tokenFile << "--------------------------------------------------\n";
tokenFile << " Token File \n";
tokenFile << "--------------------------------------------------\n";
for (auto& token : tokens)
{
tokenFile << TokenToString(token) << "\n";
}
return true;
}
} // Noble::Compiler