Luma 0.1.0
A low-level compiled alternative to C, C++, and more!
Loading...
Searching...
No Matches
Functions
stmt.c File Reference

Statement parsing implementation for the programming language compiler. More...

#include <stdio.h>
#include <string.h>
#include "../ast/ast.h"
#include "parser.h"
#include "src/lexer/lexer.h"
Include dependency graph for stmt.c:

Functions

Stmtexpr_stmt (Parser *parser)
 Parses an expression statement.
 
Stmtuse_stmt (Parser *parser)
 
Stmtos_stmt (Parser *parser)
 
Stmtlink_stmt (Parser *parser)
 
Stmtconst_stmt (Parser *parser, bool is_public, bool returns_ownership, bool takes_ownership)
 Parses a constant declaration statement.
 
Stmtfn_stmt (Parser *parser, const char *name, bool is_public, bool is_static, bool returns_ownership, bool takes_ownership)
 Parses a function declaration statement.
 
Stmtenum_stmt (Parser *parser, const char *name, bool is_public)
 Parses an enumeration declaration statement.
 
Stmtstruct_stmt (Parser *parser, const char *name, bool is_public)
 Parses a structure declaration statement.
 
Stmtvar_stmt (Parser *parser, bool is_public)
 Parses a variable declaration statement.
 
Stmtreturn_stmt (Parser *parser)
 Parses a return statement.
 
Stmtblock_stmt (Parser *parser)
 Parses a block statement.
 
Stmtif_stmt (Parser *parser)
 Parses if/elif/else conditional statements.
 
Stmtinfinite_loop_stmt (Parser *parser, int line, int col)
 Parses an infinite loop statement.
 
Stmtloop_init (Parser *parser, int line, int col)
 Parses a loop initializer declaration.
 
Stmtfor_loop_stmt (Parser *parser, int line, int col)
 Parses a for loop statement.
 
Stmtloop_stmt (Parser *parser)
 Parses loop statements (infinite, while, or for loops)
 
Stmtprint_stmt (Parser *parser, bool ln)
 Parses print/println statements.
 
Stmtbreak_continue_stmt (Parser *parser, bool is_continue)
 Parses break and continue statements.
 
Stmtdefer_stmt (Parser *parser)
 
Stmtswitch_stmt (Parser *parser)
 
Stmtimpl_stmt (Parser *parser)
 

Detailed Description

Statement parsing implementation for the programming language compiler.

This file contains implementations for parsing all types of statements in the programming language, including declarations, control flow statements, and compound statements. Each parsing function is responsible for consuming the appropriate tokens and constructing the corresponding AST nodes.

Supported statement types:

Author
Connor Harris
Date
2025
Version
1.0

Function Documentation

◆ block_stmt()

Stmt * block_stmt ( Parser parser)

Parses a block statement.

Handles block statements with the syntax: { statement1; statement2; ... }

Parameters
parserPointer to the parser instance
Returns
Pointer to the block statement AST node, or NULL on failure
Note
Empty blocks are allowed and create a valid block statement with 0 statements
Each statement in the block is parsed recursively using parse_stmt()
Handles memory allocation for the statement array using growable arrays
Continues parsing on individual statement failures (for error recovery)
See also
parse_stmt(), create_block_stmt()

◆ break_continue_stmt()

Stmt * break_continue_stmt ( Parser parser,
bool  is_continue 
)

Parses break and continue statements.

Handles loop control statements with the syntax:

  • break; - Exit the current loop
  • continue; - Skip to the next iteration of the current loop
Parameters
parserPointer to the parser instance
is_continueWhether this is a continue (true) or break (false) statement
Returns
Pointer to the break/continue statement AST node, or NULL on failure
Note
Both statements require semicolon terminators
These statements are only valid within loop contexts (enforced at semantic analysis)
Break exits the innermost enclosing loop
Continue skips to the next iteration of the innermost enclosing loop
See also
create_break_continue_stmt()

◆ const_stmt()

Stmt * const_stmt ( Parser parser,
bool  is_public,
bool  returns_ownership,
bool  takes_ownership 
)

Parses a constant declaration statement.

