Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
add.c File Reference

Execute a bytecode program starting from the given entry point. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

 if ((a.type==VAL_INT||a.type==VAL_FLOAT) &&(b.type==VAL_INT||b.type==VAL_FLOAT))
else if (a.type==VAL_STRING &&b.type==VAL_STRING)
 exit (1)

Variables

case OP_ADD
Value a = pop_value(vm)
 else
 break

Detailed Description

Execute a bytecode program starting from the given entry point.

Implements the OP_ADD opcode for arithmetic and string concatenation in the VM.

Sets up the initial frame and runs the main interpreter loop until there are no more frames. Honors debugger stepping/finish/continue requests and, when enabled, traps exit paths to enter a REPL via on_error_repl.

Parameters
vmVM instance to run.
entryEntry function bytecode (must not be NULL).

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]

Definition in file add.c.

Function Documentation

◆ exit()

exit(1)

◆ if() [1/2]

if((a.type==VAL_INT||a.type==VAL_FLOAT) &&(b.type==VAL_INT||b.type==VAL_FLOAT))

Definition at line 38 of file add.c.

Here is the call graph for this function:

◆ if() [2/2]

else if(a.type = VAL_STRING && b.type == VAL_STRING)

Definition at line 52 of file add.c.

Variable Documentation

◆ a

Value a = pop_value(vm)

Definition at line 37 of file add.c.

◆ break

break

Definition at line 81 of file add.c.

◆ else

else
Initial value:
{
fprintf(stderr, "Runtime type error: ADD expects both numbers, both strings, or both arrays, got %s and %s\n",
value_type_name(a.type), value_type_name(b.type))
Value a
Definition add.c:37
uint32_t b
Definition band.c:32
#define fprintf
Definition vm.c:200

Definition at line 76 of file add.c.

◆ OP_ADD

case OP_ADD

Definition at line 35 of file add.c.