From a01f15e1cee3bf1d8c8008ec887de02197592c5c Mon Sep 17 00:00:00 2001
From: dx
Date: Sat, 23 May 2020 21:37:32 +0100
Subject: +compile_file function
This will read the file (filename) then compile it line by line.
---
Compiler/includes/compiler.hpp | 1 +
Compiler/src/compiler.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 46 insertions(+)
(limited to 'Compiler')
diff --git a/Compiler/includes/compiler.hpp b/Compiler/includes/compiler.hpp
index d761dcf..bd97a8e 100644
--- a/Compiler/includes/compiler.hpp
+++ b/Compiler/includes/compiler.hpp
@@ -14,5 +14,6 @@ enum Type
};
std::string compile_line(const char *raw);
+std::string compile_file(const char *filename);
#endif // __COMPILER_H_
diff --git a/Compiler/src/compiler.cpp b/Compiler/src/compiler.cpp
index 1220457..4be30e0 100644
--- a/Compiler/src/compiler.cpp
+++ b/Compiler/src/compiler.cpp
@@ -1,4 +1,7 @@
#include "../includes/compiler.hpp"
+#include
+#include
+#include
#include
static const unsigned short N_ITEMS = 4;
@@ -39,3 +42,45 @@ std::string compile_line(const char *raw)
return result;
}
+
+std::string compile_file(const char *filename)
+{
+ std::vector v;
+ std::ifstream fp(filename);
+
+ if (!fp.is_open())
+ {
+ std::cerr << "File " << filename << " not found" << std::endl;
+ exit(1);
+ }
+
+ bool paragraphing = false;
+ std::string line;
+ for (int i = 0; std::getline(fp, line) ; ++i) {
+ std::cout << "Compiled " << i + 1 << " of " << filename << "\n";
+ if (line[0] == '#' || line[0] == '-')
+ {
+ if (paragraphing)
+ v.push_back("
");
+ paragraphing = false;
+ }
+ else if (line[0] == '\0' || line[0] == '\n')
+ {
+ if (paragraphing)
+ v.push_back("");
+ else
+ v.push_back("");
+ paragraphing = !paragraphing;
+ continue;
+ }
+
+ v.push_back(compile_line(line.c_str()));
+ }
+ fp.close();
+
+ std::string result = "";
+ for (auto i : v)
+ result += i + "\n";
+
+ return result;
+}
--
cgit v1.2.3-13-gbd6f