git @ Cat's Eye Technologies Equipage / 352ba0d
Dumps the function so parsed. Chris Pressey 6 years ago
1 changed file(s) with 81 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
8282
8383 /**** Function Utilities ****/
8484
85 /*
86 * This function takes ownership of the lhs and rhs passed to it.
87 */
8588 struct function *make_function(enum fntype fntype, struct function *lhs, struct function *rhs) {
8689 struct function *fn;
8790
99102 }
100103
101104
105 struct function *copy_function(struct function *src) {
106 return make_function(src->fntype, src->lhs, src->rhs);
107 }
108
109
110 void dump_function(struct function *fn) {
111 switch (fn->fntype) {
112 case APPLY:
113 printf("apply");
114 break;
115 case COMPOSE:
116 printf("compose");
117 break;
118 case POP:
119 printf("pop");
120 break;
121 case SWAP:
122 printf("swap");
123 break;
124 case ADD:
125 printf("add");
126 break;
127 case SUB:
128 printf("sub");
129 break;
130 case SIGN:
131 printf("sign");
132 break;
133 case PICK:
134 printf("pick");
135 break;
136 case ONE:
137 printf("one");
138 break;
139 case NOP:
140 printf("nop");
141 break;
142
143 case PUSH:
144 printf("(push ");
145 dump_function(fn->lhs);
146 printf(")");
147 break;
148 case CONCAT:
149 printf("(concat ");
150 dump_function(fn->lhs);
151 printf(" ");
152 dump_function(fn->rhs);
153 printf(")");
154 break;
155 }
156 }
157
158
102159 /**** Evaluation ****/
103160
104161 void apply_(struct function *fn, struct stack *stack) {
112169 case COMPOSE:
113170 pop(stack, &a);
114171 pop(stack, &b);
115 // FIXME no - will need to make copies of a and b
116172 c.num = 0;
173 /* Even though a and b are local variables, the functions they point to, aren't. So this is OK. */
174 /* (I think.) */
117175 c.fn = make_function(CONCAT, a.fn, b.fn);
118176 push(stack, &c);
119177 break;
205263 }
206264
207265
208 struct function *parse(char *text) {
266 struct function *parse_stream(FILE *stream) {
267 char c;
209268 struct function *fn;
210269
211270 fn = make_function(NOP, NULL, NULL);
212271
213 for (int i = 0; text[i]; i++) {
214 fn = make_function(CONCAT, semantics(text[i]), fn);
272 c = fgetc(stream);
273 while (!feof(stream)) {
274 fn = make_function(CONCAT, semantics(c), fn);
275 c = fgetc(stream);
215276 }
216277
217278 return fn;
219280
220281
221282 int main(int argc, char **argv) {
222 }
283 FILE *f;
284 struct function *program;
285
286 f = fopen(argv[1], "r");
287 if (!f) {
288 fprintf(stderr, "Couldn't open '%s' for reading\n", argv[1]);
289 exit(1);
290 }
291
292 program = parse_stream(f);
293 fclose(f);
294
295 dump_function(program);
296
297 return 0;
298 }