![]() |
Fun 0.41.5
The programming language that makes you have fun!
|
Implements the Fun language parser that converts source code to bytecode. More...
#include "parser.h"#include "value.h"#include "vm.h"#include <ctype.h>#include <stdarg.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "parser_utils.c"Go to the source code of this file.
Data Structures | |
| struct | LocalEnv |
| struct | LoopCtx |
Macros | |
| #define | TYPE_META_STRING 10001 |
| Type metadata tag used for string enforcement in declared types. | |
| #define | TYPE_META_BOOLEAN 10002 |
| Type metadata tag used for boolean enforcement in declared types. | |
| #define | TYPE_META_NIL 10003 |
| Type metadata tag indicating explicit nil type. | |
| #define | TYPE_META_CLASS 10004 |
| Type metadata tag marking class/instance values. | |
| #define | TYPE_META_FLOAT 10005 |
| Type metadata tag marking floating point numbers. | |
| #define | TYPE_META_ARRAY 10006 |
| Type metadata tag marking array values. | |
| #define | MAP_TYPE_KIND(t) |
Typedefs | |
| typedef struct LoopCtx | LoopCtx |
Functions | |
| char * | preprocess_includes_with_path (const char *src, const char *current_path) |
| Preprocess includes with a known file path to improve span markers. | |
| static void | skip_to_eol (const char *src, size_t len, size_t *pos) |
| Advance position to the end of the current line, validating tail. | |
| static int | read_line_start (const char *src, size_t len, size_t *pos, int *out_indent) |
| Read the start of a logical line and compute indentation. | |
| static void | parse_block (Bytecode *bc, const char *src, size_t len, size_t *pos, int current_indent) |
| Parse a block of statements at a given indentation level. | |
| static int | env_truthy (const char *name) |
| Interpret an environment variable as a boolean flag. | |
| static int | fun_debug_enabled (void) |
| Determine whether parser/compiler debug tracing is enabled. | |
| static void | parser_fail (size_t pos, const char *fmt,...) |
| Record a parser/compiler error at a given source position. | |
| static void | calc_line_col (const char *src, size_t len, size_t pos, int *out_line, int *out_col) |
| Compute one-based line and column from a byte position. | |
| static void | ns_aliases_reset (void) |
| Reset and free all tracked namespace aliases. | |
| static void | ns_aliases_scan (const char *src, size_t len) |
| Scan preprocessed source for namespace alias markers. | |
| static int | is_ns_alias (const char *name) |
| Check whether an identifier is a registered namespace alias. | |
| static int | sym_find (const char *name) |
| Find a global symbol index by name. | |
| static int | sym_index (const char *name) |
| Get or create a global symbol index for a name. | |
| static int | name_in_outer_envs (const char *name) |
| Check whether a local name exists in any outer function environment. | |
| static int | local_find (const char *name) |
| Find the index of a local variable in the current function. | |
| static int | local_add (const char *name) |
| Add a new local variable to the current function environment. | |
| static int | emit_expression (Bytecode *bc, const char *src, size_t len, size_t *pos) |
| Parse and emit a full expression using precedence climbing. | |
| static int | emit_primary (Bytecode *bc, const char *src, size_t len, size_t *pos) |
| Parse and emit bytecode for primary expressions. | |
| static int | emit_unary (Bytecode *bc, const char *src, size_t len, size_t *pos) |
| Parse and emit unary expressions. | |
| static int | emit_multiplicative (Bytecode *bc, const char *src, size_t len, size_t *pos) |
| Parse and emit multiplicative expressions. | |
| static int | emit_additive (Bytecode *bc, const char *src, size_t len, size_t *pos) |
| Parse and emit additive expressions. | |
| static int | emit_relational (Bytecode *bc, const char *src, size_t len, size_t *pos) |
| Parse and emit relational expressions. | |
| static int | emit_equality (Bytecode *bc, const char *src, size_t len, size_t *pos) |
| Parse and emit equality/inequality expressions. | |
| static int | emit_and_expr (Bytecode *bc, const char *src, size_t len, size_t *pos) |
| Parse and emit logical AND (&&) with short-circuiting. | |
| static int | emit_or_expr (Bytecode *bc, const char *src, size_t len, size_t *pos) |
| Parse and emit logical OR (||) with short-circuiting. | |
| static int | emit_conditional (Bytecode *bc, const char *src, size_t len, size_t *pos) |
| Parse and emit the ternary conditional operator. | |
| static void | parse_simple_statement (Bytecode *bc, const char *src, size_t len, size_t *pos) |
| Parse a single statement on the current line and emit bytecode. | |
| static Bytecode * | compile_minimal (const char *src, size_t len) |
| Compile a full source buffer into bytecode. | |
| Bytecode * | parse_file_to_bytecode (const char *path) |
| Parse a .fun source file and return compiled bytecode. | |
| Bytecode * | parse_string_to_bytecode (const char *source) |
| Parse a source string and return compiled bytecode. | |
| int | parser_last_error (char *msgBuf, unsigned long msgCap, int *outLine, int *outCol) |
| Retrieve the last parser/compiler error information, if any. | |
Variables | |
| static const char * | g_current_source_path = NULL |
| static int | g_has_error = 0 |
| static size_t | g_err_pos = 0 |
| static char | g_err_msg [256] |
| static int | g_err_line = 0 |
| static int | g_err_col = 0 |
| static int | g_temp_counter = 0 |
| static char * | g_ns_aliases [64] |
| static int | g_ns_alias_count = 0 |
| struct { | |
| char * names [MAX_GLOBALS] | |
| int types [MAX_GLOBALS] | |
| int is_class [MAX_GLOBALS] | |
| int count | |
| } | G = {{0}, {0}, {0}, 0} |
| static LocalEnv * | g_locals = NULL |
| static LocalEnv * | g_func_env_stack [64] |
| static int | g_func_env_depth = 0 |
| static LoopCtx * | g_loop_ctx = NULL |
Implements the Fun language parser that converts source code to bytecode.
This file contains the main parsing logic for the Fun programming language. It handles converting .fun source files into executable bytecode for the VM.
Key Features:
Functions:
Error Handling:
Example: Bytecode *bc = parse_file_to_bytecode("example.fun"); if (bc) { vm_run(&vm, bc); bytecode_free(bc); }
Definition in file parser.c.
| #define MAP_TYPE_KIND | ( | t | ) |
| #define TYPE_META_ARRAY 10006 |
| #define TYPE_META_BOOLEAN 10002 |
| #define TYPE_META_CLASS 10004 |
| #define TYPE_META_FLOAT 10005 |
| #define TYPE_META_NIL 10003 |
| #define TYPE_META_STRING 10001 |
| typedef struct LoopCtx LoopCtx |
|
static |
Compute one-based line and column from a byte position.
Counts newlines up to the smaller of pos and len to derive a human-friendly (line, column) pair. Columns are one-based and reset after each newline.
| src | Source buffer. |
| len | Source length in bytes. |
| pos | Target byte position within the source. |
| out_line | Optional out parameter for the computed line. |
| out_col | Optional out parameter for the computed column. |
|
static |
Compile a full source buffer into bytecode.
Preprocesses namespace aliases, handles shebangs and top-level constructs, parses the indentation-aware block and appends a final HALT instruction.
| src | Source buffer to compile (preprocessed when used via file API). |
| len | Length of the source buffer in bytes. |
|
static |
|
static |
Parse and emit logical AND (&&) with short-circuiting.
Evaluates left-to-right, jumping around subsequent operands when a false operand is encountered.
| bc | Target bytecode. |
| src | Source buffer. |
| len | Source length. |
| pos | In/out byte position pointer. |
|
static |
|
static |
|
static |
Parse and emit a full expression using precedence climbing.
Delegates to emit_conditional which handles the highest-level precedence (ternary). This serves as the common entry for expression parsing at various grammar positions.
| bc | Target bytecode. |
| src | Source buffer. |
| len | Source length in bytes. |
| pos | In/out byte position pointer. |
|
static |
|
static |
Parse and emit logical OR (||) with short-circuiting.
Evaluates left-to-right; when a true operand is encountered, remaining operands are skipped and the expression yields true.
| bc | Target bytecode. |
| src | Source buffer. |
| len | Source length. |
| pos | In/out byte position pointer. |
|
static |
Parse and emit bytecode for primary expressions.
Handles literals, identifiers, parenthesized expressions, function literals, array/map literals, indexing, calls, member access, and related constructs.
| bc | Target bytecode under construction. |
| src | Source buffer. |
| len | Source length in bytes. |
| pos | In/out byte position pointer; advanced past the parsed primary. |
|
static |
|
static |
Parse and emit unary expressions.
Supports logical not (!) and unary minus (-) with right associativity, falling back to primary expressions.
| bc | Target bytecode. |
| src | Source buffer. |
| len | Source length. |
| pos | In/out byte position pointer. |
|
static |
Interpret an environment variable as a boolean flag.
Recognizes common truthy forms: 1, true/TRUE, yes/YES, on/ON. Any other value (including unset) is considered false.
| name | Environment variable name (must not be NULL). |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
Scan preprocessed source for namespace alias markers.
Looks for lines starting with "// __ns_alias__: <name>" and stores <name> so that member-style calls on that identifier are parsed as plain calls (no implicit receiver).
| src | Preprocessed source buffer. |
| len | Length of src in bytes. |
|
static |
Parse a block of statements at a given indentation level.
Continues parsing lines while their indentation is >= current_indent. On dedent, returns control to the caller so upstream constructs (e.g., if/while) can close. Inserts OP_LINE markers to improve runtime diagnostics.
| bc | Target bytecode. |
| src | Source buffer. |
| len | Source length. |
| pos | In/out byte position pointer. |
| current_indent | Indentation level (spaces) of the enclosing block. |
| Bytecode * parse_file_to_bytecode | ( | const char * | path | ) |
Parse a .fun source file and return compiled bytecode.
Parse a .fun source file and compile it into a bytecode chunk.
Reads the file, preprocesses includes (tracking original file paths), resets the global error state, and compiles to Bytecode while attaching source metadata (name, source_file).
| path | Filesystem path to the source file. |
|
static |
Parse a single statement on the current line and emit bytecode.
Handles assignments, function calls, control flow starters, print/debug statements and other simple constructs that fit on one logical line.
| bc | Target bytecode. |
| src | Source buffer. |
| len | Source length. |
| pos | In/out byte position pointer at start of the statement line. |
| Bytecode * parse_string_to_bytecode | ( | const char * | source | ) |
Parse a source string and return compiled bytecode.
Parse source from a provided string buffer (REPL/tests helper).
Suitable for REPL or tests. Performs include preprocessing with no base path, compiles, and attaches generic source metadata.
| source | NUL-terminated source code string. |
|
static |
Record a parser/compiler error at a given source position.
Formats an error message into the parser's global error buffer and stores the offending byte position. Line and column are derived later on demand.
| pos | Byte offset in the current (preprocessed) source. |
| fmt | printf-style format string for the message. |
| ... | Arguments matching fmt. |
| int parser_last_error | ( | char * | msgBuf, |
| unsigned long | msgCap, | ||
| int * | outLine, | ||
| int * | outCol ) |
Retrieve the last parser/compiler error information, if any.
Retrieve information about the last parser error, if any.
Copies the error message into msgBuf (truncated to msgCap-1), and returns the one-based line and column where available. If no error is pending, returns 0 and leaves outputs unchanged.
| msgBuf | Destination buffer for the error message (may be NULL). |
| msgCap | Capacity of msgBuf in bytes. |
| outLine | Optional out param for one-based line number. |
| outCol | Optional out param for one-based column number. |
|
extern |
Preprocess includes with a known file path to improve span markers.
Definition at line 973 of file parser_utils.c.
|
static |
Read the start of a logical line and compute indentation.
Consumes blank lines and comment-only lines. Tabs are forbidden for indentation; only spaces are allowed, counted in units of 2.
| src | Source buffer. |
| len | Source length. |
| pos | In/out byte position pointer; advanced to first non-space. |
| out_indent | Receives the number of leading spaces on the line. |
Read the start of a logical line and compute indentation.
Consumes blank lines and comment-only lines. Tabs are forbidden for indentation; only spaces are allowed, counted in units of 2.
| src | Source buffer. |
| len | Source length. |
| pos | In/out byte position pointer; advanced to first non-space. |
| out_indent | Receives the number of leading spaces on the line. |
|
static |
Advance position to the end of the current line, validating tail.
Skips spaces and inline/block comments until CR/LF/CRLF or end-of-input. Reports an error if trailing non-space/comment characters are found.
| src | Source buffer. |
| len | Source length. |
| pos | In/out byte position pointer; set to first char of next line. |
|
static |
|
static |
Get or create a global symbol index for a name.
Ensures the symbol exists in the global table, creating a new entry with default metadata when absent.
| name | Symbol name. |
| struct { ... } G |
| int is_class[MAX_GLOBALS] |
| char* names[MAX_GLOBALS] |
| int types[MAX_GLOBALS] |