int SystemImports::get_index_of(const char *namw) { int bestMatch = -1; char altName[200]; sprintf(altName, "%s$", namw); int idx = btree.findValue((const char*)namw); if (idx >= 0) return idx; // if it's a function with a mangled name, allow it idx = btree.findValue(altName, &ccCompareStringsPartial); if (idx >= 0) return idx; if ((strlen(namw) > 3) && ((namw[strlen(namw) - 2] == '^') || (namw[strlen(namw) - 3] == '^'))) { // Function with number of prametrs on the end // attempt to find it without the param count strcpy(altName, namw); strrchr(altName, '^')[0] = 0; return get_index_of(altName); } return -1; }
const ScriptImport *SystemImports::getByName(const char *name) { int o = get_index_of(name); if (o < 0) return NULL; return &imports[o]; }
void SystemImports::remove(const char *nameToRemove) { int idx = get_index_of(nameToRemove); if (idx < 0) return; btree.removeEntry(imports[idx].Name); imports[idx].Name = NULL; imports[idx].Value.Invalidate(); imports[idx].InstancePtr = NULL; }
int is_label(char *line) { int pos; pos = -1; if (line[0] == '\t') return (-1); if ((pos = get_index_of(line, ':')) == -1) return (-1); if (line[pos + 1] != ' ' && line[pos + 1] != '\t') return (-1); return (0); }
static void emit_code(void) { int c, n, op, id, commented = 0; c = getchar(); while (c != EOF) { id = get_index_of("[]+-<>.,", c); if (commented && id != -1) { printf(" */\n"); commented = 0; } if (id == 0) { /* '[' */ print_code("for(;*ptr;){"); ++g_indent_level; c = getchar(); } else if (id == 1) { /* ']' */ if (--g_indent_level < 0) { fprintf(stderr, "Error: Extra ']' found.\n"); return; } print_code("}"); c = getchar(); } else if (2 <= id && id <= 5) { /* '+', '-', '<', '>' */ op = c; n = 0; for (;c == op; n++) c = getchar(); emit_ptr_arith(n, op); } else if (id == 6) { /* '.' */ print_code("putchar(*ptr);"); c = getchar(); } else if (id == 7) { /* ',' */ print_code("*ptr=getchar();"); c = getchar(); } else { /* others */ if (c != '\r' && c != '\n') { if (!commented) { printf("/* "); commented = 1; } putchar(c); } c = getchar(); } } }
int SystemImports::add(const char *name, const RuntimeScriptValue &value, ccInstance *anotherscr) { int ixof; if ((ixof = get_index_of(name)) >= 0) { // Only allow override if not a script-exported function if (anotherscr == NULL) { imports[ixof].Value = value; imports[ixof].InstancePtr = anotherscr; } return 0; } ixof = numimports; for (int ii = 0; ii < numimports; ii++) { if (imports[ii].Name == NULL) { ixof = ii; break; } } if (ixof >= this->bufferSize) { if (this->bufferSize > 50000) return -1; // something has gone badly wrong this->bufferSize += 1000; this->imports = (ScriptImport*)realloc(this->imports, sizeof(ScriptImport) * this->bufferSize); } btree.addEntry(name, ixof); imports[ixof].Name = name; // TODO: rather make a string copy here for safety reasons imports[ixof].Value = value; imports[ixof].InstancePtr = anotherscr; if (ixof == numimports) numimports++; return 0; }