Handles multiple forms of constant declarations:

  • const name: Type = value; - Explicit type annotation
  • const name = fn ... - Function declaration
  • const name = struct ... - Struct declaration
  • const name = enum ... - Enum declaration
Parameters
parserPointer to the parser instance
is_publicWhether this declaration has public visibility
Returns
Pointer to the appropriate declaration statement AST node, or NULL on failure
Note
For simple constants, creates a variable declaration with immutable flag
For complex types (functions, structs, enums), delegates to specialized parsers
If no type is specified, it defaults to the type of the value
See also
fn_stmt(), struct_stmt(), enum_stmt(), create_var_decl_stmt()

◆ defer_stmt()

Stmt * defer_stmt ( Parser parser)

◆ enum_stmt()

Stmt * enum_stmt ( Parser parser,
const char *  name,
bool  is_public 
)

Parses an enumeration declaration statement.

Handles enum declarations with the syntax: enum { member1, member2, member3, ... };

Parameters
parserPointer to the parser instance
nameEnum name (already parsed by caller)
is_publicWhether this enum has public visibility
Returns
Pointer to the enum declaration AST node, or NULL on failure
Note
Enum members are identifiers separated by commas
Trailing commas are allowed but not required
Requires a semicolon terminator after the closing brace
See also
create_enum_decl_stmt()

◆ expr_stmt()

Stmt * expr_stmt ( Parser parser)

Parses an expression statement.

An expression statement consists of any expression followed by a semicolon. This is used for statements that evaluate an expression for its side effects, such as function calls or assignment expressions.

Parameters
parserPointer to the parser instance
Returns
Pointer to the created expression statement AST node, or NULL on failure
Note
Captures line and column information at the start of parsing
Requires a semicolon terminator
See also
parse_expr(), create_expr_stmt()

◆ fn_stmt()

Stmt * fn_stmt ( Parser parser,
const char *  name,
bool  is_public,
bool  is_static,
bool  returns_ownership,
bool  takes_ownership 
)

Parses a function declaration statement.

Handles function declarations with the syntax: fn(param1: Type1, param2: Type2, ...) ReturnType { body }

Parameters
parserPointer to the parser instance
nameFunction name (already parsed by caller)
is_publicWhether this function has public visibility
Returns
Pointer to the function declaration AST node, or NULL on failure
Note
Function parameters are stored as parallel arrays of names and types
Return type is required and parsed after the parameter list
Function body must be a block statement
Memory for parameter arrays is allocated using the arena allocator
See also
parse_type(), block_stmt(), create_func_decl_stmt()

◆ for_loop_stmt()

Stmt * for_loop_stmt ( Parser parser,
int  line,
int  col 
)

Parses a for loop statement.

Handles for loops with the syntax:

loop [i: int = 0, j: int = 1](condition) { ... }
loop [i: int = 0, j: int = 1](condition) : (optional_condition) { ... }
Parameters
parserPointer to the parser instance
lineLine number where the loop statement starts
colColumn number where the loop statement starts
Returns
Pointer to the for loop statement AST node, or NULL on failure
Note
Supports multiple initializer variables separated by commas
Main condition is required and parenthesized
Optional secondary condition can be provided after a colon
Loop body must be a block statement
See also
loop_init(), parse_expr(), block_stmt(), create_for_loop_stmt()

◆ if_stmt()

Stmt * if_stmt ( Parser parser)

Parses if/elif/else conditional statements.

Handles complex conditional statements with multiple branches:

if (condition1) { ... }
elif (condition2) { ... }
elif (condition3) { ... }
else { ... }
Parameters
parserPointer to the parser instance
Returns
Pointer to the if statement AST node, or NULL on failure
Note
Supports multiple elif clauses
Else clause is optional
Each condition must be parenthesized
Each branch must be a block statement
Elif statements are collected in an array rather than nested recursively
See also
parse_expr(), block_stmt(), create_if_stmt()

◆ impl_stmt()

Stmt * impl_stmt ( Parser parser)

◆ infinite_loop_stmt()

Stmt * infinite_loop_stmt ( Parser parser,
int  line,
int  col 
)

Parses an infinite loop statement.

Handles infinite loops with the syntax: loop { ... }

Parameters
parserPointer to the parser instance
lineLine number where the loop statement starts
colColumn number where the loop statement starts
Returns
Pointer to the infinite loop statement AST node, or NULL on failure
Note
The loop body must be a block statement
This creates a loop that runs indefinitely unless broken by break/return
See also
block_stmt(), create_infinite_loop_stmt()

◆ link_stmt()

Stmt * link_stmt ( Parser parser)

◆ loop_init()

Stmt * loop_init ( Parser parser,
int  line,
int  col 
)

Parses a loop initializer declaration.

Helper function for parsing variable declarations within for-loop initializers. Handles the syntax: name: Type = expression

Parameters
parserPointer to the parser instance
lineLine number for the declaration
colColumn number for the declaration
Returns
Pointer to the variable declaration statement, or NULL on failure
Note
Creates a mutable, non-public variable declaration
Used exclusively within for-loop initializer lists
See also
create_var_decl_stmt()

◆ loop_stmt()

Stmt * loop_stmt ( Parser parser)

Parses loop statements (infinite, while, or for loops)

Dispatcher function that determines the type of loop based on the following tokens and delegates to the appropriate specialized parser:

  • loop { ... } → infinite loop
  • loop [initializers](...) { ... } → for loop
  • loop (condition) { ... } → while loop
  • loop (condition) : (optional_condition) { ... } → while loop with secondary condition
Parameters
parserPointer to the parser instance
Returns
Pointer to the appropriate loop statement AST node, or NULL on failure
Note
The specific loop type is determined by the token following 'loop'
All loop bodies must be block statements
See also
infinite_loop_stmt(), for_loop_stmt(), parse_expr(), block_stmt(), create_loop_stmt()

◆ os_stmt()

Stmt * os_stmt ( Parser parser)

◆ print_stmt()

Stmt * print_stmt ( Parser parser,
bool  ln 
)

Parses print/println statements.

Handles output statements with the syntax:

  • print(expr1, expr2, ...);
  • println(expr1, expr2, ...);
Parameters
parserPointer to the parser instance
lnWhether this is a println (true) or print (false) statement
Returns
Pointer to the print statement AST node, or NULL on failure
Note
Supports multiple expressions separated by commas
println automatically adds a newline after output
Empty argument lists are allowed: print();
All expressions are evaluated and their values are printed
See also
parse_expr(), create_print_stmt()

◆ return_stmt()

Stmt * return_stmt ( Parser parser)

Parses a return statement.

Handles return statements with optional return values:

  • return; - Return with no value (void return)
  • return expression; - Return with a value
Parameters
parserPointer to the parser instance
Returns
Pointer to the return statement AST node, or NULL on failure
Note
The return value is optional; if not present, creates a void return
Requires a semicolon terminator
See also
parse_expr(), create_return_stmt()

◆ struct_stmt()

Stmt * struct_stmt ( Parser parser,
const char *  name,
bool  is_public 
)

Parses a structure declaration statement.

Handles struct declarations with public/private member visibility:

struct {
public:
field1: Type1,
method1 = fn() Type { ... }
private:
field2: Type2,
};
Definition ast.h:142
Parameters
parserPointer to the parser instance
nameStruct name (already parsed by caller)
is_publicWhether this struct has public visibility
Returns
Pointer to the struct declaration AST node, or NULL on failure
Note
Members are separated into public and private arrays
Supports both data fields (name: Type) and methods (name = fn ...)
Visibility defaults to public unless explicitly changed
Visibility changes affect all subsequent members until changed again
See also
fn_stmt(), create_field_decl_stmt(), create_struct_decl_stmt()

◆ switch_stmt()

Stmt * switch_stmt ( Parser parser)

◆ use_stmt()

Stmt * use_stmt ( Parser parser)

◆ var_stmt()

Stmt * var_stmt ( Parser parser,
bool  is_public 
)

Parses a variable declaration statement.

Handles variable declarations with the syntax: var name: Type = value;

Parameters
parserPointer to the parser instance
is_publicWhether this variable has public visibility
Returns
Pointer to the variable declaration AST node, or NULL on failure
Note
Variables are mutable by default (unlike constants)
Type annotation is required
Initial value assignment is required
Creates a variable declaration with is_mutable set to true
See also
parse_type(), parse_expr(), create_var_decl_stmt()