summaryrefslogtreecommitdiff
path: root/expr.c
blob: f50d42ca40da3b26c9a806e368a3c022af1a5393 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

#include "sheets.h"

/*
EXPR := VALUE
| VALUE POSTFIX
| '(' EXPR ')'

VALUE := NUM
| LOC

POSTFIX := OP EXPR
OP := '+' | '-' | '*' | '/' | '^'
lOC := '[' NUM ',' NUM ']'
*/

#define TODO(s) fprintf(stderr, "TODO:%d: %s\n", __LINE__, s); exit(1);
 
typedef struct expr expr; 
typedef struct value value;  /* for recursive case */
typedef struct postfix postfix;

typedef enum exprType {
	VALUE,
	POSTFIX,	
	NESTED,
} exprType;

typedef enum valueType {
	NUM,
	LOC,
} valueType;

typedef struct loc {
        int x, y;
} loc;

typedef struct expr {
	char *expr;
	exprType t;
	union {
		expr *nested;
		struct {
			value *value;
			postfix *postfix;
		};
	};
} expr;

typedef struct postfix {
	char op;
	expr *expr;
} postfix;

typedef struct value {
	valueType t;
	union {
		int num;
		loc *loc;
	};
} value;

char isop(char c) {
        switch (c) {
                case '+':
                case '-':
                case '*':
                case '/':
                case '^':
                        return c;
        }
        return 0;
}

char *in;
char *Pin;

void consumeleading(char c) {
loop:
	if (*in == c) {
		in++;
		goto loop;
	}
}

expr *parseExpr(); 

loc *parseLoc() {
	consumeleading(' ');
	loc *l = malloc(sizeof(loc));
	if (*in == '[') {
		int i;
		sscanf(in, "[%d,%d]%n", &l->x, &l->y, &i);
		if (i != 0) { 
			in += i;
			return l;
		}
		TODO("ERROR");
	} 
	return NULL;
}

postfix *parsePostfix() {
	consumeleading(' ');
	postfix *p = malloc(sizeof(postfix));
	if (p->op = isop(*in)) {
		in++;
		if (p->expr = parseExpr()) 
			return p;		
	}
	return NULL;
}

value *parseValue() {
	consumeleading(' ');
	value *v = malloc(sizeof(value));
	if (v->loc = parseLoc()) {
		v->t = LOC;
		return v;
	}
	else if (isdigit(*in)) {
		int i;
		sscanf(in, "%d%n", &v->num, &i);
		if (i != 0) {
			in += i;
			v->t = NUM;
			return v;
		}
		TODO("ERROR");
	} 
	return NULL;
}

expr *parseExpr() {
	consumeleading(' ');
	expr *e = malloc(sizeof(expr));
	if (e->value = parseValue()) {
		e->t = VALUE;
		if (e->postfix = parsePostfix())
			e->t = POSTFIX;

		return e;
	}

	else if (*in == '(') {
		in++;
		if (e->nested = parseExpr()) 
			if (*in == ')')	{
				e->t = NESTED;
				return e;
			}
		TODO("ERROR");
	}

	return NULL;
}

double evalExpr(expr *e, sheet *s);

double evalValue(value *v, sheet *s) {
	if (v->t == NUM) return v->num;	
	return evalExpr(s->cells[v->loc->y][v->loc->x], s);
}

double evalPostfix(value *v, postfix *p, sheet *s) {
	switch (p->op) {
		case '+': return evalValue(v, s) + evalExpr(p->expr, s);
		case '-': return evalValue(v, s) - evalExpr(p->expr, s);
		case '*': return evalValue(v, s) * evalExpr(p->expr, s);
		case '/': return evalValue(v, s) / evalExpr(p->expr, s);
		case '^': return pow(evalValue(v, s), evalExpr(p->expr, s));
	}
}

double evalExpr(expr *e, sheet *s) {
	switch (e->t) {  
		case VALUE: return evalValue(e->value, s); break;
		case POSTFIX: return evalPostfix(e->value, e->postfix, s); break;
		case NESTED: return evalExpr(e->nested, s);
	}
}

expr *makeNumber(int n) {
	in = Pin;
	memset(in, 0, 255);
	snprintf(in, 16, "%d", n);
	expr *e = parseExpr();
	e->expr = strdup(Pin);
	return e;
}

void freeValue(value *v) {
	if (v->t == LOC) free(v->loc);
	free(v);
}

void freePostfix(value *v, postfix *p) {
	freeValue(v);
	freeExpr(p->expr);
	free(p);
}

void freeExpr(expr *e) {
	switch (e->t) {  
		case VALUE: freeValue(e->value); break;
		case POSTFIX: freePostfix(e->value, e->postfix); break;
		case NESTED: freeExpr(e->nested); break;
	}
	free(e);
}