Added disassembler, thanks @hiperiondev, bumped minor version

This commit is contained in:
2023-08-14 23:06:05 +10:00
parent ce54912232
commit cce8ae1ea3
9 changed files with 706 additions and 2 deletions

View File

@@ -0,0 +1,73 @@
//changing of the guard - made for @hyperiondev's microcontrollers
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
std::string convert(std::string str) {
str = str.substr(str.find_last_of("\\/")+1);
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
std::replace(str.begin(), str.end(), '.', '_');
return str;
}
std::string convertToGuardStart(std::string str) {
str = convert(str);
return "#ifndef " + str + "\n#define " + str + "\n";
}
std::string convertToGuardEnd(std::string str) {
str = convert(str);
return "\n#endif //" + str + "\n";
}
int main(int argc, char** argv) {
if (argc <= 1) {
return -1;
}
for (int fileCounter = 1; fileCounter < argc; fileCounter++) {
std::ifstream is; //input stream
std::string buffer; //store output file
//open
is.open(argv[fileCounter]);
if (!is.is_open()) {
return -1;
}
while (!is.eof()) {
std::string top; //I dislike C++
getline(is, top);
//check for pragma guard
if (top == "#pragma once") {
top = convertToGuardStart(argv[fileCounter]);
getline(is, buffer, '\0');
buffer += convertToGuardEnd(argv[fileCounter]);
}
else {
top += "\n";
getline(is, buffer, '\0');
}
buffer = top + buffer;
}
//finally
is.close();
std::ofstream os;
os.open(argv[fileCounter]);
if (!os.is_open()) {
return -1;
}
os << buffer;
os.close();
}
return 0;
}

105
tools/misc/mecha.cpp Normal file
View File

@@ -0,0 +1,105 @@
//MECHA: Markdown Embedded Comment Heuristic Analyzer
/*!
# Handy-Dandy *MECHA* Docs!
The following is the source code for MECHA - a tool for reading in source code, and splicing out the markdown embedded within. It can also spit out chunks of code, though that's a bit uglier.
In theory, this should work correctly on all languages that conform to C-style comments, including Toy.
To build it, simply run `g++ -o mecha mecha.cpp`.
To run the result, you must pass in a series of filenames via the command line.
## License
This tool is considered part of Toy's toolchain, so Toy's zlib license should cover it.
## Header Files
If you're reading this as a comment in the source code, this is what I mean by ugly. Thankfully, this section is outputted correctly in markdown.
!*/
//*!```
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
//!*
/*!```
# Recursive Docs
If you run mecha on it's own source code, it should Just Work, producing a file called mecha_cpp.md
!*/
int main(int argc, char** argv) {
if (argc <= 1) {
return -1;
}
for (int fileCounter = 1; fileCounter < argc; fileCounter++) {
std::ifstream is; //input stream
std::string buffer; //store output file
//open
is.open(argv[fileCounter]);
if (!is.is_open()) {
return -1;
}
while (!is.eof()) {
//skip until correct characters found
if (is.get() != '*') {
continue;
}
if (is.get() != '!') {
continue;
}
//found the start of the block - begin reading markdown content
while (!is.eof()) {
char c = is.get();
if (c == '!' && is.peek() == '*') {
break;
}
buffer += c;
}
//bugfix
if (buffer.length() >= 2 && buffer.substr(buffer.length()-2) == "//") {
buffer.pop_back();
buffer.pop_back();
}
}
//finally
is.close();
if (buffer.length() == 0) {
continue;
}
std::ofstream os;
std::string ofname = argv[fileCounter];
std::replace(ofname.begin(), ofname.end(), '.', '_');
ofname += ".md";
os.open(ofname);
if (!os.is_open()) {
return -1;
}
os << buffer;
os.close();
}
return 0;
}

130
tools/misc/uptown.cpp Normal file
View File

@@ -0,0 +1,130 @@
//UPTOWN: Utility to Prepend Text Of Whatever Next
#include <iostream>
#include <fstream>
#include <string>
#include <map>
//protip: This gets more efficient the more files passed in
//protip: pass in the headers first
bool is_alpha(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
}
int main(int argc, char** argv) {
if (argc <= 1) {
return -1;
}
//cache replacements for all files
std::map<std::string, std::string> replacements;
for (int fileCounter = 1; fileCounter < argc; fileCounter++) {
std::ifstream is; //input stream
std::string buffer; //store file
//open
is.open(argv[fileCounter]);
if (!is.is_open()) {
return -1;
}
while (!is.eof()) {
char c = is.peek();
//skip comments (make it easier)
if (c == '/') {
buffer += is.get();
//single lines
if (is.peek() == '/') {
do {
buffer += is.get();
} while(is.peek() != '\n');
continue;
}
//multi-line
if (is.peek() == '*') {
while (true) {
do {
buffer += is.get();
} while(is.peek() != '*');
if (is.eof()) { //just in case
return -1;
} else {
buffer += is.get();
if (is.peek() == '/') {
buffer += is.get();
break;
}
}
}
continue;
}
}
//skip strings (causes issues)
if (c == '"') {
do {
buffer += is.get();
} while (is.peek() != '"');
buffer += is.get();
continue;
}
//skip non-words
if (!is_alpha(c)) {
buffer += is.get();
continue;
}
//read word
std::string word = "";
while(is_alpha(is.peek())) {
word += is.get();
}
//get replacement word, if it doesn't exist
if (replacements.find(word) == replacements.end()) {
std::cout << word << " : ";
std::string prepend = "";
getline(std::cin, prepend);
if (prepend.length() == 0) {
replacements[word] = word;
}
else {
replacements[word] = prepend + word;
}
}
//append the replacement
buffer += replacements[word];
}
//finally
is.close();
std::ofstream os;
os.open(argv[fileCounter]);
if (!os.is_open()) {
return -1;
}
//bugfix
if (!buffer.empty()) {
buffer.pop_back();
}
os << buffer;
os.close();
}
return 0;
}