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.

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