/* * Looks up a given clause name into the specified hashtables */ t_clause *lookup(char *cname, t_hashtable *xs, t_hashtable *ys, t_hashtable *ts) { t_clause *clause = NULL; clause = find_clause(xs, cname); if (!clause) { clause = find_clause(ys, cname); if (!clause) clause = find_clause(ts, cname); } return clause; }
bool read_clause (PrologElement * parameters, char * module_name, char * atom_name, int ordering) { AREA area; int module_id = find_module_id (area, module_name); if (module_id < 0) return false; int atom_id = find_atom_id (area, module_id, atom_name); if (atom_id < 0) return false; PrologAtom * atom = find_or_create_atom (module_name, atom_name); if (atom == NULL) return false; db_clause clause; if (! find_clause (area, module_id, atom_id, ordering, & clause)) return false; if (fast) { mysql_term_reader trd (root, area); PrologElement * trdx = trd . readElement (); if (trdx != 0) { root -> getValue (trdx, area, 0); parameters -> setPair (trdx, root -> earth ()); return true; } return false; } parameters -> setPair (); parameters = parameters -> getLeft (); parameters -> setPair (); PrologElement * head = parameters -> getLeft (); head -> setPair (); head -> getLeft () -> setAtom (atom); head = head -> getRight (); if (! find_element (area, module_name, head, clause . parameters)) return false; // head -> setInteger (clause . parameters); head = parameters -> getRight (); if (! find_element (area, module_name, head, clause . body)) return false; // head -> setInteger (clause . body); return true; }
/* * Reads each line from the input file, then malloc()s and populates main * data structures X, T, Y */ void compile(FILE *fin, t_clause ***X, t_clause ***T, t_clause ***Y, int *params, char *xs) { int r, value; int count = 0; t_fsm *fsmp = fsmparser(); char line[LINLEN]; char copy[LINLEN]; char *token; char *op; char c; t_clause *cl; // used for keeping track of input and output symbols t_hashtable *inputs = hashtable(IN_HASHSIZE); t_hashtable *outputs = hashtable(OUT_HASHSIZE); t_hashtable *temps = hashtable(TMP_HASHSIZE); /* check hashtable pointers */ if (!inputs || !outputs || !temps) { fprintf(stderr, "-- compile() : could not init hashtables\n"); return; } /* process each line at a time */ while (fgets(line, LINLEN, fin)) { int l = lincpy(line, copy, "\n\t\r;"); if (l > 1) { /* initialize data structures */ if (isdigit((c=copy[0]))) { token = strtok(copy, " "); value = atoi(token); token = strtok(NULL, " "); if (streq(token, "gates") || streq(token, "gate")) { *T = malloc(value*sizeof(t_clause*)); params[2]=value; r = next(fsmp, LEN); } else if (streq(token, "inputs")) { *X = malloc(value*sizeof(t_clause*)); params[0]=value; r = next(fsmp, IN); } else if (streq(token, "outputs")) { *Y = malloc(value*sizeof(t_clause*)); params[1]=value; r = next(fsmp, OUT); } else r = next(fsmp, ERR); } /* line == 'begin' or line == 'end' */ else if (streq(copy, "begin")) r = next(fsmp, PROG); else if (streq(copy, "end")) r = next(fsmp, END); /* list of input symbols */ else if (getstate(fsmp) == IN) { token=strtok(copy, " "); while (token) { cl = clause(NULL, NULL, NULL, token, count); printf("0x%x @ %s ", cl, get_clause_name(cl)); if (xs) { int xval = -48; bool b; xval += (count < strlen(xs)) ? xs[count] : 48; b = (!xval) ? FALSE : TRUE; set_value(cl, b); printf("= %d", b); } (*X)[count++] = cl; put_clause(inputs, cl); printf("\n"); token = strtok(NULL, " "); } count=0; } /* list of output symbols */ else if (getstate(fsmp) == OUT) { token = strtok(copy, " "); while (token) { cl = clause(NULL, NULL, NULL, token, count++); put_clause(outputs, cl); printf("0x%x @ %s\n", cl, get_clause_name(cl)); token=strtok(NULL, " "); } count=0; } /* parse clauses */ else if (getstate(fsmp) == PROG) { token = strtok(copy, "= "); t_clause *target = find_clause(outputs, token); char *lcname, *rcname, *op; t_clause *lcl, *rcl; lcname = strtok(NULL, "= "); op = strtok(NULL, "= "); rcname = strtok(NULL, "= "); /* composite output clause */ if (op && lcname && rcname && target) { // find left and right operands lcl = lookup(lcname, inputs, outputs, temps); rcl = lookup(rcname, inputs, outputs, temps); if (!lcl || !rcl) { r = next(fsmp, ERR); continue; } cl = clause(lcl, rcl, op, token, count); printf("0x%x @ %s = %s %s %s\n", target, get_clause_name(target), get_clause_name(lcl), op, get_clause_name(rcl)); int num = enumerate(target); clause_copy(cl, target); (*Y)[num] = target; } /* simple output clause */ else if (target && lcname) { lcl = lookup(lcname, inputs, outputs, temps); if (!lcl) { r = next(fsmp, ERR); continue; } printf("0x%x @ %s = %s\n", target, get_clause_name(target), get_clause_name(lcl)); int num = enumerate(target); clause_copy(lcl, target); (*Y)[num] = target; } /* just a temp clause ?! */ else { lcl = lookup(lcname, inputs, outputs, temps); rcl = lookup(rcname, inputs, outputs, temps); if (!lcl || !rcl) { r = next(fsmp, ERR); continue; } cl = clause(lcl, rcl, op, token, count); put_clause(temps, cl); (*T)[count++]=cl; printf("0x%x @ %s\n", cl, get_clause_name(cl)); } } else { /* invalid line */ r = next(fsmp, ERR); } } else { /* l <= 1 means line is comment or blank */ r = next(fsmp, IGN); } /* valid transaction? */ if (!r || getstate(fsmp) == ERR) { fprintf(stderr, "compile() :: parsing error (r=%d\ts=%d)\n", r, getstate(fsmp)); if (*T) free(*T); if (*Y) free(*Y); if (*X) free(*X); exit(1); } } // get rid of unnecessary hashtables // note that this won't affect clauses, which // remain available through X, Y, and T wipe_hashtable(inputs, FALSE); wipe_hashtable(outputs, FALSE); wipe_hashtable(temps, FALSE); }