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

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: protected:
void dump_tree(StringBuilder& builder) override; void dump_tree(StringBuilder& builder) override;

View file

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

View file

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

View file

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

View file

@ -29,7 +29,7 @@ RecursionDecision ReferenceResolvingPass::on_entry(Tree tree)
void ReferenceResolvingPass::on_leave(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) { if (auto reference = as<UnresolvedReference>(tree); reference) {
auto name = reference->m_name; auto name = reference->m_name;

View file

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

View file

@ -9,9 +9,23 @@
namespace JSSpecCompiler { namespace JSSpecCompiler {
Function::Function(ExecutionContext* context, StringView name, Tree ast) FunctionDefinitionRef TranslationUnit::adopt_function(NonnullRefPtr<FunctionDefinition>&& function)
: m_context(context) {
, m_name(name) 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_ast(move(ast))
, m_return_value(make_ref_counted<VariableDeclaration>("$return"sv)) , m_return_value(make_ref_counted<VariableDeclaration>("$return"sv))
{ {

View file

@ -15,17 +15,27 @@
namespace JSSpecCompiler { namespace JSSpecCompiler {
class ExecutionContext { struct TranslationUnit {
public: FunctionDefinitionRef adopt_function(NonnullRefPtr<FunctionDefinition>&& function);
HashMap<StringView, FunctionPointerRef> m_functions;
Vector<NonnullRefPtr<FunctionDefinition>> function_definitions;
HashMap<StringView, FunctionPointerRef> function_index;
}; };
class Function : public RefCounted<Function> { class FunctionDeclaration : public RefCounted<FunctionDeclaration> {
public: 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; StringView m_name;
};
class FunctionDefinition : public FunctionDeclaration {
public:
FunctionDefinition(StringView name, Tree ast);
Tree m_ast; Tree m_ast;
VariableDeclarationRef m_return_value; VariableDeclarationRef m_return_value;
HashMap<StringView, VariableDeclarationRef> m_local_variables; HashMap<StringView, VariableDeclarationRef> m_local_variables;

View file

@ -19,12 +19,12 @@ ErrorOr<int> serenity_main(Main::Arguments)
{ {
using namespace JSSpecCompiler; using namespace JSSpecCompiler;
ExecutionContext context; TranslationUnit translation_unit;
// Functions referenced in DifferenceISODate // Functions referenced in DifferenceISODate
// TODO: This is here just for testing. In a long run, we need some place, which is not // 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. // `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("CompareISODate"sv, make_ref_counted<FunctionPointer>("CompareISODate"sv));
functions.set("CreateDateDurationRecord"sv, make_ref_counted<FunctionPointer>("CreateDateDurationRecord"sv)); functions.set("CreateDateDurationRecord"sv, make_ref_counted<FunctionPointer>("CreateDateDurationRecord"sv));
functions.set("AddISODate"sv, make_ref_counted<FunctionPointer>("AddISODate"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 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) for (auto const& argument : spec_function.m_arguments)
function->m_local_variables.set(argument.name, make_ref_counted<VariableDeclaration>(argument.name)); function->m_local_variables.set(argument.name, make_ref_counted<VariableDeclaration>(argument.name));