int is_solution(char *sol, char *test, char *pion, int slot) { int good; int bad; int i; i = 0; good = 0; bad = 0; if (my_strlen(sol) != my_strlen(test)) print_len_err(slot); while (sol[i] && test[i]) { if (is_instr(test[i], pion) == EXIT_FAILURE) return (put_error_pion(test[i])); if (test[i] == sol[i]) good++; else if (is_instr(test[i], sol) == EXIT_SUCCESS) bad++; i++; } if (good == my_strlen(sol)) return (1); print_placement(good, bad); return (0); }
int scan_from(char *str, char *buf, int type) { char *p; char *p1; /* Skip leading white space. */ p = str; while(*p != '\0' && isspace(*p)) p++; /* Scan for a beginning marker. */ while(*p != '\0' && !is_instr(*p, start_type[type])) p++; if(*p == '\0') return 0; /* Strip off marker, if there is one. */ if(*start_type[type] != '\0') p++; p1 = buf; /* Copy in relevant portion of the string. */ while(*p != '\0' && !is_instr(*p, end_type[type])) { *p1 = *p; p++; p1++; } /* Strip off trailing white space. */ p1--; while(isspace(*p1)) p1--; p1++; *p1 = '\0'; return 1; }
int is_inpion(char *pion, char *enter) { int i; i = 0; while (enter[i]) { if (is_instr(enter[i], pion) == EXIT_FAILURE) return (EXIT_FAILURE); i++; } return (EXIT_SUCCESS); }
void read_file(char *filename,struct Process **pproc) { FILE *fp; char *endfile,line[MAXSTR],*p,save_token[MAXSTR]; int label_bool=0,line_count=0,num_code=0,n_args; struct instruction_node *new_instr; struct Process *proc; struct expr_node *new_expr; proc=(struct Process*)malloc(sizeof(struct Process)); if(proc==NULL) die("errore nell'allocare struct Process"); proc->pt=NULL; proc->pc=NULL; proc->prev=NULL; proc->next=NULL; proc->processID=get_processID(); fp=fopen(filename,"r"); if(fp==NULL) die("error opening file"); proc->pc=(struct process_construct*)malloc(sizeof(struct process_construct)); if(proc->pc==NULL) die("errore nell'allocare process_construct"); proc->pc->first=NULL; proc->pc->last=NULL; proc->pc->len=0; proc->pc->org[0]='\0'; proc->pc->vt_first=NULL; proc->pc->vt_last=NULL; while((endfile=fgets(line,MAXSTR,fp))!=NULL) { line_count++; p=&line[0]; if(*p==';') continue; p=skip_space(p); if(*p=='\n') continue; p=get_token(p); p=skip_space(p); if((strcmp(my_token,"org"))==0) { p=get_word(p);strncpy(proc->pc->org,my_token,MAXSTR); if(*p!='\n') {sprintf(save_token,"parse error at line %d. not an end line after the org argument",line_count);die(save_token);} continue; } if((strcmp(my_token,"end"))==0) { p=skip_space(p); if(*p!='\n') {sprintf(save_token,"parse error after 'end' at line %d.",line_count);die(save_token);} break; } if((strcmp(my_token,"assert"))==0) { take_assert(p); new_expr=(struct expr_node*)malloc(sizeof(struct expr_node)); if(new_expr==NULL) {printf("at line %d, ",line_count);die("error allocating new_expr");} p=skip_space(p); p=get_b_arg(&new_expr,p,line_count); insert_in_vt(ASSERT_STR,new_expr,line_count,proc); continue; } if(*p==':') { insert_label(my_token,num_code,line_count,proc);p=skip_space(++p); if(*p=='\n') continue; p=get_token(p); } n_args=is_instr(my_token,line_count); if(n_args!=-1) { new_instr=(struct instruction_node*)malloc(sizeof(struct instruction_node)); if(new_instr==NULL) {printf("at line %d , ",line_count);die("error allocating new_instr");} strncpy(new_instr->instr,my_token,MAXSTR); new_instr->num_node=num_code++; new_instr->line_count=line_count; new_instr->prev=NULL; new_instr->next=NULL; new_instr->code=NULL; new_instr->left=NULL; new_instr->right=NULL; new_instr->laddr[0]='#';new_instr->laddr[1]='\0'; new_instr->raddr[0]='#';new_instr->raddr[1]='\0'; strcpy(new_instr->modifier,"NULL"); if(*p=='.'){ p=get_word(++p);is_modifier(my_token,line_count); strncpy(new_instr->modifier,my_token,MAXMOD); } p=skip_space(p); if(n_args>0) { p=get_addr_mode(p); //$ by default. the result is in my_token new_instr->laddr[0]=my_token[0]; new_instr->laddr[1]='\0'; //p=get_token(p); //new_instr->left=(struct expr_node*)malloc(sizeof(struct expr_node)); //if(new_instr->left==NULL){ //printf("at line %d , ",line_count);die("error alocating left expr");} p=get_arg(&new_instr->left,p,line_count); p=skip_space(p); if(n_args>1) { if(*p!=',') {printf("at line %d , ",line_count);die("a comma expected (,)");} p=skip_space(++p); p=get_addr_mode(p); //$ by default. the result is in my_token new_instr->raddr[0]=my_token[0]; new_instr->raddr[1]='\0'; //p=get_token(p); //new_instr->right=(struct expr_node*)malloc(sizeof(struct expr_node)); //if(new_instr->right==NULL){ //printf("at line %d , ",line_count);die("error allocating right expr");} p=get_arg(&new_instr->right,p,line_count); p=skip_space(p); } } if(*p!='\n') {printf("at line %d , ",line_count);die("not an ending line after command");} //add the node add_node(new_instr,proc); continue; } strncpy(save_token,my_token,MAXSTR); p=get_token(p); if((strcmp(my_token,"equ"))==0) { new_expr=(struct expr_node*)malloc(sizeof(struct expr_node)); if(new_expr==NULL) {printf("at line %d, ",line_count);die("error allocating new_expr");} p=skip_space(p); p=get_arg(&new_expr,p,line_count); insert_in_vt(save_token,new_expr,line_count,proc); continue; } } fclose(fp); proc->pc->len=num_code; add_proc(proc); *pproc=proc; }