void readInstructions(FILE * fp) { int address = 0; char opcode[OPCODE_SIZE]; char operand[OPERAND_SIZE]; printf("\n** Output **\n\n"); while(fscanf(fp, "%s", opcode) != EOF){ // operand check(read into operand var) if(hasOperand(opcode)){ fscanf(fp, "%s", operand); } else{ operand[0] = 0; } // insert the instruction into the instruction table insertInstruction(address, opcode, operand); address++; // discard any possible comments on current line discardline(fp); } }
virtual IHqlExpression * createTransformed(IHqlExpression * expr) { OwnedHqlExpr transformed = NewHqlTransformer::createTransformed(expr); if (transformed->getOperator() == no_getgraphresult) { if (hasOperand(transformed, graph)) return removeAttribute(transformed, externalAtom); } return transformed.getClear(); }
void printInstructionTable(){ int i; for(i = 0; i < instrTable.instrCount; i++){ if(hasOperand(instrTable.instructions[i].opcode)) printf("%s %s\n", instrTable.instructions[i].opcode, instrTable.instructions[i].operand); else printf("%s\n", instrTable.instructions[i].opcode); printf("\n"); } }
int validOp(char opcode[OPCODE_SIZE]){ if(hasOperand(opcode) || strcmp(opcode, "nop") == 0 || strcmp(opcode, "add") == 0 || strcmp(opcode, "sub") == 0 || strcmp(opcode, "mult") == 0 || strcmp(opcode, "div") == 0 || strcmp(opcode, "not") == 0 || strcmp(opcode, "and") == 0 || strcmp(opcode, "or") == 0 || strcmp(opcode, "tsteq") == 0 || strcmp(opcode, "tstne") == 0 || strcmp(opcode, "tstlt") == 0 || strcmp(opcode, "tstle") == 0 || strcmp(opcode, "tstgt") == 0 || strcmp(opcode, "tstge") == 0 || strcmp(opcode, "halt") == 0){ return 1; } return 0; }
void main(int argc, char *argv[]){ char *filename = argv[1]; FILE *fp; if(strcmp(filename, "-s") == 0){ testStack(); exit(0); } if(strcmp(filename, "-t") == 0){ testTable(); exit(0); } if(strcmp(filename, "-i") == 0){ testInstruction(); exit(0); } if(argc == 1){ printf("Proper use: wi <filename.wic>\n"); exit(0); } else if((fp = fopen(filename, "r")) == NULL) { printf("Error opening file. File doesn't exist.\n"); exit(0); } initialize(); char opcode[OPCODE_SIZE]; char operand[OPERAND_SIZE]; int address = 0; while(fscanf(fp, "%s", opcode) != EOF){ if(hasOperand(opcode)){ if(fscanf(fp, "%s", operand) == EOF){ insertInstruction(address, opcode, operand); address++; break; } } if(!validOp(opcode)){ if(fscanf(fp, "%s", operand) != EOF && strcmp(operand, "label") == 0){ char temp[OPERAND_SIZE]; strcpy(temp, operand); strcpy(operand, opcode); strcpy(opcode, temp); } else{ printf("Invalid opcode: %s\n", opcode); exit(0); } } else if(hasOperand(opcode) == 0){ strcpy(operand, ""); } insertInstruction(address, opcode, operand); address++; //discard the rest of the line while(fgetc(fp) != '\n'){}; } fclose(fp); runProgram(); }