main.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Format.h>
  7. #include <LibCore/ArgsParser.h>
  8. #include <LibMain/Main.h>
  9. #include "Compiler/Passes/FunctionCallCanonicalizationPass.h"
  10. #include "Compiler/Passes/IfBranchMergingPass.h"
  11. #include "Compiler/Passes/ReferenceResolvingPass.h"
  12. #include "Function.h"
  13. #include "Parser/CppASTConverter.h"
  14. #include "Parser/SpecParser.h"
  15. using namespace JSSpecCompiler;
  16. struct CompilationStepWithDumpOptions {
  17. OwnPtr<CompilationStep> step;
  18. bool dump_ast = false;
  19. };
  20. class CompilationPipeline {
  21. public:
  22. template<typename T>
  23. void add_compilation_pass()
  24. {
  25. auto func = +[](TranslationUnitRef translation_unit) {
  26. T { translation_unit }.run();
  27. };
  28. add_step(adopt_own_if_nonnull(new NonOwningCompilationStep(T::name, func)));
  29. }
  30. template<typename T>
  31. void for_each_step_in(StringView pass_list, T&& func)
  32. {
  33. HashTable<StringView> selected_steps;
  34. for (auto pass : pass_list.split_view(',')) {
  35. if (pass == "all") {
  36. for (auto const& step : m_pipeline)
  37. selected_steps.set(step.step->name());
  38. } else if (pass == "last") {
  39. selected_steps.set(m_pipeline.last().step->name());
  40. } else if (pass.starts_with('-')) {
  41. VERIFY(selected_steps.remove(pass.substring_view(1)));
  42. } else {
  43. selected_steps.set(pass);
  44. }
  45. }
  46. for (auto& step : m_pipeline)
  47. if (selected_steps.contains(step.step->name()))
  48. func(step);
  49. }
  50. void add_step(OwnPtr<CompilationStep>&& step)
  51. {
  52. m_pipeline.append({ move(step) });
  53. }
  54. auto const& pipeline() const { return m_pipeline; }
  55. private:
  56. Vector<CompilationStepWithDumpOptions> m_pipeline;
  57. };
  58. ErrorOr<int> serenity_main(Main::Arguments arguments)
  59. {
  60. Core::ArgsParser args_parser;
  61. StringView filename;
  62. args_parser.add_positional_argument(filename, "File to compile", "file");
  63. constexpr StringView language_spec = "spec"sv;
  64. constexpr StringView language_cpp = "c++"sv;
  65. StringView language = language_spec;
  66. args_parser.add_option(Core::ArgsParser::Option {
  67. .argument_mode = Core::ArgsParser::OptionArgumentMode::Optional,
  68. .help_string = "Specify the language of the input file.",
  69. .short_name = 'x',
  70. .value_name = "{c++|spec}",
  71. .accept_value = [&](StringView value) {
  72. language = value;
  73. return language.is_one_of(language_spec, language_cpp);
  74. },
  75. });
  76. StringView passes_to_dump_ast;
  77. args_parser.add_option(passes_to_dump_ast, "Dump AST after specified passes.", "dump-ast", 0, "{all|last|<pass-name>|-<pass-name>[,...]}");
  78. args_parser.parse(arguments);
  79. CompilationPipeline pipeline;
  80. if (language == language_cpp)
  81. pipeline.add_step(adopt_own_if_nonnull(new CppParsingStep()));
  82. else
  83. pipeline.add_step(adopt_own_if_nonnull(new SpecParsingStep()));
  84. pipeline.add_compilation_pass<FunctionCallCanonicalizationPass>();
  85. pipeline.add_compilation_pass<IfBranchMergingPass>();
  86. pipeline.add_compilation_pass<ReferenceResolvingPass>();
  87. pipeline.for_each_step_in(passes_to_dump_ast, [](CompilationStepWithDumpOptions& step) {
  88. step.dump_ast = true;
  89. });
  90. TranslationUnit translation_unit;
  91. translation_unit.filename = filename;
  92. // Functions referenced in DifferenceISODate
  93. // TODO: This is here just for testing. In a long run, we need some place, which is not
  94. // `serenity_main`, to store built-in functions.
  95. auto& functions = translation_unit.function_index;
  96. functions.set("CompareISODate"sv, make_ref_counted<FunctionPointer>("CompareISODate"sv));
  97. functions.set("CreateDateDurationRecord"sv, make_ref_counted<FunctionPointer>("CreateDateDurationRecord"sv));
  98. functions.set("AddISODate"sv, make_ref_counted<FunctionPointer>("AddISODate"sv));
  99. functions.set("ISODaysInMonth"sv, make_ref_counted<FunctionPointer>("ISODaysInMonth"sv));
  100. functions.set("ISODateToEpochDays"sv, make_ref_counted<FunctionPointer>("ISODateToEpochDays"sv));
  101. functions.set("truncate"sv, make_ref_counted<FunctionPointer>("truncate"sv));
  102. functions.set("remainder"sv, make_ref_counted<FunctionPointer>("remainder"sv));
  103. for (auto const& step : pipeline.pipeline()) {
  104. step.step->run(&translation_unit);
  105. if (step.dump_ast) {
  106. outln(stderr, "===== AST after {} =====", step.step->name());
  107. for (auto const& function : translation_unit.function_definitions) {
  108. outln(stderr, "{}():", function->m_name);
  109. outln(stderr, "{}", function->m_ast);
  110. }
  111. }
  112. }
  113. return 0;
  114. }