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

Implements the OP_WRITE_FILE opcode for writing to a file 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 (path.type !=VAL_STRING||data.type !=VAL_STRING)
 if (f)
 free_value (path)
 free_value (data)
 push_value (vm, make_int(ok ? 1 :0))

Variables

case OP_WRITE_FILE
Value path = pop_value(vm)
const char * p = path.s ? path.s : ""
FILE * f = fopen(p, "wb")
int ok = 0
 break

Detailed Description

Implements the OP_WRITE_FILE opcode for writing to a file in the VM.

This file handles the OP_WRITE_FILE instruction, which writes data to a file. The file path and data are popped from the stack, and a success/failure flag is pushed back.

Behavior:

  • Pops the file path and data from the stack.
  • Writes the data to the file.
  • Pushes 1 (success) or 0 (failure) onto the stack.

Error Handling:

  • Exits with an error if the file path is invalid or the file cannot be written.

Example: // Bytecode: OP_WRITE_FILE // Stack before: ["file.txt", "data"] // Stack after: [1]

Definition in file write_file.c.

Function Documentation

◆ free_value() [1/2]

free_value(data)

◆ free_value() [2/2]

free_value(path)

◆ if() [1/2]

if(f)

Definition at line 41 of file write_file.c.

Here is the call graph for this function:

◆ if() [2/2]

if(path.type ! = VAL_STRING || data.type != VAL_STRING)

Definition at line 34 of file write_file.c.

◆ push_value()

push_value(vm,
make_int(ok ? 1 :0) )

Variable Documentation

◆ break

break

Definition at line 49 of file write_file.c.

◆ f

FILE* f = fopen(p, "wb")

Definition at line 39 of file write_file.c.

◆ ok

int ok = 0

Definition at line 40 of file write_file.c.

◆ OP_WRITE_FILE

case OP_WRITE_FILE

Definition at line 31 of file write_file.c.

◆ p

const char* p = path.s ? path.s : ""

Definition at line 38 of file write_file.c.

◆ path

Value path = pop_value(vm)

Definition at line 33 of file write_file.c.