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

Implements the OP_READ_FILE opcode for reading file contents 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)
 if (!f)
 if (fseek(f, 0, SEEK_END) !=0)
 if (sz< 0)
 rewind (f)
 fclose (f)
 free (buf)
 free_value (path)
 push_value (vm, out)

Variables

case OP_READ_FILE
const char * p = path.s ? path.s : ""
FILE * f = fopen(p, "rb")
long sz = ftell(f)
char * buf = (char *)malloc((size_t)sz + 1)
size_t n = buf ? fread(buf, 1, (size_t)sz, f) : 0
Value out = make_string(buf)
 break

Detailed Description

Implements the OP_READ_FILE opcode for reading file contents in the VM.

This file handles the OP_READ_FILE instruction, which reads the contents of a file and pushes the result as a string onto the stack.

Behavior:

  • Pops the file path from the stack.
  • Reads the file contents.
  • Pushes the contents as a string onto the stack.

Error Handling:

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

Example: // Bytecode: OP_READ_FILE // Stack before: ["file.txt"] // Stack after: ["file contents"]

Definition in file read_file.c.

Function Documentation

◆ fclose()

fclose(f)
Here is the caller graph for this function:

◆ free()

free(buf)

◆ free_value()

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

◆ if() [1/4]

if(!f)

Definition at line 39 of file read_file.c.

Here is the call graph for this function:

◆ if() [2/4]

if(fseek(f, 0, SEEK_END) ! = 0)

Definition at line 44 of file read_file.c.

Here is the call graph for this function:

◆ if() [3/4]

if(path.type ! = VAL_STRING)

Definition at line 33 of file read_file.c.

◆ if() [4/4]

if()

Definition at line 51 of file read_file.c.

Here is the call graph for this function:

◆ push_value()

push_value(vm,
out )
Here is the caller graph for this function:

◆ rewind()

rewind(f)

Variable Documentation

◆ break

break

Definition at line 71 of file read_file.c.

◆ buf

buf[n] = (char *)malloc((size_t)sz + 1)

Definition at line 58 of file read_file.c.

◆ f

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

Definition at line 38 of file read_file.c.

◆ n

size_t n = buf ? fread(buf, 1, (size_t)sz, f) : 0

Definition at line 59 of file read_file.c.

◆ OP_READ_FILE

case OP_READ_FILE

Definition at line 31 of file read_file.c.

◆ out

Definition at line 67 of file read_file.c.

◆ p

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

Definition at line 37 of file read_file.c.

◆ sz

long sz = ftell(f)

Definition at line 50 of file read_file.c.