Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
add.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
10/**
11 * @file add.c
12 * @brief Implements the OP_ADD opcode for arithmetic and string concatenation in the VM.
13 *
14 * This file handles the OP_ADD instruction, which performs addition or concatenation
15 * depending on the types of the operands:
16 * - Integers: Adds two integers.
17 * - Strings: Concatenates two strings.
18 * - Arrays: Concatenates two arrays.
19 *
20 * Behavior:
21 * - Pops two values from the stack.
22 * - Performs the operation based on the types of the operands.
23 * - Pushes the result back onto the stack.
24 *
25 * Error Handling:
26 * - Exits with an error if the operands are of incompatible types.
27 * - Exits with an error if memory allocation fails during string concatenation.
28 *
29 * Example:
30 * - Bytecode: OP_ADD
31 * - Stack before: [2, 3]
32 * - Stack after: [5]
33 */
34
35case OP_ADD: {
36 Value b = pop_value(vm);
38 if ((a.type == VAL_INT || a.type == VAL_FLOAT) && (b.type == VAL_INT || b.type == VAL_FLOAT)) {
39 if (a.type == VAL_FLOAT || b.type == VAL_FLOAT) {
40 double da = (a.type == VAL_FLOAT) ? a.d : (double)a.i;
41 double db = (b.type == VAL_FLOAT) ? b.d : (double)b.i;
45 push_value(vm, res);
46 } else {
47 Value res = make_int(a.i + b.i);
50 push_value(vm, res);
51 }
52 } else if (a.type == VAL_STRING && b.type == VAL_STRING) {
53 const char *sa = a.s ? a.s : "";
54 const char *sb = b.s ? b.s : "";
55 size_t la = strlen(sa);
56 size_t lb = strlen(sb);
57 char *buf = (char *)malloc(la + lb + 1);
58 if (!buf) {
59 fprintf(stderr, "Runtime error: out of memory during string concatenation\n");
60 exit(1);
61 }
62 memcpy(buf, sa, la);
63 memcpy(buf + la, sb, lb);
64 buf[la + lb] = '\0';
65 Value res;
66 res.type = VAL_STRING;
67 res.s = buf;
70 push_value(vm, res);
71 } else if (a.type == VAL_ARRAY && b.type == VAL_ARRAY) {
72 Value res = array_concat(&a, &b);
75 push_value(vm, res);
76 } else {
77 fprintf(stderr, "Runtime type error: ADD expects both numbers, both strings, or both arrays, got %s and %s\n",
78 value_type_name(a.type), value_type_name(b.type));
79 exit(1);
80 }
81 break;
82}
Value a
Definition add.c:37
uint32_t b
Definition band.c:32
@ OP_ADD
Definition bytecode.h:44
double da
Definition fmax.c:26
double db
Definition fmax.c:27
FunCurlBuf buf
Definition get.c:48
const char * res
Definition get_string.c:38
Tagged union representing a Fun value.
Definition value.h:68
Value array_concat(const Value *av, const Value *bv)
Concatenate two array Values.
Definition value.c:386
void free_value(Value v)
Free dynamic storage owned by a Value.
Definition value.c:517
Value make_float(double v)
Construct a Value representing a double-precision float.
Definition value.c:64
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
@ VAL_ARRAY
Definition value.h:55
@ VAL_STRING
Definition value.h:53
@ VAL_INT
Definition value.h:51
@ VAL_FLOAT
Definition value.h:58
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580
static const char * value_type_name(ValueType t)
Get a human-readable name for a ValueType.
Definition vm.c:318
static void push_value(VM *vm, Value v)
Push a Value onto the VM operand stack.
Definition vm.c:564
#define fprintf
Definition vm.c:200
#define exit(code)
Definition vm.c:230