/** * test_alternation_vlist_new: * @fn test_alternation_vlist_new * Tests method compilerkit_alternation_vlist_new in CompilerKitAlternation struct. * @pre None * @param None * @return void */ void test_alternation_vlist_new (void) { GObject* alt; GObject* one; GObject* two; GObject* three; g_test_message ("Testing Alternation vlist new"); g_test_timer_start (); one = compilerkit_symbol_new('a'); two = compilerkit_symbol_new('b'); three = compilerkit_symbol_new('c'); alt = compilerkit_alternation_vlist_new(one, two, three, NULL); g_assert (COMPILERKIT_IS_ALTERNATION(alt)); g_assert (one != two); g_assert (two != three); g_assert (one != three); g_assert (one != alt); g_assert (two != alt); g_assert (three != alt); g_assert (three == compilerkit_alternation_get_right(COMPILERKIT_ALTERNATION(alt))); g_assert (one == compilerkit_alternation_get_left(COMPILERKIT_ALTERNATION(compilerkit_alternation_get_left(COMPILERKIT_ALTERNATION(alt))))); g_assert (two == compilerkit_alternation_get_right(COMPILERKIT_ALTERNATION(compilerkit_alternation_get_left(COMPILERKIT_ALTERNATION(alt))))); g_object_unref (alt); // This will unref one, two and three as well g_assert_cmpfloat(g_test_timer_elapsed (), <=, 1); }
/** * compilerkit_alternation_new: * @fn compilerkit_alternation_new * @memberof CompilerKitAlternation * Construct a CompilerKitAlternation instance. * @pre GObject* are all non NULL. * @param GObject* Left side of alternation * @param GObject* Right side of alternation * @return A new CompilerKitAlternation struct, cast to GObject*. */ GObject *compilerkit_alternation_new (GObject *left, GObject *right) { CompilerKitAlternation *result = COMPILERKIT_ALTERNATION (g_object_new (COMPILERKIT_TYPE_ALTERNATION, NULL)); result->priv->left = left; result->priv->right = right; return G_OBJECT(result); }
/* ToNfa alternation. */ static GObject *to_nfa_alternation (CompilerKitVisitor *self, GObject *obj) { CompilerKitAlternation *alt; g_assert(COMPILERKIT_IS_ALTERNATION(obj)); alt = COMPILERKIT_ALTERNATION (obj); compilerkit_visitor_visit(self, compilerkit_alternation_get_left (alt)); compilerkit_visitor_visit(self, compilerkit_alternation_get_right (alt)); return NULL; }
int main (int argc, char ** argv) { GObject *alternationOne; GObject *alternationTwo; GObject *left; GObject *right; GObject *left_left; GObject *left_right; GObject *subAlternation; g_type_init(); alternationOne = compilerkit_alternation_new(compilerkit_symbol_new('a'),compilerkit_symbol_new('b')); // creates an alternation ('a' | 'b') alternationTwo = compilerkit_alternation_vlist_new(compilerkit_symbol_new('c'), compilerkit_symbol_new('d'), compilerkit_symbol_new('e'), NULL); //NULL terminated list of at least 2 GObjects //creates an alternation (('c' | 'd') | 'e') left = compilerkit_alternation_get_left(COMPILERKIT_ALTERNATION(alternationOne)); //returns the symbol 'a' right = compilerkit_alternation_get_right(COMPILERKIT_ALTERNATION(alternationOne)); //returns the symbol 'b' subAlternation = compilerkit_alternation_get_left(COMPILERKIT_ALTERNATION(alternationTwo)); //returns the alternation ('c' | 'd') left_left = compilerkit_alternation_get_left(COMPILERKIT_ALTERNATION(compilerkit_alternation_get_left(COMPILERKIT_ALTERNATION(alternationTwo)))); //returns the symbol 'c' left_right = compilerkit_alternation_get_right(COMPILERKIT_ALTERNATION(compilerkit_alternation_get_left(COMPILERKIT_ALTERNATION(alternationTwo)))); //returns the symbol 'd' right = compilerkit_alternation_get_right(COMPILERKIT_ALTERNATION(alternationTwo)); //returns the symbol 'e' g_object_unref (alternationOne); //Also de-references the parts (left and right) g_object_unref (alternationTwo); //Also de-references the parts (subAlternation, left_left, left_right, and right) }
/** * compilerkit_alternation_dispose: * @fn compilerkit_alternation_dispose * Reverse what compilerkit_alternation_init allocated. * @pre GObject is not NULL. * @param GObject* An object to dispose. * @return void */ static void compilerkit_alternation_dispose (GObject* object) { CompilerKitAlternation *self = COMPILERKIT_ALTERNATION (object); CompilerKitAlternationPrivate* priv; priv = COMPILERKIT_ALTERNATION_GET_PRIVATE (self); g_object_unref (priv->left); g_object_unref (priv->right); G_OBJECT_CLASS (compilerkit_alternation_parent_class)->dispose (object); }
/** * test_alternation_get_left_and_right: * @fn test_alternation_get_left_and_right * Tests method compilerkit_alternation_get_left and compilerkit_alternation_get_right in CompilerKitAlternation struct. * @pre None * @param None * @return void */ void test_alternation_get_left_and_right (void) { GObject* alt; GObject* left; GObject* right; g_test_message ("Testing Alternation get_left and get_right"); g_test_timer_start (); left = compilerkit_symbol_new('a'); right = compilerkit_symbol_new('b'); alt = compilerkit_alternation_new(left, right); g_assert (COMPILERKIT_IS_ALTERNATION(alt)); g_assert (left == compilerkit_alternation_get_left(COMPILERKIT_ALTERNATION(alt))); g_assert (right == compilerkit_alternation_get_right(COMPILERKIT_ALTERNATION(alt))); g_assert (left != alt); g_assert (right != alt); g_object_unref (alt); // This will unref left and right as well g_assert_cmpfloat(g_test_timer_elapsed (), <=, 1); }
/* StringBuilder alternation. */ static GObject *string_builder_alternation (CompilerKitVisitor *self, GObject *obj) { CompilerKitAlternation *alt; GString *str = (GString *) compilerkit_visitor_get_state(self); g_assert(COMPILERKIT_IS_ALTERNATION(obj)); alt = COMPILERKIT_ALTERNATION (obj); compilerkit_visitor_visit(self, compilerkit_alternation_get_left (alt)); g_string_append_c(str, '|'); compilerkit_visitor_visit(self, compilerkit_alternation_get_right (alt)); return NULL; }