Skip to content

Commit 02432ec

Browse files
committed
add ability to print
1 parent 5ca10eb commit 02432ec

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

docs/COMMANDS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# The bread programming language commands
22

3-
`print/"Hello World!"` - Prints hello world
3+
`print/Hello World!` - Prints hello world, No quotes required!

src/compiler.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#include<iostream>
22
#include<fstream>
33
#include<filesystem>
4+
#include<vector>
5+
#include<string>
46

57
std::string filename;
68
std::filesystem::path tmp_dir = "/tmp/bread/";
9+
std::filesystem::path cpp_dir = "/tmp/bread/temp.cpp";
710
std::string cppname;
11+
std::vector<std::string> file_contents;
812

913
int create_tmp_dir_if_not_exist() {
1014
try {
@@ -22,14 +26,34 @@ int create_tmp_dir_if_not_exist() {
2226

2327
int compile_to_cpp(std::string filename) {
2428
std::ifstream breadfile;
29+
std::ofstream cpp_file;
2530

2631
breadfile.open(filename);
2732
if (!breadfile.is_open()) {
2833
std::cerr << "error opening file" << std::endl;
2934
throw 500;
3035
return 1;
3136
}
37+
std::string c;
38+
while (std::getline(breadfile, c)) {
39+
file_contents.push_back(c);
40+
} // save file into vector
3241
breadfile.close();
42+
cpp_file.open(cpp_dir);
43+
if (!cpp_file.is_open()) {
44+
std::cerr << "error creating temporary cpp file" << std::endl;
45+
throw 500;
46+
return 1;
47+
}
48+
cpp_file << "//transpiled from the bread programming language, do not edit this file\n#include<iostream>\nint main() {\n";
49+
for (std::string content : file_contents) {
50+
if (content.find("print/") == 0) {
51+
content.erase(0, 6);
52+
cpp_file << "std::cout << " << content << ";\n";
53+
}
54+
}
55+
cpp_file << "return 0;\n}\n";
56+
cpp_file.close();
3357
return 0;
3458
}
3559

0 commit comments

Comments
 (0)