New test.
[mono.git] / mono / monoburg / sample.brg
1 /* 
2  * This header (everything before the first "%%") is copied 
3  * directly to the output.
4  */
5
6 #include <glib.h>
7 #include <stdio.h>
8 #include <string.h>
9
10 #define MBTREE_TYPE  MBTree
11
12 typedef struct _MBTree MBTree;
13 struct _MBTree {
14         guint16 op;
15         MBTree *left, *right;
16         gpointer state;
17 };
18
19 %% these are the monoburg definition
20 #
21 # This is the start of the definitions
22 # comments start with a '#' as first line character
23 #
24
25 #
26 # we must fisrt define the terminals
27 # with or without numbers
28
29 %term Assign Constant Fetch=3 Four=8 Mul=5 Plus=6 AltFetch=7
30
31 #
32 # optional start nonterminal
33 #
34 %start reg
35
36 con:  Constant                0 
37
38 con:  Four                    0 
39
40 addr: con                     0 
41
42 addr: Plus(con,reg)           
43 {
44         int ern = mono_burg_rule (tree->state, MB_NTERM_addr);
45         printf ("%s\n", mono_burg_rule_string [ern]);
46 } cost 
47 {  
48   return 1;
49 }
50
51 addr: Plus(con,Mul(Four,reg)) 2 
52 {
53         int ern = mono_burg_rule (tree->state, MB_NTERM_addr);
54         printf ("%s\n", mono_burg_rule_string [ern]);
55
56
57 reg:  AltFetch(addr),
58 reg:  Fetch(addr)             1 
59 {
60         int ern = mono_burg_rule (tree->state, MB_NTERM_reg);
61         printf ("%s\n", mono_burg_rule_string [ern]);
62 }
63
64 reg:  Assign(addr,reg)        1 
65 {
66         int ern = mono_burg_rule (tree->state, MB_NTERM_reg);
67         printf ("%s\n", mono_burg_rule_string [ern]);
68 }
69
70 %% the rest is also copied directly to the output
71 /* everything below the second "%%" is also copied directly
72  * to the output file.
73  */
74
75 static MBTree *
76 create_tree (int op, MBTree *left, MBTree *right)
77 {
78         MBTree *t = g_new0 (MBTree, 1);
79         
80         t->op = op;
81         t->left = left;
82         t->right = right;
83
84         return t;
85 }
86
87 static void
88 reduce (MBTree *tree, int goal) 
89 {
90         MBTree *kids[10];
91         int ern = mono_burg_rule (tree->state, goal);
92         guint16 *nts = mono_burg_nts [ern];
93         int i, n;
94
95         mono_burg_kids (tree, ern, kids);
96
97         // printf ("TEST %d %d %s %d\n", goal, ern, mono_burg_rule_string [ern], nts [0]);
98         
99         for (i = 0; nts [i]; i++) 
100                 reduce (kids [i], nts [i]);
101
102         n = (tree->left != NULL) + (tree->right != NULL);
103
104         if (n) { /* not a terminal */
105           // printf ("XXTE %s %d\n", mono_burg_rule_string [ern], n);
106                 if (mono_burg_func [ern])
107                         mono_burg_func [ern] (tree, NULL);
108                 else
109                         g_warning ("no code for rule %s\n", 
110                                    mono_burg_rule_string [ern]);
111         } else {
112                 if (mono_burg_func [ern])
113                         g_warning ("unused code in rule %s\n", 
114                                    mono_burg_rule_string [ern]);
115         }
116 }
117
118 int
119 main ()
120 {
121         MBTree *t, *l, *r;
122         MBState *s;
123
124         l = create_tree (MB_TERM_Constant, NULL, NULL);
125
126         r = create_tree (MB_TERM_Fetch, l, NULL);
127         l = create_tree (MB_TERM_Constant, NULL, NULL);
128
129         r = create_tree (MB_TERM_Assign, l, r);
130         l = create_tree (MB_TERM_Four, NULL, NULL);
131
132         r = create_tree (MB_TERM_Mul, l, r);
133         l = create_tree (MB_TERM_Constant, NULL, NULL);
134
135         l = create_tree (MB_TERM_Plus, l, r);
136
137         t = create_tree (MB_TERM_Fetch, l, NULL);
138
139         s = mono_burg_label (t, NULL);
140
141         g_assert (s);
142
143         reduce (t, MB_NTERM_reg);
144
145         return 0;
146 }