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

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"
Include dependency graph for parser.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.
Bytecodeparse_file_to_bytecode (const char *path)
 Parse a .fun source file and return compiled bytecode.
Bytecodeparse_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.

Detailed Description

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:

  • Handles shebang lines
  • Skips whitespace and comments
  • Parses string literals with both single and double quotes
  • Supports basic function definitions
  • Compiles print statements
  • Generates bytecode with proper constants and instructions

Functions:

Error Handling:

  • Returns NULL on parse errors
  • Tracks error messages and positions
  • Validates syntax before bytecode generation

Example: Bytecode *bc = parse_file_to_bytecode("example.fun"); if (bc) { vm_run(&vm, bc); bytecode_free(bc); }

Author
Johannes Findeisen
Date
2025-09-16

Definition in file parser.c.

Macro Definition Documentation

◆ MAP_TYPE_KIND

#define MAP_TYPE_KIND(t)
Value:
( \
((t) && strcmp((t), "string") == 0) ? 2 : ((t) && strcmp((t), "nil") == 0) ? 3 \
: ((t) && (strcmp((t), "boolean") == 0 || strcmp((t), "number") == 0 || strcmp((t), "byte") == 0 || strncmp((t), "uint", 4) == 0 || strncmp((t), "sint", 4) == 0 || strncmp((t), "int", 3) == 0)) ? 1 \
: 0)
long t
Definition sleep_ms.c:32

◆ TYPE_META_ARRAY

#define TYPE_META_ARRAY   10006

Type metadata tag marking array values.

Definition at line 130 of file parser.c.

◆ TYPE_META_BOOLEAN

#define TYPE_META_BOOLEAN   10002

Type metadata tag used for boolean enforcement in declared types.

Definition at line 122 of file parser.c.

◆ TYPE_META_CLASS

#define TYPE_META_CLASS   10004

Type metadata tag marking class/instance values.

Definition at line 126 of file parser.c.

◆ TYPE_META_FLOAT

#define TYPE_META_FLOAT   10005

Type metadata tag marking floating point numbers.

Definition at line 128 of file parser.c.

◆ TYPE_META_NIL

#define TYPE_META_NIL   10003

Type metadata tag indicating explicit nil type.

Definition at line 124 of file parser.c.

◆ TYPE_META_STRING

#define TYPE_META_STRING   10001

Type metadata tag used for string enforcement in declared types.

Definition at line 120 of file parser.c.

Typedef Documentation

◆ LoopCtx

typedef struct LoopCtx LoopCtx

Function Documentation

◆ parse_file_to_bytecode()

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).

Parameters
pathFilesystem path to the source file.
Returns
Bytecode pointer on success; NULL on I/O or parse error.

Definition at line 7529 of file parser.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_string_to_bytecode()

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.

Parameters
sourceNUL-terminated source code string.
Returns
Bytecode pointer on success; NULL on parse error.

Definition at line 7714 of file parser.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parser_last_error()

int parser_last_error(char *msgBuf,
unsigned longmsgCap,
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.

Parameters
msgBufDestination buffer for the error message (may be NULL).
msgCapCapacity of msgBuf in bytes.
outLineOptional out param for one-based line number.
outColOptional out param for one-based column number.
Returns
1 if an error was available and copied, 0 otherwise.

Definition at line 7770 of file parser.c.

Here is the caller graph for this function:

◆ preprocess_includes_with_path()

char * preprocess_includes_with_path(const char *src,
const char *current_path )
extern

Preprocess includes with a known file path to improve span markers.

Definition at line 973 of file parser_utils.c.

Here is the caller graph for this function:

Variable Documentation

◆ count

int count

Definition at line 266 of file parser.c.

◆ is_class

int is_class[MAX_GLOBALS]

Definition at line 265 of file parser.c.

◆ names

char* names[MAX_GLOBALS]

Definition at line 263 of file parser.c.

◆ types

int types[MAX_GLOBALS]

Definition at line 264 of file parser.c.