vm.c (5640B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <assert.h> 4 5 #include "vm.h" 6 #include "formating.h" 7 #include "tactics.h" 8 9 /* OBJECT CREATION */ 10 11 struct 12 state new_state(void) 13 { 14 struct state s; 15 init(s, attitude); 16 s.focus = NULL; 17 s.cursor = NULL; 18 s.head = NULL; 19 return s; 20 } 21 22 struct term* 23 create_term(struct symbol *head, size_t nargs) 24 { 25 struct term *t = malloc(sizeof(struct term)); 26 t->head = head; 27 t->nargs = nargs; 28 t->subterms = malloc(sizeof(struct term*) * nargs); 29 for (size_t i = 0; i < nargs; i++) 30 t->subterms[i] = NULL; 31 return t; 32 } 33 34 struct attitude* 35 create_attitude(struct symbol *marker, size_t nvars, size_t npres) 36 { 37 struct attitude *a = malloc(sizeof(struct attitude)); 38 a->marker = marker; 39 a->nvars = nvars; 40 a->vars = calloc(nvars, sizeof(struct symbol*)); 41 a->npres = npres; 42 a->pressupositions = calloc(npres, sizeof(struct term*)); 43 return a; 44 } 45 46 struct symbol* 47 create_symbol(char *name) 48 { 49 struct symbol *sym = malloc(sizeof(struct symbol)); 50 sym->name = name; 51 return sym; 52 } 53 54 /* 55 struct assumption* 56 create_assumption(struct state *st, struct symbol *name, struct symbol *marker, size_t nvars, size_t nsub) 57 { 58 struct attitude *att = lookup_attitude(st, marker); 59 assert(att != NULL); 60 struct assumption a = { 61 .name = name, 62 .marker = marker, 63 .nvars = nvars, 64 .nsub = nsub, 65 .vars = malloc(nvars * sizeof(struct symbol)), 66 .sub = malloc(nsub * sizeof(struct assumption)), 67 .judgment = malloc(1 * sizeof(struct term)), 68 .nproofs = att->npres, 69 .proofs = malloc(att->npres * sizeof(struct proof)) 70 }; 71 72 return push(st, assumption, a); 73 } 74 */ 75 76 /* 77 struct proof* 78 create_proof(struct state *st, struct term *thesis) 79 { 80 struct proof p = { 81 .thesis = thesis, 82 .argument = malloc(1 * sizeof(struct symbol)) 83 }; 84 85 return push(st, proof, p); 86 } 87 */ 88 89 /* GOAL MECHANICS */ 90 91 struct goal* 92 create_goal(ref_t type, void *target) 93 { 94 struct goal *new = malloc(sizeof(struct goal)); 95 new->next = NULL; 96 new->parent = NULL; 97 new->sub = NULL; 98 new->type = type; 99 new->target = target; 100 return new; 101 } 102 103 void 104 add_top_goal(struct state *st, ref_t type) 105 { 106 void *addr; 107 assert(type == ATTITUDE); 108 assert(!st->focus); 109 if (type == ATTITUDE) { 110 addr = reserve(st, attitude); 111 } 112 st->focus = create_goal(type, addr); 113 } 114 115 void 116 add_subgoal(struct state *st, ref_t type, void *target) 117 { 118 assert(st->focus); 119 struct goal *new = create_goal(type, target); 120 121 if (st->cursor) { 122 /* There are non-commited already subgoals */ 123 st->cursor->next = new; 124 st->cursor = new; 125 } else { 126 /* Creating first subgoal */ 127 st->cursor = new; 128 if (st->focus) st->focus->sub = new; 129 st->next = new; 130 } 131 } 132 133 void 134 commit(struct state *st) 135 { 136 assert(st->focus); 137 if (st->focus->sub) { 138 st->focus = st->focus->sub; 139 } else if (st->focus->next) { 140 st->focus = st->focus->next; 141 } else if (st->focus->parent) { 142 /* Do resolution */ 143 st->focus = st->focus->parent; 144 } else { 145 st->focus = NULL; 146 } 147 st->cursor = NULL; 148 } 149 150 void 151 solve(struct state *st, void *dest) 152 { 153 assert(st->focus); 154 switch (st->focus->type) { 155 case TERM: 156 *st->focus->target = (struct term*)dest; 157 break; 158 case SYMBOL: 159 *st->focus->target = (struct symbol*)dest; 160 break; 161 case ATTITUDE: 162 *st->focus->target = (struct attitude*)dest; 163 break; 164 case PROOF: 165 *st->focus->target = (struct proof*)dest; 166 break; 167 case ASSUMPTION: 168 *st->focus->target = (struct assumption*)dest; 169 break; 170 } 171 } 172 173 struct symbol 174 *read_symbol(struct tape *program) 175 { 176 assert(program->code[program->cur].code == SYM); 177 return program->code[program->cur++].sym; 178 } 179 180 unsigned int 181 read_number(struct tape *program) 182 { 183 assert(program->code[program->cur].code == NUM); 184 return program->code[program->cur++].num; 185 } 186 187 ref_t 188 read_top(struct tape *program) 189 { 190 assert(program->code[program->cur].code == TOP); 191 return program->code[program->cur++].type; 192 } 193 194 void 195 step(struct state *st, struct tape *program) 196 { 197 unsigned int n, m; 198 struct symbol *sym; // , *mk; 199 200 if (!st->focus) { 201 /* No active goal. Expect goal creation */ 202 ref_t ty = read_top(program); 203 add_top_goal(st, ty); 204 } else { 205 switch (st->focus->type) { 206 case SYMBOL: 207 sym = read_symbol(program); 208 tactic_symbol(st, sym); 209 break; 210 case ATTITUDE: 211 sym = read_symbol(program); 212 n = read_number(program); 213 m = read_number(program); 214 tactic_attitude(st, sym, n, m); 215 break; 216 case ASSUMPTION: 217 // sym = read_symbol(program); 218 // mk = read_symbol(program); 219 // n = read_number(program); 220 // m = read_number(program); 221 // tactic_assumption(st, sym, mk, n, m); 222 break; 223 case TERM: 224 sym = read_symbol(program); 225 n = read_number(program); 226 tactic_term(st, sym, n); 227 break; 228 case PROOF: 229 exit(1); 230 break; 231 } 232 } 233 } 234 235 void 236 run(struct state *st, struct tape *program) 237 { 238 while (program->code[program->cur].code != HLT) 239 step(st, program); 240 } 241 242 int 243 main(void) { 244 struct state s = new_state(); 245 246 struct symbol *wft = create_symbol("wft"); 247 struct symbol *colon = create_symbol(":"); 248 struct symbol *var_a = create_symbol("a"); 249 struct symbol *var_b = create_symbol("b"); 250 // struct symbol *type = create_symbol("type"); 251 252 struct op code[] = { 253 { TOP, { .type = ATTITUDE } }, 254 255 { SYM, { .sym = wft } }, 256 { NUM, { .num = 1 } }, 257 { NUM, { .num = 0 } }, 258 259 { SYM, { .sym = var_a } }, 260 261 262 { TOP, { .type = ATTITUDE } }, 263 264 { SYM, { .sym = colon } }, 265 { NUM, { .num = 2 } }, 266 { NUM, { .num = 1 } }, 267 268 { SYM, { .sym = var_a } }, 269 270 { SYM, { .sym = var_b } }, 271 272 { SYM, { .sym = wft } }, 273 { NUM, { .num = 1 } }, 274 275 { SYM, { .sym = var_b } }, 276 { NUM, { .num = 0 } }, 277 278 { HLT , { 0 } } 279 }; 280 281 struct tape t = { 282 .code = code, 283 .cur = 0 284 }; 285 286 run(&s, &t); 287 print_state(stdout, &s); 288 289 return 0; 290 }