Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
div.c File Reference

Implements the OP_DIV opcode (numeric division) 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_DIV
Value a = pop_value(vm)
 else
 break

Detailed Description

Implements the OP_DIV opcode (numeric division) in the VM.

Handles the OP_DIV instruction, dividing two numeric operands and pushing the result. If either operand is a float, division is performed in double precision and a VAL_FLOAT is produced; otherwise integer division is used and a VAL_INT is produced.

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.
  • Raises a runtime error on division by zero (both integer and floating cases).

Example:

  • Bytecode: OP_DIV
  • Stack before: [10, 2]
  • Stack after: [5]
  • Stack before: [5.0, 2]
  • Stack after: [2.5]

Definition in file div.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 39 of file div.c.

Variable Documentation

◆ a

Value a = pop_value(vm)

Definition at line 38 of file div.c.

◆ break

break

Definition at line 66 of file div.c.

◆ else

else
Initial value:
{
fprintf(stderr, "Runtime type error: DIV expects numbers, got %s and %s\n",
Value a
Definition add.c:37
uint32_t b
Definition band.c:32
static const char * value_type_name(ValueType t)
Get a human-readable name for a ValueType.
Definition vm.c:318
#define fprintf
Definition vm.c:200

Definition at line 61 of file div.c.

◆ OP_DIV

case OP_DIV

Definition at line 36 of file div.c.