1
0
Fork 0
forked from fun/fun

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

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

20
LICENSE
View file

@ -1,13 +1,13 @@
Copyright 2025 Johannes Findeisen Copyright 2025 Johannes Findeisen
Licensed under the Apache License, Version 2.0 (the "License"); Permission to use, copy, modify, and/or distribute this software for any
you may not use this file except in compliance with the License. purpose with or without fee is hereby granted, provided that the above
You may obtain a copy of the License at copyright notice and this permission notice appear in all copies.
http://www.apache.org/licenses/LICENSE-2.0 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
Unless required by applicable law or agreed to in writing, software FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
distributed under the License is distributed on an "AS IS" BASIS, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
See the License for the specific language governing permissions and OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
limitations under the License. PERFORMANCE OF THIS SOFTWARE.

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
// Arrays basics // Arrays basics
arr = [1, 2, 3] arr = [1, 2, 3]
print(arr) // -> [1, 2, 3] print(arr) // -> [1, 2, 3]

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
// Advanced Arrays Demo // Advanced Arrays Demo
// Start with a basic array // Start with a basic array

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
// for-in over an array literal // for-in over an array literal
for x in [1, 2, 3] for x in [1, 2, 3]
print(x) // prints: 1, then 2, then 3 print(x) // prints: 1, then 2, then 3

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
// Conversions and length built-ins demo // Conversions and length built-ins demo
// len on array and string // len on array and string

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
// Built-ins: strings, array utils, iteration helpers, and math // Built-ins: strings, array utils, iteration helpers, and math
// Strings // Strings

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
// Maps, map/filter/reduce, labeled break/continue (depth), and file I/O // Maps, map/filter/reduce, labeled break/continue (depth), and file I/O
// Map literal and indexing // Map literal and indexing

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
// Expressions test: arithmetic, precedence, comparisons, logical ops, unary, parentheses. // Expressions test: arithmetic, precedence, comparisons, logical ops, unary, parentheses.
print("=== Expressions test start ===") print("=== Expressions test start ===")

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
// for/range test: globals, nested loops, and a function using range // for/range test: globals, nested loops, and a function using range
print("=== for/range test start ===") print("=== for/range test start ===")

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
// Functions test: definitions, calls, parameters, locals, returns, and if/else. // Functions test: definitions, calls, parameters, locals, returns, and if/else.
print("=== Functions test start ===") print("=== Functions test start ===")

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/* /*
* Have fun in Fun * Have fun in Fun
*/ */

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
// Have fun in Fun // Have fun in Fun
string s = "Have fun!" string s = "Have fun!"

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
// Test else / else if chains and nested if-blocks (two-space indentation) // Test else / else if chains and nested if-blocks (two-space indentation)
print("=== if/else-if/else test ===") print("=== if/else-if/else test ===")

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
// break and continue examples // break and continue examples
// 1) while loop: print odd numbers, stop after printing 7 // 1) while loop: print odd numbers, stop after printing 7

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
// Nested loops: break and continue behavior // 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 // 1) Nested for range: inner continue on j==2, inner break on j==4, outer break on i==3

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
// Objects via maps: bracket access and method calls with explicit self // Objects via maps: bracket access and method calls with explicit self
// Construct an object as a map (attach methods later) // Construct an object as a map (attach methods later)

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
// Objects as maps: nested fields, methods with explicit self // Objects as maps: nested fields, methods with explicit self
// A method to move a 2D point by dx, dy // A method to move a 2D point by dx, dy

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
// Short-circuit demo for || and && // Short-circuit demo for || and &&
print("=== short-circuit demo ===") print("=== short-circuit demo ===")

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
// Strings test: concatenation with variables and literals, functions returning strings // Strings test: concatenation with variables and literals, functions returning strings
print("=== strings test start ===") print("=== strings test start ===")

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
// While loops feature test: simple loops, nested with if/else, and functions // While loops feature test: simple loops, nested with if/else, and functions
print("=== while test start ===") print("=== while test start ===")

View file

@ -1 +1,9 @@
print("Have fun!") /*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
print("Have fun!")

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/* /*
* Have, fun! * Have, fun!
*/ */

View file

@ -1,5 +1,14 @@
#!/usr/bin/env fun #!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
print("Yay, the playground for having fun... ;)") print("Yay, the playground for having fun... ;)")
number n = 10 number n = 10

View file

