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

Implements the OP_POW opcode for exponentiation in the VM. More...

Go to the source code of this file.

Functions

 if (a.type !=VAL_INT||b.type !=VAL_INT)
 if (exp< 0)
 free_value (a)
 free_value (b)

Variables

case OP_POW
Value a = pop_value(vm)
int64_t base = a.i
int64_t exp = b.i
int64_t res = 1
 else
 break

Detailed Description

Implements the OP_POW opcode for exponentiation in the VM.

This file handles the OP_POW instruction, which computes the power of two integer values. The values are popped from the stack, and the result is pushed back.

Behavior:

  • Pops two integer values from the stack.
  • Computes the power of the first value raised to the second.
  • Pushes the result onto the stack.

Error Handling:

  • Exits with an error if the operands are not integers.

Example: // Bytecode: OP_POW // Stack before: [2, 3] // Stack after: [8]

Definition in file pow.c.

Function Documentation

◆ free_value() [1/2]

free_value(a)

◆ free_value() [2/2]

free_value(b)

◆ if() [1/2]

if(a.type ! = VAL_INT || b.type != VAL_INT)

Definition at line 34 of file pow.c.

◆ if() [2/2]

if()

Definition at line 41 of file pow.c.

Variable Documentation

◆ a

Value a = pop_value(vm)

Definition at line 33 of file pow.c.

◆ base

int64_t base = a.i

Definition at line 38 of file pow.c.

◆ break

break

Definition at line 53 of file pow.c.

◆ else

else
Initial value:
{
while (exp > 0) {
if (exp & 1) res *= base;
exp >>= 1;
}
}
int res
Definition and.c:34
push_value(vm, out)
int64_t exp
Definition pow.c:39
int64_t base
Definition pow.c:38
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51

Definition at line 43 of file pow.c.

◆ exp

int64_t exp = b.i

Definition at line 39 of file pow.c.

◆ OP_POW

case OP_POW

Definition at line 31 of file pow.c.

◆ res

int64_t res = 1

Definition at line 40 of file pow.c.