You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
10 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #include "molasses/lexer.h"
  2. #include "molasses/parser_primitives.h"
  3. #include <cstring>
  4. #include <filesystem>
  5. #include <fstream>
  6. #include <iostream>
  7. #include <stack>
  8. #include <string>
  9. #include <variant>
  10. using compile_element = std::variant<molasses::lexed_output, molasses::generate_context, std::string>;
  11. struct build_system_error : std::runtime_error {
  12. explicit build_system_error(const std::string& message)
  13. : std::runtime_error(message)
  14. {}
  15. };
  16. int main(int argc, char** argv) {
  17. std::vector<std::string> arguments;
  18. while(argc > 1) {
  19. argv++;
  20. arguments.emplace_back(*argv, strlen(*argv));
  21. argc--;
  22. }
  23. try {
  24. std::stack<compile_element> compile_stack;
  25. for(auto elem : arguments) {
  26. if(elem == "generate") {
  27. if(not compile_stack.empty() and std::holds_alternative<std::string>(compile_stack.top())) {
  28. auto filename = std::get<std::string>(compile_stack.top());
  29. compile_stack.pop();
  30. if(not compile_stack.empty() and std::holds_alternative<molasses::generate_context>(compile_stack.top())) {
  31. auto generator = std::get<molasses::generate_context>(compile_stack.top());
  32. compile_stack.pop();
  33. auto assembler = molasses::generate(generator);
  34. std::ofstream output(filename + ".s");
  35. for(const auto& line : assembler) {
  36. output << line;
  37. }
  38. compile_stack.emplace(filename);
  39. } else
  40. throw build_system_error("generate expects a parsed output\n");
  41. } else
  42. throw build_system_error("generate expects a filename\n");
  43. } else if(elem == "parse") {
  44. molasses::parser_context ctx;
  45. ctx = molasses::register_integers(ctx);
  46. ctx = molasses::register_i32_operations(ctx);
  47. if(not compile_stack.empty() and std::holds_alternative<molasses::lexed_output>(compile_stack.top())) {
  48. auto lexer = std::get<molasses::lexed_output>(compile_stack.top());
  49. compile_stack.pop();
  50. auto generator = molasses::parse(ctx, lexer);
  51. compile_stack.emplace(generator);
  52. } else
  53. throw build_system_error("parse expects a lexed output\n");
  54. } else if(elem == "lex") {
  55. if(not compile_stack.empty() and std::holds_alternative<std::string>(compile_stack.top())) {
  56. auto filename = std::get<std::string>(compile_stack.top());
  57. compile_stack.pop();
  58. if(not std::filesystem::exists(filename))
  59. throw build_system_error("file " + filename + " does not exist\n");
  60. std::ifstream t(filename);
  61. std::stringstream buffer;
  62. buffer << t.rdbuf();
  63. auto lexed = molasses::lex(filename, buffer.str());
  64. compile_stack.emplace(lexed);
  65. } else
  66. throw build_system_error("lex expects a filename\n");
  67. } else if(elem == "lex-all") {
  68. std::vector<molasses::lexed_output> lexed_list;
  69. while(not compile_stack.empty() and std::holds_alternative<std::string>(compile_stack.top())) {
  70. auto filename = std::get<std::string>(compile_stack.top());
  71. compile_stack.pop();
  72. if(not std::filesystem::exists(filename))
  73. throw build_system_error("file " + filename + " does not exist\n");
  74. std::ifstream t(filename);
  75. std::stringstream buffer;
  76. buffer << t.rdbuf();
  77. auto lexed = molasses::lex(filename, buffer.str());
  78. lexed_list.emplace_back(lexed);
  79. }
  80. for(auto& lexed : lexed_list) {
  81. compile_stack.emplace(std::move(lexed));
  82. }
  83. } else if(elem == "merge") {
  84. if(not compile_stack.empty() and std::holds_alternative<molasses::lexed_output>(compile_stack.top())) {
  85. auto lexer_1 = std::get<molasses::lexed_output>(compile_stack.top());
  86. compile_stack.pop();
  87. if(not compile_stack.empty() and std::holds_alternative<molasses::lexed_output>(compile_stack.top())) {
  88. auto lexer_2 = std::get<molasses::lexed_output>(compile_stack.top());
  89. compile_stack.pop();
  90. compile_stack.emplace(molasses::concatenate(lexer_1, lexer_2));
  91. } else
  92. throw build_system_error("merge expects 2 lexed outputs\n");
  93. } else
  94. throw build_system_error("merge expects 2 lexed outputs\n");
  95. } else if(elem == "merge-all") {
  96. if(not compile_stack.empty() and std::holds_alternative<molasses::lexed_output>(compile_stack.top())) {
  97. auto lexer_1 = std::get<molasses::lexed_output>(compile_stack.top());
  98. compile_stack.pop();
  99. while(not compile_stack.empty() and std::holds_alternative<molasses::lexed_output>(compile_stack.top())) {
  100. auto lexer_2 = std::get<molasses::lexed_output>(compile_stack.top());
  101. compile_stack.pop();
  102. lexer_1 = molasses::concatenate(lexer_1, lexer_2);
  103. }
  104. compile_stack.emplace(lexer_1);
  105. } else
  106. throw build_system_error("merge-all expects at least 1 lexed outputs\n");
  107. } else if(elem == "assemble") {
  108. if(not compile_stack.empty() and std::holds_alternative<std::string>(compile_stack.top())) {
  109. auto filename = std::get<std::string>(compile_stack.top());
  110. compile_stack.pop();
  111. std::stringstream compile;
  112. compile << "clang -c " << filename << ".s -o " << filename << ".o";
  113. std::stringstream link;
  114. link << "ld -e _start " << filename << ".o -o " << filename;
  115. std::cout << compile.str() << std::endl;
  116. system(compile.str().c_str());
  117. std::cout << link.str() << std::endl;
  118. system(link.str().c_str());
  119. } else
  120. throw build_system_error("assemble expects an assembly file\n");
  121. } else if(elem == "help" or elem == "--help") {
  122. std::cout << "# Sugar\n\n";
  123. std::cout << "## Commands\n\n";
  124. std::cout << "lex : string ➔ lexed_output\n";
  125. std::cout << "> takes a filename to a file that must be compiled\n\n";
  126. std::cout << "lex-all : string* ➔ lexed_output*\n";
  127. std::cout << "> takes as many filenames to files that must be compiled as can be read and passes them through the lexed\n\n";
  128. std::cout << "merge : lexed_output lexed_output ➔ lexed_output\n";
  129. std::cout << "> merges two lexed modules together\n\n";
  130. std::cout << "merge-all : lexed_output* ➔ lexed_output\n";
  131. std::cout << "> merges as many lexed modules together as present on the top of the stack\n\n";
  132. std::cout << "parse : lexed_output ➔ parsed_output\n";
  133. std::cout << "> prepares code for generation\n\n";
  134. std::cout << "generate : parsed_output string ➔ string\n";
  135. std::cout << "> takes a root filename, it will be appended with \".s\" and that will be the generated assembly file,\n";
  136. std::cout << "> the filename will not be consumed\n\n";
  137. std::cout << "assemble : string ➔ _ \n";
  138. std::cout
  139. << "> takes a root filename, it will be appended with \".s\" and that file will be compiled,\n";
  140. std::cout << "> the compiled output will be the given filename\n\n";
  141. std::cout << "help : _ ➔ _ \n";
  142. std::cout << "> prints this help\n\n";
  143. std::cout << "## Examples\n\n";
  144. std::cout << "- compile the file \"example.mol\" into the \"potato.s\" assembly file\n";
  145. std::cout << "> `$ sugar example.mol lex parse potato generate`\n";
  146. std::cout << "\n";
  147. std::cout << "- compile the file \"example.mol\" into the \"potato\" executable\n";
  148. std::cout << "> `$ sugar example.mol lex parse potato generate assemble`\n";
  149. std::cout << "\n";
  150. std::cout << "- compile the file \"example.mol\" and \"2.mol\" into the \"potato\" executable\n";
  151. std::cout << "> `$ sugar example.mol lex 2.mol lex merge parse potato generate assemble`\n";
  152. } else
  153. compile_stack.emplace(elem);
  154. }
  155. if(compile_stack.size() > 1) throw std::runtime_error("build left unfinished operations");
  156. if(not compile_stack.empty()) {
  157. if(std::holds_alternative<molasses::lexed_output>(compile_stack.top())) {
  158. auto lexer = std::get<molasses::lexed_output>(compile_stack.top());
  159. for(const auto& elem : lexer.symbols) {
  160. std::cout << elem << " ";
  161. }
  162. std::cout << "\n\n";
  163. for(auto [id, name] : lexer.dictionary) {
  164. std::cout << id << ": " << name << "\n";
  165. }
  166. } else if(std::holds_alternative<molasses::generate_context>(compile_stack.top())) {
  167. auto generator = std::get<molasses::generate_context>(compile_stack.top());
  168. for(const auto& elem : generator.procedures) {
  169. std::cout << elem->_name << " : ";
  170. for(const auto& args : elem->_args) {
  171. std::cout << args << " ";
  172. }
  173. std::cout << "->";
  174. for(const auto& rets : elem->_rets) {
  175. std::cout << " " << rets;
  176. }
  177. std::cout << "\n";
  178. }
  179. }
  180. }
  181. } catch (molasses::parser_error& error) {
  182. std::cerr << "COMPILER ERROR:\n" << error.what();
  183. return 1;
  184. } catch (build_system_error& error) {
  185. std::cerr << "BUILD SYSTEM ERROR:\n" << error.what();
  186. return 1;
  187. }
  188. }