summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Kosarek <mattkae@protonmail.com>2021-06-21 20:05:08 -0400
committerMatthew Kosarek <mattkae@protonmail.com>2021-06-21 20:05:08 -0400
commita36f425491aaf019243a31179e80cb10ea62db59 (patch)
treef0c2e4677011b6595a51b628326bc2595bd08681
parent454fa3628bd35a37b7ed147a081f8f25590b3232 (diff)
(mkosarek) Transpiling code snippets in a shotty way, but it is good enough
-rw-r--r--2d/rigidbody/rigidbody_1.html36
-rw-r--r--2d/rigidbody/rigidbody_1.html.content4
-rw-r--r--index.css20
-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
10 files changed, 175 insertions, 24 deletions
diff --git a/2d/rigidbody/rigidbody_1.html b/2d/rigidbody/rigidbody_1.html
index e7e848d..0751499 100644
--- a/2d/rigidbody/rigidbody_1.html
+++ b/2d/rigidbody/rigidbody_1.html
@@ -93,8 +93,14 @@
<p>
Now that we have that understanding, we can begin setting up our rigidbody data structure.
- {{{rigidbody_1/snippet1.cpp}}}
-
+ <pre><code><span class="code_keyword">struct</span> Rigidbody {
+ <span class="code_keyword">Vector2</span> force = { 0, 0 };
+ <span class="code_keyword">Vector2</span> acceleration = { 0, 0 };
+ <span class="code_keyword">Vector2</span> velocity = { 0, 0 };
+ <span class="code_keyword">Vector2</span> position = { 0, 0 };
+ <span class="code_keyword">float32</span> mass = 1.f;
+};
+</code></pre>
As you can see, the base data structure exactly mirrors what we already know from 2D newtonian physics.
</p>
</section>
@@ -103,8 +109,30 @@
<p>
Now, let's put that Rigidbody data structure to work! As I mentioned earlier, you can think of dynamics as the <i>input</i> to the system. What we're going to do now is add a way to
- {{{rigidbody_1/snippet2.cpp}}}
- </p>
+ <pre><code><span class="code_keyword">struct</span> Rigidbody {
+ <span class="code_keyword">Vector2</span> force = { 0, 0 };
+ <span class="code_keyword">Vector2</span> velocity = { 0, 0 };
+ <span class="code_keyword">Vector2</span> position = { 0, 0 };
+ <span class="code_keyword">float32</span> mass = 1.f;
+
+ <span class="code_keyword">void</span> applyForce(Vector2 f) {
+ force += f;
+ }
+
+ <span class="code_keyword">void</span> applyGravity(float32 deltaTimeSeconds) {
+ velocity += (Vector2 { 0.f, -50.f } * deltaTimeSeconds);
+ }
+
+ <span class="code_keyword">void</span> update(float32 deltaTimeSeconds) {
+ applyGravity(deltaTimeSeconds);
+
+ <span class="code_keyword">Vector2</span> acceleration = force / mass;
+ velocity += (acceleration * deltaTimeSeconds);
+ position += (velocity * deltaTimeSeconds);
+ force = <span class="code_keyword">Vector2</span> { 0.f, 0.f };
+ }
+};
+</code></pre> </p>
</section>
<section>
<h2>
diff --git a/2d/rigidbody/rigidbody_1.html.content b/2d/rigidbody/rigidbody_1.html.content
index 0e75bdc..de3898a 100644
--- a/2d/rigidbody/rigidbody_1.html.content
+++ b/2d/rigidbody/rigidbody_1.html.content
@@ -43,7 +43,7 @@
<p>
Now that we have that understanding, we can begin setting up our rigidbody data structure.
- {{{rigidbody_1/snippet1.cpp}}}
+ #SNIPPET rigidbody_1/snippet1.cpp
As you can see, the base data structure exactly mirrors what we already know from 2D newtonian physics.
</p>
@@ -53,7 +53,7 @@
<p>
Now, let's put that Rigidbody data structure to work! As I mentioned earlier, you can think of dynamics as the <i>input</i> to the system. What we're going to do now is add a way to
- {{{rigidbody_1/snippet2.cpp}}}
+ #SNIPPET rigidbody_1/snippet2.cpp
</p>
</section>
<section>
diff --git a/index.css b/index.css
index 758afa3..8f0670b 100644
--- a/index.css
+++ b/index.css
@@ -152,16 +152,6 @@ section > h2 {
font-weight: 500;
}
-section pre {
- background-color: white;
- border: 1px solid black;
- border-radius: 3px;
- overflow-x: auto;
- padding: 0.5rem;
- font-size: 0.9rem;
- line-height: 1.1rem;
-}
-
#references a {
color: inherit;
text-decoration: none;
@@ -175,6 +165,16 @@ section pre {
/***********************************
Code stylings
************************************/
+pre {
+ background-color: white;
+ border: 1px solid #D0D0D0;
+ border-radius: 3px;
+ overflow-x: auto;
+ padding: 1rem;
+ font-size: 0.8rem;
+ line-height: 1.0rem;
+}
+
.code_keyword {
color: blue;
font-weight: bold;
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;