main.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/File.h>
  8. #include <LibMain/Main.h>
  9. #include <LibXML/Parser/Parser.h>
  10. #include "Compiler/FunctionCallCanonicalizationPass.h"
  11. #include "Compiler/IfBranchMergingPass.h"
  12. #include "Compiler/ReferenceResolvingPass.h"
  13. #include "Function.h"
  14. #include "Parser/SpecParser.h"
  15. ErrorOr<int> serenity_main(Main::Arguments)
  16. {
  17. using namespace JSSpecCompiler;
  18. TranslationUnit translation_unit;
  19. // Functions referenced in DifferenceISODate
  20. // TODO: This is here just for testing. In a long run, we need some place, which is not
  21. // `serenity_main`, to store built-in functions.
  22. auto& functions = translation_unit.function_index;
  23. functions.set("CompareISODate"sv, make_ref_counted<FunctionPointer>("CompareISODate"sv));
  24. functions.set("CreateDateDurationRecord"sv, make_ref_counted<FunctionPointer>("CreateDateDurationRecord"sv));
  25. functions.set("AddISODate"sv, make_ref_counted<FunctionPointer>("AddISODate"sv));
  26. functions.set("ISODaysInMonth"sv, make_ref_counted<FunctionPointer>("ISODaysInMonth"sv));
  27. functions.set("ISODateToEpochDays"sv, make_ref_counted<FunctionPointer>("ISODateToEpochDays"sv));
  28. functions.set("truncate"sv, make_ref_counted<FunctionPointer>("truncate"sv));
  29. functions.set("remainder"sv, make_ref_counted<FunctionPointer>("remainder"sv));
  30. auto input = TRY(TRY(Core::File::standard_input())->read_until_eof());
  31. XML::Parser parser { StringView(input.bytes()) };
  32. auto maybe_document = parser.parse();
  33. if (maybe_document.is_error()) {
  34. outln("{}", maybe_document.error());
  35. return 1;
  36. }
  37. auto document = maybe_document.release_value();
  38. auto maybe_function = JSSpecCompiler::SpecFunction::create(&document.root());
  39. if (maybe_function.is_error()) {
  40. outln("{}", maybe_function.error()->to_string());
  41. return 1;
  42. }
  43. auto spec_function = maybe_function.value();
  44. auto* function = translation_unit.adopt_function(
  45. make_ref_counted<FunctionDefinition>(spec_function.m_name, spec_function.m_algorithm.m_tree));
  46. for (auto const& argument : spec_function.m_arguments)
  47. function->m_local_variables.set(argument.name, make_ref_counted<VariableDeclaration>(argument.name));
  48. FunctionCallCanonicalizationPass(function).run();
  49. IfBranchMergingPass(function).run();
  50. ReferenceResolvingPass(function).run();
  51. out("{}", function->m_ast);
  52. return 0;
  53. }