diff --git a/CMakeLists.txt b/CMakeLists.txt index a70597c..76e34a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.16) -project(fun VERSION 0.2.2 LANGUAGES C) +project(fun VERSION 0.3.1 LANGUAGES C) set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED ON) diff --git a/src/bytecode.c b/src/bytecode.c index ea93901..8ecb0d2 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -86,11 +86,11 @@ static const char *opcode_name(OpCode op) { case OP_INDEX_GET: return "INDEX_GET"; case OP_INDEX_SET: return "INDEX_SET"; case OP_LEN: return "LEN"; - case OP_ARR_PUSH: return "ARR_PUSH"; - case OP_ARR_POP: return "ARR_POP"; - case OP_ARR_SET: return "ARR_SET"; - case OP_ARR_INSERT: return "ARR_INSERT"; - case OP_ARR_REMOVE: return "ARR_REMOVE"; + case OP_PUSH: return "ARR_PUSH"; + case OP_APOP: return "ARR_POP"; + case OP_SET: return "ARR_SET"; + case OP_INSERT: return "ARR_INSERT"; + case OP_REMOVE: return "ARR_REMOVE"; case OP_SLICE: return "SLICE"; case OP_TO_NUMBER: return "TO_NUMBER"; case OP_TO_STRING: return "TO_STRING"; diff --git a/src/bytecode.h b/src/bytecode.h index f6cb18d..a076625 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -62,11 +62,11 @@ typedef enum { // array and builtin helpers OP_LEN, // pops array or string; pushes length - OP_ARR_PUSH, // pops value, array; pushes new length - OP_ARR_POP, // pops array; pushes removed element - OP_ARR_SET, // pops value, index, array; pushes value - OP_ARR_INSERT, // pops value, index, array; pushes new length - OP_ARR_REMOVE, // pops index, array; pushes removed element + OP_PUSH, // pops value, array; pushes new length + OP_APOP, // pops array; pushes removed element + OP_SET, // pops value, index, array; pushes value + OP_INSERT, // pops value, index, array; pushes new length + OP_REMOVE, // pops index, array; pushes removed element OP_SLICE, // pops end, start, array; pushes new array // conversions diff --git a/src/opcode_names.c b/src/opcode_names.c deleted file mode 100644 index a933a36..0000000 --- a/src/opcode_names.c +++ /dev/null @@ -1,21 +0,0 @@ -/** - * This file is part of the Fun programming language. - * https://hanez.org/project/fun/ - * - * Copyright 2025 Johannes Findeisen - * Licensed under the terms of the ISC license. - * https://opensource.org/license/isc-license-txt - */ - -#include "vm.h" - -const char *opcode_names[] = { - "OP_NOP", "OP_LOAD_CONST", "OP_LOAD_LOCAL", "OP_STORE_LOCAL", - "OP_LOAD_GLOBAL", "OP_STORE_GLOBAL", "OP_ADD", "OP_SUB", - "OP_MUL", "OP_DIV", "OP_LT", "OP_LTE", - "OP_GT", "OP_GTE", "OP_EQ", "OP_NEQ", - "OP_POP", "OP_JUMP", "OP_JUMP_IF_FALSE", "OP_CALL", - "OP_RETURN", "OP_PRINT", "OP_MOD", "OP_AND", - "OP_OR", "OP_NOT", "OP_DUP", "OP_SWAP" -}; - diff --git a/src/parser.c b/src/parser.c index d8a5d17..e328474 100644 --- a/src/parser.c +++ b/src/parser.c @@ -386,7 +386,7 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos) if (*pos < len && src[*pos] == ',') { (*pos)++; skip_spaces(src, len, pos); } else { parser_fail(*pos, "push expects 2 args"); free(name); return 0; } if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "push expects value"); free(name); return 0; } if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after push args"); free(name); return 0; } - bytecode_add_instruction(bc, OP_ARR_PUSH, 0); + bytecode_add_instruction(bc, OP_PUSH, 0); free(name); return 1; } @@ -394,7 +394,7 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos) (*pos)++; /* '(' */ if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "pop expects array"); free(name); return 0; } if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after pop arg"); free(name); return 0; } - bytecode_add_instruction(bc, OP_ARR_POP, 0); + bytecode_add_instruction(bc, OP_APOP, 0); free(name); return 1; } @@ -406,7 +406,7 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos) if (*pos < len && src[*pos] == ',') { (*pos)++; skip_spaces(src, len, pos); } else { parser_fail(*pos, "set expects 3 args"); free(name); return 0; } if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "set expects value"); free(name); return 0; } if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after set args"); free(name); return 0; } - bytecode_add_instruction(bc, OP_ARR_SET, 0); + bytecode_add_instruction(bc, OP_SET, 0); free(name); return 1; } @@ -418,7 +418,7 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos) if (*pos < len && src[*pos] == ',') { (*pos)++; skip_spaces(src, len, pos); } else { parser_fail(*pos, "insert expects 3 args"); free(name); return 0; } if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "insert expects value"); free(name); return 0; } if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after insert args"); free(name); return 0; } - bytecode_add_instruction(bc, OP_ARR_INSERT, 0); + bytecode_add_instruction(bc, OP_INSERT, 0); free(name); return 1; } @@ -428,7 +428,7 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos) if (*pos < len && src[*pos] == ',') { (*pos)++; skip_spaces(src, len, pos); } else { parser_fail(*pos, "remove expects 2 args"); free(name); return 0; } if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "remove expects index"); free(name); return 0; } if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after remove args"); free(name); return 0; } - bytecode_add_instruction(bc, OP_ARR_REMOVE, 0); + bytecode_add_instruction(bc, OP_REMOVE, 0); free(name); return 1; } @@ -641,7 +641,7 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos) else { bytecode_add_instruction(bc, OP_LOAD_GLOBAL, gv); } /* Append via insert: res.insert(index=len(res), value) */ - bytecode_add_instruction(bc, OP_ARR_INSERT, 0); + bytecode_add_instruction(bc, OP_INSERT, 0); /* discard returned new length */ bytecode_add_instruction(bc, OP_POP, 0); @@ -720,7 +720,7 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos) else { bytecode_add_instruction(bc, OP_LOAD_GLOBAL, gvf); } /* Append via insert: res.insert(index=len(res), value) */ - bytecode_add_instruction(bc, OP_ARR_INSERT, 0); + bytecode_add_instruction(bc, OP_INSERT, 0); /* discard returned new length */ bytecode_add_instruction(bc, OP_POP, 0); diff --git a/src/vm.c b/src/vm.c index 1724131..fd47e69 100644 --- a/src/vm.c +++ b/src/vm.c @@ -207,12 +207,16 @@ void vm_run(VM *vm, Bytecode *entry) { #include "vm/arithmetic/mul.c" #include "vm/arithmetic/sub.c" - #include "vm/arrays/arr_insert.c" - #include "vm/arrays/arr_pop.c" - #include "vm/arrays/arr_push.c" - #include "vm/arrays/arr_remove.c" - #include "vm/arrays/arr_set.c" + #include "vm/arrays/insert.c" + + #include "vm/arrays/apop.c" + + + #include "vm/arrays/push.c" + #include "vm/arrays/remove.c" + #include "vm/arrays/set.c" #include "vm/arrays/clear.c" + #include "vm/arrays/contains.c" #include "vm/arrays/enumerate.c" #include "vm/arrays/index_get.c" diff --git a/src/vm.h b/src/vm.h index fff052e..31143c6 100644 --- a/src/vm.h +++ b/src/vm.h @@ -34,7 +34,7 @@ static const char *opcode_names[] = { "JUMP_IF_FALSE","CALL","RETURN","PRINT","HALT", "MOD","AND","OR","NOT","DUP","SWAP", "MAKE_ARRAY","INDEX_GET","INDEX_SET", - "LEN","ARR_PUSH","ARR_POP","ARR_SET","ARR_INSERT","ARR_REMOVE","SLICE", + "LEN","PUSH","APOP","SET","INSERT","REMOVE","SLICE", "TO_NUMBER","TO_STRING","TYPEOF", "SPLIT","JOIN","SUBSTR","FIND", "CONTAINS","INDEX_OF","CLEAR", diff --git a/src/vm/arrays/arr_pop.c b/src/vm/arrays/apop.c similarity index 76% rename from src/vm/arrays/arr_pop.c rename to src/vm/arrays/apop.c index e23ad31..41a84fa 100644 --- a/src/vm/arrays/arr_pop.c +++ b/src/vm/arrays/apop.c @@ -8,10 +8,10 @@ */ /** -* @file arr_pop.c - * @brief Implements the OP_ARR_POP opcode for removing elements from arrays in the VM. +* @file apop.c + * @brief Implements the OP_APOP opcode for removing elements from arrays in the VM. * - * This file handles the OP_ARR_POP instruction, which removes the last element from an array + * This file handles the OP_APOP instruction, which removes the last element from an array * and pushes the removed element onto the stack. The array is popped from the stack. * * Behavior: @@ -24,7 +24,7 @@ * - Exits with an error if the array is empty. * * Example: - * // Bytecode: OP_ARR_POP + * // Bytecode: OP_APOP * // Stack before: [[10, 20, 30]] * // Stack after: [30] * @@ -32,10 +32,10 @@ * @date 2025-10-16 */ -case OP_ARR_POP: { +case OP_APOP: { Value arr = pop_value(vm); if (arr.type != VAL_ARRAY) { - fprintf(stderr, "Runtime type error: ARR_POP expects array\n"); + fprintf(stderr, "Runtime type error: ARR_APOP expects array\n"); exit(1); } Value out; diff --git a/src/vm/arrays/arr_insert.c b/src/vm/arrays/insert.c similarity index 84% rename from src/vm/arrays/arr_insert.c rename to src/vm/arrays/insert.c index a5c2183..2c56eed 100644 --- a/src/vm/arrays/arr_insert.c +++ b/src/vm/arrays/insert.c @@ -8,10 +8,10 @@ */ /** -* @file arr_insert.c - * @brief Implements the OP_ARR_INSERT opcode for inserting elements into arrays in the VM. +* @file insert.c + * @brief Implements the OP_INSERT opcode for inserting elements into arrays in the VM. * - * This file handles the OP_ARR_INSERT instruction, which inserts a value into an array + * This file handles the OP_INSERT instruction, which inserts a value into an array * at a specified index. The array, index, and value are popped from the stack, and the * new length of the array is pushed back onto the stack. * @@ -25,7 +25,7 @@ * - Exits with an error if memory allocation fails during the insertion. * * Example: - * // Bytecode: OP_ARR_INSERT + * // Bytecode: OP_INSERT * // Stack before: [42, 1, [10, 20, 30]] * // Stack after: [4] * @@ -33,7 +33,7 @@ * @date 2025-10-16 */ -case OP_ARR_INSERT: { +case OP_INSERT: { Value v = pop_value(vm); Value idx = pop_value(vm); Value arr = pop_value(vm); diff --git a/src/vm/arrays/arr_push.c b/src/vm/arrays/push.c similarity index 98% rename from src/vm/arrays/arr_push.c rename to src/vm/arrays/push.c index b89635a..a969ca0 100644 --- a/src/vm/arrays/arr_push.c +++ b/src/vm/arrays/push.c @@ -32,7 +32,7 @@ * @date 2025-10-16 */ -case OP_ARR_PUSH: { +case OP_PUSH: { Value v = pop_value(vm); Value arr = pop_value(vm); if (arr.type != VAL_ARRAY) { diff --git a/src/vm/arrays/arr_remove.c b/src/vm/arrays/remove.c similarity index 98% rename from src/vm/arrays/arr_remove.c rename to src/vm/arrays/remove.c index a265ec4..a06b474 100644 --- a/src/vm/arrays/arr_remove.c +++ b/src/vm/arrays/remove.c @@ -33,7 +33,7 @@ * @date 2025-10-16 */ -case OP_ARR_REMOVE: { +case OP_REMOVE: { Value idx = pop_value(vm); Value arr = pop_value(vm); if (arr.type != VAL_ARRAY || idx.type != VAL_INT) { diff --git a/src/vm/arrays/arr_set.c b/src/vm/arrays/set.c similarity index 98% rename from src/vm/arrays/arr_set.c rename to src/vm/arrays/set.c index c22d278..3c139ef 100644 --- a/src/vm/arrays/arr_set.c +++ b/src/vm/arrays/set.c @@ -32,7 +32,7 @@ * @date 2025-10-16 */ -case OP_ARR_SET: { +case OP_SET: { Value v = pop_value(vm); Value idx = pop_value(vm); Value arr = pop_value(vm);