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

Interactive Read–Eval–Print Loop (REPL) for the Fun language. More...

#include "repl.h"
#include "bytecode.h"
#include "parser.h"
#include "value.h"
#include "vm.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <dirent.h>
#include <limits.h>
#include <sys/stat.h>
#include <termios.h>
#include <unistd.h>
Include dependency graph for repl.c:

Go to the source code of this file.

Macros

#define FUN_VERSION   "0.0.0-dev"
#define RL_REDRAW()

Enumerations

enum  { RL_HIST_MAX = 1000 }

Functions

int map_expanded_line_to_include_path (const char *path, int line, char *out_path, size_t out_path_cap, int *out_line)
 Map a line number in expanded source back to original include path/line.
static const char * rl_hist_last (void)
 Return the last history line or NULL if history is empty.
static void rl_hist_add (const char *s)
 Append a non-empty line to in-memory history, de-duplicating consecutive duplicates. Trailing newlines are stripped.
static void rl_hist_load_file (const char *path)
 Load history entries from a file (one line per entry).
static void repl_disable_raw (void)
 Disable terminal raw mode if previously enabled.
static int repl_enable_raw (void)
 Enable minimal terminal raw mode for interactive input.
static int is_dir_path (const char *path)
 Return 1 if path refers to a directory, 0 otherwise.
static size_t lcp_suffix (const char **names, int count, size_t base_len)
 Longest common suffix length among names[i] starting at base_len.
static void rl_word_left (const char *out, size_t len, size_t *pos)
 Move cursor left by one word.
static void rl_word_right (const char *out, size_t len, size_t *pos)
 Move cursor right by one word.
static int complete_load_path (char *buf, size_t *len_io)
 Complete a :load file path in-place within the buffer.
static int is_ident_start (int c)
 Return 1 if c can start an identifier.
static int is_ident_char (int c)
 Return 1 if c is a valid identifier continuation character.
static void std_syms_add (const char *name)
 Add a stdlib symbol name to the completion table.
static void scan_symbols_from_file (const char *path)
 Scan a .fun file and collect top-level definitions for completion.
static void scan_dir_recursive (const char *dir)
 Recursively scan a directory tree for .fun files.
static void load_stdlib_symbols (const char *libdir)
 Populate stdlib completion symbols from a base directory.
static void free_stdlib_symbols (void)
 Free all memory held by stdlib completion symbols.
static int complete_stdlib_ident (char *buf, size_t *len_io, size_t *pos_io, size_t cap)
 Complete the trailing identifier in buf from stdlib symbols.
static int read_line_edit (char *out, size_t out_cap, const char *prompt)
 Read a line with basic line-editing and history support.
static int is_blank_line (const char *s)
static const char * lstrip (const char *s)
 Advance pointer past leading spaces and tabs.
static int ends_with_opener (const char *line)
 Heuristic: check if a line ends with an operator that suggests continuation.
static int compute_open_indent_blocks (const char *buf)
 Compute number of open indentation blocks (2 spaces per level).
static int buffer_looks_incomplete (const char *buf)
 Heuristic to determine if the current buffer likely needs another line. Looks for open quotes/escapes or unmatched indentation.
static void show_repl_help (void)
 Print REPL help with available commands.
static char * read_entire_file (const char *path, size_t *out_len)
 Read a file fully into a newly allocated buffer.
static int write_entire_file (const char *path, const char *data, size_t len)
 Write a buffer to a file path, replacing existing contents.
static int cmd_is_one_of (const char *cmd, const char *const names[])
 Return 1 if cmd equals any non-NULL entry in names[] (terminated by NULL).
static void hexdump_to (FILE *out, const unsigned char *data, size_t len, size_t base_off)
 Dump a byte buffer in hex to a FILE*, with offsets.
static void print_last_n_lines (const char *path, int n)
 Print the last n lines of a file to stdout (tail-like).
static void append_history (FILE *hist, const char *buffer)
 Append multi-line buffer to history file, one line per entry.
static void env_show_usage (void)
 Print usage for :env commands.
static void env_get (const char *name)
 Show the value of an environment variable.
static void env_set (const char *name, const char *value)
 Set or unset (when value is NULL) an environment variable.
int fun_run_repl (VM *vm)
 Run the interactive Fun REPL session.

Variables

static char * rl_hist [RL_HIST_MAX]
static int rl_count = 0
static struct termios g_orig_tios
static int g_raw_enabled = 0
static char ** g_std_syms = NULL
static int g_std_syms_count = 0
static int g_std_syms_cap = 0

Detailed Description

Interactive Read–Eval–Print Loop (REPL) for the Fun language.

Provides line editing, history, simple completion for :load paths and stdlib identifiers, multi-line input heuristics, and a command interface (e.g. :help, :env, :load). Built only when FUN_WITH_REPL is defined.

Definition in file repl.c.

Macro Definition Documentation

◆ FUN_VERSION

