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

Implements the OP_SUB opcode (numeric subtraction) 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 ((a.type==VAL_INT||a.type==VAL_FLOAT) &&(b.type==VAL_INT||b.type==VAL_FLOAT))
 exit (1)

Variables

case OP_SUB
Value a = pop_value(vm)
 else
 break

Detailed Description

Implements the OP_SUB opcode (numeric subtraction) in the VM.

Handles the OP_SUB instruction, subtracting two numeric operands and pushing the result. If either operand is a float, subtraction is performed in double precision; otherwise it is 64-bit integer subtraction.

Behavior:

  • Pops two values from the stack.
  • If any operand is VAL_FLOAT, computes (double)a - (double)b and pushes a VAL_FLOAT.
  • Else computes a.i - b.i and pushes a VAL_INT.

Error Handling:

  • Raises a runtime error and aborts execution if operands are not numeric.

Example: // Bytecode: OP_SUB // Stack before: [10, 4] // Stack after: [6] // Stack before: [10.0, 3] // Stack after: [7.0]

Definition in file sub.c.

Function Documentation

◆ exit()

exit(1)

◆ if()

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

Definition at line 38 of file sub.c.

Here is the call graph for this function:

Variable Documentation

◆ a

Value a = pop_value(vm)

Definition at line 37 of file sub.c.

◆ break

break

Definition at line 57 of file sub.c.

◆ else

else
Initial value:
{
fprintf(stderr, "Runtime type error: SUB expects numbers, 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 52 of file sub.c.

◆ OP_SUB

case OP_SUB

Definition at line 35 of file sub.c.