-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug-output.hpp
109 lines (102 loc) · 3.79 KB
/
debug-output.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
/*
* MIT License
*
* Copyright(c) 2018 Paul Bernitz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files(the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include "meta-gen.hpp"
#include "opcode-analyser.hpp"
namespace c8s
{
// Debug output seperator line.
void print_separator(bool strong = false)
{
for (unsigned i = 0; i < 65; ++i)
std::cout << ((strong) ? '=' : '-');
std::cout << '\n';
}
// Debug output tokens.
void print_tokens(const std::vector<c8s::Token>& tokens)
{
print_separator();
std::cout << "1] Split input code into tokens\n";
print_separator();
for (const auto& e : tokens)
{
std::cout << "T[" << e.value << "] ";
if (e.type == c8s::TokenType::ClosingStatement)
std::cout << '\n';
}
std::cout << '\n';
}
// Debug output ast.
void print_ast(c8s::ASTNode node, unsigned depth = 0)
{
if (depth == 0)
{
print_separator();
std::cout << "2] Parse tokens into abstract syntax tree\n";
print_separator();
}
for (unsigned i = 0; i < depth; ++i) std::cout << "|`\t";
//std::cout << "|\n";
//for (unsigned i = 0; i < depth; ++i) std::cout << "|\t";
std::cout << "+--[";
if (node.type == c8s::ASTNodeType::VarExpression) std::cout << "VarExpression, ";
if (node.type == c8s::ASTNodeType::NumberLiteral) std::cout << "NumberLiteral, ";
if (node.type == c8s::ASTNodeType::VarDeclaration) std::cout << "VarDeclaration, ";
if (node.type == c8s::ASTNodeType::Identifier) std::cout << "Identifier, ";
if (node.type == c8s::ASTNodeType::Operator) std::cout << "Operator, ";
if (node.type == c8s::ASTNodeType::IfStatement) std::cout << "IfStmt, ";
if (node.type == c8s::ASTNodeType::ForLoop) std::cout << "ForLoop, ";
if (node.type == c8s::ASTNodeType::Program) std::cout << "Program, ";
if (node.type == c8s::ASTNodeType::FunctionCall) std::cout << "FunctionCall, ";
if (node.type == c8s::ASTNodeType::OpenBrace) std::cout << "Opening brace, ";
if (node.type == c8s::ASTNodeType::ClosingBrace) std::cout << "Closing brace, ";
std::cout << node.value << "]\n";
for (auto& e : node.params)
{
print_ast(e, depth + 1);
}
}
// Debug output meta-opcodes.
void print_meta(std::vector<std::string> meta_ops)
{
print_separator();
std::cout << "3] Creating `meta-opcodes` from the AST\n";
print_separator();
std::cout << "(This is the last step before the finished opcodes.\n";
std::cout << "Now we only have to resolve the labels `<1>,<2>..`\n";
std::cout << "to their line - number at `<!1!> , <!2!>..`)\n";
for (auto op : meta_ops)
{
std::cout << "0x" << op << '\n';
}
}
// Debug output opcodes.
void print_opcodes(std::vector<u16> opcodes)
{
print_separator();
std::cout << "4] Creating finished opcodes from `meta`\n";
print_separator();
analyse_opcodes(opcodes);
}
}