#define FUN_VERSION   "0.0.0-dev"

Definition at line 45 of file repl.c.

◆ RL_REDRAW

#define RL_REDRAW ( )
Value:
do { \
/* Count explicit newlines to estimate rows to draw (prompt line + content lines) */ \
int rows = 1; \
int has_nl = 0; \
for (size_t __i = 0; __i < len; ++__i) { \
if (out[__i] == '\n') { \
rows++; \
has_nl = 1; \
} \
} \
/* Move cursor to the start (top) of the previous render block */ \
if (prev_rows > 1) { \
fprintf(stdout, "\x1b[%dA", prev_rows - 1); \
} \
fputc('\r', stdout); \
/* Repaint prompt + content */ \
if (prompt) fputs(prompt, stdout); \
fwrite(out, 1, len, stdout); \
/* Clear everything below the current cursor (removes remnants of older, longer renders) */ \
fputs("\x1b[J", stdout); \
/* Cursor policy: keep at end for multi-line; for single-line respect pos */ \
if (!has_nl && pos < len) { \
size_t back = (size_t)(len - pos); \
if (back > 0) fprintf(stdout, "\x1b[%zuD", back); \
} else { \
pos = len; \
} \
prev_rows = rows; \
fflush(stdout); \
} while (0)
else
Definition add.c:76
Value out
Definition apop.c:38
size_t len
Definition input_line.c:102
Value rows
Definition query.c:41
#define fprintf
Definition vm.c:200

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
RL_HIST_MAX 

Definition at line 49 of file repl.c.

Function Documentation

◆ append_history()

void append_history ( FILE * hist,
const char * buffer )
static

Append multi-line buffer to history file, one line per entry.

Definition at line 1259 of file repl.c.

◆ buffer_looks_incomplete()

int buffer_looks_incomplete ( const char * buf)
static

Heuristic to determine if the current buffer likely needs another line. Looks for open quotes/escapes or unmatched indentation.

Definition at line 973 of file repl.c.

◆ cmd_is_one_of()

int cmd_is_one_of ( const char * cmd,
const char *const names[] )
static

Return 1 if cmd equals any non-NULL entry in names[] (terminated by NULL).

Definition at line 1191 of file repl.c.

◆ complete_load_path()

int complete_load_path ( char * buf,
size_t * len_io )
static

Complete a :load file path in-place within the buffer.

Returns
1 if buffer changed, 2 if a menu was printed, 0 otherwise.

Definition at line 208 of file repl.c.

◆ complete_stdlib_ident()

int complete_stdlib_ident ( char * buf,
size_t * len_io,
size_t * pos_io,
size_t cap )
static

Complete the trailing identifier in buf from stdlib symbols.

Returns
1 if buffer changed, 2 if menu printed, 0 otherwise.

Definition at line 488 of file repl.c.

◆ compute_open_indent_blocks()

int compute_open_indent_blocks ( const char * buf)
static

Compute number of open indentation blocks (2 spaces per level).

Definition at line 899 of file repl.c.

◆ ends_with_opener()

int ends_with_opener ( const char * line)
static

Heuristic: check if a line ends with an operator that suggests continuation.

Definition at line 882 of file repl.c.

◆ env_get()

void env_get ( const char * name)
static

Show the value of an environment variable.

Definition at line 1280 of file repl.c.

◆ env_set()

void env_set ( const char * name,
const char * value )
static

Set or unset (when value is NULL) an environment variable.

Definition at line 1291 of file repl.c.

◆ env_show_usage()

void env_show_usage ( void )
static

Print usage for :env commands.

Definition at line 1270 of file repl.c.

◆ free_stdlib_symbols()

void free_stdlib_symbols ( void )
static

Free all memory held by stdlib completion symbols.

Definition at line 474 of file repl.c.

◆ fun_run_repl()

int fun_run_repl ( VM * vm)

Run the interactive Fun REPL session.

Run the interactive REPL using an already-initialized VM.

Parameters
vmInitialized VM to execute user input within.
Returns
Exit status code for the REPL.

Definition at line 1310 of file repl.c.

◆ hexdump_to()

void hexdump_to ( FILE * out,
const unsigned char * data,
size_t len,
size_t base_off )
static

Dump a byte buffer in hex to a FILE*, with offsets.

Definition at line 1203 of file repl.c.

◆ is_blank_line()

int is_blank_line ( const char * s)
static

Return 1 if the line consists only of whitespace.

Definition at line 860 of file repl.c.

◆ is_dir_path()

int is_dir_path ( const char * path)
static

Return 1 if path refers to a directory, 0 otherwise.

Definition at line 141 of file repl.c.

◆ is_ident_char()

int is_ident_char ( int c)
static

Return 1 if c is a valid identifier continuation character.

Definition at line 369 of file repl.c.

◆ is_ident_start()

int is_ident_start ( int c)
static

Return 1 if c can start an identifier.

Definition at line 363 of file repl.c.

