JSSpecCompiler: Adopt more C++ terminology

Let's not use strange names like `ExecutionContext`, which nobody will
understand in the future.
This commit is contained in:
Dan Klishch 2023-09-19 10:29:57 -04:00 committed by Jelle Raaijmakers
parent 4578004ad6
commit 567b1f6e7c
9 changed files with 51 additions and 21 deletions

View file

@ -456,7 +456,12 @@ public:
{
}
Variant<StringView, FunctionRef> m_function;
FunctionPointer(FunctionDefinitionRef function_definition)
: m_function(function_definition)
{
}
Variant<StringView, FunctionDefinitionRef> m_function;
protected:
void dump_tree(StringBuilder& builder) override;

View file

@ -164,7 +164,7 @@ void FunctionPointer::dump_tree(StringBuilder& builder)
[&](StringView name) {
dump_node(builder, "Func external \"{}\"", name);
},
[&](FunctionRef function) {
[&](FunctionDefinitionRef function) {
dump_node(builder, "Func local \"{}\"", function->m_name);
});
}

View file

@ -15,7 +15,7 @@ namespace JSSpecCompiler {
class CompilerPass {
public:
CompilerPass(FunctionRef function)
CompilerPass(FunctionDefinitionRef function)
: m_function(function)
{
}
@ -25,7 +25,7 @@ public:
virtual void run() = 0;
protected:
FunctionRef m_function;
FunctionDefinitionRef m_function;
};
}

View file

@ -36,7 +36,7 @@ class GenericASTPass
: public CompilerPass
, protected RecursiveASTVisitor {
public:
GenericASTPass(FunctionRef function)
GenericASTPass(FunctionDefinitionRef function)
: CompilerPass(function)
{
}

View file

@ -29,7 +29,7 @@ RecursionDecision ReferenceResolvingPass::on_entry(Tree tree)
void ReferenceResolvingPass::on_leave(Tree tree)
{
auto& functions = m_function->m_context->m_functions;
auto& functions = m_function->m_translation_unit->function_index;
if (auto reference = as<UnresolvedReference>(tree); reference) {
auto name = reference->m_name;

View file

@ -60,8 +60,8 @@ class Algorithm;
class SpecFunction;
// Function.h
class ExecutionContext;
class Function;
using FunctionRef = Function*;
struct TranslationUnit;
class FunctionDefinition;
using FunctionDefinitionRef = FunctionDefinition*;
}

View file

@ -9,9 +9,23 @@
namespace JSSpecCompiler {
Function::Function(ExecutionContext* context, StringView name, Tree ast)
: m_context(context)
, m_name(name)
FunctionDefinitionRef TranslationUnit::adopt_function(NonnullRefPtr<FunctionDefinition>&& function)
{
function->m_translation_unit = this;
function_index.set(function->m_name, make_ref_counted<FunctionPointer>(function));
FunctionDefinitionRef result = function.ptr();
function_definitions.append(move(function));
return result;
}
FunctionDeclaration::FunctionDeclaration(StringView name)
: m_name(name)
{
}
FunctionDefinition::FunctionDefinition(StringView name, Tree ast)
: FunctionDeclaration(name)
, m_ast(move(ast))
, m_return_value(make_ref_counted<VariableDeclaration>("$return"sv))
{

View file

@ -15,17 +15,27 @@
namespace JSSpecCompiler {
class ExecutionContext {
public:
HashMap<StringView, FunctionPointerRef> m_functions;
struct TranslationUnit {
FunctionDefinitionRef adopt_function(NonnullRefPtr<FunctionDefinition>&& function);
Vector<NonnullRefPtr<FunctionDefinition>> function_definitions;
HashMap<StringView, FunctionPointerRef> function_index;
};
class Function : public RefCounted<Function> {
class FunctionDeclaration : public RefCounted<FunctionDeclaration> {
public:
Function(ExecutionContext* context, StringView name, Tree ast);
FunctionDeclaration(StringView name);
ExecutionContext* m_context;
virtual ~FunctionDeclaration() = default;
TranslationUnit* m_translation_unit = nullptr;
StringView m_name;
};
class FunctionDefinition : public FunctionDeclaration {
public:
FunctionDefinition(StringView name, Tree ast);
Tree m_ast;
VariableDeclarationRef m_return_value;
HashMap<StringView, VariableDeclarationRef> m_local_variables;

View file

@ -19,12 +19,12 @@ ErrorOr<int> serenity_main(Main::Arguments)
{
using namespace JSSpecCompiler;
ExecutionContext context;
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 = context.m_functions;
auto& functions = translation_unit.function_index;
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));
@ -50,7 +50,8 @@ ErrorOr<int> serenity_main(Main::Arguments)
}
auto spec_function = maybe_function.value();
auto function = make_ref_counted<JSSpecCompiler::Function>(&context, spec_function.m_name, spec_function.m_algorithm.m_tree);
auto* function = translation_unit.adopt_function(
make_ref_counted<FunctionDefinition>(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));