summaryrefslogtreecommitdiff
path: root/tools/transpiler/replacer.cpp
diff options
context:
space:
mode:
authorMatthew Kosarek <mattkae@protonmail.com>2021-07-01 19:46:08 -0400
committerMatthew Kosarek <mattkae@protonmail.com>2021-07-01 19:46:08 -0400
commit4878f0fc6a039d220dd7adecb18d19c688ae50b0 (patch)
tree993893f1d894aedb350e86c759370c0e8c54c443 /tools/transpiler/replacer.cpp
parent9f968320c83ce79f98006dec71674feff4686e3b (diff)
(mkosarek) Decent SAT description for now
Diffstat (limited to 'tools/transpiler/replacer.cpp')
-rw-r--r--tools/transpiler/replacer.cpp128
1 files changed, 128 insertions, 0 deletions
diff --git a/tools/transpiler/replacer.cpp b/tools/transpiler/replacer.cpp
new file mode 100644
index 0000000..3f48089
--- /dev/null
+++ b/tools/transpiler/replacer.cpp
@@ -0,0 +1,128 @@
+#include "replacer.h"
+#include "Logger.h"
+#include <ctype.h>
+
+const char* keywords[] = {
+ "cosnt", "struct", "static", "return"
+};
+
+const char* types[] = {
+ "void",
+ "int", "int8", "int16", "int32", "int64",
+ "float", "float8", "float16", "float32", "float64",
+ "long",
+ "double",
+ "bool",
+ "Vector2", "Vector3", "Vector4",
+ "struct", "class", "static", "return", "const"
+};
+
+const char* commentType = "//";
+
+bool isType(char* str, int size) {
+ if (size == 0) {
+ return false;
+ }
+
+ for (int i = 0; i < sizeof(types) / sizeof(char*); i++) {
+ if (size == strlen(types[i]) && strncmp(str, types[i], size) == 0) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool isTokenDelim(char c) {
+ return isspace(c) || c == '\0' || c == ';' || c == ',';
+}
+
+// @TODO: This has brought me to two realizations:
+// 1) C++ string manipulaton is difficult for no apparent reason
+// 2) My current implementation of String is lacking so many features that I would want
+// 3) I have no plans to update my current implementation any time soon, so I will
+// begrudginly give my heart and soul over to the standard library.
+std::string insertCodeSnippets(char* workingDirectory, char* bodyContent) {
+ std::string strWorkingDirectory (workingDirectory);
+ strWorkingDirectory = strWorkingDirectory.substr(0, strWorkingDirectory.find_last_of("/") + 1);
+ std::string strContent(bodyContent);
+
+ size_t found = -1;
+ do {
+ found = strContent.find("#SNIPPET", found + 1);
+ if (found == std::string::npos) {
+ break;
+ }
+ int startFound = found;
+ found += strlen("#SNIPPET ");
+
+ std::string fileName(strWorkingDirectory);
+ while (!isspace(strContent[found])) {
+ fileName += strContent[found];
+ found++;
+ }
+
+ size_t endFound = found + 1;
+
+ FILE* snippetFile = fopen(fileName.c_str(), "r+");
+ if (snippetFile == NULL) {
+ logger_warning("Could not find snippet: %s", fileName.c_str());
+ continue;
+ }
+
+ fseek(snippetFile, 0, SEEK_END);
+ long fsize = ftell(snippetFile);
+ fseek(snippetFile, 0, SEEK_SET);
+
+ char* snippetContent = new char[fsize + 1];
+ fread(snippetContent, 1, fsize, snippetFile);
+ snippetContent[fsize] = '\0';
+
+ std::string s;
+ int tokenStart = 0, tokenEnd = 0;
+ while (snippetContent[tokenEnd] != '\0') {
+ while (!isTokenDelim(snippetContent[tokenEnd])) {
+ tokenEnd++;
+ }
+
+ int tokenLength = (tokenEnd - tokenStart);
+
+ if (tokenLength == strlen(commentType) && strncmp(&snippetContent[tokenStart], commentType, tokenLength) == 0) {
+ // @NOTE: Assuming comments are always on a single line
+ s.append("<span class=\"code_comment\">");
+ while (snippetContent[tokenEnd] != '\n') {
+ tokenEnd++;
+ }
+ s.append(&snippetContent[tokenStart], tokenEnd - tokenStart);
+ s.append("</span>");
+ } else if (isType(&snippetContent[tokenStart], tokenLength)) {
+ s.append("<span class=\"code_keyword\">");
+ s.append(&snippetContent[tokenStart], tokenLength);
+ s.append("</span>");
+ } else {
+ s.append(&snippetContent[tokenStart], tokenLength);
+ }
+
+ s += snippetContent[tokenEnd];
+
+ tokenStart = tokenEnd + 1;
+ tokenEnd = tokenStart;
+ }
+
+ while (s[0] == '\n') {
+ s.erase(0, 1);
+ }
+
+ s.insert(0, "<code>");
+ s.insert(0, "<pre>");
+ s.append("</code>");
+ s.append("</pre>");
+
+ delete [] snippetContent;
+
+ strContent.replace(startFound, endFound - startFound, s);
+
+ } while (true);
+
+ return strContent;
+}