int parse_desc_file(FILE* f, DESC* desc) { int c = 0; char* value; char line[256], buffer[256]; while (fgets(line, sizeof(line), f) != 0 && c < 4) { if (sscanf(line, "%s", buffer) != 1) return PARSE_ERROR; value = strchr(buffer, '='); if (!(value && strlen(value) > 1)) return PARSE_ERROR; value++; switch (c) { case 0: if (!parse_name(value, desc->name)) return PARSE_ERROR; break; case 1: if (!parse_type(value, &(desc->type))) return PARSE_ERROR; break; case 2: if (!parse_opcode(value, &(desc->opcode))) return PARSE_ERROR; break; case 3: if (desc->type == 'R') { if (!parse_function(value, &(desc->function))) return PARSE_ERROR; } else { desc->function = -1; } break; } c++; } return PARSE_SUCCESS; }
/* MU/RT and this should be added to the current chemical potential */ void get_activity_coef(void) { int i, L; double coef; /* First calculate ln(solvent) if it is implemented */ /* ln(solvent) implementation */ /* Now calculate the activity coefficient */ for (i = 1; i <= mr; i++) { L = ind[i]; if (VCS_act[L] != MARKER) { coef = parse_opcode(VCS_act[L]); #ifdef DEBUG2 fprintf(debug_prn_file,"\nln gamma[%2d] = %+4e mu = %+4e sigma = %+4e",i,coef,initial_chem_pot[i],ds[i]); #endif ff[i] += alpha*(initial_chem_pot[i] + coef - ff[i]); } } }
// Takes the first expression from a string. // Pass null to take the next expression out of the string. c16_expr *get_expr(char *str){ char *in = (str) ? str : NULL; c16_expr *ret = malloc(sizeof(c16_expr)); char *tok; static const char* const bin_ops[][2] = { { "and", "&&" }, { "or", "||" }, { "xand", "!&" }, { "xor", "!|" }, { "lshift","<<" }, { "rshift",">>" }, { "add", "+" }, { "sub", "-" }, { "mul", "*" }, { "div", "/" }, { "mod", "%" }, { "min", "" }, { "max", "" } }; static const char* const un_ops[][2] = { { "inv", "~" }, { "inc", "++" }, { "dec", "--" }, { "set", "=" }, { "swap", "\\\\" }, { "gt", ">" }, { "lt", "<" }, { "gte", ">=" }, { "lte", "<=" }, { "eq", "==" }, { "neq", "!=" } }; static const char* const nrp_ops[][2] = { { "push", ":" }, { "write", "" }, { "pop", "$" }, { "peek", "@" }, { "read", "" } }; static const char* const fr_ops[][2] = { { "flush", "#" }, { "halt", "" }, { "nop", "" } }; static const char* const jmp_ops[][2] = { { "jmp", "=>" }, { "jmpt", "->" }, { "jmpf", "<-" } }; int n; ret->is_valid = true; tok = strtok(in," \n"); if (!tok){ ret->op = OP_TERM; ret->param_1 = no_param; ret->param_2 = no_param; ret->param_3 = no_param; ret->str_data = NULL; return ret; } if (tok[0] == '@' && strlen(tok) > 1){ ret->op = OP_LABEL; ret->param_1 = no_param; ret->param_2 = no_param; ret->param_3 = no_param; ret->str_data = &tok[1]; v_msgf("Adding label to expr list: '%s'\n",tok); return ret; } if (!strcmp(tok,"mset") || !strcmp(tok,":=")){ ret->op = OP_MSET_; ret->param_1 = next_param(); ret->param_2 = next_param(); ret->param_3 = no_param; ret->str_data = NULL; specialize_expr(ret); return ret; } for (n = 0;n < 13;n++){ if (!strcmp(tok,bin_ops[n][0]) || !strcmp(tok,bin_ops[n][1])){ ret->op = parse_opcode(tok); ret->param_1 = next_param(); ret->param_2 = next_param(); ret->param_3 = next_param(); ret->str_data = NULL; specialize_expr(ret); return ret; } } for (n = 0;n < 11;n++){ if (!strcmp(tok,un_ops[n][0]) || !strcmp(tok,un_ops[n][1])){ ret->op = parse_opcode(tok); ret->param_1 = next_param(); ret->param_2 = next_param(); ret->param_3 = no_param; ret->str_data = NULL; specialize_expr(ret); return ret; } } for (n = 0;n < 5;n++){ if (!strcmp(tok,nrp_ops[n][0]) || !strcmp(tok,nrp_ops[n][1])){ ret->op = parse_opcode(tok); ret->param_1 = next_param(); ret->param_2 = no_param; ret->param_3 = no_param; ret->str_data = NULL; specialize_expr(ret); return ret; } } for (n = 0;n < 3;n++){ if (!strcmp(tok,fr_ops[n][0]) || !strcmp(tok,fr_ops[n][1])){ ret->op = parse_opcode(tok); ret->param_1 = no_param; ret->param_2 = no_param; ret->param_3 = no_param; ret->str_data = NULL; specialize_expr(ret); return ret; } } for (n = 0;n < 3;n++){ if (!strcmp(tok,jmp_ops[n][0]) || !strcmp(tok,jmp_ops[n][1])){ ret->op = parse_opcode(tok); ret->param_1 = malloc(sizeof(c16_param)); ret->param_1->type = WORDLIT_PARAM; ret->param_1->data = 0; ret->param_2 = no_param; ret->param_3 = no_param; ret->str_data = strdup(strtok(NULL," \n")); return ret; } } v_msgf("Read an unreckognized token: '%s'\n",tok); no_param = malloc(sizeof(c16_param)); no_param->type = NO_PARAM; no_param->str = NULL; ret->op = OP_INVALID; ret->param_1 = no_param; ret->param_2 = no_param; ret->param_3 = no_param; ret->str_data = strdup(tok); specialize_expr(ret); return ret; }
void debugger() { int n, i; fd_set input_set; uint8_t dbg_z_instr; uint8_t dbg_z_instr_form; uint8_t dbg_number_of_operands; uint8_t *dbg_pc = pc; debugger_output(newsockfd, "\nEntering debugger.\n"); for(;;) { parse_opcode( &dbg_z_instr, &dbg_z_instr_form, &dbg_number_of_operands, &dbg_pc); sprintf(buffer, "\n: %6lx: %d %d %d\n", (long)(pc - z_mem), dbg_z_instr, dbg_z_instr_form, dbg_number_of_operands); debugger_output(newsockfd, buffer); for (i=0; i<number_of_locals_active; i++) { if (i != 0) debugger_output(newsockfd, " "); sprintf(buffer, "L%02d:%x", i, local_variable_storage_index[i]); debugger_output(newsockfd, buffer); } debugger_output(newsockfd, "\n# "); FD_ZERO(&input_set); FD_SET(newsockfd, &input_set); while (select(newsockfd+1, &input_set, NULL, NULL, NULL) == -1) { if (errno != EINTR) { debugger_output(newsockfd, "select() socket error.\n"); perror("select"); break; } } n = read(newsockfd, buffer, BUFFER_SIZE-1); while (n < 0) { if (errno != EINTR) { debugger_output(newsockfd, "read() socket error.\n"); break; } } if (n > 2) buffer[n-2]=0; else *buffer = 0; debugger_output(newsockfd, "\n"); if ( (strcmp(buffer, "exit") == 0) || (strcmp(buffer, "quit") == 0) ) { break; } else if (strcmp(buffer, "help") == 0) { debugger_output(newsockfd, "Valid commands:\n"); debugger_output(newsockfd, " - stack: Dump stack contents.\n"); debugger_output(newsockfd, " - story: Print story file information.\n"); debugger_output(newsockfd, " - exit, quit: Leave debugger.\n"); } else if (strcmp(buffer, "stack") == 0) { i = 0; n = z_stack_index - z_stack; while (i < n) { if ( (i % 8) == 0) { if (i > 0) { debugger_output(newsockfd, "\n"); } sprintf(buffer, "%06x:", i); debugger_output(newsockfd, buffer); } sprintf(buffer, " %04x", z_stack[i]); debugger_output(newsockfd, buffer); i++; } debugger_output(newsockfd, "\n"); } else if (strcmp(buffer, "story") == 0) { sprintf(buffer, "Z-Story version: %d.\n", active_z_story->version); debugger_output(newsockfd, buffer); sprintf(buffer, "Release code: %d.\n", active_z_story->release_code); debugger_output(newsockfd, buffer); sprintf(buffer, "Serial code: %s.\n", active_z_story->serial_code); debugger_output(newsockfd, buffer); sprintf(buffer, "Checksum: %d.\n", active_z_story->checksum); debugger_output(newsockfd, buffer); sprintf(buffer, "Dynamic memory end: $%lx.\n", (long)(active_z_story->dynamic_memory_end - z_mem)); debugger_output(newsockfd, buffer); sprintf(buffer, "Static memory end: $%lx.\n", (long)(active_z_story->static_memory_end - z_mem)); debugger_output(newsockfd, buffer); sprintf(buffer, "High memory: $%lx.\n", (long)(active_z_story->high_memory - z_mem)); debugger_output(newsockfd, buffer); sprintf(buffer, "High memory end: $%lx.\n", (long)(active_z_story->high_memory_end - z_mem)); debugger_output(newsockfd, buffer); } else { debugger_output(newsockfd, "Unknown command \""); debugger_output(newsockfd, buffer); debugger_output(newsockfd, "\".\n"); } } debugger_output(newsockfd, "Leaving debugger.\n"); //close(newsockfd); return; }
/* using fel[], tmole1[] & wt[] as temporary values */ enum path calc_result(void) { #ifdef DEBUG3 int i,j,k; /* loop counters , temp values */ int mr1; double g; #endif #ifdef DEBUG2 int L; double lnK; #endif #ifdef DEBUG1 fprintf(debug_prn_file,"\nTransfering data to spreadsheet"); #endif #ifdef DEBUG3 j = mr + 1; k = nr + 1; for (i = 1; i <= nr; i++) { j--;k--; dg[j] = dg[k]; } for (i = 1; i < MAXPHASE; i++) tmole1[i] = 0.0; /* first calculate total mass */ /* we will use fel as the temp variable for the mass% */ /* and tmole1 as the total mass for the phase */ for ( i = 1; i <= mr; i++ ) { fel[i] = c_mole_mass(VCS_specie[ind[i]])*w[i]; tmole1[si[i]] += fel[i]; } /* calculate mass% and mole fractions */ for (i = 1; i <= mr; i++) { if ( si[i] == SINGLE ) { if ( w[i] == 0.0) { wt[i] = 0.0; /* single-species phase */ fel[i] = 0.0; /* wt% */ } else if ( w[i] > 0.0 ) { wt[i] = 1.0; /* mole fraction */ fel[i] = 100.0; /* wt% */ } } else { if (tmole1[si[i]] != 0.0) { wt[i] = w[i]/tmole[si[i]]; /* all other phases */ fel[i] = fel[i]/tmole1[si[i]]*100.0; } else fel[i] = 0.0; } } /* --------------- print out the results -------------------- */ /* --------- to be included ------------*/ fprintf(debug_prn_file, CONDENSED_ON); if (ICONV) fprintf(debug_prn_file,"\n Convergence criterion not statisfied."); fprintf(debug_prn_file,"\n Number of iterations = %3d",iter); fprintf(debug_prn_file,"\n Number of times a new stoichiometry matrix was evaluated = %3d\n",nopt); fprintf(debug_prn_file,"Species Equilibrium moles Mole Fraction DG/RT Reaction Mass Perc ln gamma\n"); /* All the components */ for (i = 1; i <= nc; i++) { for(j = 0; j < 10; j++) fprintf(debug_prn_file,"%c",comp_array[VCS_specie[ind[i]]].name[j]); fprintf(debug_prn_file," "); fprintf(debug_prn_file,"%+.7e %+.7e %8.4f", w[i], wt[i], fel[i] ); if (VCS_act[ind[i]] != MARKER) fprintf(debug_prn_file," %8.4f", parse_opcode(VCS_act[ind[i]])); fprintf(debug_prn_file,"\n"); } /* Now all the reactions */ for (i = nc+1;i <= mr; i++) { for(j = 0; j < 10; j++) fprintf(debug_prn_file,"%c",comp_array[VCS_specie[ind[i]]].name[j]); fprintf(debug_prn_file," "); fprintf(debug_prn_file,"%+.7e %+.7e %+.7e %8.4f", w[i], wt[i], dg[i], fel[i]); if (VCS_act[ind[i]] != MARKER) fprintf(debug_prn_file," %8.4f", parse_opcode(VCS_act[ind[i]])); fprintf(debug_prn_file,"\n"); } /* print the deleted species */ if ( mr != m ) { fprintf(debug_prn_file,"\n LESS THAN 1.E-32 MOLES \n"); mr1 = mr + 1; for (i = mr1; i <= m; i++) { for(j = 0; j <= 10; j++) fprintf(debug_prn_file,"%c",comp_array[VCS_specie[ind[i]]].name[j]); fprintf(debug_prn_file,"\n"); } } g = 0.0; for (i = 1; i <= no_phases; i++) if ( tmole[i] > 0.0 && tinert[i] > 0.0 ) g += tinert[i]*log((double)tinert[i]/tmole[i]); for (i =1; i <= mr; i++) g += w[i]*fe[i]; fprintf(debug_prn_file,"\nDG/RT = %+.7e",g); for (i = 1; i <= no_phases; i++) fprintf(debug_prn_file,"\nTotal phase %1d moles = %+.7e mass = %+.7e kg",i,tmole[i], tmole1[i]/1000.0); fprintf(debug_prn_file,"\n\nElemental abundances"); for (i =1; i <= ne; i++) fprintf(debug_prn_file,"\n %2d %s %+.7e",i,Elem_txt[VCS_elem[i]], ga[i]); #endif #ifdef DEBUG2 fprintf(debug_prn_file,"\n\nStoichiometric matrix sc[i][j]\n\n "); for (i = 1; i <= nc; i++) { for(j = 0; j < 8; j++) fprintf(debug_prn_file,"%c",comp_array[VCS_specie[ind[i]]].name[j]); fprintf(debug_prn_file," "); } /* now lets calculate ln K for the non-ideal case */ fprintf(debug_prn_file," Delta Gø ln K(non-ideal)\n"); for (i = 1; i <= nr; i++) { L = ir[i]; lnK = ff[L]; /* - ln K */ g = da[L]; /* delta G */ for (j = 1; j <= nc; j++) { g += sc[j][i]*da[j]; lnK += sc[j][i]*ff[j]; } fprintf(debug_prn_file," 0 = "); for (j = 0; j < 7; j++) fprintf(debug_prn_file,"%c",comp_array[VCS_specie[ind[L]]].name[j]); fprintf(debug_prn_file," "); for (j = 1; j <= nc; j++) fprintf(debug_prn_file,"%4.2g ",sc[j][i]); fprintf(debug_prn_file,"%-8.6f %-8.6f\n", g, -lnK); } fprintf(debug_prn_file,CONDENSED_OFF); #endif return ( route = RESULT ); }
int parse_function(char* str, uint* function) { /* idem, 6 binary digits */ return parse_opcode(str, function); }