Look [code] How would you call this style?
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Look
#include <cstdlib>
#include <cstdio>
#include <cstdint>
#include <cassert>
#include <cstring>
#define TODO() do { \
fprintf(stderr, "TODO: %s(%s:%d): ", __FILE__, __func__, __LINE__); \
assert(!"Not implemented"); \
} while (0)
template <typename T>
struct Maybe {
int exist;
T data;
Maybe(): exist(0) {}
Maybe(T val): exist(1), data(val) {}
};
template <typename T>
struct Buffer {
int len;
T *items;
int size() { return len * sizeof(T); }
int sizeof_element() { return sizeof(T); }
Buffer(int len): len(len) {
items = new T[len];
std::memset(items, 0, size());
}
T &operator[](int idx) {
assert(idx < len);
return items[idx];
}
};
template <typename T>
struct Slice: public Buffer<T> {
int cap;
Slice(int new_cap = 100): Buffer<T>(new_cap), cap(new_cap) {
len = 0;
}
operator Buffer<T>() const {
return {len, cap};
}
void push(T val) {
if (cap <= len) {
// assert(!"ERR:Slice:push: Not implemented");
cap *= 1.5;
items = (T *)std::realloc(items, cap);
}
items[len++] = val;
}
T pop() {
if (len == 0) assert(!"ERR:Slice:pop: Underflow");
return items[--len];
}
Maybe<T> try_pop() {
if (len == 0) return Maybe<T>();
return Maybe(items[--len]);
}
T &top() {
return items[len];
}
};
enum Op: uint8_t {
EXIT,
MOVE_RIGHT,
MOVE_LEFT,
INC,
DEC,
OUT,
IN,
LOOP_BEGIN,
LOOP_END,
};
struct VM {
Buffer<uint8_t> tape; // TODO: split data, and program or start program from the back
int dp, ip;
VM(Buffer<uint8_t> tape): tape(tape), dp(0), ip(0) {}
bool eval() {
Op inst = (Op)tape[ip++];
switch (inst) {
case Op::EXIT: return false;
case Op::MOVE_RIGHT: dp++; break;
case Op::MOVE_LEFT: dp--; break;
case Op::INC: tape[dp]++; break;
case Op::DEC: tape[dp]--; break;
case Op::OUT: {
auto val = tape[dp];
std::printf("%d, %c\n", val, val);
}; break;
case Op::IN: {
uint8_t val = getchar();
tape[dp] = val;
}; break;
case Op::LOOP_BEGIN: {
TODO();
} break;
case Op::LOOP_END: {
TODO();
} break;
}
return true;
}
};
void compile(const char *path) {
FILE *fp = std::fopen(path, "r");
if (!fp) {
perror("ERR:main");
exit(-1);
}
std::fseek(fp, 0, SEEK_END);
auto size = ftell(fp);
std::rewind(fp);
auto data = new char[size + 1];
std::fread(data, 1, size, fp);
data[size] = 0;
std::fclose(fp);
std::printf("File content:\n\"\"\"\n%s\n\"\"\"\n", data);
Slice<Op> stack(150);
for (int i = 0; i < size; i++) {
char c = data[i];
switch (c) {
case '>': stack.push(Op::MOVE_RIGHT); break;
case '<': stack.push(Op::MOVE_LEFT); break;
case '+': stack.push(Op::INC); break;
case '-': stack.push(Op::DEC); break;
case '.': stack.push(Op::OUT); break;
case ',': stack.push(Op::IN); break;
case '[': stack.push(Op::LOOP_BEGIN); break;
case ']': stack.push(Op::LOOP_END); break;
case '/': {
if (i + 1 < size && data[i + 1] == '/') {
i++;
while (data[++i] != '\n' || i < size);
}
} break;
case ' ': break;
default: {
fprintf(stderr, "Err: character %c:%d isn't valid", c, c);
assert(!"Err:main: Unknown operation");
}
}
}
fp = nullptr;
fp = std::fopen("out.ebf", "wb");
if (!fp) {
perror("Err:main");
exit(-1);
}
std::fwrite(stack.items, sizeof(Op), stack.len, fp);
std::fclose(fp);
return;
}
int main(int argc, char **argv) {
for (int i = 1; i < argc; i++) {
char *cp;
for (cp = argv[i]; *cp != '.' && *cp != 0; cp++) ;
printf("%s\n", cp);
if (!strcmp(cp, ".bf")) compile(argv[i]);
else if (!strcmp(cp, ".ebf")) TODO();
else assert(!"Fuck!");
}
Buffer<uint8_t> bf(1000);
int ip = 0;
bf[ip++] = Op::INC;
bf[ip++] = Op::INC;
bf[ip++] = Op::INC;
// bf[ip++] = Op::INC;
bf[ip++] = Op::OUT;
bf[ip++] = Op::EXIT;
bf[ip++] = Op::EXIT;
VM vm(bf);
while (vm.eval());
return 0;
}
;
How would you call this style?
This code exhibits a mix of styles, making it hard to pin down with a single label, but we can identify key characteristics:
1. C-Style Foundations with C++ Enhancements:
FILE*, fopen, fread, fwrite, printf, getchar - all functions inherited from C.new, delete (implicitly through Buffer destruction), and realloc. While C++ has smart pointers and RAII to automate this, this code opts for explicit control. The realloc in Slice::push is a classic C-style dynamic array growth mechanism.struct for Data Structures: Uses struct for Maybe, Buffer, Slice, and VM. While classes could be used, structs are often preferred in C-like C++ for simpler data containers.std::memset: C-style memory initialization.2. Some Object-Oriented Flavors:
Buffer, Slice, and VM structures have methods (like size(), push(), eval()). This is an object-oriented concept, though the level of abstraction and encapsulation is limited.Slice class overloads the [] operator for array-like access and Buffer<T>() for implicit conversion.3. Explicit and Low-Level:
uint8_t: Explicit use of unsigned 8-bit integers for the tape in VM and the Op enum. This reflects a focus on representing data in a specific format.enum Op: uint8_t: Enumerated type for operations.dp and ip: Explicit index pointers (dp for data pointer, ip for instruction pointer) for the VM.4. Functional aspects:
5. Error Handling:
assert: Heavy use of assert for runtime checks. This is good for development but typically removed in production builds. The error messages are fairly basic.perror: Used for system call errors (like fopen failing).TODO() macro: A common placeholder for unimplemented features, using assert to halt execution if reached.Possible Labels (with caveats):
Why it's hard to give a single label:
The code blends elements from different paradigms. It's not purely object-oriented, nor is it pure C. It's a hybrid approach.
In summary:
The style is best described as a procedural/imperative style with some object-oriented features and a strong focus on low-level data representation and explicit memory management. It looks like a developer comfortable with C but utilizing some C++ features to organize the code better, particularly the Buffer and Slice classes.