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,12 @@
/**
* 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
*/
#include "value.h"
#include <string.h>

View file

@ -1,3 +1,13 @@
/**
* 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
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

View file

@ -1,3 +1,12 @@
/**
* 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
*/
#include "value.h"
#include <stdlib.h>

View file

@ -1,3 +1,41 @@
/**
* 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 builtins_math.c
* @brief Implements built-in math functions for the Fun VM.
*
* This file provides helper functions for mathematical operations, including:
* - Minimum, maximum, and clamping.
* - Absolute value and exponentiation.
* - Random number generation.
*
* Functions:
* - `bm_min`: Returns the smaller of two integers.
* - `bm_max`: Returns the larger of two integers.
* - `bm_clamp`: Clamps a value between a lower and upper bound.
* - `bm_abs`: Returns the absolute value of an integer.
* - `bm_pow`: Computes the power of an integer.
* - `bm_random_seed`: Seeds the random number generator.
* - `bm_random_int`: Generates a random integer within a range.
*
* Example:
* ```c
* int64_t min = bm_min(10, 20); // 10
* int64_t rand = bm_random_int(1, 100); // Random number between 1 and 99
* ```
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
#include <stdint.h>
#include <stdlib.h>

View file

@ -1,3 +1,12 @@
/**
* 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
*/
#include "value.h"
#include <stdlib.h>

View file

@ -1,3 +1,13 @@
/**
* 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
*/
#include "bytecode.h"
#include <stdlib.h>
#include <string.h>
@ -127,4 +137,3 @@ void bytecode_dump(const Bytecode *bc) {
printf(" %3d: %-15s %d\n", i, opcode_name(ins->op), ins->operand);
}
}

View file

@ -1,3 +1,13 @@
/**
* 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
*/
#ifndef FUN_BYTECODE_H
#define FUN_BYTECODE_H
@ -122,4 +132,3 @@ void bytecode_free(Bytecode *bc);
void bytecode_dump(const Bytecode *bc);
#endif

View file

@ -1,3 +1,41 @@
/**
* 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 fun.c
* @brief Main entry point for the Fun language interpreter.
*
* This file contains the core logic for the Fun interpreter, including:
* - Command-line argument parsing.
* - File and REPL mode handling.
* - Utility functions for parsing and executing Fun code.
*
* Key Functions:
* - `is_blank_line`: Checks if a line is blank or contains only whitespace.
* - `lstrip`: Strips leading whitespace from a string.
* - `ends_with_opener`: Determines if a line ends with a continuation operator.
* - `compute_open_indent_blocks`: Computes the number of open indentation blocks.
* - `buffer_looks_incomplete`: Detects if a buffer contains incomplete code.
*
* Error Handling:
* - Exits with an error if file loading or parsing fails.
*
* Example:
* ```bash
* $ fun script.fun
* ```
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
#include "bytecode.h"
#include "value.h"
#include "vm.h"
@ -759,4 +797,3 @@ int main(int argc, char **argv) {
free(buffer);
return 0;
}

View file

@ -1,3 +1,12 @@
/**
* 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
*/
#include "bytecode.h"
#include "value.h"
#include "vm.h"
@ -130,4 +139,3 @@ int main(void) {
bytecode_free(bc);
return 0;
}

View file

@ -1,3 +1,12 @@
/**
* 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
*/
#include "value.h"
#include <stdlib.h>
#include <string.h>

View file

@ -1,3 +1,12 @@
/**
* 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
*/
#include "vm.h"
const char *opcode_names[] = {

View file

@ -1,3 +1,48 @@
/**
* 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 parser.c
* @brief Implements the Fun language parser that converts source code to bytecode.
*
* This file contains the main parsing logic for the Fun programming language.
* It handles converting .fun source files into executable bytecode for the VM.
*
* Key Features:
* - Handles shebang lines
* - Skips whitespace and comments
* - Parses string literals with both single and double quotes
* - Supports basic function definitions
* - Compiles print statements
* - Generates bytecode with proper constants and instructions
*
* Functions:
* - parse_file_to_bytecode(): Main entry point for file parsing
* - parse_string_to_bytecode(): Parses code from string buffers
* - parser_last_error(): Retrieves parsing errors
*
* Error Handling:
* - Returns NULL on parse errors
* - Tracks error messages and positions
* - Validates syntax before bytecode generation
*
* Example:
* Bytecode *bc = parse_file_to_bytecode("example.fun");
* if (bc) {
* vm_run(&vm, bc);
* bytecode_free(bc);
* }
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
#include "parser.h"
#include "value.h"
#include "vm.h"

View file

@ -1,4 +1,13 @@
/*
/**
* 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
*/
/**
* Parse a .fun source file and compile it into entry bytecode.
* Minimal support:
* - Optional shebang on the first line.

View file

@ -1,3 +1,12 @@
/**
* 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
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

View file

@ -1,3 +1,12 @@
/**
* 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
*/
#include "value.h"
#include <stdlib.h>
#include <string.h>

View file

@ -1,3 +1,12 @@
/**
* 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
*/
#include "vm.h"
#include "bytecode.h"
#include "value.h"

View file

@ -1,3 +1,12 @@
/**
* 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
*/
#include "value.h"
#include <stdlib.h>
#include <string.h>
@ -409,6 +418,3 @@ int value_equals(const Value *a, const Value *b) {
default: return 0;
}
}

View file

@ -1,3 +1,41 @@
/**
* 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 value.h
* @brief Defines the Value type and associated functions for the Fun VM.
*
* This file defines the `Value` type, which represents all possible data types
* in the Fun language, including integers, strings, functions, arrays, maps, and nil.
* It also provides utility functions for creating, copying, and freeing `Value` objects.
*
* Key Types:
* - `ValueType`: Enumeration of supported value types (e.g., `VAL_INT`, `VAL_STRING`).
* - `Value`: Union type that can hold any Fun value.
*
* Functions:
* - `make_int`, `make_string`, `make_function`, `make_nil`: Constructors for `Value`.
* - `array_length`, `array_get_copy`, `array_set`: Array manipulation functions.
* - `make_map_empty`, `map_set`, `map_get_copy`: Map manipulation functions.
* - `copy_value`, `deep_copy_value`, `free_value`: Functions for copying and freeing values.
*
* Example:
* ```c
* Value num = make_int(42);
* Value str = make_string("Hello, Fun!");
* ```
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
#ifndef FUN_VALUE_H
#define FUN_VALUE_H
@ -78,4 +116,3 @@ Value string_split_to_array(const char *s, const char *sep); /* array of strin
char *array_join_with_sep(const Value *arr, const char *sep); /* join items as strings */
#endif

View file

@ -1,3 +1,12 @@
/**
* 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
*/
#define _GNU_SOURCE
#include "vm.h"
#include "value.h"
@ -278,4 +287,3 @@ void vm_run(VM *vm, Bytecode *entry) {
}
}
}

View file

@ -1,3 +1,21 @@
/**
* 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
*/
/**
* 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
*/
#ifndef FUN_VM_H
#define FUN_VM_H
@ -68,4 +86,3 @@ static inline int opcode_is_valid(int op) {
}
#endif

View file

@ -1,3 +1,40 @@
/**
* 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 add.c
* @brief Implements the OP_ADD opcode for arithmetic and string concatenation in the VM.
*
* This file handles the OP_ADD instruction, which performs addition or concatenation
* depending on the types of the operands:
* - Integers: Adds two integers.
* - Strings: Concatenates two strings.
* - Arrays: Concatenates two arrays.
*
* Behavior:
* - Pops two values from the stack.
* - Performs the operation based on the types of the operands.
* - Pushes the result back onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are of incompatible types.
* - Exits with an error if memory allocation fails during string concatenation.
*
* Example:
* // Bytecode: OP_ADD
* // Stack before: [2, 3]
* // Stack after: [5]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_ADD: {
Value b = pop_value(vm);
Value a = 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 div.c
* @brief Implements the OP_DIV opcode for integer division in the VM.
*
* This file handles the OP_DIV instruction, which performs integer division
* on two integer values popped from the stack and pushes the result back onto the stack.
*
* Behavior:
* - Pops two integer values from the stack.
* - Performs integer division (`a / b`).
* - Pushes the result back onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are not integers.
* - Exits with an error if division by zero is attempted.
*
* Example:
* // Bytecode: OP_DIV
* // Stack before: [10, 2]
* // Stack after: [5]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_DIV: {
Value b = pop_value(vm);
Value a = 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 mul.c
* @brief Implements the OP_MUL opcode for integer multiplication in the VM.
*
* This file handles the OP_MUL instruction, which performs integer multiplication
* on two integer values popped from the stack and pushes the result back onto the stack.
*
* Behavior:
* - Pops two integer values from the stack.
* - Performs integer multiplication (`a * b`).
* - Pushes the result back onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are not integers.
*
* Example:
* // Bytecode: OP_MUL
* // Stack before: [3, 4]
* // Stack after: [12]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_MUL: {
Value b = pop_value(vm);
Value a = 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 sub.c
* @brief Implements the OP_SUB opcode for integer subtraction in the VM.
*
* This file handles the OP_SUB instruction, which performs integer subtraction
* on two integer values popped from the stack and pushes the result back onto the stack.
*
* Behavior:
* - Pops two integer values from the stack.
* - Performs integer subtraction (`a - b`).
* - Pushes the result back onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are not integers.
*
* Example:
* // Bytecode: OP_SUB
* // Stack before: [10, 4]
* // Stack after: [6]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_SUB: {
Value b = pop_value(vm);
Value a = 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_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);

View file

@ -1,3 +1,32 @@
/**
* 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 call.c
* @brief Implements the OP_CALL opcode for function calls in the VM.
*
* This file handles the OP_CALL instruction, which calls a function with arguments.
*
* Behavior:
* - operand specifies number of arguments
* - Pops args and function from stack
* - Creates new frame with args in locals
* - Sets IP to start of function
*
* Error Handling:
* - Exits with error if not enough args
* - Exits if function isn't callable
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_CALL: {
int argc = inst.operand;
if (argc < 0) argc = 0;

View file

@ -1,3 +1,35 @@
/**
* 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 dup.c
* @brief Implements the OP_DUP opcode for duplicating the top stack value in the VM.
*
* This file handles the OP_DUP instruction, which duplicates the top value on the stack.
* The duplicated value is pushed back onto the stack.
*
* Behavior:
* - Duplicates the top value on the stack.
* - Pushes the duplicated value onto the stack.
*
* Error Handling:
* - Exits with an error if the stack is empty.
*
* Example:
* // Bytecode: OP_DUP
* // Stack before: [42]
* // Stack after: [42, 42]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_DUP: {
if (vm->sp < 0) {
fprintf(stderr, "Runtime error: stack underflow for DUP\n");

View file

@ -1,2 +1,30 @@
/**
* 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 halt.c
* @brief Implements the OP_HALT opcode for stopping VM execution.
*
* This file handles the OP_HALT instruction, which stops the execution of the VM.
* No stack operations are performed.
*
* Behavior:
* - Stops the VM execution immediately.
*
* Example:
* // Bytecode: OP_HALT
* // Stack before: [42]
* // Stack after: [42]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_HALT:
return;

View file

@ -1,3 +1,32 @@
/**
* 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 jump.c
* @brief Implements the OP_JUMP opcode for unconditional jumps in the VM.
*
* This file handles the OP_JUMP instruction, which performs an unconditional
* jump to a new instruction pointer location.
*
* Behavior:
* - Sets IP to operand value
* - No stack manipulation
*
* Used for:
* - Loops
* - Function returns
* - Conditional control flow
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_JUMP: {
f->ip = inst.operand;
break;

View file

@ -1,3 +1,33 @@
/**
* 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 jump_if_false.c
* @brief Implements the OP_JUMP_IF_FALSE opcode for conditional jumps in the VM.
*
* This file handles the OP_JUMP_IF_FALSE instruction, which jumps if the top
* stack value is falsey (0, false, nil, etc).
*
* Behavior:
* - Pops condition value from stack
* - Jumps to operand IP if falsey
* - Continues normally if truthy
*
* Used for:
* - If statements
* - While loops
* - Logical expressions
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_JUMP_IF_FALSE: {
Value cond = pop_value(vm);
int truthy = value_is_truthy(&cond);

View file

@ -1,3 +1,30 @@
/**
* 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 load_const.c
* @brief Implements the OP_LOAD_CONST opcode for loading constants in the VM.
*
* This file handles the OP_LOAD_CONST instruction, which loads a constant value
* from the bytecode's constant pool onto the stack.
*
* Behavior:
* - operand is index into constant pool
* - Pushes copy of constant onto stack
*
* Error Handling:
* - Exits if invalid constant index
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_LOAD_CONST: {
int idx = inst.operand;
if (idx < 0 || idx >= f->fn->const_count) {

View file

@ -1,3 +1,35 @@
/**
* 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 load_global.c
* @brief Implements the OP_LOAD_GLOBAL opcode for loading global variables in the VM.
*
* This file handles the OP_LOAD_GLOBAL instruction, which loads a global variable
* onto the stack using its index.
*
* Behavior:
* - Loads the global variable at the specified index.
* - Pushes the value onto the stack.
*
* Error Handling:
* - Exits with an error if the index is out of bounds.
*
* Example:
* // Bytecode: OP_LOAD_GLOBAL 0
* // Stack before: []
* // Stack after: [global_value]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_LOAD_GLOBAL: {
int idx = inst.operand;
if (idx < 0 || idx >= VM_MAX_GLOBALS) {

View file

@ -1,3 +1,35 @@
/**
* 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 load_local.c
* @brief Implements the OP_LOAD_LOCAL opcode for loading local variables in the VM.
*
* This file handles the OP_LOAD_LOCAL instruction, which loads a local variable
* onto the stack using its index.
*
* Behavior:
* - Loads the local variable at the specified index.
* - Pushes the value onto the stack.
*
* Error Handling:
* - Exits with an error if the index is out of bounds.
*
* Example:
* // Bytecode: OP_LOAD_LOCAL 0
* // Stack before: []
* // Stack after: [local_value]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_LOAD_LOCAL: {
int slot = inst.operand;
if (slot < 0 || slot >= FRAME_MAX_LOCALS) {

View file

@ -1,2 +1,30 @@
/**
* 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 nop.c
* @brief Implements the OP_NOP opcode for no operation in the VM.
*
* This file handles the OP_NOP instruction, which performs no operation.
* The stack remains unchanged.
*
* Behavior:
* - Does nothing.
*
* Example:
* // Bytecode: OP_NOP
* // Stack before: [42]
* // Stack after: [42]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_NOP:
break;

View file

@ -1,3 +1,33 @@
/**
* 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 pop.c
* @brief Implements the OP_POP opcode for removing the top stack value in the VM.
*
* This file handles the OP_POP instruction, which removes the top value from the stack.
*
* Behavior:
* - Removes the top value from the stack.
*
* Error Handling:
* - Exits with an error if the stack is empty.
*
* Example:
* // Bytecode: OP_POP
* // Stack before: [42]
* // Stack after: []
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_POP: {
if (vm->sp < 0) {
fprintf(stderr, "Runtime error: stack underflow for POP\n");

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 return.c
* @brief Implements the OP_RETURN opcode for returning from a function in the VM.
*
* This file handles the OP_RETURN instruction, which returns from the current function
* and optionally pushes a return value onto the stack.
*
* Behavior:
* - Pops the optional return value from the stack.
* - Returns to the caller frame.
* - Pushes the return value onto the stack (if any).
*
* Error Handling:
* - Exits with an error if the frame stack is empty.
*
* Example:
* // Bytecode: OP_RETURN
* // Stack before: [42]
* // Stack after: [42]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_RETURN: {
Value retv;
if (vm->sp >= 0) retv = pop_value(vm);

View file

@ -1,3 +1,35 @@
/**
* 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 store_global.c
* @brief Implements the OP_STORE_GLOBAL opcode for storing global variables in the VM.
*
* This file handles the OP_STORE_GLOBAL instruction, which stores a value into a global variable
* using its index.
*
* Behavior:
* - Pops the value from the stack.
* - Stores the value into the global variable at the specified index.
*
* Error Handling:
* - Exits with an error if the index is out of bounds.
*
* Example:
* // Bytecode: OP_STORE_GLOBAL 0
* // Stack before: [42]
* // Stack after: []
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_STORE_GLOBAL: {
int idx = inst.operand;
if (idx < 0 || idx >= VM_MAX_GLOBALS) {

View file

@ -1,3 +1,35 @@
/**
* 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 store_local.c
* @brief Implements the OP_STORE_LOCAL opcode for storing local variables in the VM.
*
* This file handles the OP_STORE_LOCAL instruction, which stores a value into a local variable
* using its index.
*
* Behavior:
* - Pops the value from the stack.
* - Stores the value into the local variable at the specified index.
*
* Error Handling:
* - Exits with an error if the index is out of bounds.
*
* Example:
* // Bytecode: OP_STORE_LOCAL 0
* // Stack before: [42]
* // Stack after: []
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_STORE_LOCAL: {
int slot = inst.operand;
if (slot < 0 || slot >= FRAME_MAX_LOCALS) {

View file

@ -1,3 +1,30 @@
/**
* 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 swap.c
* @brief Implements the OP_SWAP opcode for stack manipulation in the VM.
*
* This file handles the OP_SWAP instruction, which swaps the top two values
* on the stack.
*
* Behavior:
* - Swaps stack[sp] and stack[sp-1]
* - No type checking
*
* Error Handling:
* - Exits if stack underflow
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_SWAP: {
if (vm->sp < 1) {
fprintf(stderr, "Runtime error: stack underflow for SWAP\n");

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 read_file.c
* @brief Implements the OP_READ_FILE opcode for reading file contents in the VM.
*
* This file handles the OP_READ_FILE instruction, which reads the contents of a file
* and pushes the result as a string onto the stack.
*
* Behavior:
* - Pops the file path from the stack.
* - Reads the file contents.
* - Pushes the contents as a string onto the stack.
*
* Error Handling:
* - Exits with an error if the file path is invalid or the file cannot be read.
*
* Example:
* // Bytecode: OP_READ_FILE
* // Stack before: ["file.txt"]
* // Stack after: ["file contents"]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_READ_FILE: {
Value path = pop_value(vm);
if (path.type != VAL_STRING) { fprintf(stderr, "READ_FILE expects string\n"); exit(1); }

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 write_file.c
* @brief Implements the OP_WRITE_FILE opcode for writing to a file in the VM.
*
* This file handles the OP_WRITE_FILE instruction, which writes data to a file.
* The file path and data are popped from the stack, and a success/failure flag is pushed back.
*
* Behavior:
* - Pops the file path and data from the stack.
* - Writes the data to the file.
* - Pushes 1 (success) or 0 (failure) onto the stack.
*
* Error Handling:
* - Exits with an error if the file path is invalid or the file cannot be written.
*
* Example:
* // Bytecode: OP_WRITE_FILE
* // Stack before: ["file.txt", "data"]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_WRITE_FILE: {
Value data = pop_value(vm);
Value path = 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 len.c
* @brief Implements the OP_LEN opcode for getting the length of arrays or strings in the VM.
*
* This file handles the OP_LEN instruction, which retrieves the length of an array or string.
* The array or string is popped from the stack, and the length is pushed back.
*
* Behavior:
* - Pops the array or string from the stack.
* - Retrieves the length of the array or string.
* - Pushes the length onto the stack.
*
* Error Handling:
* - Exits with an error if the operand is not an array or string.
*
* Example:
* // Bytecode: OP_LEN
* // Stack before: ["hello"]
* // Stack after: [5]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_LEN: {
Value a = pop_value(vm);
int len = 0;

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 and.c
* @brief Implements the OP_AND opcode for logical AND in the VM.
*
* This file handles the OP_AND instruction, which performs a logical AND operation
* on two boolean values. The values are popped from the stack, and the result is pushed back.
*
* Behavior:
* - Pops two boolean values from the stack.
* - Performs a logical AND operation.
* - Pushes the result (1 or 0) onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are not boolean values.
*
* Example:
* // Bytecode: OP_AND
* // Stack before: [1, 0]
* // Stack after: [0]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_AND: {
Value b = pop_value(vm);
Value a = 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 eq.c
* @brief Implements the OP_EQ opcode for equality comparison in the VM.
*
* This file handles the OP_EQ instruction, which checks if two values are equal.
* The values are popped from the stack, and the result (1 or 0) is pushed back.
*
* Behavior:
* - Pops two values from the stack.
* - Checks if the values are equal.
* - Pushes 1 (true) or 0 (false) onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are of incompatible types.
*
* Example:
* // Bytecode: OP_EQ
* // Stack before: [42, 42]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_EQ: {
Value b = pop_value(vm);
Value a = 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 gt.c
* @brief Implements the OP_GT opcode for greater-than comparison in the VM.
*
* This file handles the OP_GT instruction, which checks if the first value is greater than the second.
* The values are popped from the stack, and the result (1 or 0) is pushed back.
*
* Behavior:
* - Pops two values from the stack.
* - Checks if the first value is greater than the second.
* - Pushes 1 (true) or 0 (false) onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are of incompatible types.
*
* Example:
* ```c
* // Bytecode: OP_GT
* // Stack before: [42, 10]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_GT: {
Value b = pop_value(vm);
Value a = 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 gte.c
* @brief Implements the OP_GTE opcode for greater-than-or-equal comparison in the VM.
*
* This file handles the OP_GTE instruction, which checks if the first value is greater than or equal to the second.
* The values are popped from the stack, and the result (1 or 0) is pushed back.
*
* Behavior:
* - Pops two values from the stack.
* - Checks if the first value is greater than or equal to the second.
* - Pushes 1 (true) or 0 (false) onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are of incompatible types.
*
* Example:
* // Bytecode: OP_GTE
* // Stack before: [42, 42]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_GTE: {
Value b = pop_value(vm);
Value a = 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 lt.c
* @brief Implements the OP_LT opcode for less-than comparison in the VM.
*
* This file handles the OP_LT instruction, which checks if the first value is less than the second.
* The values are popped from the stack, and the result (1 or 0) is pushed back.
*
* Behavior:
* - Pops two values from the stack.
* - Checks if the first value is less than the second.
* - Pushes 1 (true) or 0 (false) onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are of incompatible types.
*
* Example:
* // Bytecode: OP_LT
* // Stack before: [10, 42]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_LT: {
Value b = pop_value(vm);
Value a = 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 lte.c
* @brief Implements the OP_LTE opcode for less-than-or-equal comparison in the VM.
*
* This file handles the OP_LTE instruction, which checks if the first value is less than or equal to the second.
* The values are popped from the stack, and the result (1 or 0) is pushed back.
*
* Behavior:
* - Pops two values from the stack.
* - Checks if the first value is less than or equal to the second.
* - Pushes 1 (true) or 0 (false) onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are of incompatible types.
*
* Example:
* // Bytecode: OP_LTE
* // Stack before: [42, 42]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_LTE: {
Value b = pop_value(vm);
Value a = 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 neq.c
* @brief Implements the OP_NEQ opcode for inequality comparison in the VM.
*
* This file handles the OP_NEQ instruction, which checks if two values are not equal.
* The values are popped from the stack, and the result (1 or 0) is pushed back.
*
* Behavior:
* - Pops two values from the stack.
* - Checks if the values are not equal.
* - Pushes 1 (true) or 0 (false) onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are of incompatible types.
*
* Example:
* // Bytecode: OP_NEQ
* // Stack before: [42, 10]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_NEQ: {
Value b = pop_value(vm);
Value a = 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 not.c
* @brief Implements the OP_NOT opcode for logical NOT in the VM.
*
* This file handles the OP_NOT instruction, which performs a logical NOT operation
* on a boolean value. The value is popped from the stack, and the result is pushed back.
*
* Behavior:
* - Pops a boolean value from the stack.
* - Performs a logical NOT operation.
* - Pushes the result (1 or 0) onto the stack.
*
* Error Handling:
* - Exits with an error if the operand is not a boolean value.
*
* Example:
* // Bytecode: OP_NOT
* // Stack before: [0]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_NOT: {
Value v = pop_value(vm);
int res = !value_is_truthy(&v);

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 or.c
* @brief Implements the OP_OR opcode for logical OR in the VM.
*
* This file handles the OP_OR instruction, which performs a logical OR operation
* on two boolean values. The values are popped from the stack, and the result is pushed back.
*
* Behavior:
* - Pops two boolean values from the stack.
* - Performs a logical OR operation.
* - Pushes the result (1 or 0) onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are not boolean values.
*
* Example:
* // Bytecode: OP_OR
* // Stack before: [1, 0]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_OR: {
Value b = pop_value(vm);
Value a = pop_value(vm);

View file

@ -1,3 +1,30 @@
/**
* 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 has_key.c
* @brief Implements the OP_HAS_KEY opcode for map key checking in the VM.
*
* This file handles the OP_HAS_KEY instruction, which checks if a map contains
* a specific key.
*
* Behavior:
* - Pops key and map from stack
* - Pushes 1 if key exists, 0 otherwise
*
* Error Handling:
* - Exits if arguments wrong types
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_HAS_KEY: {
Value key = pop_value(vm);
Value m = 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 keys.c
* @brief Implements the OP_KEYS opcode for retrieving map keys in the VM.
*
* This file handles the OP_KEYS instruction, which retrieves the keys of a map
* and pushes them as an array onto the stack.
*
* Behavior:
* - Pops the map from the stack.
* - Retrieves the keys of the map.
* - Pushes the keys as an array onto the stack.
*
* Error Handling:
* - Exits with an error if the map is of the wrong type.
*
* Example:
* // Bytecode: OP_KEYS
* // Stack before: [{"a": 1, "b": 2}]
* // Stack after: [["a", "b"]]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_KEYS: {
Value m = pop_value(vm);
if (m.type != VAL_MAP) { fprintf(stderr, "KEYS expects map\n"); exit(1); }

View file

@ -1,3 +1,39 @@
/**
* 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_map.c
* @brief Implements the OP_MAKE_MAP opcode for creating maps in the VM.
*
* This file handles the OP_MAKE_MAP instruction, which pops `pairs` key-value pairs
* from the stack, creates a map from them, and pushes the resulting map back onto the stack.
*
* Behavior:
* - Validates the number of pairs to ensure it is non-negative.
* - Ensures that map keys are strings.
* - Constructs the map using `make_map_empty` and `map_set`.
* - Pushes the map onto the stack.
*
* Error Handling:
* - Exits with an error if the number of pairs is invalid, if keys are not strings,
* or if map construction fails.
*
* Example:
* // Bytecode: OP_MAKE_MAP 2
* // Stack before: ["key1", 1, "key2", 2]
* // Stack after: [{"key1": 1, "key2": 2}]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_MAKE_MAP: {
int pairs = inst.operand;
if (pairs < 0) { fprintf(stderr, "MAKE_MAP invalid pair count\n"); exit(1); }

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 values.c
* @brief Implements the OP_VALUES opcode for retrieving map values in the VM.
*
* This file handles the OP_VALUES instruction, which retrieves the values of a map
* and pushes them as an array onto the stack.
*
* Behavior:
* - Pops the map from the stack.
* - Retrieves the values of the map.
* - Pushes the values as an array onto the stack.
*
* Error Handling:
* - Exits with an error if the map is of the wrong type.
*
* Example:
* // Bytecode: OP_VALUES
* // Stack before: [{"a": 1, "b": 2}]
* // Stack after: [[1, 2]]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_VALUES: {
Value m = pop_value(vm);
if (m.type != VAL_MAP) { fprintf(stderr, "VALUES expects map\n"); exit(1); }

View file

@ -1,3 +1,30 @@
/**
* 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 abs.c
* @brief Implements the OP_ABS opcode for absolute value in the VM.
*
* This file handles the OP_ABS instruction, which computes the absolute value
* of an integer.
*
* Behavior:
* - Pops value from stack
* - Pushes |value|
*
* Error Handling:
* - Exits if not integer
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_ABS: {
Value x = pop_value(vm);
if (x.type != VAL_INT) { fprintf(stderr, "ABS expects int\n"); exit(1); }

View file

@ -1,3 +1,31 @@
/**
* 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 clamp.c
* @brief Implements the OP_CLAMP opcode for value clamping in the VM.
*
* This file handles the OP_CLAMP instruction, which clamps a value between
* a minimum and maximum.
*
* Behavior:
* - Pops x, lo, hi from stack
* - Pushes x clamped to [lo,hi]
*
* Error Handling:
* - Exits if arguments wrong types
* - Handles hi < lo case
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_CLAMP: {
Value hi = pop_value(vm);
Value lo = 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 max.c
* @brief Implements the OP_MAX opcode for finding the maximum of two values in the VM.
*
* This file handles the OP_MAX instruction, which finds the maximum of two integer values.
* The values are popped from the stack, and the result is pushed back.
*
* Behavior:
* - Pops two integer values from the stack.
* - Finds the maximum of the two values.
* - Pushes the result onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are not integers.
*
* Example:
* // Bytecode: OP_MAX
* // Stack before: [10, 42]
* // Stack after: [42]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_MAX: {
Value b = pop_value(vm);
Value a = 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 min.c
* @brief Implements the OP_MIN opcode for finding the minimum of two values in the VM.
*
* This file handles the OP_MIN instruction, which finds the minimum of two integer values.
* The values are popped from the stack, and the result is pushed back.
*
* Behavior:
* - Pops two integer values from the stack.
* - Finds the minimum of the two values.
* - Pushes the result onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are not integers.
*
* Example:
* // Bytecode: OP_MIN
* // Stack before: [10, 42]
* // Stack after: [10]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_MIN: {
Value b = pop_value(vm);
Value a = 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 mod.c
* @brief Implements the OP_MOD opcode for modulo operation in the VM.
*
* This file handles the OP_MOD instruction, which computes the modulo of two integer values.
* The values are popped from the stack, and the result is pushed back.
*
* Behavior:
* - Pops two integer values from the stack.
* - Computes the modulo of the first value by the second.
* - Pushes the result onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are not integers.
* - Exits with an error if the second operand is zero.
*
* Example:
* // Bytecode: OP_MOD
* // Stack before: [10, 3]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_MOD: {
Value b = pop_value(vm);
Value a = 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 pow.c
* @brief Implements the OP_POW opcode for exponentiation in the VM.
*
* This file handles the OP_POW instruction, which computes the power of two integer values.
* The values are popped from the stack, and the result is pushed back.
*
* Behavior:
* - Pops two integer values from the stack.
* - Computes the power of the first value raised to the second.
* - Pushes the result onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are not integers.
*
* Example:
* // Bytecode: OP_POW
* // Stack before: [2, 3]
* // Stack after: [8]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_POW: {
Value b = pop_value(vm);
Value a = 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 random_int.c
* @brief Implements the OP_RANDOM_INT opcode for generating random integers in the VM.
*
* This file handles the OP_RANDOM_INT instruction, which generates a random integer
* within a specified range. The range bounds are popped from the stack, and the result
* is pushed back.
*
* Behavior:
* - Pops the upper and lower bounds from the stack.
* - Generates a random integer within the range.
* - Pushes the result onto the stack.
*
* Error Handling:
* - Exits with an error if the bounds are not integers.
* - Exits with an error if the lower bound is greater than the upper bound.
*
* Example:
* // Bytecode: OP_RANDOM_INT
* // Stack before: [10, 1]
* // Stack after: [7]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_RANDOM_INT: {
Value hi = pop_value(vm);
Value lo = pop_value(vm);

View file

@ -1,3 +1,35 @@
/**
* 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 random_seed.c
* @brief Implements the OP_RANDOM_SEED opcode for seeding the random number generator in the VM.
*
* This file handles the OP_RANDOM_SEED instruction, which seeds the random number generator
* with a specified value. The seed is popped from the stack.
*
* Behavior:
* - Pops the seed value from the stack.
* - Seeds the random number generator with the value.
*
* Error Handling:
* - Exits with an error if the seed is not an integer.
*
* Example:
* // Bytecode: OP_RANDOM_SEED
* // Stack before: [42]
* // Stack after: []
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_RANDOM_SEED: {
Value seed = pop_value(vm);
if (seed.type != VAL_INT) { fprintf(stderr, "RANDOM_SEED expects int\n"); exit(1); }

View file

@ -1,3 +1,32 @@
/**
* 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 print.c
* @brief Implements the OP_PRINT opcode for printing values in the VM.
*
* This file handles the OP_PRINT instruction, which prints the top value on the stack
* to the output buffer. The value is popped from the stack.
*
* Behavior:
* - Pops the value from the stack.
* - Prints the value to the output buffer.
*
* Example:
* // Bytecode: OP_PRINT
* // Stack before: [42]
* // Stack after: []
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_PRINT: {
Value v = pop_value(vm);
Value snap = deep_copy_value(&v);

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 find.c
* @brief Implements the OP_FIND opcode for finding substrings in the VM.
*
* This file handles the OP_FIND instruction, which finds the index of a substring
* within a string. The substring and string are popped from the stack, and the index
* (or -1) is pushed back.
*
* Behavior:
* - Pops the substring and string from the stack.
* - Finds the index of the substring within the string.
* - Pushes the index (or -1 if not found) onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are not strings.
*
* Example:
* // Bytecode: OP_FIND
* // Stack before: ["world", "hello world"]
* // Stack after: [6]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_FIND: {
Value needle = pop_value(vm);
Value hay = 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 split.c
* @brief Implements the OP_SPLIT opcode for splitting strings in the VM.
*
* This file handles the OP_SPLIT instruction, which splits a string into an array
* of substrings using a separator. The separator and string are popped from the stack,
* and the resulting array is pushed back.
*
* Behavior:
* - Pops the separator and string from the stack.
* - Splits the string into an array of substrings using the separator.
* - Pushes the resulting array onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are not strings.
*
* Example:
* // Bytecode: OP_SPLIT
* // Stack before: [", ", "a, b, c"]
* // Stack after: [["a", "b", "c"]]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_SPLIT: {
Value sep = pop_value(vm);
Value str = 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 substr.c
* @brief Implements the OP_SUBSTR opcode for extracting substrings in the VM.
*
* This file handles the OP_SUBSTR instruction, which extracts a substring from a string
* using a start index and length. The length, start index, and string are popped from the stack,
* and the resulting substring is pushed back.
*
* Behavior:
* - Pops the length, start index, and string from the stack.
* - Extracts the substring from the string.
* - Pushes the resulting substring onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are not integers or strings.
* - Exits with an error if the start index or length is out of bounds.
*
* Example:
* // Bytecode: OP_SUBSTR
* // Stack before: [5, 6, "hello world"]
* // Stack after: ["world"]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_SUBSTR: {
Value lenv = pop_value(vm);
Value startv = 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 to_number.c
* @brief Implements the OP_TO_NUMBER opcode for converting values to integers in the VM.
*
* This file handles the OP_TO_NUMBER instruction, which converts a value to an integer.
* The value is popped from the stack, and the result is pushed back.
*
* Behavior:
* - Pops the value from the stack.
* - Converts the value to an integer.
* - Pushes the result onto the stack.
*
* Error Handling:
* - Exits with an error if the conversion fails.
*
* Example:
* // Bytecode: OP_TO_NUMBER
* // Stack before: ["42"]
* // Stack after: [42]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_TO_NUMBER: {
Value v = pop_value(vm);
if (v.type == VAL_INT) {

View file

@ -1,3 +1,42 @@
/**
* 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 to_string.c
* @brief Implements the OP_TO_STRING opcode for converting values to strings in the VM.
*
* This file handles the OP_TO_STRING instruction, which converts a value of any type
* into its string representation and pushes the result onto the stack.
*
* Behavior:
* - Pops a value from the stack.
* - Converts the value to a string based on its type:
* - Integers: Convert to decimal string (e.g., 42 "42")
* - Strings: Return a copy of the string
* - Arrays: Convert to "[array n=<length>]"
* - Maps: Convert to "{map n=<length>}"
* - Functions: Convert to "<function@<address>>"
* - Nil: Convert to "nil"
* - Pushes the resulting string onto the stack.
*
* Error Handling:
* - Exits with an error if memory allocation fails during string creation.
*
* Example:
* // Bytecode: OP_TO_STRING
* // Stack before: [42]
* // Stack after: ["42"]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_TO_STRING: {
Value v = pop_value(vm);
char *s = value_to_string_alloc(&v);