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

Implements the OP_PUSH opcode for appending elements to arrays in the VM. More...

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

Go to the source code of this file.

Functions

 if (arr.type !=VAL_ARRAY)
 if (n< 0)
 free_value (arr)
 push_value (vm, make_int(n))

Variables

case OP_PUSH
Value arr = pop_value(vm)
int n = array_push(&arr, v)
 break

Detailed Description

Implements the OP_PUSH opcode for appending elements to arrays in the VM.

Handles the OP_PUSH instruction, which appends a value to the end of an array. The array and value are popped from the stack; the opcode pushes the new array length (integer) as a result.

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 a runtime error if the first operand is not an array.
  • Exits with a runtime error if memory allocation fails.

Example: // Bytecode: OP_PUSH // Stack before: [42, [10, 20, 30]] // Stack after: [4]

Definition in file push.c.

Function Documentation

◆ free_value()

free_value(arr)

◆ if() [1/2]

if(arr.type ! = VAL_ARRAY)

Definition at line 36 of file push.c.

◆ if() [2/2]

if()

Definition at line 41 of file push.c.

◆ push_value()

push_value(vm,
make_int(n) )

Variable Documentation

◆ arr

Value arr = pop_value(vm)

Definition at line 35 of file push.c.

◆ break

break

Definition at line 47 of file push.c.

◆ n

int n = array_push(&arr, v)

Definition at line 40 of file push.c.

◆ OP_PUSH

case OP_PUSH

Definition at line 33 of file push.c.