2828 */
2929
3030/*
31- #define DEBUG_RESOLVE
3231 */
32+ #define DEBUG_RESOLVE
3333
3434/* regular (and very slow) sanity checks on symbols ... needs DEBUG in
3535 * symbol.c as well
36- #define DEBUG_SANITY
3736 */
37+ #define DEBUG_SANITY
3838
3939/* count how many nodes we find with common sub-expression removal.
40- #define DEBUG_COMMON
4140 */
41+ #define DEBUG_COMMON
4242
4343/* show what everything compiled to
44- #define DEBUG_RESULT
4544 */
45+ #define DEBUG_RESULT
4646
4747/* trace list comp compile
48- #define DEBUG_LCOMP
4948 */
49+ #define DEBUG_LCOMP
5050
5151/* trace pattern LHS generation
52- #define DEBUG_PATTERN
5352 */
53+ #define DEBUG_PATTERN
5454
5555/*
5656#define DEBUG
@@ -1468,6 +1468,29 @@ compile_remove_subexpr(Compile *compile, PElement *root)
14681468 return TRUE;
14691469}
14701470
1471+ /* This is a def with multiple RHS. Check that:
1472+ *
1473+ * - all defs hjave the same number of args
1474+ * - no more then def RHS has no pattern matching args
1475+ * - if there is a no-pattern def, it must be the last one
1476+ */
1477+ static gboolean
1478+ compile_check_rhs (Compile * compile )
1479+ {
1480+ g_assert (!compile -> sym -> generated );
1481+ g_assert (compile -> sym -> next_rhs );
1482+
1483+ int nargs = -1 ;
1484+ for (Symbol * p = compile -> sym ; p ; p = p -> next_rhs )
1485+ if (nargs == -1 )
1486+ nargs = p -> nargs ;
1487+ else if (nargs != p -> nargs ) {
1488+ error ...
1489+ }
1490+
1491+ return TRUE;
1492+ }
1493+
14711494/* Top-level compiler driver.
14721495 */
14731496
@@ -1483,6 +1506,14 @@ compile_heap(Compile *compile)
14831506 if (compile -> sym -> placeholder )
14841507 return NULL ;
14851508
1509+ /* There could be multiple RHS ... if this is the first RHS, check the
1510+ * rules around parameters and patterns.
1511+ */
1512+ if (!compile -> sym -> generated &&
1513+ compile -> sym -> next_rhs &&
1514+ !compile_check_rhs (compile ))
1515+ return compile -> sym ;
1516+
14861517 PEPOINTE (& base , & compile -> base );
14871518
14881519 /* Is there an existing function base? GC it away.
@@ -1570,7 +1601,8 @@ compile_symbol_sub(Symbol *sym)
15701601{
15711602 Compile * compile ;
15721603
1573- if (sym -> expr && (compile = sym -> expr -> compile ))
1604+ if (sym -> expr &&
1605+ (compile = sym -> expr -> compile ))
15741606 if (compile_object_sub (compile ))
15751607 return sym ;
15761608
@@ -1652,14 +1684,12 @@ compile_check_i18n(Compile *compile, ParseNode *pn)
16521684 static GHashTable * msgid = NULL ;
16531685
16541686 if (!msgid )
1655- msgid = g_hash_table_new (
1656- g_str_hash , g_str_equal );
1687+ msgid = g_hash_table_new (g_str_hash , g_str_equal );
16571688
16581689 if (!g_hash_table_lookup (msgid , text )) {
16591690 char buf [MAX_STRSIZE ];
16601691
1661- g_hash_table_insert (msgid ,
1662- (void * ) text , NULL );
1692+ g_hash_table_insert (msgid , (void * ) text , NULL );
16631693 my_strecpy (buf , text , TRUE);
16641694 printf ("msgid \"%s\"\n" , buf );
16651695 printf ("msgstr \"\"\n\n" );
@@ -2458,11 +2488,9 @@ compile_pattern_access(Compile *compile,
24582488 * item in the trail in the list of elements.
24592489 */
24602490 c .type = PARSE_CONST_NUM ;
2461- c .val .num = g_slist_index (trail [i ]-> elist ,
2462- trail [i + 1 ]);
2491+ c .val .num = g_slist_index (trail [i ]-> elist , trail [i + 1 ]);
24632492 right = tree_const_new (compile , c );
2464- node = tree_binop_new (compile ,
2465- BI_SELECT , node , right );
2493+ node = tree_binop_new (compile , BI_SELECT , node , right );
24662494 break ;
24672495
24682496 default :
@@ -2501,34 +2529,27 @@ compile_pattern_condition(Compile *compile,
25012529 /* Generate is_complex x.
25022530 */
25032531 left = tree_leaf_new (compile , "is_complex" );
2504- right = compile_pattern_access (compile ,
2505- leaf , trail , i );
2532+ right = compile_pattern_access (compile , leaf , trail , i );
25062533 node2 = tree_appl_new (compile , left , right );
25072534
2508- node = tree_binop_new (compile ,
2509- BI_LAND , node2 , node );
2535+ node = tree_binop_new (compile , BI_LAND , node2 , node );
25102536 break ;
25112537
25122538 case BI_CONS :
25132539 /* Generate is_list x && x != [].
25142540 */
25152541 left = tree_leaf_new (compile , "is_list" );
2516- right = compile_pattern_access (compile ,
2517- leaf , trail , i );
2542+ right = compile_pattern_access (compile , leaf , trail , i );
25182543 node2 = tree_appl_new (compile , left , right );
25192544
2520- node = tree_binop_new (compile ,
2521- BI_LAND , node2 , node );
2545+ node = tree_binop_new (compile , BI_LAND , node2 , node );
25222546
2523- left = compile_pattern_access (compile ,
2524- leaf , trail , i );
2547+ left = compile_pattern_access (compile , leaf , trail , i );
25252548 n .type = PARSE_CONST_ELIST ;
25262549 right = tree_const_new (compile , n );
2527- node2 = tree_binop_new (compile ,
2528- BI_NOTEQ , left , right );
2550+ node2 = tree_binop_new (compile , BI_NOTEQ , left , right );
25292551
2530- node = tree_binop_new (compile ,
2531- BI_LAND , node , node2 );
2552+ node = tree_binop_new (compile , BI_LAND , node , node2 );
25322553 break ;
25332554
25342555 default :
@@ -2540,8 +2561,7 @@ compile_pattern_condition(Compile *compile,
25402561 /* Generate is_list x && is_list_len n x.
25412562 */
25422563 left = tree_leaf_new (compile , "is_list" );
2543- right = compile_pattern_access (compile ,
2544- leaf , trail , i );
2564+ right = compile_pattern_access (compile , leaf , trail , i );
25452565 node2 = tree_appl_new (compile , left , right );
25462566
25472567 node = tree_binop_new (compile , BI_LAND , node2 , node );
@@ -2551,8 +2571,7 @@ compile_pattern_condition(Compile *compile,
25512571 n .val .num = g_slist_length (trail [i ]-> elist );
25522572 right = tree_const_new (compile , n );
25532573 left = tree_appl_new (compile , left , right );
2554- right = compile_pattern_access (compile ,
2555- leaf , trail , i );
2574+ right = compile_pattern_access (compile , leaf , trail , i );
25562575 node2 = tree_appl_new (compile , left , right );
25572576
25582577 node = tree_binop_new (compile , BI_LAND , node , node2 );
@@ -2561,8 +2580,7 @@ compile_pattern_condition(Compile *compile,
25612580 case NODE_CONST :
25622581 /* Generate x == n.
25632582 */
2564- left = compile_pattern_access (compile ,
2565- leaf , trail , i );
2583+ left = compile_pattern_access (compile , leaf , trail , i );
25662584 right = tree_const_new (compile , trail [i ]-> con );
25672585 node2 = tree_binop_new (compile , BI_EQ , left , right );
25682586
@@ -2577,8 +2595,7 @@ compile_pattern_condition(Compile *compile,
25772595 n .val .str = g_strdup (trail [i ]-> tag );
25782596 right = tree_const_new (compile , n );
25792597 node2 = tree_appl_new (compile , left , right );
2580- right = compile_pattern_access (compile ,
2581- leaf , trail , i );
2598+ right = compile_pattern_access (compile , leaf , trail , i );
25822599 node2 = tree_appl_new (compile , node2 , right );
25832600
25842601 node = tree_binop_new (compile , BI_LAND , node2 , node );
0 commit comments