main.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. ExecutionContext context;
  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 = context.m_functions;
  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 = make_ref_counted<JSSpecCompiler::Function>(&context, spec_function.m_name, spec_function.m_algorithm.m_tree);
  45. for (auto const& argument : spec_function.m_arguments)
  46. function->m_local_variables.set(argument.name, make_ref_counted<VariableDeclaration>(argument.name));
  47. FunctionCallCanonicalizationPass(function).run();
  48. IfBranchMergingPass(function).run();
  49. ReferenceResolvingPass(function).run();
  50. out("{}", function->m_ast);
  51. return 0;
  52. }