X-Git-Url: http://git.cascardo.info/?p=cascardo%2Fgrammar.git;a=blobdiff_plain;f=grammar.c;h=72248e0a06d6b27938dbc7b303c7c2a2031b7953;hp=b73cd399640c74c508e6b9373b017938b9dba1d0;hb=fecd14f5105395f94825326ea177e77956af2b04;hpb=2da4a1b6506f7eb6949b504ac1f93c393f4f7302 diff --git a/grammar.c b/grammar.c index b73cd39..72248e0 100644 --- a/grammar.c +++ b/grammar.c @@ -14,6 +14,23 @@ symbol_t* symbol_new (gboolean terminal, gint value) return symbol; } +guint symbol_hash (gconstpointer data) +{ + symbol_t* symbol; + symbol = (symbol_t*) data; + return g_direct_hash ((gpointer)symbol->value); +} + +gboolean symbol_equal (gconstpointer data1, gconstpointer data2) +{ + symbol_t* symbol1; + symbol_t* symbol2; + symbol1 = (symbol_t*) data1; + symbol2 = (symbol_t*) data2; + return symbol1->value == symbol2->value && + symbol1->terminal == symbol2->terminal; +} + rule_t* rule_new () { rule_t* rule; @@ -27,28 +44,34 @@ void rule_append (rule_t* rule, symbol_t* right) rule->right = g_list_append (rule->right, right); } -guint symbol_hash (gconstpointer data) +void rule_delete (rule_t* rule) { - symbol_t* symbol; - symbol = (symbol_t*) data; - return g_direct_hash ((gpointer)symbol->value); + GList* l; + for (l = g_list_first (rule->right); l != NULL; l = g_list_next (l)) + { + g_free (l->data); + } + g_list_free (rule->right); + g_free (rule); } -gboolean symbol_equal (gconstpointer data1, gconstpointer data2) +void rules_delete (GList** list) { - symbol_t* symbol1; - symbol_t* symbol2; - symbol1 = (symbol_t*) data1; - symbol2 = (symbol_t*) data2; - return symbol1->value == symbol2->value && - symbol1->terminal == symbol2->terminal; + GList* l; + for (l = g_list_first (*list); l != NULL; l = g_list_next (l)) + { + rule_delete (l->data); + } + g_list_free (*list); + g_free (list); } static void grammar_init (GTypeInstance* instance, gpointer g_class) { Grammar* self = GRAMMAR(instance); self->grammar = g_hash_table_new_full (symbol_hash, symbol_equal, - g_free, g_free); + g_free, + (GDestroyNotify) rules_delete); } static void grammar_finalize (GObject* obj)