1 | /* File: infix.c |
---|
2 | * Author: Paul S. Wang (based on code by Simon Gray) |
---|
3 | * Date: 9/16/96 |
---|
4 | */ |
---|
5 | |
---|
6 | #include "MP.h" |
---|
7 | #include "node.h" |
---|
8 | #include "label.h" |
---|
9 | #include "leaf.h" |
---|
10 | #include "infix.h" |
---|
11 | |
---|
12 | static void get_op_string(/* node_t dnp, char buf[] */); |
---|
13 | |
---|
14 | /****************************************************************** |
---|
15 | * FUNCTION: display_node_infix |
---|
16 | * ARGUMENT: dnp - a pointer to the root of a subtree. |
---|
17 | * RETURN: none |
---|
18 | * PURPOSE: Output the node dnp, not including annotations, |
---|
19 | * to stdout in an infix format. |
---|
20 | ******************************************************************/ |
---|
21 | |
---|
22 | void display_node_infix(dnp, p_op) |
---|
23 | node_t *dnp; |
---|
24 | char p_op; |
---|
25 | { node_t **p; |
---|
26 | int i; |
---|
27 | char op[16]; |
---|
28 | /* display simple data node */ |
---|
29 | if ( display_data_node(dnp) ) return; |
---|
30 | |
---|
31 | /* get operator */ |
---|
32 | get_op_string(dnp, op); /* sets op to string */ |
---|
33 | |
---|
34 | |
---|
35 | /* display infix exprssion */ |
---|
36 | if ((dnp->num_children > 0) && (dnp->type != MP_RawType)) |
---|
37 | p = dnp->child_list; |
---|
38 | else return; |
---|
39 | |
---|
40 | /* arithemetic ops with more than 1 oprands */ |
---|
41 | if ( (dnp->num_children > 1) |
---|
42 | && (*(op) == '+' || *(op) == '-' |
---|
43 | || *(op) == '*' || *(op) == '/' || *(op) == '^') |
---|
44 | ) |
---|
45 | { switch(*op) |
---|
46 | { case '+': |
---|
47 | if ( p_op != '+' ) printf("("); |
---|
48 | for (i = 0; i < dnp->num_children; i++, p++) |
---|
49 | { if ( i > 0 ) |
---|
50 | { if ( is_neg(*p) ) |
---|
51 | { printf(" - "); |
---|
52 | display_node_infix(*((*p)->child_list), *op); |
---|
53 | } |
---|
54 | else |
---|
55 | { printf(" + "); |
---|
56 | display_node_infix(*p, *op); |
---|
57 | } |
---|
58 | } |
---|
59 | else |
---|
60 | display_node_infix(*p, *op); |
---|
61 | } |
---|
62 | if ( p_op != '+' ) printf(")"); |
---|
63 | break; |
---|
64 | case '-': |
---|
65 | if ( p_op != '+' ) printf("("); |
---|
66 | display_node_infix(p[0], '+'); |
---|
67 | printf(" - "); |
---|
68 | display_node_infix(p[1], '-'); |
---|
69 | if ( p_op != '+' ) printf(")"); |
---|
70 | break; |
---|
71 | case '*': |
---|
72 | if ( p_op == '/' || p_op == '^') printf("("); |
---|
73 | for (i = 0; i < dnp->num_children-1; i++, p++) |
---|
74 | { display_node_infix(*p, '*'); |
---|
75 | printf(" ", op); |
---|
76 | } |
---|
77 | display_node_infix(*p, '*'); |
---|
78 | if ( p_op == '/' || p_op == '^') printf(")"); |
---|
79 | break; |
---|
80 | case '/': |
---|
81 | if ( p_op == '^' || p_op == '/') printf("("); |
---|
82 | display_node_infix(p[0], '*'); |
---|
83 | printf(" / "); |
---|
84 | display_node_infix(p[1], '/'); |
---|
85 | if ( p_op == '^' || p_op == '/') printf(")"); |
---|
86 | break; |
---|
87 | case '^': |
---|
88 | if ( p_op == '^') printf("("); |
---|
89 | display_node_infix(p[0], '^'); |
---|
90 | printf("^"); |
---|
91 | display_node_infix(p[1], '^'); |
---|
92 | if ( p_op == '^') printf(")"); |
---|
93 | break; |
---|
94 | } |
---|
95 | } |
---|
96 | /* basically the -x case */ |
---|
97 | else if ( (dnp->num_children == 1) |
---|
98 | && (*(op) == '-' || *(op) == '+') |
---|
99 | && is_data_node(p[0]) |
---|
100 | ) |
---|
101 | { printf(op); |
---|
102 | display_data_node(p[0]); |
---|
103 | } |
---|
104 | /* arithemetic ops with 1 oprand, or other oprators */ |
---|
105 | else |
---|
106 | { printf(op); printf("("); |
---|
107 | for (i = 0; i < dnp->num_children; i++, p++) |
---|
108 | { display_node_infix(*p, '+'); |
---|
109 | if ( i < dnp->num_children - 1) printf(", "); |
---|
110 | } |
---|
111 | printf(")"); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | static void get_op_string(dnp, buf) |
---|
117 | node_t *dnp; |
---|
118 | char buf[]; |
---|
119 | { switch(dnp->type) |
---|
120 | { case MP_OperatorType: |
---|
121 | strcpy(buf, dnp->data); |
---|
122 | return; |
---|
123 | case MP_CommonOperatorType: |
---|
124 | sprintf(buf, "MPop%d ",*((MP_Common_t *)dnp->data)); |
---|
125 | return; |
---|
126 | default : fprintf(stderr, "Unknown type %d\n", dnp->type); |
---|
127 | exit(1); |
---|
128 | } |
---|
129 | /* printing operator annotation as an op-list */ |
---|
130 | /* hard to handle for infix |
---|
131 | if (dnp->annot_list != NULL) |
---|
132 | print_annot(dnp->annot_list, stdout); |
---|
133 | */ |
---|
134 | } |
---|