Term::Term(Type type, void* source = 0) : type(type) { switch (type) { case NOOP: break; case PUSH: case POP: break; case WRITE_SIGNED: value.as_signed = *static_cast<Signed*>(source); break; case WRITE_UNSIGNED: value.as_unsigned = *static_cast<Unsigned*>(source); break; case WRITE_DOUBLE: value.as_double = *static_cast<Double*>(source); break; default: IMPOSSIBLE("invalid Term type"); } }
void listing_init(FILE *in, char *base_filename) { long in_file_size; char *buffer; char *pos; size_t bytes_read; size_t bytes_left; char c; char *begin; char *end; int current_line; if(!option_generate_listing) { return; } if(!in) { IMPOSSIBLE("Bad input file stream given for listing"); } if(!base_filename || strlen(base_filename) <= 0) { IMPOSSIBLE("Bad base filename given for listing"); } /* +5 is for ".lst" and terminating NULL */ SAFE_MALLOC(listing_filename, char *, sizeof(char) * (strlen(base_filename) + 5)); sprintf(listing_filename, "%s.lst", base_filename); listing_file = fopen(listing_filename, "wb"); if(!listing_file) { ERROR("Could not open listing file, \"%s\", for output", listing_filename); } if(fseek(in, 0, SEEK_END) != 0) { ERROR("Couldn't seek to end of input file for some reason"); } /* * Read in the file to a temporary buffer */ in_file_size = ftell(in); if(in_file_size <= 0) { ERROR("The input file is empty"); } rewind(in); SAFE_MALLOC(buffer, char *, in_file_size + 1); bytes_left = in_file_size; pos = buffer; while((bytes_read = fread(pos, 1, bytes_left, in)) < bytes_left) { pos += bytes_read; bytes_left -= bytes_read; } line_cnt = 0; end = buffer; while((c = *end) != '\0') { if(c == '\n') { line_cnt++; } end++; } if(*(end - 1) != '\n') { line_cnt++; } SAFE_MALLOC(lines, char **, sizeof(char *) * line_cnt); line_digits = calculate_digits(line_cnt + 1); begin = buffer; end = buffer; current_line = 0; while((c = *end) != '\0') { if(c == '\n') { int line_length = (int)(end - begin); SAFE_MALLOC(lines[current_line], char *, sizeof(char) * (line_length + 1)); memcpy(lines[current_line], begin, line_length); current_line++; end++; begin = end; } else {