@ -1,4 +1,14 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""
This file is part of the Fun programming language.
https://hanez.org/project/fun/
Copyright 2025 Johannes Findeisen <you@hanez.org>
Licensed under the terms of the ISC license.
https://opensource.org/license/isc-license-txt
"""
import re import re
import sys import sys
import argparse import argparse

View file

@ -1,6 +1,13 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
# This file is part of the Fun programming language.
# https://hanez.org/project/fun/
#
# Copyright 2025 Johannes Findeisen <you@hanez.org>
# Licensed under the terms of the ISC license.
# https://opensource.org/license/isc-license-txt
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. && pwd)" ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. && pwd)"
EX_DIR="$ROOT/examples" EX_DIR="$ROOT/examples"

View file

@ -6,7 +6,7 @@
implementation or programming. Just a compiled version of many ideas... ;) implementation or programming. Just a compiled version of many ideas... ;)
Copyright 2025 Johannes Findeisen <you@hanez.org> Copyright 2025 Johannes Findeisen <you@hanez.org>
License: Apache License, Version 2.0 License: ISC License, https://opensource.org/license/isc-license-txt
Birthdate: 2025-09-10 Birthdate: 2025-09-10
*/ */
@ -153,4 +153,3 @@ cast(a)
fun get_string(a_string, b_string) fun get_string(a_string, b_string)
return a_string + " " + b_string return a_string + " " + b_string
eof eof

View file

@ -306,4 +306,4 @@ print "Current time: " + now
## 12. License ## 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.

View file

@ -413,4 +413,4 @@ print("Current time: " + now)
## 15. License ## 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.

View file

@ -1,3 +1,12 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
#include "value.h" #include "value.h"
#include <string.h> #include <string.h>

View file

@ -1,3 +1,13 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>

View file

@ -1,3 +1,12 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
#include "value.h" #include "value.h"
#include <stdlib.h> #include <stdlib.h>

View file

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

View file

@ -1,3 +1,12 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
#include "value.h" #include "value.h"
#include <stdlib.h> #include <stdlib.h>

View file

@ -1,3 +1,13 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
#include "bytecode.h" #include "bytecode.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -127,4 +137,3 @@ void bytecode_dump(const Bytecode *bc) {
printf(" %3d: %-15s %d\n", i, opcode_name(ins->op), ins->operand); printf(" %3d: %-15s %d\n", i, opcode_name(ins->op), ins->operand);
} }
} }

View file

@ -1,3 +1,13 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
#ifndef FUN_BYTECODE_H #ifndef FUN_BYTECODE_H
#define FUN_BYTECODE_H #define FUN_BYTECODE_H
@ -122,4 +132,3 @@ void bytecode_free(Bytecode *bc);
void bytecode_dump(const Bytecode *bc); void bytecode_dump(const Bytecode *bc);
#endif #endif

View file

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

View file

@ -1,3 +1,12 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
#include "bytecode.h" #include "bytecode.h"
#include "value.h" #include "value.h"
#include "vm.h" #include "vm.h"
@ -130,4 +139,3 @@ int main(void) {
bytecode_free(bc); bytecode_free(bc);
return 0; return 0;
} }

View file

@ -1,3 +1,12 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
#include "value.h" #include "value.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>

View file

@ -1,3 +1,12 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
#include "vm.h" #include "vm.h"
const char *opcode_names[] = { const char *opcode_names[] = {

View file

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

View file

@ -1,4 +1,13 @@
/* /**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* Parse a .fun source file and compile it into entry bytecode. * Parse a .fun source file and compile it into entry bytecode.
* Minimal support: * Minimal support:
* - Optional shebang on the first line. * - Optional shebang on the first line.

View file

@ -1,3 +1,12 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>

View file

@ -1,3 +1,12 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
#include "value.h" #include "value.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>

View file

@ -1,3 +1,12 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
#include "vm.h" #include "vm.h"
#include "bytecode.h" #include "bytecode.h"
#include "value.h" #include "value.h"

View file

@ -1,3 +1,12 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
#include "value.h" #include "value.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -409,6 +418,3 @@ int value_equals(const Value *a, const Value *b) {
default: return 0; default: return 0;
} }
} }

View file

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

View file

@ -1,3 +1,12 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
#define _GNU_SOURCE #define _GNU_SOURCE
#include "vm.h" #include "vm.h"
#include "value.h" #include "value.h"
@ -278,4 +287,3 @@ void vm_run(VM *vm, Bytecode *entry) {
} }
} }
} }

View file

@ -1,3 +1,21 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
#ifndef FUN_VM_H #ifndef FUN_VM_H
#define FUN_VM_H #define FUN_VM_H
@ -68,4 +86,3 @@ static inline int opcode_is_valid(int op) {
} }
#endif #endif

View file

@ -1,3 +1,40 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file add.c
* @brief Implements the OP_ADD opcode for arithmetic and string concatenation in the VM.
*
* This file handles the OP_ADD instruction, which performs addition or concatenation
* depending on the types of the operands:
* - Integers: Adds two integers.
* - Strings: Concatenates two strings.
* - Arrays: Concatenates two arrays.
*
* Behavior:
* - Pops two values from the stack.
* - Performs the operation based on the types of the operands.
* - Pushes the result back onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are of incompatible types.
* - Exits with an error if memory allocation fails during string concatenation.
*
* Example:
* // Bytecode: OP_ADD
* // Stack before: [2, 3]
* // Stack after: [5]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_ADD: { case OP_ADD: {
Value b = pop_value(vm); Value b = pop_value(vm);
Value a = pop_value(vm); Value a = pop_value(vm);

View file

@ -1,3 +1,37 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file div.c
* @brief Implements the OP_DIV opcode for integer division in the VM.
*
* This file handles the OP_DIV instruction, which performs integer division
* on two integer values popped from the stack and pushes the result back onto the stack.
*
* Behavior:
* - Pops two integer values from the stack.
* - Performs integer division (`a / b`).
* - Pushes the result back onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are not integers.
* - Exits with an error if division by zero is attempted.
*
* Example:
* // Bytecode: OP_DIV
* // Stack before: [10, 2]
* // Stack after: [5]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_DIV: { case OP_DIV: {
Value b = pop_value(vm); Value b = pop_value(vm);
Value a = pop_value(vm); Value a = pop_value(vm);

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file mul.c
* @brief Implements the OP_MUL opcode for integer multiplication in the VM.
*
* This file handles the OP_MUL instruction, which performs integer multiplication
* on two integer values popped from the stack and pushes the result back onto the stack.
*
* Behavior:
* - Pops two integer values from the stack.
* - Performs integer multiplication (`a * b`).
* - Pushes the result back onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are not integers.
*
* Example:
* // Bytecode: OP_MUL
* // Stack before: [3, 4]
* // Stack after: [12]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_MUL: { case OP_MUL: {
Value b = pop_value(vm); Value b = pop_value(vm);
Value a = pop_value(vm); Value a = pop_value(vm);

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file sub.c
* @brief Implements the OP_SUB opcode for integer subtraction in the VM.
*
* This file handles the OP_SUB instruction, which performs integer subtraction
* on two integer values popped from the stack and pushes the result back onto the stack.
*
* Behavior:
* - Pops two integer values from the stack.
* - Performs integer subtraction (`a - b`).
* - Pushes the result back onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are not integers.
*
* Example:
* // Bytecode: OP_SUB
* // Stack before: [10, 4]
* // Stack after: [6]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_SUB: { case OP_SUB: {
Value b = pop_value(vm); Value b = pop_value(vm);
Value a = pop_value(vm); Value a = pop_value(vm);

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,3 +1,37 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file join.c
* @brief Implements the OP_JOIN opcode for joining array elements into a string in the VM.
*
* This file handles the OP_JOIN instruction, which joins the elements of an array into a string
* using a separator. The separator and array are popped from the stack, and the resulting string
* is pushed back.
*
* Behavior:
* - Pops the separator and array from the stack.
* - Joins the array elements into a string using the separator.
* - Pushes the resulting string onto the stack.
*
* Error Handling:
* - Exits with an error if the array or separator is of the wrong type.
*
* Example:
* // Bytecode: OP_JOIN
* // Stack before: [", ", ["a", "b", "c"]]
* // Stack after: ["a, b, c"]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_JOIN: { case OP_JOIN: {
Value sep = pop_value(vm); Value sep = pop_value(vm);
Value arr = pop_value(vm); Value arr = pop_value(vm);

View file

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

View file

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

View file

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

View file

@ -1,3 +1,32 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file call.c
* @brief Implements the OP_CALL opcode for function calls in the VM.
*
* This file handles the OP_CALL instruction, which calls a function with arguments.
*
* Behavior:
* - operand specifies number of arguments
* - Pops args and function from stack
* - Creates new frame with args in locals
* - Sets IP to start of function
*
* Error Handling:
* - Exits with error if not enough args
* - Exits if function isn't callable
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_CALL: { case OP_CALL: {
int argc = inst.operand; int argc = inst.operand;
if (argc < 0) argc = 0; if (argc < 0) argc = 0;

View file

@ -1,3 +1,35 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file dup.c
* @brief Implements the OP_DUP opcode for duplicating the top stack value in the VM.
*
* This file handles the OP_DUP instruction, which duplicates the top value on the stack.
* The duplicated value is pushed back onto the stack.
*
* Behavior:
* - Duplicates the top value on the stack.
* - Pushes the duplicated value onto the stack.
*
* Error Handling:
* - Exits with an error if the stack is empty.
*
* Example:
* // Bytecode: OP_DUP
* // Stack before: [42]
* // Stack after: [42, 42]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_DUP: { case OP_DUP: {
if (vm->sp < 0) { if (vm->sp < 0) {
fprintf(stderr, "Runtime error: stack underflow for DUP\n"); fprintf(stderr, "Runtime error: stack underflow for DUP\n");

View file

@ -1,2 +1,30 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file halt.c
* @brief Implements the OP_HALT opcode for stopping VM execution.
*
* This file handles the OP_HALT instruction, which stops the execution of the VM.
* No stack operations are performed.
*
* Behavior:
* - Stops the VM execution immediately.
*
* Example:
* // Bytecode: OP_HALT
* // Stack before: [42]
* // Stack after: [42]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_HALT: case OP_HALT:
return; return;

View file

@ -1,3 +1,32 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file jump.c
* @brief Implements the OP_JUMP opcode for unconditional jumps in the VM.
*
* This file handles the OP_JUMP instruction, which performs an unconditional
* jump to a new instruction pointer location.
*
* Behavior:
* - Sets IP to operand value
* - No stack manipulation
*
* Used for:
* - Loops
* - Function returns
* - Conditional control flow
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_JUMP: { case OP_JUMP: {
f->ip = inst.operand; f->ip = inst.operand;
break; break;

View file

@ -1,3 +1,33 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file jump_if_false.c
* @brief Implements the OP_JUMP_IF_FALSE opcode for conditional jumps in the VM.
*
* This file handles the OP_JUMP_IF_FALSE instruction, which jumps if the top
* stack value is falsey (0, false, nil, etc).
*
* Behavior:
* - Pops condition value from stack
* - Jumps to operand IP if falsey
* - Continues normally if truthy
*
* Used for:
* - If statements
* - While loops
* - Logical expressions
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_JUMP_IF_FALSE: { case OP_JUMP_IF_FALSE: {
Value cond = pop_value(vm); Value cond = pop_value(vm);
int truthy = value_is_truthy(&cond); int truthy = value_is_truthy(&cond);

View file

@ -1,3 +1,30 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file load_const.c
* @brief Implements the OP_LOAD_CONST opcode for loading constants in the VM.
*
* This file handles the OP_LOAD_CONST instruction, which loads a constant value
* from the bytecode's constant pool onto the stack.
*
* Behavior:
* - operand is index into constant pool
* - Pushes copy of constant onto stack
*
* Error Handling:
* - Exits if invalid constant index
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_LOAD_CONST: { case OP_LOAD_CONST: {
int idx = inst.operand; int idx = inst.operand;
if (idx < 0 || idx >= f->fn->const_count) { if (idx < 0 || idx >= f->fn->const_count) {

View file

@ -1,3 +1,35 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file load_global.c
* @brief Implements the OP_LOAD_GLOBAL opcode for loading global variables in the VM.
*
* This file handles the OP_LOAD_GLOBAL instruction, which loads a global variable
* onto the stack using its index.
*
* Behavior:
* - Loads the global variable at the specified index.
* - Pushes the value onto the stack.
*
* Error Handling:
* - Exits with an error if the index is out of bounds.
*
* Example:
* // Bytecode: OP_LOAD_GLOBAL 0
* // Stack before: []
* // Stack after: [global_value]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_LOAD_GLOBAL: { case OP_LOAD_GLOBAL: {
int idx = inst.operand; int idx = inst.operand;
if (idx < 0 || idx >= VM_MAX_GLOBALS) { if (idx < 0 || idx >= VM_MAX_GLOBALS) {

View file

@ -1,3 +1,35 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file load_local.c
* @brief Implements the OP_LOAD_LOCAL opcode for loading local variables in the VM.
*
* This file handles the OP_LOAD_LOCAL instruction, which loads a local variable
* onto the stack using its index.
*
* Behavior:
* - Loads the local variable at the specified index.
* - Pushes the value onto the stack.
*
* Error Handling:
* - Exits with an error if the index is out of bounds.
*
* Example:
* // Bytecode: OP_LOAD_LOCAL 0
* // Stack before: []
* // Stack after: [local_value]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_LOAD_LOCAL: { case OP_LOAD_LOCAL: {
int slot = inst.operand; int slot = inst.operand;
if (slot < 0 || slot >= FRAME_MAX_LOCALS) { if (slot < 0 || slot >= FRAME_MAX_LOCALS) {

View file

@ -1,2 +1,30 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file nop.c
* @brief Implements the OP_NOP opcode for no operation in the VM.
*
* This file handles the OP_NOP instruction, which performs no operation.
* The stack remains unchanged.
*
* Behavior:
* - Does nothing.
*
* Example:
* // Bytecode: OP_NOP
* // Stack before: [42]
* // Stack after: [42]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_NOP: case OP_NOP:
break; break;

View file

@ -1,3 +1,33 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file pop.c
* @brief Implements the OP_POP opcode for removing the top stack value in the VM.
*
* This file handles the OP_POP instruction, which removes the top value from the stack.
*
* Behavior:
* - Removes the top value from the stack.
*
* Error Handling:
* - Exits with an error if the stack is empty.
*
* Example:
* // Bytecode: OP_POP
* // Stack before: [42]
* // Stack after: []
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_POP: { case OP_POP: {
if (vm->sp < 0) { if (vm->sp < 0) {
fprintf(stderr, "Runtime error: stack underflow for POP\n"); fprintf(stderr, "Runtime error: stack underflow for POP\n");

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file return.c
* @brief Implements the OP_RETURN opcode for returning from a function in the VM.
*
* This file handles the OP_RETURN instruction, which returns from the current function
* and optionally pushes a return value onto the stack.
*
* Behavior:
* - Pops the optional return value from the stack.
* - Returns to the caller frame.
* - Pushes the return value onto the stack (if any).
*
* Error Handling:
* - Exits with an error if the frame stack is empty.
*
* Example:
* // Bytecode: OP_RETURN
* // Stack before: [42]
* // Stack after: [42]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_RETURN: { case OP_RETURN: {
Value retv; Value retv;
if (vm->sp >= 0) retv = pop_value(vm); if (vm->sp >= 0) retv = pop_value(vm);

View file

@ -1,3 +1,35 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file store_global.c
* @brief Implements the OP_STORE_GLOBAL opcode for storing global variables in the VM.
*
* This file handles the OP_STORE_GLOBAL instruction, which stores a value into a global variable
* using its index.
*
* Behavior:
* - Pops the value from the stack.
* - Stores the value into the global variable at the specified index.
*
* Error Handling:
* - Exits with an error if the index is out of bounds.
*
* Example:
* // Bytecode: OP_STORE_GLOBAL 0
* // Stack before: [42]
* // Stack after: []
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_STORE_GLOBAL: { case OP_STORE_GLOBAL: {
int idx = inst.operand; int idx = inst.operand;
if (idx < 0 || idx >= VM_MAX_GLOBALS) { if (idx < 0 || idx >= VM_MAX_GLOBALS) {

View file

@ -1,3 +1,35 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file store_local.c
* @brief Implements the OP_STORE_LOCAL opcode for storing local variables in the VM.
*
* This file handles the OP_STORE_LOCAL instruction, which stores a value into a local variable
* using its index.
*
* Behavior:
* - Pops the value from the stack.
* - Stores the value into the local variable at the specified index.
*
* Error Handling:
* - Exits with an error if the index is out of bounds.
*
* Example:
* // Bytecode: OP_STORE_LOCAL 0
* // Stack before: [42]
* // Stack after: []
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_STORE_LOCAL: { case OP_STORE_LOCAL: {
int slot = inst.operand; int slot = inst.operand;
if (slot < 0 || slot >= FRAME_MAX_LOCALS) { if (slot < 0 || slot >= FRAME_MAX_LOCALS) {

View file

@ -1,3 +1,30 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file swap.c
* @brief Implements the OP_SWAP opcode for stack manipulation in the VM.
*
* This file handles the OP_SWAP instruction, which swaps the top two values
* on the stack.
*
* Behavior:
* - Swaps stack[sp] and stack[sp-1]
* - No type checking
*
* Error Handling:
* - Exits if stack underflow
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_SWAP: { case OP_SWAP: {
if (vm->sp < 1) { if (vm->sp < 1) {
fprintf(stderr, "Runtime error: stack underflow for SWAP\n"); fprintf(stderr, "Runtime error: stack underflow for SWAP\n");

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file read_file.c
* @brief Implements the OP_READ_FILE opcode for reading file contents in the VM.
*
* This file handles the OP_READ_FILE instruction, which reads the contents of a file
* and pushes the result as a string onto the stack.
*
* Behavior:
* - Pops the file path from the stack.
* - Reads the file contents.
* - Pushes the contents as a string onto the stack.
*
* Error Handling:
* - Exits with an error if the file path is invalid or the file cannot be read.
*
* Example:
* // Bytecode: OP_READ_FILE
* // Stack before: ["file.txt"]
* // Stack after: ["file contents"]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_READ_FILE: { case OP_READ_FILE: {
Value path = pop_value(vm); Value path = pop_value(vm);
if (path.type != VAL_STRING) { fprintf(stderr, "READ_FILE expects string\n"); exit(1); } if (path.type != VAL_STRING) { fprintf(stderr, "READ_FILE expects string\n"); exit(1); }

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file write_file.c
* @brief Implements the OP_WRITE_FILE opcode for writing to a file in the VM.
*
* This file handles the OP_WRITE_FILE instruction, which writes data to a file.
* The file path and data are popped from the stack, and a success/failure flag is pushed back.
*
* Behavior:
* - Pops the file path and data from the stack.
* - Writes the data to the file.
* - Pushes 1 (success) or 0 (failure) onto the stack.
*
* Error Handling:
* - Exits with an error if the file path is invalid or the file cannot be written.
*
* Example:
* // Bytecode: OP_WRITE_FILE
* // Stack before: ["file.txt", "data"]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_WRITE_FILE: { case OP_WRITE_FILE: {
Value data = pop_value(vm); Value data = pop_value(vm);
Value path = pop_value(vm); Value path = pop_value(vm);

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file len.c
* @brief Implements the OP_LEN opcode for getting the length of arrays or strings in the VM.
*
* This file handles the OP_LEN instruction, which retrieves the length of an array or string.
* The array or string is popped from the stack, and the length is pushed back.
*
* Behavior:
* - Pops the array or string from the stack.
* - Retrieves the length of the array or string.
* - Pushes the length onto the stack.
*
* Error Handling:
* - Exits with an error if the operand is not an array or string.
*
* Example:
* // Bytecode: OP_LEN
* // Stack before: ["hello"]
* // Stack after: [5]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_LEN: { case OP_LEN: {
Value a = pop_value(vm); Value a = pop_value(vm);
int len = 0; int len = 0;

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file and.c
* @brief Implements the OP_AND opcode for logical AND in the VM.
*
* This file handles the OP_AND instruction, which performs a logical AND operation
* on two boolean values. The values are popped from the stack, and the result is pushed back.
*
* Behavior:
* - Pops two boolean values from the stack.
* - Performs a logical AND operation.
* - Pushes the result (1 or 0) onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are not boolean values.
*
* Example:
* // Bytecode: OP_AND
* // Stack before: [1, 0]
* // Stack after: [0]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_AND: { case OP_AND: {
Value b = pop_value(vm); Value b = pop_value(vm);
Value a = pop_value(vm); Value a = pop_value(vm);

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file eq.c
* @brief Implements the OP_EQ opcode for equality comparison in the VM.
*
* This file handles the OP_EQ instruction, which checks if two values are equal.
* The values are popped from the stack, and the result (1 or 0) is pushed back.
*
* Behavior:
* - Pops two values from the stack.
* - Checks if the values are equal.
* - Pushes 1 (true) or 0 (false) onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are of incompatible types.
*
* Example:
* // Bytecode: OP_EQ
* // Stack before: [42, 42]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_EQ: { case OP_EQ: {
Value b = pop_value(vm); Value b = pop_value(vm);
Value a = pop_value(vm); Value a = pop_value(vm);

View file

@ -1,3 +1,37 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file gt.c
* @brief Implements the OP_GT opcode for greater-than comparison in the VM.
*
* This file handles the OP_GT instruction, which checks if the first value is greater than the second.
* The values are popped from the stack, and the result (1 or 0) is pushed back.
*
* Behavior:
* - Pops two values from the stack.
* - Checks if the first value is greater than the second.
* - Pushes 1 (true) or 0 (false) onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are of incompatible types.
*
* Example:
* ```c
* // Bytecode: OP_GT
* // Stack before: [42, 10]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_GT: { case OP_GT: {
Value b = pop_value(vm); Value b = pop_value(vm);
Value a = pop_value(vm); Value a = pop_value(vm);

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file gte.c
* @brief Implements the OP_GTE opcode for greater-than-or-equal comparison in the VM.
*
* This file handles the OP_GTE instruction, which checks if the first value is greater than or equal to the second.
* The values are popped from the stack, and the result (1 or 0) is pushed back.
*
* Behavior:
* - Pops two values from the stack.
* - Checks if the first value is greater than or equal to the second.
* - Pushes 1 (true) or 0 (false) onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are of incompatible types.
*
* Example:
* // Bytecode: OP_GTE
* // Stack before: [42, 42]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_GTE: { case OP_GTE: {
Value b = pop_value(vm); Value b = pop_value(vm);
Value a = pop_value(vm); Value a = pop_value(vm);

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file lt.c
* @brief Implements the OP_LT opcode for less-than comparison in the VM.
*
* This file handles the OP_LT instruction, which checks if the first value is less than the second.
* The values are popped from the stack, and the result (1 or 0) is pushed back.
*
* Behavior:
* - Pops two values from the stack.
* - Checks if the first value is less than the second.
* - Pushes 1 (true) or 0 (false) onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are of incompatible types.
*
* Example:
* // Bytecode: OP_LT
* // Stack before: [10, 42]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_LT: { case OP_LT: {
Value b = pop_value(vm); Value b = pop_value(vm);
Value a = pop_value(vm); Value a = pop_value(vm);

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file lte.c
* @brief Implements the OP_LTE opcode for less-than-or-equal comparison in the VM.
*
* This file handles the OP_LTE instruction, which checks if the first value is less than or equal to the second.
* The values are popped from the stack, and the result (1 or 0) is pushed back.
*
* Behavior:
* - Pops two values from the stack.
* - Checks if the first value is less than or equal to the second.
* - Pushes 1 (true) or 0 (false) onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are of incompatible types.
*
* Example:
* // Bytecode: OP_LTE
* // Stack before: [42, 42]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_LTE: { case OP_LTE: {
Value b = pop_value(vm); Value b = pop_value(vm);
Value a = pop_value(vm); Value a = pop_value(vm);

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file neq.c
* @brief Implements the OP_NEQ opcode for inequality comparison in the VM.
*
* This file handles the OP_NEQ instruction, which checks if two values are not equal.
* The values are popped from the stack, and the result (1 or 0) is pushed back.
*
* Behavior:
* - Pops two values from the stack.
* - Checks if the values are not equal.
* - Pushes 1 (true) or 0 (false) onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are of incompatible types.
*
* Example:
* // Bytecode: OP_NEQ
* // Stack before: [42, 10]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_NEQ: { case OP_NEQ: {
Value b = pop_value(vm); Value b = pop_value(vm);
Value a = pop_value(vm); Value a = pop_value(vm);

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file not.c
* @brief Implements the OP_NOT opcode for logical NOT in the VM.
*
* This file handles the OP_NOT instruction, which performs a logical NOT operation
* on a boolean value. The value is popped from the stack, and the result is pushed back.
*
* Behavior:
* - Pops a boolean value from the stack.
* - Performs a logical NOT operation.
* - Pushes the result (1 or 0) onto the stack.
*
* Error Handling:
* - Exits with an error if the operand is not a boolean value.
*
* Example:
* // Bytecode: OP_NOT
* // Stack before: [0]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_NOT: { case OP_NOT: {
Value v = pop_value(vm); Value v = pop_value(vm);
int res = !value_is_truthy(&v); int res = !value_is_truthy(&v);

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file or.c
* @brief Implements the OP_OR opcode for logical OR in the VM.
*
* This file handles the OP_OR instruction, which performs a logical OR operation
* on two boolean values. The values are popped from the stack, and the result is pushed back.
*
* Behavior:
* - Pops two boolean values from the stack.
* - Performs a logical OR operation.
* - Pushes the result (1 or 0) onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are not boolean values.
*
* Example:
* // Bytecode: OP_OR
* // Stack before: [1, 0]
* // Stack after: [1]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_OR: { case OP_OR: {
Value b = pop_value(vm); Value b = pop_value(vm);
Value a = pop_value(vm); Value a = pop_value(vm);

View file

@ -1,3 +1,30 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file has_key.c
* @brief Implements the OP_HAS_KEY opcode for map key checking in the VM.
*
* This file handles the OP_HAS_KEY instruction, which checks if a map contains
* a specific key.
*
* Behavior:
* - Pops key and map from stack
* - Pushes 1 if key exists, 0 otherwise
*
* Error Handling:
* - Exits if arguments wrong types
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_HAS_KEY: { case OP_HAS_KEY: {
Value key = pop_value(vm); Value key = pop_value(vm);
Value m = pop_value(vm); Value m = pop_value(vm);

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file keys.c
* @brief Implements the OP_KEYS opcode for retrieving map keys in the VM.
*
* This file handles the OP_KEYS instruction, which retrieves the keys of a map
* and pushes them as an array onto the stack.
*
* Behavior:
* - Pops the map from the stack.
* - Retrieves the keys of the map.
* - Pushes the keys as an array onto the stack.
*
* Error Handling:
* - Exits with an error if the map is of the wrong type.
*
* Example:
* // Bytecode: OP_KEYS
* // Stack before: [{"a": 1, "b": 2}]
* // Stack after: [["a", "b"]]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_KEYS: { case OP_KEYS: {
Value m = pop_value(vm); Value m = pop_value(vm);
if (m.type != VAL_MAP) { fprintf(stderr, "KEYS expects map\n"); exit(1); } if (m.type != VAL_MAP) { fprintf(stderr, "KEYS expects map\n"); exit(1); }

View file

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

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file values.c
* @brief Implements the OP_VALUES opcode for retrieving map values in the VM.
*
* This file handles the OP_VALUES instruction, which retrieves the values of a map
* and pushes them as an array onto the stack.
*
* Behavior:
* - Pops the map from the stack.
* - Retrieves the values of the map.
* - Pushes the values as an array onto the stack.
*
* Error Handling:
* - Exits with an error if the map is of the wrong type.
*
* Example:
* // Bytecode: OP_VALUES
* // Stack before: [{"a": 1, "b": 2}]
* // Stack after: [[1, 2]]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_VALUES: { case OP_VALUES: {
Value m = pop_value(vm); Value m = pop_value(vm);
if (m.type != VAL_MAP) { fprintf(stderr, "VALUES expects map\n"); exit(1); } if (m.type != VAL_MAP) { fprintf(stderr, "VALUES expects map\n"); exit(1); }

View file

@ -1,3 +1,30 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file abs.c
* @brief Implements the OP_ABS opcode for absolute value in the VM.
*
* This file handles the OP_ABS instruction, which computes the absolute value
* of an integer.
*
* Behavior:
* - Pops value from stack
* - Pushes |value|
*
* Error Handling:
* - Exits if not integer
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_ABS: { case OP_ABS: {
Value x = pop_value(vm); Value x = pop_value(vm);
if (x.type != VAL_INT) { fprintf(stderr, "ABS expects int\n"); exit(1); } if (x.type != VAL_INT) { fprintf(stderr, "ABS expects int\n"); exit(1); }

View file

@ -1,3 +1,31 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file clamp.c
* @brief Implements the OP_CLAMP opcode for value clamping in the VM.
*
* This file handles the OP_CLAMP instruction, which clamps a value between
* a minimum and maximum.
*
* Behavior:
* - Pops x, lo, hi from stack
* - Pushes x clamped to [lo,hi]
*
* Error Handling:
* - Exits if arguments wrong types
* - Handles hi < lo case
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_CLAMP: { case OP_CLAMP: {
Value hi = pop_value(vm); Value hi = pop_value(vm);
Value lo = pop_value(vm); Value lo = pop_value(vm);

View file

@ -1,3 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the ISC license.
* https://opensource.org/license/isc-license-txt
*/
/**
* @file max.c
* @brief Implements the OP_MAX opcode for finding the maximum of two values in the VM.
*
* This file handles the OP_MAX instruction, which finds the maximum of two integer values.
* The values are popped from the stack, and the result is pushed back.
*
* Behavior:
* - Pops two integer values from the stack.
* - Finds the maximum of the two values.
* - Pushes the result onto the stack.
*
* Error Handling:
* - Exits with an error if the operands are not integers.
*
* Example:
* // Bytecode: OP_MAX
* // Stack before: [10, 42]
* // Stack after: [42]
*
* @author Johannes Findeisen
* @date 2025-10-16
*/
case OP_MAX: { case OP_MAX: {
Value b = pop_value(vm); Value b = pop_value(vm);
Value a = pop_value(vm); Value a = pop_value(vm);

Some files were not shown because too many files have changed in this diff Show more