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

Implements the OP_RANDOM_INT opcode for generating random integers in the VM. More...

Go to the source code of this file.

Functions

 if (lo.type !=VAL_INT||hi.type !=VAL_INT)
 if (b<=a)
 push_value (vm, make_int(a+r))
 free_value (lo)
 free_value (hi)

Variables

case OP_RANDOM_INT
Value lo = pop_value(vm)
int64_t a = lo.i
int64_t b = hi.i
int64_t span = b - a
int64_t r = (int64_t)(rand() % (span))
 break

Detailed Description

Implements the OP_RANDOM_INT opcode for generating random integers in the VM.

This file handles the OP_RANDOM_INT instruction, which generates a random integer within a specified range. The range bounds are popped from the stack, and the result is pushed back.

Behavior:

  • Pops the upper and lower bounds from the stack.
  • Generates a random integer within the range.
  • Pushes the result onto the stack.

Error Handling:

  • Exits with an error if the bounds are not integers.
  • Exits with an error if the lower bound is greater than the upper bound.

Example: // Bytecode: OP_RANDOM_INT // Stack before: [10, 1] // Stack after: [7]

Definition in file random_int.c.

Function Documentation

◆ free_value() [1/2]

free_value(hi)

◆ free_value() [2/2]

free_value(lo)
Here is the caller graph for this function:

◆ if() [1/2]

if(b<=a)

Definition at line 41 of file random_int.c.

Here is the call graph for this function:

◆ if() [2/2]

if(lo.type ! = VAL_INT || hi.type != VAL_INT)

Definition at line 36 of file random_int.c.

◆ push_value()

push_value(vm,
make_int(a+r) )
Here is the caller graph for this function:

Variable Documentation

◆ a

int64_t a = lo.i

Definition at line 40 of file random_int.c.

◆ b

int64_t b = hi.i

Definition at line 40 of file random_int.c.

◆ break

break

Definition at line 52 of file random_int.c.

◆ lo

Value lo = pop_value(vm)

Definition at line 35 of file random_int.c.

◆ OP_RANDOM_INT

case OP_RANDOM_INT

Definition at line 33 of file random_int.c.

◆ r

int64_t r = (int64_t)(rand() % (span))

Definition at line 48 of file random_int.c.

◆ span

int64_t span = b - a

Definition at line 47 of file random_int.c.