Fixed the code style in *.c files to two spaces indentation and add a linter named funstx to Fun. (0.39.0)
This commit is contained in:
parent
33001a7ad2
commit
03b2532474
237 changed files with 17550 additions and 13911 deletions
|
|
@ -8,7 +8,7 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file apop.c
|
||||
* @file apop.c
|
||||
* @brief Implements the OP_APOP opcode for removing elements from arrays in the VM.
|
||||
*
|
||||
* This file handles the OP_APOP instruction, which removes the last element from an array
|
||||
|
|
@ -33,17 +33,17 @@
|
|||
*/
|
||||
|
||||
case OP_APOP: {
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY) {
|
||||
fprintf(stderr, "Runtime type error: ARR_APOP expects array\n");
|
||||
exit(1);
|
||||
}
|
||||
Value out;
|
||||
if (!array_pop(&arr, &out)) {
|
||||
fprintf(stderr, "Runtime error: pop from empty array\n");
|
||||
exit(1);
|
||||
}
|
||||
free_value(arr);
|
||||
push_value(vm, out);
|
||||
break;
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY) {
|
||||
fprintf(stderr, "Runtime type error: ARR_APOP expects array\n");
|
||||
exit(1);
|
||||
}
|
||||
Value out;
|
||||
if (!array_pop(&arr, &out)) {
|
||||
fprintf(stderr, "Runtime error: pop from empty array\n");
|
||||
exit(1);
|
||||
}
|
||||
free_value(arr);
|
||||
push_value(vm, out);
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,13 +32,13 @@
|
|||
*/
|
||||
|
||||
case OP_CLEAR: {
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY) {
|
||||
fprintf(stderr, "Runtime type error: CLEAR expects array\n");
|
||||
exit(1);
|
||||
}
|
||||
array_clear(&arr);
|
||||
free_value(arr);
|
||||
push_value(vm, make_int(0));
|
||||
break;
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY) {
|
||||
fprintf(stderr, "Runtime type error: CLEAR expects array\n");
|
||||
exit(1);
|
||||
}
|
||||
array_clear(&arr);
|
||||
free_value(arr);
|
||||
push_value(vm, make_int(0));
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file contains.c
|
||||
* @file contains.c
|
||||
* @brief Implements the OP_CONTAINS opcode for checking array membership in the VM.
|
||||
*
|
||||
* This file handles the OP_CONTAINS instruction, which checks if a value is present in an array.
|
||||
|
|
@ -32,15 +32,15 @@
|
|||
*/
|
||||
|
||||
case OP_CONTAINS: {
|
||||
Value needle = pop_value(vm);
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY) {
|
||||
fprintf(stderr, "Runtime type error: CONTAINS expects (array, value)\n");
|
||||
exit(1);
|
||||
}
|
||||
int ok = array_contains(&arr, &needle);
|
||||
free_value(arr);
|
||||
free_value(needle);
|
||||
push_value(vm, make_int(ok ? 1 : 0));
|
||||
break;
|
||||
Value needle = pop_value(vm);
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY) {
|
||||
fprintf(stderr, "Runtime type error: CONTAINS expects (array, value)\n");
|
||||
exit(1);
|
||||
}
|
||||
int ok = array_contains(&arr, &needle);
|
||||
free_value(arr);
|
||||
free_value(needle);
|
||||
push_value(vm, make_int(ok ? 1 : 0));
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file enumerate.c
|
||||
* @file enumerate.c
|
||||
* @brief Implements the OP_ENUMERATE opcode for enumerating arrays in the VM.
|
||||
*
|
||||
* This file handles the OP_ENUMERATE instruction, which creates an array of [index, value] pairs
|
||||
|
|
@ -32,13 +32,13 @@
|
|||
*/
|
||||
|
||||
case OP_ENUMERATE: {
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY) {
|
||||
fprintf(stderr, "Runtime type error: ENUMERATE expects array\n");
|
||||
exit(1);
|
||||
}
|
||||
Value out = bi_enumerate(&arr);
|
||||
free_value(arr);
|
||||
push_value(vm, out);
|
||||
break;
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY) {
|
||||
fprintf(stderr, "Runtime type error: ENUMERATE expects array\n");
|
||||
exit(1);
|
||||
}
|
||||
Value out = bi_enumerate(&arr);
|
||||
free_value(arr);
|
||||
push_value(vm, out);
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file index_get.c
|
||||
* @file index_get.c
|
||||
* @brief Implements the OP_INDEX_GET opcode for array and map indexing in the VM.
|
||||
*
|
||||
* This file handles the OP_INDEX_GET instruction, which retrieves an element from
|
||||
|
|
@ -34,34 +34,41 @@
|
|||
*/
|
||||
|
||||
case OP_INDEX_GET: {
|
||||
Value idx = pop_value(vm);
|
||||
Value container = pop_value(vm);
|
||||
Value idx = pop_value(vm);
|
||||
Value container = pop_value(vm);
|
||||
#ifdef FUN_DEBUG
|
||||
fprintf(stderr, "DEBUG INDEX_GET: container.type=%d idx.type=%d\n",
|
||||
container.type, idx.type);
|
||||
fprintf(stderr, "DEBUG INDEX_GET: container.type=%d idx.type=%d\n",
|
||||
container.type, idx.type);
|
||||
#endif
|
||||
if (container.type == VAL_ARRAY) {
|
||||
if (idx.type != VAL_INT) { fprintf(stderr, "INDEX_GET index must be int for array\n"); exit(1); }
|
||||
Value elem;
|
||||
if (!array_get_copy(&container, (int)idx.i, &elem)) {
|
||||
fprintf(stderr, "Runtime error: index out of range\n"); exit(1);
|
||||
}
|
||||
free_value(container);
|
||||
free_value(idx);
|
||||
push_value(vm, elem);
|
||||
} else if (container.type == VAL_MAP) {
|
||||
if (idx.type != VAL_STRING) { fprintf(stderr, "INDEX_GET key must be string for map\n"); exit(1); }
|
||||
Value out;
|
||||
if (!map_get_copy(&container, idx.s ? idx.s : "", &out)) {
|
||||
out = make_nil();
|
||||
}
|
||||
free_value(container);
|
||||
free_value(idx);
|
||||
push_value(vm, out);
|
||||
} else {
|
||||
fprintf(stderr, "Runtime type error: INDEX_GET expects array or map (got container=%s, index=%s)\n",
|
||||
value_type_name(container.type), value_type_name(idx.type));
|
||||
exit(1);
|
||||
if (container.type == VAL_ARRAY) {
|
||||
if (idx.type != VAL_INT) {
|
||||
fprintf(stderr, "INDEX_GET index must be int for array\n");
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
Value elem;
|
||||
if (!array_get_copy(&container, (int)idx.i, &elem)) {
|
||||
fprintf(stderr, "Runtime error: index out of range\n");
|
||||
exit(1);
|
||||
}
|
||||
free_value(container);
|
||||
free_value(idx);
|
||||
push_value(vm, elem);
|
||||
} else if (container.type == VAL_MAP) {
|
||||
if (idx.type != VAL_STRING) {
|
||||
fprintf(stderr, "INDEX_GET key must be string for map\n");
|
||||
exit(1);
|
||||
}
|
||||
Value out;
|
||||
if (!map_get_copy(&container, idx.s ? idx.s : "", &out)) {
|
||||
out = make_nil();
|
||||
}
|
||||
free_value(container);
|
||||
free_value(idx);
|
||||
push_value(vm, out);
|
||||
} else {
|
||||
fprintf(stderr, "Runtime type error: INDEX_GET expects array or map (got container=%s, index=%s)\n",
|
||||
value_type_name(container.type), value_type_name(idx.type));
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file index_of.c
|
||||
* @file index_of.c
|
||||
* @brief Implements the OP_INDEX_OF opcode for finding the index of a value in an array in the VM.
|
||||
*
|
||||
* This file handles the OP_INDEX_OF instruction, which finds the index of a value in an array.
|
||||
|
|
@ -32,15 +32,15 @@
|
|||
*/
|
||||
|
||||
case OP_INDEX_OF: {
|
||||
Value needle = pop_value(vm);
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY) {
|
||||
fprintf(stderr, "Runtime type error: INDEX_OF expects (array, value)\n");
|
||||
exit(1);
|
||||
}
|
||||
int idx = array_index_of(&arr, &needle);
|
||||
free_value(arr);
|
||||
free_value(needle);
|
||||
push_value(vm, make_int(idx));
|
||||
break;
|
||||
Value needle = pop_value(vm);
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY) {
|
||||
fprintf(stderr, "Runtime type error: INDEX_OF expects (array, value)\n");
|
||||
exit(1);
|
||||
}
|
||||
int idx = array_index_of(&arr, &needle);
|
||||
free_value(arr);
|
||||
free_value(needle);
|
||||
push_value(vm, make_int(idx));
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file index_set.c
|
||||
* @file index_set.c
|
||||
* @brief Implements the OP_INDEX_SET opcode for array and map assignment in the VM.
|
||||
*
|
||||
* This file handles the OP_INDEX_SET instruction, which assigns a value to an
|
||||
|
|
@ -32,32 +32,39 @@
|
|||
* @date 2025-10-16
|
||||
*/
|
||||
|
||||
|
||||
case OP_INDEX_SET: {
|
||||
Value v = pop_value(vm);
|
||||
Value idx = pop_value(vm);
|
||||
Value container = pop_value(vm);
|
||||
Value v = pop_value(vm);
|
||||
Value idx = pop_value(vm);
|
||||
Value container = pop_value(vm);
|
||||
#ifdef FUN_DEBUG
|
||||
fprintf(stderr, "DEBUG INDEX_SET: container.type=%d idx.type=%d value.type=%d\n",
|
||||
container.type, idx.type, v.type);
|
||||
fprintf(stderr, "DEBUG INDEX_SET: container.type=%d idx.type=%d value.type=%d\n",
|
||||
container.type, idx.type, v.type);
|
||||
#endif
|
||||
if (container.type == VAL_ARRAY) {
|
||||
if (idx.type != VAL_INT) { fprintf(stderr, "INDEX_SET index must be int for array\n"); exit(1); }
|
||||
if (!array_set(&container, (int)idx.i, v)) {
|
||||
fprintf(stderr, "Runtime error: index out of range\n"); exit(1);
|
||||
}
|
||||
free_value(container);
|
||||
free_value(idx);
|
||||
} else if (container.type == VAL_MAP) {
|
||||
if (idx.type != VAL_STRING) { fprintf(stderr, "INDEX_SET key must be string for map\n"); exit(1); }
|
||||
if (!map_set(&container, idx.s ? idx.s : "", v)) {
|
||||
fprintf(stderr, "Runtime error: map set failed\n"); exit(1);
|
||||
}
|
||||
free_value(container);
|
||||
free_value(idx);
|
||||
} else {
|
||||
fprintf(stderr, "Runtime type error: INDEX_SET expects array or map\n");
|
||||
exit(1);
|
||||
if (container.type == VAL_ARRAY) {
|
||||
if (idx.type != VAL_INT) {
|
||||
fprintf(stderr, "INDEX_SET index must be int for array\n");
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
if (!array_set(&container, (int)idx.i, v)) {
|
||||
fprintf(stderr, "Runtime error: index out of range\n");
|
||||
exit(1);
|
||||
}
|
||||
free_value(container);
|
||||
free_value(idx);
|
||||
} else if (container.type == VAL_MAP) {
|
||||
if (idx.type != VAL_STRING) {
|
||||
fprintf(stderr, "INDEX_SET key must be string for map\n");
|
||||
exit(1);
|
||||
}
|
||||
if (!map_set(&container, idx.s ? idx.s : "", v)) {
|
||||
fprintf(stderr, "Runtime error: map set failed\n");
|
||||
exit(1);
|
||||
}
|
||||
free_value(container);
|
||||
free_value(idx);
|
||||
} else {
|
||||
fprintf(stderr, "Runtime type error: INDEX_SET expects array or map\n");
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file insert.c
|
||||
* @file insert.c
|
||||
* @brief Implements the OP_INSERT opcode for inserting elements into arrays in the VM.
|
||||
*
|
||||
* This file handles the OP_INSERT instruction, which inserts a value into an array
|
||||
|
|
@ -34,20 +34,20 @@
|
|||
*/
|
||||
|
||||
case OP_INSERT: {
|
||||
Value v = pop_value(vm);
|
||||
Value idx = pop_value(vm);
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY || idx.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: ARR_INSERT expects (array, int, value)\n");
|
||||
exit(1);
|
||||
}
|
||||
int n = array_insert(&arr, (int)idx.i, v);
|
||||
if (n < 0) {
|
||||
fprintf(stderr, "Runtime error: insert failed (OOM?)\n");
|
||||
exit(1);
|
||||
}
|
||||
free_value(arr);
|
||||
free_value(idx);
|
||||
push_value(vm, make_int(n));
|
||||
break;
|
||||
Value v = pop_value(vm);
|
||||
Value idx = pop_value(vm);
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY || idx.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: ARR_INSERT expects (array, int, value)\n");
|
||||
exit(1);
|
||||
}
|
||||
int n = array_insert(&arr, (int)idx.i, v);
|
||||
if (n < 0) {
|
||||
fprintf(stderr, "Runtime error: insert failed (OOM?)\n");
|
||||
exit(1);
|
||||
}
|
||||
free_value(arr);
|
||||
free_value(idx);
|
||||
push_value(vm, make_int(n));
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file join.c
|
||||
* @file join.c
|
||||
* @brief Implements the OP_JOIN opcode for joining array elements into a string in the VM.
|
||||
*
|
||||
* This file handles the OP_JOIN instruction, which joins the elements of an array into a string
|
||||
|
|
@ -27,21 +27,21 @@
|
|||
* // Bytecode: OP_JOIN
|
||||
* // Stack before: [", ", ["a", "b", "c"]]
|
||||
* // Stack after: ["a, b, c"]
|
||||
*
|
||||
*
|
||||
* @author Johannes Findeisen
|
||||
* @date 2025-10-16
|
||||
*/
|
||||
|
||||
case OP_JOIN: {
|
||||
Value sep = pop_value(vm);
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY || sep.type != VAL_STRING) {
|
||||
fprintf(stderr, "Runtime type error: JOIN expects (array, string)\n");
|
||||
exit(1);
|
||||
}
|
||||
Value out = bi_join(&arr, &sep);
|
||||
free_value(arr);
|
||||
free_value(sep);
|
||||
push_value(vm, out);
|
||||
break;
|
||||
Value sep = pop_value(vm);
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY || sep.type != VAL_STRING) {
|
||||
fprintf(stderr, "Runtime type error: JOIN expects (array, string)\n");
|
||||
exit(1);
|
||||
}
|
||||
Value out = bi_join(&arr, &sep);
|
||||
free_value(arr);
|
||||
free_value(sep);
|
||||
push_value(vm, out);
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file make_array.c
|
||||
* @file make_array.c
|
||||
* @brief Implements the OP_MAKE_ARRAY opcode for creating arrays in the VM.
|
||||
*
|
||||
* This file handles the OP_MAKE_ARRAY instruction, which pops `n` values from the stack,
|
||||
|
|
@ -33,23 +33,26 @@
|
|||
*/
|
||||
|
||||
case OP_MAKE_ARRAY: {
|
||||
int n = inst.operand;
|
||||
if (n < 0 || vm->sp + 1 < n) {
|
||||
fprintf(stderr, "Runtime error: invalid element count for MAKE_ARRAY\n");
|
||||
exit(1);
|
||||
}
|
||||
/* pop n values into temp array preserving original order */
|
||||
Value *vals = (Value*)malloc(sizeof(Value) * n);
|
||||
if (!vals) { fprintf(stderr, "Runtime error: OOM in MAKE_ARRAY\n"); exit(1); }
|
||||
for (int i = n - 1; i >= 0; --i) {
|
||||
vals[i] = pop_value(vm); /* take ownership */
|
||||
}
|
||||
/* build array by copying values, then free originals */
|
||||
Value arr = make_array_from_values(vals, n);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
free_value(vals[i]);
|
||||
}
|
||||
free(vals);
|
||||
push_value(vm, arr);
|
||||
break;
|
||||
int n = inst.operand;
|
||||
if (n < 0 || vm->sp + 1 < n) {
|
||||
fprintf(stderr, "Runtime error: invalid element count for MAKE_ARRAY\n");
|
||||
exit(1);
|
||||
}
|
||||
/* pop n values into temp array preserving original order */
|
||||
Value *vals = (Value *)malloc(sizeof(Value) * n);
|
||||
if (!vals) {
|
||||
fprintf(stderr, "Runtime error: OOM in MAKE_ARRAY\n");
|
||||
exit(1);
|
||||
}
|
||||
for (int i = n - 1; i >= 0; --i) {
|
||||
vals[i] = pop_value(vm); /* take ownership */
|
||||
}
|
||||
/* build array by copying values, then free originals */
|
||||
Value arr = make_array_from_values(vals, n);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
free_value(vals[i]);
|
||||
}
|
||||
free(vals);
|
||||
push_value(vm, arr);
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file arr_push.c
|
||||
* @file arr_push.c
|
||||
* @brief Implements the OP_ARR_PUSH opcode for appending elements to arrays in the VM.
|
||||
*
|
||||
* This file handles the OP_ARR_PUSH instruction, which appends a value to the end of an array.
|
||||
|
|
@ -33,18 +33,18 @@
|
|||
*/
|
||||
|
||||
case OP_PUSH: {
|
||||
Value v = pop_value(vm);
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY) {
|
||||
fprintf(stderr, "Runtime type error: ARR_PUSH expects array\n");
|
||||
exit(1);
|
||||
}
|
||||
int n = array_push(&arr, v);
|
||||
if (n < 0) {
|
||||
fprintf(stderr, "Runtime error: push failed (OOM?)\n");
|
||||
exit(1);
|
||||
}
|
||||
free_value(arr);
|
||||
push_value(vm, make_int(n));
|
||||
break;
|
||||
Value v = pop_value(vm);
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY) {
|
||||
fprintf(stderr, "Runtime type error: ARR_PUSH expects array\n");
|
||||
exit(1);
|
||||
}
|
||||
int n = array_push(&arr, v);
|
||||
if (n < 0) {
|
||||
fprintf(stderr, "Runtime error: push failed (OOM?)\n");
|
||||
exit(1);
|
||||
}
|
||||
free_value(arr);
|
||||
push_value(vm, make_int(n));
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file arr_remove.c
|
||||
* @file arr_remove.c
|
||||
* @brief Implements the OP_ARR_REMOVE opcode for removing elements from arrays in the VM.
|
||||
*
|
||||
* This file handles the OP_ARR_REMOVE instruction, which removes an element from an array
|
||||
|
|
@ -34,19 +34,19 @@
|
|||
*/
|
||||
|
||||
case OP_REMOVE: {
|
||||
Value idx = pop_value(vm);
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY || idx.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: ARR_REMOVE expects (array, int)\n");
|
||||
exit(1);
|
||||
}
|
||||
Value out;
|
||||
if (!array_remove(&arr, (int)idx.i, &out)) {
|
||||
fprintf(stderr, "Runtime error: remove index out of range\n");
|
||||
exit(1);
|
||||
}
|
||||
free_value(arr);
|
||||
free_value(idx);
|
||||
push_value(vm, out);
|
||||
break;
|
||||
Value idx = pop_value(vm);
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY || idx.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: ARR_REMOVE expects (array, int)\n");
|
||||
exit(1);
|
||||
}
|
||||
Value out;
|
||||
if (!array_remove(&arr, (int)idx.i, &out)) {
|
||||
fprintf(stderr, "Runtime error: remove index out of range\n");
|
||||
exit(1);
|
||||
}
|
||||
free_value(arr);
|
||||
free_value(idx);
|
||||
push_value(vm, out);
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file arr_set.c
|
||||
* @file arr_set.c
|
||||
* @brief Implements the OP_ARR_SET opcode for setting elements in arrays in the VM.
|
||||
*
|
||||
* This file handles the OP_ARR_SET instruction, which sets a value at a specified index in an array.
|
||||
|
|
@ -33,21 +33,21 @@
|
|||
*/
|
||||
|
||||
case OP_SET: {
|
||||
Value v = pop_value(vm);
|
||||
Value idx = pop_value(vm);
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY || idx.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: ARR_SET expects (array, int, value)\n");
|
||||
exit(1);
|
||||
}
|
||||
if (!array_set(&arr, (int)idx.i, v)) {
|
||||
fprintf(stderr, "Runtime error: set index out of range\n");
|
||||
exit(1);
|
||||
}
|
||||
free_value(arr);
|
||||
free_value(idx);
|
||||
/* v already owned by array; push copy for return value */
|
||||
push_value(vm, copy_value(&v));
|
||||
free_value(v);
|
||||
break;
|
||||
Value v = pop_value(vm);
|
||||
Value idx = pop_value(vm);
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY || idx.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: ARR_SET expects (array, int, value)\n");
|
||||
exit(1);
|
||||
}
|
||||
if (!array_set(&arr, (int)idx.i, v)) {
|
||||
fprintf(stderr, "Runtime error: set index out of range\n");
|
||||
exit(1);
|
||||
}
|
||||
free_value(arr);
|
||||
free_value(idx);
|
||||
/* v already owned by array; push copy for return value */
|
||||
push_value(vm, copy_value(&v));
|
||||
free_value(v);
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file slice.c
|
||||
* @file slice.c
|
||||
* @brief Implements the OP_SLICE opcode for array slicing in the VM.
|
||||
*
|
||||
* This file handles the OP_SLICE instruction, which creates a new array containing
|
||||
|
|
@ -33,17 +33,17 @@
|
|||
*/
|
||||
|
||||
case OP_SLICE: {
|
||||
Value end = pop_value(vm);
|
||||
Value start = pop_value(vm);
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY || start.type != VAL_INT || end.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: SLICE expects (array, int, int)\n");
|
||||
exit(1);
|
||||
}
|
||||
Value out = array_slice(&arr, (int)start.i, (int)end.i);
|
||||
free_value(arr);
|
||||
free_value(start);
|
||||
free_value(end);
|
||||
push_value(vm, out);
|
||||
break;
|
||||
Value end = pop_value(vm);
|
||||
Value start = pop_value(vm);
|
||||
Value arr = pop_value(vm);
|
||||
if (arr.type != VAL_ARRAY || start.type != VAL_INT || end.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: SLICE expects (array, int, int)\n");
|
||||
exit(1);
|
||||
}
|
||||
Value out = array_slice(&arr, (int)start.i, (int)end.i);
|
||||
free_value(arr);
|
||||
free_value(start);
|
||||
free_value(end);
|
||||
push_value(vm, out);
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file zip.c
|
||||
* @file zip.c
|
||||
* @brief Implements the OP_ZIP opcode for array zipping in the VM.
|
||||
*
|
||||
* This file handles the OP_ZIP instruction, which combines two arrays into
|
||||
|
|
@ -24,7 +24,7 @@
|
|||
* - Exits with error if arguments aren't arrays
|
||||
*
|
||||
* Example:
|
||||
* // Bytecode: OP_ZIP
|
||||
* // Bytecode: OP_ZIP
|
||||
* // Stack before: [[1,2], ['a','b']]
|
||||
* // Stack after: [[[1,'a'], [2,'b']]]
|
||||
*
|
||||
|
|
@ -33,15 +33,15 @@
|
|||
*/
|
||||
|
||||
case OP_ZIP: {
|
||||
Value b = pop_value(vm);
|
||||
Value a = pop_value(vm);
|
||||
if (a.type != VAL_ARRAY || b.type != VAL_ARRAY) {
|
||||
fprintf(stderr, "Runtime type error: ZIP expects (array, array)\n");
|
||||
exit(1);
|
||||
}
|
||||
Value out = bi_zip(&a, &b);
|
||||
free_value(a);
|
||||
free_value(b);
|
||||
push_value(vm, out);
|
||||
break;
|
||||
Value b = pop_value(vm);
|
||||
Value a = pop_value(vm);
|
||||
if (a.type != VAL_ARRAY || b.type != VAL_ARRAY) {
|
||||
fprintf(stderr, "Runtime type error: ZIP expects (array, array)\n");
|
||||
exit(1);
|
||||
}
|
||||
Value out = bi_zip(&a, &b);
|
||||
free_value(a);
|
||||
free_value(b);
|
||||
push_value(vm, out);
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue