From d7843274ee25a0ab1a605b85cbffbf2afa0db4e2 Mon Sep 17 00:00:00 2001 From: hanez Date: Tue, 16 Sep 2025 02:11:22 +0200 Subject: [PATCH] Added tons of opcode documentation, changed license to ISC and a lot of file refactoring. --- LICENSE | 20 ++++----- examples/arrays.fun | 9 ++++ examples/arrays_advanced.fun | 9 ++++ examples/arrays_iter.fun | 9 ++++ examples/builtins_conversions.fun | 9 ++++ examples/builtins_extended.fun | 9 ++++ examples/builtins_maps_and_more.fun | 9 ++++ examples/expressions_test.fun | 9 ++++ examples/for_range_test.fun | 9 ++++ examples/functions_test.fun | 9 ++++ examples/have_fun.fun | 9 ++++ examples/have_fun_function.fun | 9 ++++ examples/if_else_test.fun | 9 ++++ examples/loops_break_continue.fun | 9 ++++ examples/nested_loops.fun | 9 ++++ examples/objects_basic.fun | 9 ++++ examples/objects_more.fun | 9 ++++ examples/short_circuit_test.fun | 9 ++++ examples/strings_test.fun | 9 ++++ examples/while_test.fun | 9 ++++ history/0.1.0-print.fun | 10 ++++- ...fiers-simple-assignments-globals-print.fun | 9 ++++ play.fun | 9 ++++ scripts/check_op_includes.py | 10 +++++ scripts/run_examples.sh | 7 +++ spec/draft.fun | 3 +- spec/v0.1.md | 2 +- spec/v0.2.md | 2 +- src/array_utils.c | 9 ++++ src/builtins_io.c | 10 +++++ src/builtins_iter.c | 9 ++++ src/builtins_math.c | 38 ++++++++++++++++ src/builtins_string.c | 9 ++++ src/bytecode.c | 11 ++++- src/bytecode.h | 11 ++++- src/fun.c | 39 +++++++++++++++- src/fun_test.c | 10 ++++- src/map.c | 9 ++++ src/opcode_names.c | 9 ++++ src/parser.c | 45 +++++++++++++++++++ src/parser.h | 11 ++++- src/parser_utils.c | 9 ++++ src/str_utils.c | 9 ++++ src/test_opcodes.c | 9 ++++ src/value.c | 12 +++-- src/value.h | 39 +++++++++++++++- src/vm.c | 10 ++++- src/vm.h | 19 +++++++- src/vm/arithmetic/add.c | 37 +++++++++++++++ src/vm/arithmetic/div.c | 34 ++++++++++++++ src/vm/arithmetic/mul.c | 33 ++++++++++++++ src/vm/arithmetic/sub.c | 33 ++++++++++++++ src/vm/arrays/arr_insert.c | 35 +++++++++++++++ src/vm/arrays/arr_pop.c | 34 ++++++++++++++ src/vm/arrays/arr_push.c | 34 ++++++++++++++ src/vm/arrays/arr_remove.c | 35 +++++++++++++++ src/vm/arrays/arr_set.c | 34 ++++++++++++++ src/vm/arrays/clear.c | 33 ++++++++++++++ src/vm/arrays/contains.c | 33 ++++++++++++++ src/vm/arrays/enumerate.c | 33 ++++++++++++++ src/vm/arrays/index_get.c | 35 +++++++++++++++ src/vm/arrays/index_of.c | 33 ++++++++++++++ src/vm/arrays/index_set.c | 35 +++++++++++++++ src/vm/arrays/join.c | 34 ++++++++++++++ src/vm/arrays/make_array.c | 34 ++++++++++++++ src/vm/arrays/slice.c | 34 ++++++++++++++ src/vm/arrays/zip.c | 34 ++++++++++++++ src/vm/core/call.c | 29 ++++++++++++ src/vm/core/dup.c | 32 +++++++++++++ src/vm/core/halt.c | 28 ++++++++++++ src/vm/core/jump.c | 29 ++++++++++++ src/vm/core/jump_if_false.c | 30 +++++++++++++ src/vm/core/load_const.c | 27 +++++++++++ src/vm/core/load_global.c | 32 +++++++++++++ src/vm/core/load_local.c | 32 +++++++++++++ src/vm/core/nop.c | 28 ++++++++++++ src/vm/core/pop.c | 30 +++++++++++++ src/vm/core/return.c | 33 ++++++++++++++ src/vm/core/store_global.c | 32 +++++++++++++ src/vm/core/store_local.c | 32 +++++++++++++ src/vm/core/swap.c | 27 +++++++++++ src/vm/io/read_file.c | 33 ++++++++++++++ src/vm/io/write_file.c | 33 ++++++++++++++ src/vm/len.c | 33 ++++++++++++++ src/vm/logic/and.c | 33 ++++++++++++++ src/vm/logic/eq.c | 33 ++++++++++++++ src/vm/logic/gt.c | 34 ++++++++++++++ src/vm/logic/gte.c | 33 ++++++++++++++ src/vm/logic/lt.c | 33 ++++++++++++++ src/vm/logic/lte.c | 33 ++++++++++++++ src/vm/logic/neq.c | 33 ++++++++++++++ src/vm/logic/not.c | 33 ++++++++++++++ src/vm/logic/or.c | 33 ++++++++++++++ src/vm/maps/has_key.c | 27 +++++++++++ src/vm/maps/keys.c | 33 ++++++++++++++ src/vm/maps/make_map.c | 36 +++++++++++++++ src/vm/maps/values.c | 33 ++++++++++++++ src/vm/math/abs.c | 27 +++++++++++ src/vm/math/clamp.c | 28 ++++++++++++ src/vm/math/max.c | 33 ++++++++++++++ src/vm/math/min.c | 33 ++++++++++++++ src/vm/math/mod.c | 34 ++++++++++++++ src/vm/math/pow.c | 33 ++++++++++++++ src/vm/math/random_int.c | 35 +++++++++++++++ src/vm/math/random_seed.c | 32 +++++++++++++ src/vm/print.c | 29 ++++++++++++ src/vm/strings/find.c | 34 ++++++++++++++ src/vm/strings/split.c | 34 ++++++++++++++ src/vm/strings/substr.c | 35 +++++++++++++++ src/vm/to_number.c | 33 ++++++++++++++ src/vm/to_string.c | 39 ++++++++++++++++ 111 files changed, 2597 insertions(+), 26 deletions(-) diff --git a/LICENSE b/LICENSE index d2c57e8..439d098 100644 --- a/LICENSE +++ b/LICENSE @@ -1,13 +1,13 @@ Copyright 2025 Johannes Findeisen -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. diff --git a/examples/arrays.fun b/examples/arrays.fun index a15af3e..b8fa44d 100755 --- a/examples/arrays.fun +++ b/examples/arrays.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + // Arrays basics arr = [1, 2, 3] print(arr) // -> [1, 2, 3] diff --git a/examples/arrays_advanced.fun b/examples/arrays_advanced.fun index d0fd602..336f523 100755 --- a/examples/arrays_advanced.fun +++ b/examples/arrays_advanced.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + // Advanced Arrays Demo // Start with a basic array diff --git a/examples/arrays_iter.fun b/examples/arrays_iter.fun index 395bfe9..eff1f2a 100755 --- a/examples/arrays_iter.fun +++ b/examples/arrays_iter.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + // for-in over an array literal for x in [1, 2, 3] print(x) // prints: 1, then 2, then 3 diff --git a/examples/builtins_conversions.fun b/examples/builtins_conversions.fun index 7ca499c..d0841a2 100755 --- a/examples/builtins_conversions.fun +++ b/examples/builtins_conversions.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + // Conversions and length built-ins demo // len on array and string diff --git a/examples/builtins_extended.fun b/examples/builtins_extended.fun index 98ecb8d..47758e6 100755 --- a/examples/builtins_extended.fun +++ b/examples/builtins_extended.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + // Built-ins: strings, array utils, iteration helpers, and math // Strings diff --git a/examples/builtins_maps_and_more.fun b/examples/builtins_maps_and_more.fun index 3cc0f57..5d850d0 100755 --- a/examples/builtins_maps_and_more.fun +++ b/examples/builtins_maps_and_more.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + // Maps, map/filter/reduce, labeled break/continue (depth), and file I/O // Map literal and indexing diff --git a/examples/expressions_test.fun b/examples/expressions_test.fun index 2348e30..abc7822 100755 --- a/examples/expressions_test.fun +++ b/examples/expressions_test.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + // Expressions test: arithmetic, precedence, comparisons, logical ops, unary, parentheses. print("=== Expressions test start ===") diff --git a/examples/for_range_test.fun b/examples/for_range_test.fun index 55ad07e..78b85e7 100755 --- a/examples/for_range_test.fun +++ b/examples/for_range_test.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + // for/range test: globals, nested loops, and a function using range print("=== for/range test start ===") diff --git a/examples/functions_test.fun b/examples/functions_test.fun index afd1b7e..32e9bdb 100755 --- a/examples/functions_test.fun +++ b/examples/functions_test.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + // Functions test: definitions, calls, parameters, locals, returns, and if/else. print("=== Functions test start ===") diff --git a/examples/have_fun.fun b/examples/have_fun.fun index 8550cf0..eeb068d 100755 --- a/examples/have_fun.fun +++ b/examples/have_fun.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + /* * Have fun in Fun */ diff --git a/examples/have_fun_function.fun b/examples/have_fun_function.fun index 15564e6..679033b 100755 --- a/examples/have_fun_function.fun +++ b/examples/have_fun_function.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + // Have fun in Fun string s = "Have fun!" diff --git a/examples/if_else_test.fun b/examples/if_else_test.fun index 47a5588..7d32275 100755 --- a/examples/if_else_test.fun +++ b/examples/if_else_test.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + // Test else / else if chains and nested if-blocks (two-space indentation) print("=== if/else-if/else test ===") diff --git a/examples/loops_break_continue.fun b/examples/loops_break_continue.fun index bde2f48..9cb5488 100755 --- a/examples/loops_break_continue.fun +++ b/examples/loops_break_continue.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + // break and continue examples // 1) while loop: print odd numbers, stop after printing 7 diff --git a/examples/nested_loops.fun b/examples/nested_loops.fun index 83cffed..a5eb09d 100755 --- a/examples/nested_loops.fun +++ b/examples/nested_loops.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + // Nested loops: break and continue behavior // 1) Nested for range: inner continue on j==2, inner break on j==4, outer break on i==3 diff --git a/examples/objects_basic.fun b/examples/objects_basic.fun index fda4d76..0a93b4a 100755 --- a/examples/objects_basic.fun +++ b/examples/objects_basic.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + // Objects via maps: bracket access and method calls with explicit self // Construct an object as a map (attach methods later) diff --git a/examples/objects_more.fun b/examples/objects_more.fun index 6817f6e..05a7b39 100755 --- a/examples/objects_more.fun +++ b/examples/objects_more.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + // Objects as maps: nested fields, methods with explicit self // A method to move a 2D point by dx, dy diff --git a/examples/short_circuit_test.fun b/examples/short_circuit_test.fun index ffabc1e..28fad57 100755 --- a/examples/short_circuit_test.fun +++ b/examples/short_circuit_test.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + // Short-circuit demo for || and && print("=== short-circuit demo ===") diff --git a/examples/strings_test.fun b/examples/strings_test.fun index 38fbc41..65abe31 100755 --- a/examples/strings_test.fun +++ b/examples/strings_test.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + // Strings test: concatenation with variables and literals, functions returning strings print("=== strings test start ===") diff --git a/examples/while_test.fun b/examples/while_test.fun index 1a97e12..4572743 100755 --- a/examples/while_test.fun +++ b/examples/while_test.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + // While loops feature test: simple loops, nested with if/else, and functions print("=== while test start ===") diff --git a/history/0.1.0-print.fun b/history/0.1.0-print.fun index 87168e7..729f6a0 100755 --- a/history/0.1.0-print.fun +++ b/history/0.1.0-print.fun @@ -1 +1,9 @@ -print("Have fun!") +/* + * 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 + */ + print("Have fun!") diff --git a/history/0.2.0-shebang-comments-strings-integers-booleans-identifiers-simple-assignments-globals-print.fun b/history/0.2.0-shebang-comments-strings-integers-booleans-identifiers-simple-assignments-globals-print.fun index fff999b..3efe7d6 100755 --- a/history/0.2.0-shebang-comments-strings-integers-booleans-identifiers-simple-assignments-globals-print.fun +++ b/history/0.2.0-shebang-comments-strings-integers-booleans-identifiers-simple-assignments-globals-print.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + /* * Have, fun! */ diff --git a/play.fun b/play.fun index c2ba191..3c3202d 100755 --- a/play.fun +++ b/play.fun @@ -1,5 +1,14 @@ #!/usr/bin/env fun +/* + * 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 + */ + print("Yay, the playground for having fun... ;)") number n = 10 diff --git a/scripts/check_op_includes.py b/scripts/check_op_includes.py index d560177..c6f4a27 100755 --- a/scripts/check_op_includes.py +++ b/scripts/check_op_includes.py @@ -1,4 +1,14 @@ #!/usr/bin/env python3 + +""" +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 +""" + import re import sys import argparse diff --git a/scripts/run_examples.sh b/scripts/run_examples.sh index 347e0aa..8918e76 100755 --- a/scripts/run_examples.sh +++ b/scripts/run_examples.sh @@ -1,6 +1,13 @@ #!/usr/bin/env bash set -euo pipefail +# 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 + ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. && pwd)" EX_DIR="$ROOT/examples" diff --git a/spec/draft.fun b/spec/draft.fun index eebf310..d3d87c3 100644 --- a/spec/draft.fun +++ b/spec/draft.fun @@ -6,7 +6,7 @@ implementation or programming. Just a compiled version of many ideas... ;) Copyright 2025 Johannes Findeisen - License: Apache License, Version 2.0 + License: ISC License, https://opensource.org/license/isc-license-txt Birthdate: 2025-09-10 */ @@ -153,4 +153,3 @@ cast(a) fun get_string(a_string, b_string) return a_string + " " + b_string eof - diff --git a/spec/v0.1.md b/spec/v0.1.md index af3d072..7502c4b 100644 --- a/spec/v0.1.md +++ b/spec/v0.1.md @@ -306,4 +306,4 @@ print "Current time: " + now ## 12. License -This document and the Fun language reference are licensed under the Apache License 2.0 +This document and the Fun language reference are licensed under the ISC License. diff --git a/spec/v0.2.md b/spec/v0.2.md index d7a6bfc..88c6333 100644 --- a/spec/v0.2.md +++ b/spec/v0.2.md @@ -413,4 +413,4 @@ print("Current time: " + now) ## 15. License -This document and the Fun language reference are licensed under the Apache License 2.0 +This document and the Fun language reference are licensed under the ISC License. diff --git a/src/array_utils.c b/src/array_utils.c index 656127a..2db97db 100644 --- a/src/array_utils.c +++ b/src/array_utils.c @@ -1,3 +1,12 @@ +/** + * 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 "value.h" #include diff --git a/src/builtins_io.c b/src/builtins_io.c index 1f096f9..b76ef88 100644 --- a/src/builtins_io.c +++ b/src/builtins_io.c @@ -1,3 +1,13 @@ +/** + * 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 #include #include diff --git a/src/builtins_iter.c b/src/builtins_iter.c index f988097..4e55979 100644 --- a/src/builtins_iter.c +++ b/src/builtins_iter.c @@ -1,3 +1,12 @@ +/** + * 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 "value.h" #include diff --git a/src/builtins_math.c b/src/builtins_math.c index ca36ab9..c2b4c86 100644 --- a/src/builtins_math.c +++ b/src/builtins_math.c @@ -1,3 +1,41 @@ +/** + * 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 + */ + +/** +* @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 #include diff --git a/src/builtins_string.c b/src/builtins_string.c index 7031d39..fb8273b 100644 --- a/src/builtins_string.c +++ b/src/builtins_string.c @@ -1,3 +1,12 @@ +/** + * 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 "value.h" #include diff --git a/src/bytecode.c b/src/bytecode.c index 31a601b..ea93901 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -1,3 +1,13 @@ +/** + * 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 "bytecode.h" #include #include @@ -127,4 +137,3 @@ void bytecode_dump(const Bytecode *bc) { printf(" %3d: %-15s %d\n", i, opcode_name(ins->op), ins->operand); } } - diff --git a/src/bytecode.h b/src/bytecode.h index 9b7ebc9..cf1e5c2 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -1,3 +1,13 @@ +/** + * 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 + */ + + #ifndef FUN_BYTECODE_H #define FUN_BYTECODE_H @@ -122,4 +132,3 @@ void bytecode_free(Bytecode *bc); void bytecode_dump(const Bytecode *bc); #endif - diff --git a/src/fun.c b/src/fun.c index f7be308..4e3a026 100644 --- a/src/fun.c +++ b/src/fun.c @@ -1,3 +1,41 @@ +/** + * 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 + */ + +/** +* @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; } - diff --git a/src/fun_test.c b/src/fun_test.c index 892a166..9f331a5 100644 --- a/src/fun_test.c +++ b/src/fun_test.c @@ -1,3 +1,12 @@ +/** + * 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 "bytecode.h" #include "value.h" #include "vm.h" @@ -130,4 +139,3 @@ int main(void) { bytecode_free(bc); return 0; } - diff --git a/src/map.c b/src/map.c index 6e2f45f..62d38f3 100644 --- a/src/map.c +++ b/src/map.c @@ -1,3 +1,12 @@ +/** + * 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 "value.h" #include #include diff --git a/src/opcode_names.c b/src/opcode_names.c index 8725e95..a933a36 100644 --- a/src/opcode_names.c +++ b/src/opcode_names.c @@ -1,3 +1,12 @@ +/** + * 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[] = { diff --git a/src/parser.c b/src/parser.c index 8a6c134..571e0fd 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,3 +1,48 @@ +/** + * 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 + */ + +/** +* @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" diff --git a/src/parser.h b/src/parser.h index e588d2b..c754693 100644 --- a/src/parser.h +++ b/src/parser.h @@ -1,4 +1,13 @@ -/* +/** + * 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 + */ + +/** * Parse a .fun source file and compile it into entry bytecode. * Minimal support: * - Optional shebang on the first line. diff --git a/src/parser_utils.c b/src/parser_utils.c index 0a74e0b..f261a38 100644 --- a/src/parser_utils.c +++ b/src/parser_utils.c @@ -1,3 +1,12 @@ +/** + * 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 #include #include diff --git a/src/str_utils.c b/src/str_utils.c index b05b675..ee6e72c 100644 --- a/src/str_utils.c +++ b/src/str_utils.c @@ -1,3 +1,12 @@ +/** + * 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 "value.h" #include #include diff --git a/src/test_opcodes.c b/src/test_opcodes.c index 541c2a3..c85e32d 100644 --- a/src/test_opcodes.c +++ b/src/test_opcodes.c @@ -1,3 +1,12 @@ +/** + * 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" #include "bytecode.h" #include "value.h" diff --git a/src/value.c b/src/value.c index 1c13bf1..18c31c9 100644 --- a/src/value.c +++ b/src/value.c @@ -1,3 +1,12 @@ +/** + * 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 "value.h" #include #include @@ -409,6 +418,3 @@ int value_equals(const Value *a, const Value *b) { default: return 0; } } - - - diff --git a/src/value.h b/src/value.h index 34a2713..3dd38ec 100644 --- a/src/value.h +++ b/src/value.h @@ -1,3 +1,41 @@ +/** + * 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 + */ + +/** +* @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 - diff --git a/src/vm.c b/src/vm.c index 38d972c..4ed5b84 100644 --- a/src/vm.c +++ b/src/vm.c @@ -1,3 +1,12 @@ +/** + * 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 + */ + #define _GNU_SOURCE #include "vm.h" #include "value.h" @@ -278,4 +287,3 @@ void vm_run(VM *vm, Bytecode *entry) { } } } - diff --git a/src/vm.h b/src/vm.h index c187a9f..9f90ae2 100644 --- a/src/vm.h +++ b/src/vm.h @@ -1,3 +1,21 @@ +/** + * 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 + */ + +/** + * 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 + */ + #ifndef FUN_VM_H #define FUN_VM_H @@ -68,4 +86,3 @@ static inline int opcode_is_valid(int op) { } #endif - diff --git a/src/vm/arithmetic/add.c b/src/vm/arithmetic/add.c index 20e187c..91e8db1 100644 --- a/src/vm/arithmetic/add.c +++ b/src/vm/arithmetic/add.c @@ -1,3 +1,40 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/arithmetic/div.c b/src/vm/arithmetic/div.c index aa47ebe..5dbf833 100644 --- a/src/vm/arithmetic/div.c +++ b/src/vm/arithmetic/div.c @@ -1,3 +1,37 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/arithmetic/mul.c b/src/vm/arithmetic/mul.c index 679d1fe..8c41d79 100644 --- a/src/vm/arithmetic/mul.c +++ b/src/vm/arithmetic/mul.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/arithmetic/sub.c b/src/vm/arithmetic/sub.c index de655f9..072049a 100644 --- a/src/vm/arithmetic/sub.c +++ b/src/vm/arithmetic/sub.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/arrays/arr_insert.c b/src/vm/arrays/arr_insert.c index 8f2b91f..a5c2183 100644 --- a/src/vm/arrays/arr_insert.c +++ b/src/vm/arrays/arr_insert.c @@ -1,3 +1,38 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/arrays/arr_pop.c b/src/vm/arrays/arr_pop.c index d746891..e23ad31 100644 --- a/src/vm/arrays/arr_pop.c +++ b/src/vm/arrays/arr_pop.c @@ -1,3 +1,37 @@ +/** + * 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 + */ + +/** +* @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) { diff --git a/src/vm/arrays/arr_push.c b/src/vm/arrays/arr_push.c index c2b86ed..b89635a 100644 --- a/src/vm/arrays/arr_push.c +++ b/src/vm/arrays/arr_push.c @@ -1,3 +1,37 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/arrays/arr_remove.c b/src/vm/arrays/arr_remove.c index ca56230..a265ec4 100644 --- a/src/vm/arrays/arr_remove.c +++ b/src/vm/arrays/arr_remove.c @@ -1,3 +1,38 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/arrays/arr_set.c b/src/vm/arrays/arr_set.c index 7c3d3d6..c22d278 100644 --- a/src/vm/arrays/arr_set.c +++ b/src/vm/arrays/arr_set.c @@ -1,3 +1,37 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/arrays/clear.c b/src/vm/arrays/clear.c index 7fede5f..4493c26 100644 --- a/src/vm/arrays/clear.c +++ b/src/vm/arrays/clear.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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) { diff --git a/src/vm/arrays/contains.c b/src/vm/arrays/contains.c index c21e096..7f9868c 100644 --- a/src/vm/arrays/contains.c +++ b/src/vm/arrays/contains.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/arrays/enumerate.c b/src/vm/arrays/enumerate.c index 67ac4b3..7a38d85 100644 --- a/src/vm/arrays/enumerate.c +++ b/src/vm/arrays/enumerate.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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) { diff --git a/src/vm/arrays/index_get.c b/src/vm/arrays/index_get.c index eae25e7..0f08081 100644 --- a/src/vm/arrays/index_get.c +++ b/src/vm/arrays/index_get.c @@ -1,3 +1,38 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/arrays/index_of.c b/src/vm/arrays/index_of.c index 643c065..5164d00 100644 --- a/src/vm/arrays/index_of.c +++ b/src/vm/arrays/index_of.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/arrays/index_set.c b/src/vm/arrays/index_set.c index ce5785b..bedbc0a 100644 --- a/src/vm/arrays/index_set.c +++ b/src/vm/arrays/index_set.c @@ -1,3 +1,38 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/arrays/join.c b/src/vm/arrays/join.c index bf9556c..1f60c31 100644 --- a/src/vm/arrays/join.c +++ b/src/vm/arrays/join.c @@ -1,3 +1,37 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/arrays/make_array.c b/src/vm/arrays/make_array.c index bead699..aa40b26 100644 --- a/src/vm/arrays/make_array.c +++ b/src/vm/arrays/make_array.c @@ -1,3 +1,37 @@ +/** + * 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 + */ + +/** +* @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) { diff --git a/src/vm/arrays/slice.c b/src/vm/arrays/slice.c index 406aed4..79bbc1b 100644 --- a/src/vm/arrays/slice.c +++ b/src/vm/arrays/slice.c @@ -1,3 +1,37 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/arrays/zip.c b/src/vm/arrays/zip.c index 6d62ece..a461184 100644 --- a/src/vm/arrays/zip.c +++ b/src/vm/arrays/zip.c @@ -1,3 +1,37 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/core/call.c b/src/vm/core/call.c index 1b0ccbe..fcbccbe 100644 --- a/src/vm/core/call.c +++ b/src/vm/core/call.c @@ -1,3 +1,32 @@ +/** + * 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 + */ + +/** +* @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; diff --git a/src/vm/core/dup.c b/src/vm/core/dup.c index 1cbb7eb..20fa710 100644 --- a/src/vm/core/dup.c +++ b/src/vm/core/dup.c @@ -1,3 +1,35 @@ +/** + * 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 + */ + +/** +* @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"); diff --git a/src/vm/core/halt.c b/src/vm/core/halt.c index 90c0a1d..f4aa9aa 100644 --- a/src/vm/core/halt.c +++ b/src/vm/core/halt.c @@ -1,2 +1,30 @@ +/** + * 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 + */ + +/** +* @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; diff --git a/src/vm/core/jump.c b/src/vm/core/jump.c index 0fed573..f3221b8 100644 --- a/src/vm/core/jump.c +++ b/src/vm/core/jump.c @@ -1,3 +1,32 @@ +/** + * 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 + */ + +/** +* @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; diff --git a/src/vm/core/jump_if_false.c b/src/vm/core/jump_if_false.c index 173b962..29f6d26 100644 --- a/src/vm/core/jump_if_false.c +++ b/src/vm/core/jump_if_false.c @@ -1,3 +1,33 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/core/load_const.c b/src/vm/core/load_const.c index 1d5f384..4355099 100644 --- a/src/vm/core/load_const.c +++ b/src/vm/core/load_const.c @@ -1,3 +1,30 @@ +/** + * 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 + */ + +/** +* @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) { diff --git a/src/vm/core/load_global.c b/src/vm/core/load_global.c index 3276090..ff83bf3 100644 --- a/src/vm/core/load_global.c +++ b/src/vm/core/load_global.c @@ -1,3 +1,35 @@ +/** + * 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 + */ + +/** +* @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) { diff --git a/src/vm/core/load_local.c b/src/vm/core/load_local.c index 56e191a..a3aceba 100644 --- a/src/vm/core/load_local.c +++ b/src/vm/core/load_local.c @@ -1,3 +1,35 @@ +/** + * 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 + */ + +/** +* @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) { diff --git a/src/vm/core/nop.c b/src/vm/core/nop.c index 5e2723c..ffdd285 100644 --- a/src/vm/core/nop.c +++ b/src/vm/core/nop.c @@ -1,2 +1,30 @@ +/** + * 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 + */ + +/** +* @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; diff --git a/src/vm/core/pop.c b/src/vm/core/pop.c index d2d9fd3..a332c8e 100644 --- a/src/vm/core/pop.c +++ b/src/vm/core/pop.c @@ -1,3 +1,33 @@ +/** + * 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 + */ + +/** +* @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"); diff --git a/src/vm/core/return.c b/src/vm/core/return.c index c0c28ae..610dea9 100644 --- a/src/vm/core/return.c +++ b/src/vm/core/return.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/core/store_global.c b/src/vm/core/store_global.c index a49bb31..41c6b27 100644 --- a/src/vm/core/store_global.c +++ b/src/vm/core/store_global.c @@ -1,3 +1,35 @@ +/** + * 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 + */ + +/** +* @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) { diff --git a/src/vm/core/store_local.c b/src/vm/core/store_local.c index 70aeab0..69b7e54 100644 --- a/src/vm/core/store_local.c +++ b/src/vm/core/store_local.c @@ -1,3 +1,35 @@ +/** + * 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 + */ + +/** +* @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) { diff --git a/src/vm/core/swap.c b/src/vm/core/swap.c index 7102ce9..183b7d1 100644 --- a/src/vm/core/swap.c +++ b/src/vm/core/swap.c @@ -1,3 +1,30 @@ +/** + * 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 + */ + +/** +* @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"); diff --git a/src/vm/io/read_file.c b/src/vm/io/read_file.c index 251b286..b0f141a 100644 --- a/src/vm/io/read_file.c +++ b/src/vm/io/read_file.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); } diff --git a/src/vm/io/write_file.c b/src/vm/io/write_file.c index 2669cde..a7a32a9 100644 --- a/src/vm/io/write_file.c +++ b/src/vm/io/write_file.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/len.c b/src/vm/len.c index b95c3ae..70984c0 100644 --- a/src/vm/len.c +++ b/src/vm/len.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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; diff --git a/src/vm/logic/and.c b/src/vm/logic/and.c index 98ed4e8..a1ffb4a 100644 --- a/src/vm/logic/and.c +++ b/src/vm/logic/and.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/logic/eq.c b/src/vm/logic/eq.c index 9a559bd..4b38535 100644 --- a/src/vm/logic/eq.c +++ b/src/vm/logic/eq.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/logic/gt.c b/src/vm/logic/gt.c index dd444fb..1e2b186 100644 --- a/src/vm/logic/gt.c +++ b/src/vm/logic/gt.c @@ -1,3 +1,37 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/logic/gte.c b/src/vm/logic/gte.c index 2ac9550..645aa8b 100644 --- a/src/vm/logic/gte.c +++ b/src/vm/logic/gte.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/logic/lt.c b/src/vm/logic/lt.c index 86eb0cc..a4130ee 100644 --- a/src/vm/logic/lt.c +++ b/src/vm/logic/lt.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/logic/lte.c b/src/vm/logic/lte.c index 41e9e51..98b6be8 100644 --- a/src/vm/logic/lte.c +++ b/src/vm/logic/lte.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/logic/neq.c b/src/vm/logic/neq.c index 11e2d2f..8497660 100644 --- a/src/vm/logic/neq.c +++ b/src/vm/logic/neq.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/logic/not.c b/src/vm/logic/not.c index 7d97f32..07adac2 100644 --- a/src/vm/logic/not.c +++ b/src/vm/logic/not.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/logic/or.c b/src/vm/logic/or.c index b146695..0387a32 100644 --- a/src/vm/logic/or.c +++ b/src/vm/logic/or.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/maps/has_key.c b/src/vm/maps/has_key.c index 9f59267..4844811 100644 --- a/src/vm/maps/has_key.c +++ b/src/vm/maps/has_key.c @@ -1,3 +1,30 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/maps/keys.c b/src/vm/maps/keys.c index e8524bb..72c0b69 100644 --- a/src/vm/maps/keys.c +++ b/src/vm/maps/keys.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); } diff --git a/src/vm/maps/make_map.c b/src/vm/maps/make_map.c index 8b4df69..5dcc718 100644 --- a/src/vm/maps/make_map.c +++ b/src/vm/maps/make_map.c @@ -1,3 +1,39 @@ +/** + * 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 + */ + +/** +* @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); } diff --git a/src/vm/maps/values.c b/src/vm/maps/values.c index d151f8f..8adb8e6 100644 --- a/src/vm/maps/values.c +++ b/src/vm/maps/values.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); } diff --git a/src/vm/math/abs.c b/src/vm/math/abs.c index 9ae9d49..ad66e8a 100644 --- a/src/vm/math/abs.c +++ b/src/vm/math/abs.c @@ -1,3 +1,30 @@ +/** + * 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 + */ + +/** +* @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); } diff --git a/src/vm/math/clamp.c b/src/vm/math/clamp.c index 1fc0489..a8a9d44 100644 --- a/src/vm/math/clamp.c +++ b/src/vm/math/clamp.c @@ -1,3 +1,31 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/math/max.c b/src/vm/math/max.c index 1fd8b00..01bf052 100644 --- a/src/vm/math/max.c +++ b/src/vm/math/max.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/math/min.c b/src/vm/math/min.c index fc221e6..707dd29 100644 --- a/src/vm/math/min.c +++ b/src/vm/math/min.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/math/mod.c b/src/vm/math/mod.c index 24f7a26..4c08977 100644 --- a/src/vm/math/mod.c +++ b/src/vm/math/mod.c @@ -1,3 +1,37 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/math/pow.c b/src/vm/math/pow.c index f25d5cc..e719ae1 100644 --- a/src/vm/math/pow.c +++ b/src/vm/math/pow.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/math/random_int.c b/src/vm/math/random_int.c index 24c0d72..703029d 100644 --- a/src/vm/math/random_int.c +++ b/src/vm/math/random_int.c @@ -1,3 +1,38 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/math/random_seed.c b/src/vm/math/random_seed.c index e7c50d6..e30255f 100644 --- a/src/vm/math/random_seed.c +++ b/src/vm/math/random_seed.c @@ -1,3 +1,35 @@ +/** + * 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 + */ + +/** +* @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); } diff --git a/src/vm/print.c b/src/vm/print.c index 7873c73..d5efb16 100644 --- a/src/vm/print.c +++ b/src/vm/print.c @@ -1,3 +1,32 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/strings/find.c b/src/vm/strings/find.c index 502b09c..c98d9a2 100644 --- a/src/vm/strings/find.c +++ b/src/vm/strings/find.c @@ -1,3 +1,37 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/strings/split.c b/src/vm/strings/split.c index 4d844ab..a705ce8 100644 --- a/src/vm/strings/split.c +++ b/src/vm/strings/split.c @@ -1,3 +1,37 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/strings/substr.c b/src/vm/strings/substr.c index ec1b591..29ff86e 100644 --- a/src/vm/strings/substr.c +++ b/src/vm/strings/substr.c @@ -1,3 +1,38 @@ +/** + * 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 + */ + +/** +* @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); diff --git a/src/vm/to_number.c b/src/vm/to_number.c index 0ecb668..f688178 100644 --- a/src/vm/to_number.c +++ b/src/vm/to_number.c @@ -1,3 +1,36 @@ +/** + * 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 + */ + +/** +* @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) { diff --git a/src/vm/to_string.c b/src/vm/to_string.c index 0ded744..1a61698 100644 --- a/src/vm/to_string.c +++ b/src/vm/to_string.c @@ -1,3 +1,42 @@ +/** + * 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 + */ + +/** +* @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=]" + * - Maps: Convert to "{map n=}" + * - Functions: Convert to ">" + * - 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);