Skip to content

Commit

Permalink
feat: add preprocessor to convert cppx to cpp code
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbacsur committed Oct 10, 2024
1 parent 3c8f0b4 commit ebef05b
Show file tree
Hide file tree
Showing 4 changed files with 466 additions and 56 deletions.
1 change: 1 addition & 0 deletions build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void build() {

std::vector<std::filesystem::path> lib_cpp_files = {
"src/cppx/json.cpp",
"src/cppx/preprocessor.cpp"
};

std::vector<std::filesystem::path> exe_sources = {
Expand Down
36 changes: 36 additions & 0 deletions include/cppx/preprocessor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <algorithm>
#include <cctype>
#include <iostream>
#include <regex>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

class Preprocessor {
public:
static std::string Process(const std::string& input);

private:
struct DOMNode {
std::string type;
std::string tagName;
std::unordered_map<std::string, std::string> attributes;
std::vector<DOMNode> children;
std::string textContent;
};

static std::string AddHeader(const std::string& script);
static std::vector<std::string> ExtractValidHtmlBlocks(const std::string& script);
static DOMNode ParseHTML(const std::string& html, size_t& pos);
static std::string GenerateJSON(const DOMNode& node, int indent = 0);
static std::string CorrectIndentation(const std::string& code);
static std::string Trim(const std::string& str);
static std::unordered_map<std::string, std::string> ParseAttributes(const std::string& attrString);
static const std::unordered_set<std::string> htmlTags;
static const std::string header;
};
87 changes: 31 additions & 56 deletions src/cppx/main.cpp
Original file line number Diff line number Diff line change
@@ -1,67 +1,42 @@
#include <chrono>
#include <fstream>
#include <iostream>
#include <random>
#include <string>
#include <thread>
#include <sstream>

#include "cppx/json.hpp"
#include "cppx/preprocessor.hpp"

int main() {
std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<int> dist(1000, 9999);
std::string ReadFile(const std::string& filePath) {
std::ifstream inFile(filePath);
if (!inFile) {
std::cerr << "Error: Unable to open input file: " << filePath << std::endl;
return "";
}
std::stringstream buffer;
buffer << inFile.rdbuf();
return buffer.str();
}

while (true) {
JSON json = {
"html", {
"children", {
"head", {
"children", {
"title", nullptr
}
},
"body", {
"children", {
"h1", {
"children", "Welcome to the landing page!"
},
"p", {
"children", "Your random number is: " + std::to_string(dist(rng))
},
"", "Hello, ",
"strong", {
"children", "world"
},
"", "!",
"footer", {
"children", "© 2024 CPPX 🚀"
}
}
}
}
}
};
void WriteFile(const std::string& filePath, const std::string& content) {
std::ofstream outFile(filePath);
if (!outFile) {
std::cerr << "Error: Unable to open output file: " << filePath << std::endl;
return;
}
outFile << content;
}

json["mutations"]["boolean"] = true;
json["mutations"]["integral"] = 420;
json["mutations"]["floating"] = 3.14;
json["mutations"]["string"] = "Hello, world!";
json["mutations"]["array"] = std::vector<JSON>{nullptr, true, 420, 3.14, "Hello, world!"};
json["mutations"]["object"] = JSON{
"first", 1,
"second", 2,
"third", 3
};
int main(int argc, char* argv[]) {
if (argc != 3) {
std::cerr << "Usage: cppx <input_file> <output_file>" << std::endl;
return 1;
}

std::cout << "\033[2J\033[1;1H" << json << std::endl;
std::string inputFilePath = argv[1];
std::string outputFilePath = argv[2];

try {
JSON parsed_json = JSON::parse(json.stringify());
} catch (const std::exception& e) {
std::cerr << "Parse error: " << e.what() << std::endl;
}
std::string inputScript = ReadFile(inputFilePath);
std::string transformedScript = Preprocessor::Process(inputScript);

std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
WriteFile(outputFilePath, transformedScript);

return 0;
}
Loading

0 comments on commit ebef05b

Please sign in to comment.