From eb320acbcdd58dcc5b7be9746363e6d41b6d37ee Mon Sep 17 00:00:00 2001 From: Sara Date: Thu, 26 Mar 2026 14:53:21 +0100 Subject: [PATCH] chore: progress on generative grammar --- modules/generative_grammar/symbol.cpp | 24 ++++++++++++++++-------- modules/generative_grammar/symbol.h | 9 ++++----- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/modules/generative_grammar/symbol.cpp b/modules/generative_grammar/symbol.cpp index 3101579e61..41bb56e6c0 100644 --- a/modules/generative_grammar/symbol.cpp +++ b/modules/generative_grammar/symbol.cpp @@ -1,6 +1,7 @@ #include "generative_grammar/symbol.h" #include "core/error/error_macros.h" #include "core/object/class_db.h" +#include "core/templates/hash_map.h" void Symbol::_bind_methods() { BIND_HPROPERTY(Variant::INT, symbol, PROPERTY_HINT_ENUM, Type_hint()); @@ -8,7 +9,7 @@ void Symbol::_bind_methods() { } bool Symbol::is_terminal() const { - switch (this->type) { + switch (this->symbol) { default: return false; } @@ -39,11 +40,11 @@ TypedArray> Symbol::get_children_array() const { } void Rule::_bind_methods() { - BIND_HPROPERTY(Variant::OBJECT, match, PROPERTY_HINT_RESOURCE_TYPE, "Symbol"); + BIND_HPROPERTY(Variant::OBJECT, pattern, PROPERTY_HINT_RESOURCE_TYPE, "Symbol"); BIND_HPROPERTY(Variant::OBJECT, result, PROPERTY_HINT_RESOURCE_TYPE, "Symbol"); } -bool Rule::symbols_match(Ref match, Ref pattern) { +bool Rule::match_symbols(Ref match, Ref pattern, HashMap, Ref> &matches) { if (match == pattern) { return true; // reference match or null match } @@ -57,14 +58,11 @@ bool Rule::symbols_match(Ref match, Ref pattern) { if (match->get_children().size() < pattern->get_children().size()) { return false; // too little children to be able to match } - if ((match->get_parent() == nullptr) != (pattern->get_parent() == nullptr)) { - return false; // mismatch in existence of incoming edge - } // check for matching outgoing edges for (Ref pattern_symbol : pattern->get_children()) { bool match_found{ false }; for (Ref match_symbol : match->get_children()) { - if (symbols_match(match_symbol, pattern_symbol)) { + if (match_symbols(match_symbol, pattern_symbol, matches)) { match_found = true; break; } @@ -73,8 +71,18 @@ bool Rule::symbols_match(Ref match, Ref pattern) { return false; } } + matches.insert(pattern, match); return true; } -bool Rule::matches(Ref graph) { +bool Rule::try_apply(Ref match) { + // identify corresponding symbols + HashMap, Ref> symbols{}; + if (!match_symbols(match, this->pattern, symbols)) { + return false; + } + // clear matching edges + for (KeyValue, Ref> kvp : symbols) { + } + return true; } diff --git a/modules/generative_grammar/symbol.h b/modules/generative_grammar/symbol.h index e9450906cc..287ab73010 100644 --- a/modules/generative_grammar/symbol.h +++ b/modules/generative_grammar/symbol.h @@ -1,6 +1,7 @@ #pragma once #include "core/io/resource.h" +#include "core/templates/hash_map.h" #include "core/templates/hash_set.h" #include "core/variant/type_info.h" #include "core/variant/typed_array.h" @@ -32,18 +33,16 @@ MAKE_TYPE_INFO(Symbol::Type, Variant::INT); struct Rule : public Resource { GDCLASS(Rule, Resource); static void _bind_methods(); - bool symbols_match(Ref match, Ref pattern); + bool match_symbols(Ref match, Ref pattern, HashMap, Ref> &matches); public: - bool matches(Ref graph); - void apply(Ref graph); bool try_apply(Ref graph); private: - Ref match{}; + Ref pattern{}; Ref result{}; public: - GET_SET_FNS(Ref, match); + GET_SET_FNS(Ref, pattern); GET_SET_FNS(Ref, result); };