1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 23:07:34 +00:00

JSSpecCompiler: Add reference resolving pass

It replaces UnresolvedReference with Variable, FunctionPointer, or
SlotName nodes. Also, it gathers all variable names from their
declarations.
This commit is contained in:
Dan Klishch 2023-08-19 23:17:06 -04:00 committed by Andrew Kaster
parent 326bac19d9
commit 81519975c5
9 changed files with 116 additions and 4 deletions

View file

@ -11,6 +11,7 @@
#include "Compiler/FunctionCallCanonicalizationPass.h"
#include "Compiler/IfBranchMergingPass.h"
#include "Compiler/ReferenceResolvingPass.h"
#include "Function.h"
#include "Parser/SpecParser.h"
@ -20,6 +21,18 @@ ErrorOr<int> serenity_main(Main::Arguments)
ExecutionContext context;
// 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 = context.m_functions;
functions.set("CompareISODate"sv, make_ref_counted<FunctionPointer>("CompareISODate"sv));
functions.set("CreateDateDurationRecord"sv, make_ref_counted<FunctionPointer>("CreateDateDurationRecord"sv));
functions.set("AddISODate"sv, make_ref_counted<FunctionPointer>("AddISODate"sv));
functions.set("ISODaysInMonth"sv, make_ref_counted<FunctionPointer>("ISODaysInMonth"sv));
functions.set("ISODateToEpochDays"sv, make_ref_counted<FunctionPointer>("ISODateToEpochDays"sv));
functions.set("truncate"sv, make_ref_counted<FunctionPointer>("truncate"sv));
functions.set("remainder"sv, make_ref_counted<FunctionPointer>("remainder"sv));
auto input = TRY(TRY(Core::File::standard_input())->read_until_eof());
XML::Parser parser { StringView(input.bytes()) };
@ -39,8 +52,12 @@ ErrorOr<int> serenity_main(Main::Arguments)
auto function = make_ref_counted<JSSpecCompiler::Function>(&context, 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<VariableDeclaration>(argument.name));
FunctionCallCanonicalizationPass(function).run();
IfBranchMergingPass(function).run();
ReferenceResolvingPass(function).run();
out("{}", function->m_ast);
return 0;