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(alternationOne); //returns the symbol 'a' right = compilerkit_alternation_get_right(alternationOne); //returns the symbol 'b' subAlternation = compilerkit_alternation_get_left(alternationTwo); //returns the alternation ('c' | 'd') left_left = compilerkit_alternation_get_left(compilerkit_alternation_get_left(alternationTwo)); //returns the symbol 'c' left_right = compilerkit_alternation_get_right(compilerkit_alternation_get_left(alternationTwo)); //returns the symbol 'd' right = compilerkit_alternation_get_right(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) }
/** * 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); }
/* 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; }
/* 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; }
/** * 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); }