/* Returns pointer to a parsed sentence object, Returns NULL is parsing failed */ struct sentence *parse_sentence(struct sentence *new_sen, char *line) { char word[MAXLINE]; char *line_pointer = line; int coding_times; /* fetch the first word*/ getword(line_pointer, word, sizeof(word)); /* If the first word is empty, this is an empty sentence*/ if (strlen(line) == 1) { new_sen->type = EMPTY; return new_sen; } else if (strncmp(COMMENT_SIGN, word, 1) == 0) { new_sen->type = COMMENT; return new_sen; } /* Parsing the label of the sentence */ if (sscanf(line_pointer, " %[a-zA-Z0-9]: ", new_sen->label) == 1 && *(strchr(line, new_sen->label[0]) + strlen(new_sen->label)) == ':') { /* Skipping the label start */ line_pointer = strchr(line_pointer, ':') + 1; } /* nullifing the value that was read into new_sen.label */ else strcpy(new_sen->label, ""); /* trying to scan instruction into word, if successful, moving on to parsing */ if (sscanf(line_pointer, INSTRUCTION_FORMAT, word) == 1) { /* Moving the line pointer over the intruction*/ line_pointer = strchr(line_pointer, '.') + strlen(word) + 1; return parse_instructions(new_sen, word, line_pointer); } /* The sentence is operation sentence only if the first word is not empty*/ else if (sscanf(line_pointer, " %3[a-z]%1d ", word, &coding_times) == 2 || sscanf(line_pointer, " %4[a-z]%1d ", word, &coding_times) == 2) { /* setting the line pointer over the operator */ line_pointer = strchr(line_pointer, word[0]) + 4; return parse_operation(new_sen, line_pointer, word, coding_times); } return NULL; }
int interpret_program(tvm_program_t* p, char* filename, tvm_memory_t* pMemory) { int i; FILE* pFile = NULL; int source_length = 0; char* source = NULL; tvm_lexer_t* lexer = NULL; /* Attempt to open the file. If the file cannot be opened, try once more. */ if(filename) for(i = 0; i < 2; i++) if(!pFile) pFile = tvm_fopen(filename, ".vm", "r"); if(!pFile) { printf("File was not found, or does not exist. Unable to interpret.\n"); return 1; } source_length = tvm_flength(pFile); source = malloc(source_length); tvm_fcopy(source, source_length, pFile); lexer = lexer_create(); lex(lexer, source); free(source); fclose(pFile); if(parse_labels(p, (const char***)lexer->tokens) != 0) return 1; if(parse_instructions(p, (const char***)lexer->tokens, pMemory) != 0) return 1; lexer_destroy(lexer); return 0; }
int main(int argc, char *argv[]) { PointSet *ps; int pi; char *infile, *outfile; /*Check for Input, Output files */ if (argc == 1) { printf("K5SPLODE.EXE USAGE:\nK5SPLODE.EXE <infile> <outfile>"); return 1; } if (argc != 3) { quit("Incorrect number of arguments"); return 1; } infile = argv[1]; outfile = argv[2]; srand(time(NULL)); memset(pointsets, 0, sizeof(PointSet) * MAX_POINTSETS); parse_instructions(infile); pi = 0; ps = pointsets; while(ps->points) { import_points(bmp256_load(ps->filename),pi ,ps->points); import_motions(ps->Xcomponent, ps->Ycomponent, pi, ps->points); if ((pi += ps->points) >= 4000) { quit("Points limit exceeded!"); return 1; } ps++; } export_MSC(outfile); return 0; }