1
0
Fork 0
forked from fun/fun

Added tons of opcode documentation, changed license to ISC and a lot of file refactoring.

This commit is contained in:
Johannes Findeisen 2025-09-16 02:11:22 +02:00
commit d7843274ee
111 changed files with 2597 additions and 26 deletions

View file

@ -1,3 +1,38 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file arr_insert.c
* @brief Implements the OP_ARR_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
* 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.
*
* Behavior:
* - Pops the value, index, and array from the stack.
* - Inserts the value into the array at the specified index.
* - Pushes the new length of the array onto the stack.
*
* Error Handling:
* - Exits with an error if the array or index is of the wrong type.
* - Exits with an error if memory allocation fails during the insertion.
*
* Example:
* // Bytecode: OP_ARR_INSERT
* // Stack before: [42, 1, [10, 20, 30]]
* // Stack after: [4]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_ARR_INSERT: {
Value v = pop_value(vm);
Value idx = pop_value(vm);

View file

@ -1,3 +1,37 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file arr_pop.c
* @brief Implements the OP_ARR_POP 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
* and pushes the removed element onto the stack. The array is popped from the stack.
*
* Behavior:
* - Pops the array from the stack.
* - Removes the last element from the array.
* - Pushes the removed element onto the stack.
*
* Error Handling:
* - Exits with an error if the array is of the wrong type.
* - Exits with an error if the array is empty.
*
* Example:
* // Bytecode: OP_ARR_POP
* // Stack before: [[10, 20, 30]]
* // Stack after: [30]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_ARR_POP: {
Value arr = pop_value(vm);
if (arr.type != VAL_ARRAY) {

View file

@ -1,3 +1,37 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @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.
* The array and value are popped from the stack, and the new length of the array is pushed back onto the stack.
*
* Behavior:
* - Pops the value and array from the stack.
* - Appends the value to the end of the array.
* - Pushes the new length of the array onto the stack.
*
* Error Handling:
* - Exits with an error if the array is of the wrong type.
* - Exits with an error if memory allocation fails during the append operation.
*
* Example:
* // Bytecode: OP_ARR_PUSH
* // Stack before: [42, [10, 20, 30]]
* // Stack after: [4]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_ARR_PUSH: {
Value v = pop_value(vm);
Value arr = pop_value(vm);

View file

@ -1,3 +1,38 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @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
* at a specified index. The array and index are popped from the stack, and the removed
* element is pushed back onto the stack.
*
* Behavior:
* - Pops the index and array from the stack.
* - Removes the element at the specified index from the array.
* - Pushes the removed element onto the stack.
*
* Error Handling:
* - Exits with an error if the array or index is of the wrong type.
* - Exits with an error if the index is out of bounds.
*
* Example:
* // Bytecode: OP_ARR_REMOVE
* // Stack before: [1, [10, 20, 30]]
* // Stack after: [20]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_ARR_REMOVE: {
Value idx = pop_value(vm);
Value arr = pop_value(vm);

View file

@ -1,3 +1,37 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @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.
* The value, index, and array are popped from the stack, and the value is pushed back onto the stack.
*
* Behavior:
* - Pops the value, index, and array from the stack.
* - Sets the value at the specified index in the array.
* - Pushes the value back onto the stack.
*
* Error Handling:
* - Exits with an error if the array or index is of the wrong type.
* - Exits with an error if the index is out of bounds.
*
* Example:
* // Bytecode: OP_ARR_SET
* // Stack before: [42, 1, [10, 20, 30]]
* // Stack after: [42]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_ARR_SET: {
Value v = pop_value(vm);
Value idx = pop_value(vm);

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file clear.c
* @brief Implements the OP_CLEAR opcode for clearing arrays in the VM.
*
* This file handles the OP_CLEAR instruction, which clears all elements from an array.
* The array is popped from the stack, and nothing is pushed back.
*
* Behavior:
* - Pops the array from the stack.
* - Clears all elements from the array.
*
* Error Handling:
* - Exits with an error if the array is of the wrong type.
*
* Example:
* // Bytecode: OP_CLEAR
* // Stack before: [[10, 20, 30]]
* // Stack after: []
*
* @author Johannes Findeise
* @date 2025-10-16
*/
case OP_CLEAR: {
Value arr = pop_value(vm);
if (arr.type != VAL_ARRAY) {

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @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.
* The value and array are popped from the stack, and a boolean result (1/0) is pushed back.
*
* Behavior:
* - Pops the value and array from the stack.
* - Checks if the value is present in the array.
* - Pushes 1 (true) or 0 (false) onto the stack.
*
* Error Handling:
* - Exits with an error if the array is of the wrong type.
*
* Example:
* // Bytecode: OP_CONTAINS
* // Stack before: [20, [10, 20, 30]]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_CONTAINS: {
Value needle = pop_value(vm);
Value arr = pop_value(vm);

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @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
* from an array. The array is popped from the stack, and the new array is pushed back.
*
* Behavior:
* - Pops the array from the stack.
* - Creates a new array of [index, value] pairs.
* - Pushes the new array onto the stack.
*
* Error Handling:
* - Exits with an error if the array is of the wrong type.
*
* Example:
* // Bytecode: OP_ENUMERATE
* // Stack before: [[10, 20, 30]]
* // Stack after: [[[0, 10], [1, 20], [2, 30]]]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_ENUMERATE: {
Value arr = pop_value(vm);
if (arr.type != VAL_ARRAY) {

View file

@ -1,3 +1,38 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @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
* an array or a value from a map using an index or key.
*
* Behavior:
* - Pops the index/key and container from the stack.
* - For arrays, retrieves the element at the specified index.
* - For maps, retrieves the value associated with the specified key.
* - Pushes the retrieved value onto the stack.
*
* Error Handling:
* - Exits with an error if the container is not an array or map, if the index/key
* is of the wrong type, or if the index is out of bounds.
*
* Example:
* // Bytecode: OP_INDEX_GET
* // Stack before: [1, [10, 20, 30]]
* // Stack after: [20]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_INDEX_GET: {
Value idx = pop_value(vm);
Value container = pop_value(vm);

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @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.
* The value and array are popped from the stack, and the index (or -1) is pushed back.
*
* Behavior:
* - Pops the value and array from the stack.
* - Finds the index of the value in the array.
* - Pushes the index (or -1 if not found) onto the stack.
*
* Error Handling:
* - Exits with an error if the array is of the wrong type.
*
* Example:
* // Bytecode: OP_INDEX_OF
* // Stack before: [20, [10, 20, 30]]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_INDEX_OF: {
Value needle = pop_value(vm);
Value arr = pop_value(vm);

View file

@ -1,3 +1,38 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @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
* element in an array or a key in a map.
*
* Behavior:
* - Pops the value, index/key, and container from the stack.
* - For arrays, assigns the value to the specified index.
* - For maps, assigns the value to the specified key.
*
* Error Handling:
* - Exits with an error if the container is not an array or map, if the index/key
* is of the wrong type, or if the index is out of bounds.
*
* Example:
* // Bytecode: OP_INDEX_SET
* // Stack before: [42, 1, [10, 20, 30]]
* // Stack after: [[10, 42, 30]]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_INDEX_SET: {
Value v = pop_value(vm);
Value idx = pop_value(vm);

View file

@ -1,3 +1,37 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @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
* using a separator. The separator and array are popped from the stack, and the resulting string
* is pushed back.
*
* Behavior:
* - Pops the separator and array from the stack.
* - Joins the array elements into a string using the separator.
* - Pushes the resulting string onto the stack.
*
* Error Handling:
* - Exits with an error if the array or separator is of the wrong type.
*
* Example:
* // 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);

View file

@ -1,3 +1,37 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @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,
* creates an array from them, and pushes the resulting array back onto the stack.
*
* Behavior:
* - Validates the number of elements (`n`) to ensure it is non-negative and within stack bounds.
* - Allocates temporary storage for the array elements.
* - Constructs the array using `make_array_from_values`.
* - Frees temporary storage and pushes the array onto the stack.
*
* Error Handling:
* - Exits with an error if `n` is invalid or if memory allocation fails.
*
* Example:
* // Bytecode: OP_MAKE_ARRAY 3
* // Stack before: [1, 2, 3]
* // Stack after: [[1, 2, 3]]
*
* @author Johanes Findeisen
* @date 2025-10-16
*/
case OP_MAKE_ARRAY: {
int n = inst.operand;
if (n < 0 || vm->sp + 1 < n) {

View file

@ -1,3 +1,37 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @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
* elements from the original array between specified start and end indices.
*
* Behavior:
* - Pops end index, start index, and array from the stack
* - Creates a new array containing elements from start to end-1
* - Pushes the new array onto the stack
*
* Error Handling:
* - Exits with error if arguments are wrong types
* - Handles negative indices and out-of-bounds cases gracefully
*
* Example:
* // Bytecode: OP_SLICE
* // Stack before: [3, 1, [10,20,30,40]]
* // Stack after: [[20,30]]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_SLICE: {
Value end = pop_value(vm);
Value start = pop_value(vm);

View file

@ -1,3 +1,37 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @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
* an array of pairs (subarrays with two elements).
*
* Behavior:
* - Pops two arrays from the stack
* - Creates new array of [a[i],b[i]] pairs
* - Length is minimum of input array lengths
* - Pushes resulting array onto stack
*
* Error Handling:
* - Exits with error if arguments aren't arrays
*
* Example:
* // Bytecode: OP_ZIP
* // Stack before: [[1,2], ['a','b']]
* // Stack after: [[[1,'a'], [2,'b']]]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_ZIP: {
Value b = pop_value(vm);
Value a = pop_value(vm);