/* * Copyright (c) 2023, Dan Klishch * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include "Compiler/FunctionCallCanonicalizationPass.h" #include "Compiler/IfBranchMergingPass.h" #include "Compiler/ReferenceResolvingPass.h" #include "Function.h" #include "Parser/SpecParser.h" ErrorOr serenity_main(Main::Arguments) { using namespace JSSpecCompiler; TranslationUnit translation_unit; // Functions referenced in DifferenceISODate // TODO: This is here just for testing. In a long run, we need some place, which is not // `serenity_main`, to store built-in functions. auto& functions = translation_unit.function_index; functions.set("CompareISODate"sv, make_ref_counted("CompareISODate"sv)); functions.set("CreateDateDurationRecord"sv, make_ref_counted("CreateDateDurationRecord"sv)); functions.set("AddISODate"sv, make_ref_counted("AddISODate"sv)); functions.set("ISODaysInMonth"sv, make_ref_counted("ISODaysInMonth"sv)); functions.set("ISODateToEpochDays"sv, make_ref_counted("ISODateToEpochDays"sv)); functions.set("truncate"sv, make_ref_counted("truncate"sv)); functions.set("remainder"sv, make_ref_counted("remainder"sv)); auto input = TRY(TRY(Core::File::standard_input())->read_until_eof()); XML::Parser parser { StringView(input.bytes()) }; auto maybe_document = parser.parse(); if (maybe_document.is_error()) { outln("{}", maybe_document.error()); return 1; } auto document = maybe_document.release_value(); auto maybe_function = JSSpecCompiler::SpecFunction::create(&document.root()); if (maybe_function.is_error()) { outln("{}", maybe_function.error()->to_string()); return 1; } auto spec_function = maybe_function.value(); auto* function = translation_unit.adopt_function( make_ref_counted(spec_function.m_name, spec_function.m_algorithm.m_tree)); for (auto const& argument : spec_function.m_arguments) function->m_local_variables.set(argument.name, make_ref_counted(argument.name)); FunctionCallCanonicalizationPass(function).run(); IfBranchMergingPass(function).run(); ReferenceResolvingPass(function).run(); out("{}", function->m_ast); return 0; }