main.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 "Function.h"
  13. #include "Parser/SpecParser.h"
  14. ErrorOr<int> serenity_main(Main::Arguments)
  15. {
  16. using namespace JSSpecCompiler;
  17. ExecutionContext context;
  18. auto input = TRY(TRY(Core::File::standard_input())->read_until_eof());
  19. XML::Parser parser { StringView(input.bytes()) };
  20. auto maybe_document = parser.parse();
  21. if (maybe_document.is_error()) {
  22. outln("{}", maybe_document.error());
  23. return 1;
  24. }
  25. auto document = maybe_document.release_value();
  26. auto maybe_function = JSSpecCompiler::SpecFunction::create(&document.root());
  27. if (maybe_function.is_error()) {
  28. outln("{}", maybe_function.error()->to_string());
  29. return 1;
  30. }
  31. auto spec_function = maybe_function.value();
  32. auto function = make_ref_counted<JSSpecCompiler::Function>(&context, spec_function.m_name, spec_function.m_algorithm.m_tree);
  33. FunctionCallCanonicalizationPass(function).run();
  34. IfBranchMergingPass(function).run();
  35. out("{}", function->m_ast);
  36. return 0;
  37. }