Skip to content

Commit 95590a2

Browse files
committed
start adding multiple RHS
1 parent 5a46851 commit 95590a2

File tree

8 files changed

+1102
-1042
lines changed

8 files changed

+1102
-1042
lines changed

TODO

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
- remove remove prefs workspace stuff?
2-
3-
things like max heap size should be settable ... maybe pres are useful?
41

52
- add multiple definitions, finish function argument destructuring
63

@@ -32,6 +29,46 @@
3229
- error if defs don't all have the same number of args
3330
- error if a def with no destructuring isn't the last def
3431

32+
test code:
33+
34+
fact 1 = 1;
35+
fact n = n * fact (n - 1);
36+
main = fact 3;
37+
38+
parses to:
39+
40+
fact $$arg0 = 1
41+
{
42+
$$patt0 = 1;
43+
}
44+
45+
$$fact_rhs0 n = n * fact (n - 1);
46+
47+
main = fact 3;
48+
49+
next:
50+
51+
- secret_build() needs to loop over next_rhs as well
52+
53+
- compile_heap() need to work for multiple RHS
54+
55+
- check set of RHSes follow rules above
56+
57+
- final one needs to have no patterns, or we synthesise a default rhs
58+
which flags a "pattern match failed" error
59+
60+
- generate pattern match test def
61+
62+
see compile_pattern_condition(), compile_pattern_error()
63+
64+
- chain RHSes together with pattern match tests
65+
66+
where do we make the code for "does object match pattern?"
67+
68+
- remove remove prefs workspace stuff?
69+
70+
things like max heap size should be settable ... maybe pres are useful?
71+
3572
- try < > in the image titlebar
3673

3774
seems to get stuck on eg. mp3 files

src/compile.c

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,29 @@
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);

src/dump.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
*/
3535

3636
/*
37-
#define DEBUG
3837
*/
38+
#define DEBUG
3939

4040
/* Dump a binary operator.
4141
*/
@@ -477,6 +477,15 @@ dump_symbol(Symbol *sym)
477477
IOBJECT(sym)->name, sym->ndirtychildren);
478478
printf("%s->leaf = %s\n",
479479
IOBJECT(sym)->name, bool_to_char(sym->leaf));
480+
printf("%s->generated = %s\n",
481+
IOBJECT(sym)->name, bool_to_char(sym->generated));
482+
483+
if (!sym->generated && sym->next_rhs) {
484+
printf("%s->next_rhs = ", IOBJECT(sym)->name);
485+
for (Symbol *p = sym->next_rhs; p; p = sym->next_rhs)
486+
printf("%s ", IOBJECT(sym)->name);
487+
printf("\n");
488+
}
480489

481490
printf("%s->tool = kit ", IOBJECT(sym)->name);
482491
if (sym->tool)

src/main-batch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ main(int argc, char **argv)
469469
main_error_exit("%s", _("no \"main\" found"));
470470
}
471471

472-
main_shutdown();
472+
//main_shutdown();
473473

474474
return 0;
475475
}

0 commit comments

Comments
 (0)