mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 07:32:44 +00:00 
			
		
		
		
	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.
This commit is contained in:
		
							parent
							
								
									f05d291b41
								
							
						
					
					
						commit
						cd8f4aaa7d
					
				
					 9 changed files with 74 additions and 9 deletions
				
			
		|  | @ -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,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); | ||||
|         }); | ||||
| } | ||||
| 
 | ||||
| } | ||||
|  |  | |||
|  | @ -5,6 +5,7 @@ set(SOURCES | |||
|     Parser/SpecParser.cpp | ||||
|     Parser/TextParser.cpp | ||||
|     Parser/XMLUtils.cpp | ||||
|     Function.cpp | ||||
|     main.cpp | ||||
| ) | ||||
| 
 | ||||
|  |  | |||
|  | @ -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
									
								
								Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -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
									
								
								Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -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; | ||||
| }; | ||||
| 
 | ||||
| } | ||||
|  | @ -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); | ||||
|  |  | |||
|  | @ -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); | ||||
| 
 | ||||
|  |  | |||
|  | @ -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; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Klishch
						Dan Klishch