-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgebra.c
168 lines (144 loc) · 3.6 KB
/
algebra.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* =====================================================================================
*
* Filename: algebra.c
*
* Description: Algebraic manipulations of Abstract Syntax Trees
*
* Version: 1.0
* Created: 09/20/2017 03:23:51 PM
* Revision: none
* Compiler: gcc
*
* Author: Brad Theilman (BHT), [email protected]
* Organization:
*
* =====================================================================================
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cmplr.h"
#include "algebra.h"
#include "symbol.h"
/* Abstract Syntax Tree Rewriting Functions */
Node *rewrite_minus(Node * ast)
{
/* Rewrite a - b as a + -1*b */
if (ast == NULL) {
return NULL;
}
if (ast->type == INT || ast->type == VAR) {
return ast;
}
if (ast->type == BIN_OP_MINUS) {
return plus_node(rewrite_minus(ast->left),
times_node(integer_node(-1),
rewrite_minus(ast->right)));
} else {
ast->left = rewrite_minus(ast->left);
ast->right = rewrite_minus(ast->right);
return ast;
}
}
Node *reorder_coeff(Node * ast)
{
/* Reorders coefficients so that integers are on left */
Node *tmp;
if (ast == NULL) {
return NULL;
}
if (ast->type == INT || ast->type == VAR) {
return ast;
}
if (ast->type == BIN_OP_TIMES
&& ast->right->type == INT && ast->left->type != INT) {
tmp = reorder_coeff(ast->left);
ast->left = ast->right;
ast->right = tmp;
return ast;
}
ast->left = reorder_coeff(ast->left);
ast->right = reorder_coeff(ast->right);
return ast;
}
Node *expand(Node * ast)
{
Node *out, *al;
/* if node is int or var, just return it */
if (ast->type == VAR || ast->type == INT) {
return ast;
}
/* If node is add, expand children */
if (ast->type == BIN_OP_PLUS || ast->type == BIN_OP_MINUS) {
ast->left = expand(ast->left);
ast->right = expand(ast->right);
return ast;
}
/* if node is multiply, rewrite */
if (ast->type == BIN_OP_TIMES) {
if (ast->right->type == BIN_OP_PLUS) {
al = expand(ast->left);
out =
plus_node(expand(times_node(al, expand(ast->right->left))),
expand(times_node
(al, expand(ast->right->right))));
return out;
}
if (ast->left->type == BIN_OP_PLUS) {
al = expand(ast->right);
out =
plus_node(expand(times_node(expand(ast->left->left), al)),
expand(times_node
(expand(ast->left->right), al)));
return out;
}
/* Expand Children */
ast->left = expand(ast->left);
ast->right = expand(ast->right);
/* check to see if we can distribute further */
if (ast->left->type == BIN_OP_PLUS
|| ast->right->type == BIN_OP_PLUS) {
return expand(ast);
}
/* return expanded node */
return ast;
}
/* If we have a parenthetical expression,
* expand the right branch. The left is NULL */
if (ast->type == PAREN) {
ast->right = expand(ast->right);
}
/* fall through */
return ast;
}
int evaluate(Node * ast)
{
/* evaluate an expression */
int v1;
Symbol *sym;
if (ast->type == VAR) {
if(sym = find_symbol(ast)) {
v1 = evaluate(sym->expr);
}
}
if (ast->type == INT) {
return ast->value;
}
if (ast->type == PAREN) {
return evaluate(ast->right);
}
switch (ast->type) {
case BIN_OP_PLUS:
v1 = evaluate(ast->left) + evaluate(ast->right);
break;
case BIN_OP_TIMES:
v1 = evaluate(ast->left) * evaluate(ast->right);
break;
case BIN_OP_MINUS:
v1 = evaluate(ast->left) - evaluate(ast->right);
break;
}
return v1;
}