◆ lcp_suffix()

size_t lcp_suffix ( const char ** names,
int count,
size_t base_len )
static

Longest common suffix length among names[i] starting at base_len.

Definition at line 151 of file repl.c.

◆ load_stdlib_symbols()

void load_stdlib_symbols ( const char * libdir)
static

Populate stdlib completion symbols from a base directory.

Definition at line 466 of file repl.c.

◆ lstrip()

const char * lstrip ( const char * s)
static

Advance pointer past leading spaces and tabs.

Definition at line 873 of file repl.c.

◆ map_expanded_line_to_include_path()

int map_expanded_line_to_include_path ( const char * path,
int line,
char * out_path,
size_t out_path_cap,
int * out_line )
extern

Map a line number in expanded source back to original include path/line.

Scans the expanded text for the nearest preceding __include_begin__ marker and computes the corresponding inner line number.

Parameters
pathPath to the original top-level file that was expanded.
line1-based line number in the expanded text.
out_pathOutput buffer for the resolved file path.
out_path_capCapacity of out_path.
out_lineReceives 1-based line number within resolved file.
Returns
1 on success, 0 on failure.

Definition at line 996 of file parser_utils.c.

◆ print_last_n_lines()

void print_last_n_lines ( const char * path,
int n )
static

Print the last n lines of a file to stdout (tail-like).

Definition at line 1232 of file repl.c.

◆ read_entire_file()

char * read_entire_file ( const char * path,
size_t * out_len )
static

Read a file fully into a newly allocated buffer.

Returns
Buffer on success (caller frees), or NULL on error.

Definition at line 1150 of file repl.c.

◆ read_line_edit()

int read_line_edit ( char * out,
size_t out_cap,
const char * prompt )
static

Read a line with basic line-editing and history support.

Parameters
outOutput buffer to receive the line (NUL-terminated).
out_capCapacity of the output buffer.
promptPrompt string to display (may be NULL).
Returns
1 on success, 0 on EOF/error.

Definition at line 579 of file repl.c.

◆ repl_disable_raw()

void repl_disable_raw ( void )
static

Disable terminal raw mode if previously enabled.

Definition at line 114 of file repl.c.

◆ repl_enable_raw()

int repl_enable_raw ( void )
static

Enable minimal terminal raw mode for interactive input.

Returns
1 on success, 0 otherwise.

Definition at line 124 of file repl.c.

◆ rl_hist_add()

void rl_hist_add ( const char * s)
static

Append a non-empty line to in-memory history, de-duplicating consecutive duplicates. Trailing newlines are stripped.

Definition at line 67 of file repl.c.

◆ rl_hist_last()

const char * rl_hist_last ( void )
static

Return the last history line or NULL if history is empty.

Definition at line 57 of file repl.c.

◆ rl_hist_load_file()

void rl_hist_load_file ( const char * path)
static

Load history entries from a file (one line per entry).

Definition at line 96 of file repl.c.

◆ rl_word_left()

void rl_word_left ( const char * out,
size_t len,
size_t * pos )
static

Move cursor left by one word.

Definition at line 182 of file repl.c.

◆ rl_word_right()

void rl_word_right ( const char * out,
size_t len,
size_t * pos )
static

Move cursor right by one word.

Definition at line 194 of file repl.c.

◆ scan_dir_recursive()

void scan_dir_recursive ( const char * dir)
static

Recursively scan a directory tree for .fun files.

Definition at line 440 of file repl.c.

◆ scan_symbols_from_file()

void scan_symbols_from_file ( const char * path)
static

Scan a .fun file and collect top-level definitions for completion.

Definition at line 399 of file repl.c.

◆ show_repl_help()

void show_repl_help ( void )
static

Print REPL help with available commands.

Definition at line 1107 of file repl.c.

◆ std_syms_add()

void std_syms_add ( const char * name)
static

Add a stdlib symbol name to the completion table.

Definition at line 376 of file repl.c.

◆ write_entire_file()

int write_entire_file ( const char * path,
const char * data,
size_t len )
static

Write a buffer to a file path, replacing existing contents.

Returns
1 on success, 0 on error.

Definition at line 1179 of file repl.c.

Variable Documentation

◆ g_orig_tios

struct termios g_orig_tios
static

Definition at line 108 of file repl.c.

◆ g_raw_enabled

int g_raw_enabled = 0
static

Definition at line 109 of file repl.c.

◆ g_std_syms

char** g_std_syms = NULL
static

Definition at line 356 of file repl.c.

◆ g_std_syms_cap

int g_std_syms_cap = 0
static

Definition at line 358 of file repl.c.

◆ g_std_syms_count

int g_std_syms_count = 0
static

Definition at line 357 of file repl.c.

◆ rl_count

int rl_count = 0
static

Definition at line 51 of file repl.c.

◆ rl_hist

char* rl_hist[RL_HIST_MAX]
static

Definition at line 50 of file repl.c.