Ver código fonte

JSSpecCompiler: Introduce Function and ExecutionContext classes

Currently, they are not extremely useful, but the plan is to store
all function-local state in JSSpecCompiler::Function and all
"translation unit" state in ExecutionContext.
Dan Klishch 1 ano atrás
pai
commit
cd8f4aaa7d

+ 2 - 2
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/AST/AST.h

@@ -312,11 +312,11 @@ protected:
 class FunctionPointer : public Node {
 public:
     FunctionPointer(StringView function_name)
-        : m_function_name(function_name)
+        : m_function(function_name)
     {
     }
 
-    StringView m_function_name;
+    Variant<StringView, FunctionRef> m_function;
 
 protected:
     void dump_tree(StringBuilder& builder) override;

+ 8 - 1
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/AST/ASTPrinting.cpp

@@ -8,6 +8,7 @@
 #include <AK/TemporaryChange.h>
 
 #include "AST/AST.h"
+#include "Function.h"
 
 namespace JSSpecCompiler {
 
@@ -131,7 +132,13 @@ void Variable::dump_tree(StringBuilder& builder)
 
 void FunctionPointer::dump_tree(StringBuilder& builder)
 {
-    dump_node(builder, "Func {}", m_function_name);
+    m_function.visit(
+        [&](StringView name) {
+            dump_node(builder, "Func external \"{}\"", name);
+        },
+        [&](FunctionRef function) {
+            dump_node(builder, "Func local \"{}\"", function->m_name);
+        });
 }
 
 }

+ 1 - 0
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/CMakeLists.txt

@@ -5,6 +5,7 @@ set(SOURCES
     Parser/SpecParser.cpp
     Parser/TextParser.cpp
     Parser/XMLUtils.cpp
+    Function.cpp
     main.cpp
 )
 

+ 6 - 0
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Forward.h

@@ -35,6 +35,7 @@ class FunctionCall;
 class SlotName;
 class Variable;
 class FunctionPointer;
+using FunctionPointerRef = NonnullRefPtr<FunctionPointer>;
 
 // Parser/SpecParser.h
 class AlgorithmStep;
@@ -42,4 +43,9 @@ class AlgorithmStepList;
 class Algorithm;
 class SpecFunction;
 
+// Function.h
+class ExecutionContext;
+class Function;
+using FunctionRef = Function*;
+
 }

+ 19 - 0
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.cpp

@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include "Function.h"
+#include "AST/AST.h"
+
+namespace JSSpecCompiler {
+
+Function::Function(ExecutionContext* context, StringView name, Tree ast)
+    : m_context(context)
+    , m_name(name)
+    , m_ast(move(ast))
+{
+}
+
+}

+ 32 - 0
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.h

@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/HashMap.h>
+#include <AK/RefCounted.h>
+#include <AK/RefPtr.h>
+#include <AK/StringView.h>
+
+#include "Forward.h"
+
+namespace JSSpecCompiler {
+
+class ExecutionContext {
+public:
+    HashMap<StringView, FunctionPointerRef> m_functions;
+};
+
+class Function : public RefCounted<Function> {
+public:
+    Function(ExecutionContext* context, StringView name, Tree ast);
+
+    ExecutionContext* m_context;
+    StringView m_name;
+    Tree m_ast;
+};
+
+}

+ 3 - 3
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/SpecParser.cpp

@@ -103,11 +103,11 @@ ParseErrorOr<Algorithm> Algorithm::create(XML::Node const* node)
     return algorithm;
 }
 
-ParseErrorOr<Function> Function::create(XML::Node const* element)
+ParseErrorOr<SpecFunction> SpecFunction::create(XML::Node const* element)
 {
     VERIFY(element->as_element().name == tag_emu_clause);
 
-    Function result;
+    SpecFunction result;
     result.m_id = TRY(get_attribute_by_name(element, attribute_id));
     result.m_name = TRY(get_attribute_by_name(element, attribute_aoid));
 
@@ -156,7 +156,7 @@ ParseErrorOr<Function> Function::create(XML::Node const* element)
     return result;
 }
 
-ParseErrorOr<void> Function::parse_definition(XML::Node const* element)
+ParseErrorOr<void> SpecFunction::parse_definition(XML::Node const* element)
 {
     auto tokens = TRY(tokenize_tree(element));
     TextParser parser(tokens.tokens, element);

+ 2 - 2
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/SpecParser.h

@@ -40,13 +40,13 @@ public:
     Tree m_tree = error_tree;
 };
 
-class Function {
+class SpecFunction {
 public:
     struct Argument {
         StringView name;
     };
 
-    static ParseErrorOr<Function> create(XML::Node const* element);
+    static ParseErrorOr<SpecFunction> create(XML::Node const* element);
 
     ParseErrorOr<void> parse_definition(XML::Node const* element);
 

+ 1 - 1
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/main.cpp

@@ -25,7 +25,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
     }
     auto document = maybe_document.release_value();
 
-    auto maybe_function = JSSpecCompiler::Function::create(&document.root());
+    auto maybe_function = JSSpecCompiler::SpecFunction::create(&document.root());
     if (maybe_function.is_error()) {
         outln("{}", maybe_function.error()->to_string());
         return 1;