void FileParser::parse(const std::string& filename) { std::ifstream file(filename); if (file.good()) { // Refactor this part. It should not rely on order std::string line; std::getline(file, line); _parse_format(line); std::getline(file, line); if (0 == _parse_name(line)) { name = "Placeholder Game"; } std::getline(file, line); if (0 == _parse_conditions(line)) { needed_to_live.push_back(2); needed_to_live.push_back(3); needed_to_born.push_back(3); } // It works fine, but looks like it from 80's int x, y; int x_min = 0; int x_max = 0; int y_min = 0; int y_max = 0; std::pair<int, int> pair; while (file >> x >> y) { if (x > x_max) x_max = x; if (x < x_min) x_min = x; if (y > y_max) y_max = y; if (y < y_min) y_min = y; pair.first = x; pair.second = y; points.push_back(pair); } // Calculating shift int delta = 0; if (x_min < y_min && x_min < 0) { delta = x_min; } else if (y_min < 0) { delta = y_min; } if (0 != delta) { for (auto& pair : points) { pair.first -= delta; // because delta < 0 pair.second -= delta; } } x_max -= delta; y_max -= delta; x_max > y_max ? field_size = x_max + 3 : field_size = y_max + 3; } else {
static ChimpRef * assemble_module (const char *filename) { char buf[8192]; ChimpRef *code; ChimpRef *mod; ChimpRef *method; FILE *stream = fopen (filename, "r"); if (stream == NULL) { fprintf (stderr, "error: failed to open %s\n", filename); return NULL; } code = chimp_code_new (); if (code == NULL) { fprintf (stderr, "error: failed to allocate code object\n"); return NULL; } while (!feof (stream)) { const char *ptr; ChimpRef *opname; ChimpOpcode opcode; if (fgets (buf, sizeof(buf)-1, stream) == NULL) { break; } ptr = buf; _skip_whitespace (&ptr); if (!*ptr) continue; if (*ptr == '#') continue; if (!_parse_name (&ptr, &opname)) { fprintf (stderr, "error: expected name\n"); goto error; } opcode = _str_to_opcode (CHIMP_STR_DATA (opname)); switch (opcode) { case CHIMP_OPCODE_PUSHNIL: { if (!chimp_code_pushnil (code)) { goto error; } break; } case CHIMP_OPCODE_DUP: { if (!chimp_code_dup (code)) { goto error; } break; } case CHIMP_OPCODE_NOT: { if (!chimp_code_not (code)) { goto error; } break; } case CHIMP_OPCODE_RET: { if (!chimp_code_ret (code)) { goto error; } break; } case CHIMP_OPCODE_SPAWN: { if (!chimp_code_spawn (code)) { goto error; } break; } case CHIMP_OPCODE_MAKEARRAY: { ChimpRef *nargs; if (!_parse_const_int (&ptr, &nargs)) { goto error; } if (!chimp_code_makearray ( code, CHIMP_INT(nargs)->value)) { goto error; } break; } case CHIMP_OPCODE_MAKECLOSURE: { if (!chimp_code_makeclosure (code)) { goto error; } break; } case CHIMP_OPCODE_POP: { if (!chimp_code_pop (code)) { goto error; } break; } case CHIMP_OPCODE_PUSHNAME: { ChimpRef *name; if (!_parse_name (&ptr, &name)) { goto error; } if (!chimp_code_pushname (code, name)) { goto error; } break; } case CHIMP_OPCODE_STORENAME: { ChimpRef *name; if (!_parse_name (&ptr, &name)) { goto error; } if (!chimp_code_storename (code, name)) { goto error; } break; } case CHIMP_OPCODE_GETATTR: { ChimpRef *name; if (!_parse_name (&ptr, &name)) { goto error; } if (!chimp_code_getattr (code, name)) { goto error; } break; } case CHIMP_OPCODE_GETITEM: { if (!chimp_code_getitem (code)) { goto error; } break; } case CHIMP_OPCODE_PUSHCONST: { ChimpRef *value; if (!_parse_const (&ptr, &value)) { goto error; } if (!chimp_code_pushconst (code, value)) { goto error; } break; } case CHIMP_OPCODE_ADD: { if (!chimp_code_add (code)) { goto error; } break; } case CHIMP_OPCODE_SUB: { if (!chimp_code_sub (code)) { goto error; } break; } case CHIMP_OPCODE_MUL: { if (!chimp_code_mul (code)) { goto error; } break; } case CHIMP_OPCODE_DIV: { if (!chimp_code_div (code)) { goto error; } break; } case CHIMP_OPCODE_GETCLASS: { if (!chimp_code_getclass (code)) { goto error; } break; } case CHIMP_OPCODE_CALL: { ChimpRef *nargs; if (!_parse_const_int (&ptr, &nargs)) { goto error; } if (!chimp_code_call ( code, (uint8_t) CHIMP_INT(nargs)->value)) { goto error; } break; } default: fprintf (stderr, "error: unknown or unsupported opname: %s\n", CHIMP_STR_DATA (opname)); goto error; }; _skip_whitespace (&ptr); if (*ptr) { fprintf (stderr, "error: too many arguments for op: %s\n", CHIMP_STR_DATA(opname)); goto error; } } fclose (stream); /* printf ("%s\n", CHIMP_STR_DATA (chimp_code_dump (code))); */ mod = chimp_module_new_str ("main", NULL); if (mod == NULL) { return NULL; } method = chimp_method_new_bytecode (mod, code); if (method == NULL) { return NULL; } if (!chimp_module_add_local (mod, CHIMP_STR_NEW("main"), method)) { return NULL; } return mod; error: fclose (stream); return NULL; }