summaryrefslogtreecommitdiff
path: root/transpiler
diff options
context:
space:
mode:
Diffstat (limited to 'transpiler')
-rw-r--r--transpiler/MyString.cpp6
-rw-r--r--transpiler/MyString.h4
-rwxr-xr-xtranspiler/build.sh2
-rw-r--r--transpiler/replacer.cpp117
-rw-r--r--transpiler/replacer.h3
-rwxr-xr-xtranspiler/transpilerbin38744 -> 49640 bytes
-rw-r--r--transpiler/transpiler.cpp7
7 files changed, 131 insertions, 8 deletions
diff --git a/transpiler/MyString.cpp b/transpiler/MyString.cpp
index 1cb3d43..1a63960 100644
--- a/transpiler/MyString.cpp
+++ b/transpiler/MyString.cpp
@@ -105,8 +105,8 @@ void StringBuilder::addStr(String* str) {
addStr(str->getValue());
}
-void StringBuilder::addStr(const char* str) {
- int amountLeft = strlen(str);
+void StringBuilder::addStr(const char* str, int inLength) {
+ int amountLeft = inLength < 0 ? strlen(str) : inLength;
length += amountLeft;
int ptr = 0;
@@ -229,7 +229,7 @@ void StringBuilder::format(const char* str, ...) {
addFloat(static_cast<float>(va_arg(args, double)));
break;
case 's':
- addStr(va_arg(args, char*));
+ addStr(va_arg(args, char*), -1);
default:
break;
}
diff --git a/transpiler/MyString.h b/transpiler/MyString.h
index d05d84a..805f5de 100644
--- a/transpiler/MyString.h
+++ b/transpiler/MyString.h
@@ -59,7 +59,7 @@ struct StringBuilder {
StringBuffer* getBufferAtIdx(int index);
const StringBuffer* getBufferAtIdxConst(int index) const;
void addStr(String* str);
- void addStr(const char* str);
+ void addStr(const char* str, int length = -1);
void addChar(char c);
void format(const char* str, ...);
void addInt(int value);
@@ -73,4 +73,4 @@ struct StringBuilder {
char getCharAtIdx(int index) const;
void insert(char c, int index);
void free();
-}; \ No newline at end of file
+};
diff --git a/transpiler/build.sh b/transpiler/build.sh
index 6ca478e..ea1cb93 100755
--- a/transpiler/build.sh
+++ b/transpiler/build.sh
@@ -1 +1 @@
-g++ -o transpiler transpiler.cpp Logger.cpp MyString.cpp \ No newline at end of file
+g++ -o transpiler transpiler.cpp Logger.cpp MyString.cpp replacer.cpp
diff --git a/transpiler/replacer.cpp b/transpiler/replacer.cpp
new file mode 100644
index 0000000..28c2e8e
--- /dev/null
+++ b/transpiler/replacer.cpp
@@ -0,0 +1,117 @@
+#include "replacer.h"
+#include "Logger.h"
+#include <ctype.h>
+
+const char* keywords[] = {
+ "cosnt", "struct", "static"
+};
+
+const char* types[] = {
+ "void",
+ "int", "int8", "int16", "int32", "int64",
+ "float", "float8", "float16", "float32", "float64",
+ "long",
+ "double",
+ "bool",
+ "Vector2", "Vector3", "Vector4",
+ "struct", "class"
+};
+
+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 (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;
+}
diff --git a/transpiler/replacer.h b/transpiler/replacer.h
new file mode 100644
index 0000000..f795bca
--- /dev/null
+++ b/transpiler/replacer.h
@@ -0,0 +1,3 @@
+#include <string>
+
+std::string insertCodeSnippets(char* workingDirectory, char* bodyContent);
diff --git a/transpiler/transpiler b/transpiler/transpiler
index 5672fb4..509db3e 100755
--- a/transpiler/transpiler
+++ b/transpiler/transpiler
Binary files differ
diff --git a/transpiler/transpiler.cpp b/transpiler/transpiler.cpp
index 5c1de66..a171fc6 100644
--- a/transpiler/transpiler.cpp
+++ b/transpiler/transpiler.cpp
@@ -1,6 +1,7 @@
#include "MyString.h"
#include "List.h"
#include "Logger.h"
+#include "replacer.h"
#include <cstdio>
#include <vector>
#include <cstring>
@@ -149,7 +150,6 @@ int main() {
servedFiles.add(pathCopy);
otherSb.clear();
-
while (line[0] != '\n') {
if (line[0] != '\"') {
otherSb.addChar(line[0]);
@@ -215,7 +215,10 @@ int main() {
char* bodyContent = new char[fsize + 1];
fread(bodyContent, 1, fsize, contentFile);
bodyContent[fsize] = '\0';
- sb.addStr(bodyContent);
+
+ auto processedBody = insertCodeSnippets(contentFileName.getValue(), bodyContent);
+
+ sb.addStr(processedBody.c_str());
delete [] bodyContent;