diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ast/ast.h | 228 | ||||
| -rw-r--r-- | src/codegen/codegen.h | 8 | ||||
| -rw-r--r-- | src/parser/parser.h | 464 | ||||
| -rw-r--r-- | src/zprep.h | 178 |
4 files changed, 739 insertions, 139 deletions
diff --git a/src/ast/ast.h b/src/ast/ast.h index 12d8f2b..b95c80c 100644 --- a/src/ast/ast.h +++ b/src/ast/ast.h @@ -12,136 +12,148 @@ typedef struct ASTNode ASTNode; // ** Formal Type System ** // Used for Generics, Type Inference, and robust pointer handling. +/** + * @brief Kind of literal value. + */ typedef enum { - LITERAL_INT = 0, - LITERAL_FLOAT = 1, - LITERAL_STRING = 2, - LITERAL_CHAR = 3 + LITERAL_INT = 0, ///< Integer literal. + LITERAL_FLOAT = 1, ///< Floating point literal. + LITERAL_STRING = 2, ///< String literal. + LITERAL_CHAR = 3 ///< Character literal. } LiteralKind; +/** + * @brief Formal type system kinds. + */ typedef enum { - TYPE_VOID, - TYPE_BOOL, - TYPE_CHAR, - TYPE_STRING, - TYPE_U0, - TYPE_I8, - TYPE_U8, - TYPE_I16, - TYPE_U16, - TYPE_I32, - TYPE_U32, - TYPE_I64, - TYPE_U64, - TYPE_I128, - TYPE_U128, - TYPE_F32, - TYPE_F64, - TYPE_INT, - TYPE_FLOAT, - TYPE_USIZE, - TYPE_ISIZE, - TYPE_BYTE, - TYPE_RUNE, - TYPE_UINT, - TYPE_STRUCT, - TYPE_ENUM, - TYPE_POINTER, - TYPE_ARRAY, - TYPE_FUNCTION, - TYPE_GENERIC, - TYPE_UNKNOWN + TYPE_VOID, ///< `void` type. + TYPE_BOOL, ///< `bool` type. + TYPE_CHAR, ///< `char` type. + TYPE_STRING, ///< `string` type. + TYPE_U0, ///< `u0` type. + TYPE_I8, ///< `i8` type. + TYPE_U8, ///< `u8` type. + TYPE_I16, ///< `i16` type. + TYPE_U16, ///< `u16` type. + TYPE_I32, ///< `i32` type. + TYPE_U32, ///< `u32` type. + TYPE_I64, ///< `i64` type. + TYPE_U64, ///< `u64` type. + TYPE_I128, ///< `i128` type. + TYPE_U128, ///< `u128` type. + TYPE_F32, ///< `f32` type. + TYPE_F64, ///< `f64` type. + TYPE_INT, ///< `int` (alias, usually i32). + TYPE_FLOAT, ///< `float` (alias). + TYPE_USIZE, ///< `usize` (pointer size unsigned). + TYPE_ISIZE, ///< `isize` (pointer size signed). + TYPE_BYTE, ///< `byte`. + TYPE_RUNE, ///< `rune`. + TYPE_UINT, ///< `uint` (alias). + TYPE_STRUCT, ///< Struct type. + TYPE_ENUM, ///< Enum type. + TYPE_POINTER, ///< Pointer type (*). + TYPE_ARRAY, ///< Fixed size array [N]. + TYPE_FUNCTION, ///< Function pointer or reference. + TYPE_GENERIC, ///< Generic type parameter (T). + TYPE_UNKNOWN ///< Unknown/unresolved type. } TypeKind; +/** + * @brief Represents a formal type in the type system. + */ typedef struct Type { - TypeKind kind; - char *name; // For STRUCT, GENERIC, ENUM. - struct Type *inner; // For POINTER, ARRAY. - struct Type **args; // For GENERIC args. - int arg_count; - int is_const; - int is_explicit_struct; // for example, "struct Foo" vs "Foo" + TypeKind kind; ///< The kind of type. + char *name; ///< Name of the type (for STRUCT, GENERIC, ENUM). + struct Type *inner; ///< Inner type (for POINTER, ARRAY). + struct Type **args; ///< Generic arguments (for GENERIC instantiations). + int arg_count; ///< Count of generic arguments. + int is_const; ///< 1 if const-qualified. + int is_explicit_struct; ///< 1 if defined with "struct" keyword explicitly. int is_raw; // Raw function pointer (fn*) union { - int array_size; // For fixed-size arrays [T; N]. - int is_varargs; // For function types (...). - int is_restrict; // For restrict pointers. + int array_size; ///< Size for fixed-size arrays. + int is_varargs; ///< 1 if function type is variadic. + int is_restrict; ///< 1 if pointer is restrict-qualified. struct { - int has_drop; // For RAII: does this type implement Drop? - int has_iterable; // For the for iterator: does the type implement Iterable? + int has_drop; ///< 1 if type implements Drop trait (RAII). + int has_iterable; ///< 1 if type implements Iterable trait. } traits; }; } Type; // ** AST Node Types ** +/** + * @brief AST Node Types. + */ typedef enum { - NODE_ROOT, - NODE_FUNCTION, - NODE_BLOCK, - NODE_RETURN, - NODE_VAR_DECL, - NODE_CONST, - NODE_TYPE_ALIAS, - NODE_IF, - NODE_WHILE, - NODE_FOR, - NODE_FOR_RANGE, - NODE_LOOP, - NODE_REPEAT, - NODE_UNLESS, - NODE_GUARD, - NODE_BREAK, - NODE_CONTINUE, - NODE_MATCH, - NODE_MATCH_CASE, - NODE_EXPR_BINARY, - NODE_EXPR_UNARY, - NODE_EXPR_LITERAL, - NODE_EXPR_VAR, - NODE_EXPR_CALL, - NODE_EXPR_MEMBER, - NODE_EXPR_INDEX, - NODE_EXPR_CAST, - NODE_EXPR_SIZEOF, - NODE_EXPR_STRUCT_INIT, - NODE_EXPR_ARRAY_LITERAL, - NODE_EXPR_SLICE, - NODE_STRUCT, - NODE_FIELD, - NODE_ENUM, - NODE_ENUM_VARIANT, - NODE_TRAIT, - NODE_IMPL, - NODE_IMPL_TRAIT, - NODE_INCLUDE, - NODE_RAW_STMT, - NODE_TEST, - NODE_ASSERT, - NODE_DEFER, - NODE_DESTRUCT_VAR, - NODE_TERNARY, - NODE_ASM, - NODE_LAMBDA, - NODE_PLUGIN, - NODE_GOTO, - NODE_LABEL, - NODE_DO_WHILE, - NODE_TYPEOF, - NODE_TRY, - NODE_REFLECTION, - NODE_AWAIT, - NODE_REPL_PRINT, - NODE_CUDA_LAUNCH, - NODE_VA_START, - NODE_VA_END, - NODE_VA_COPY, - NODE_VA_ARG + NODE_ROOT, ///< Root of the AST. + NODE_FUNCTION, ///< Function definition. + NODE_BLOCK, ///< Code block { ... }. + NODE_RETURN, ///< Return statement. + NODE_VAR_DECL, ///< Variable declaration. + NODE_CONST, ///< Constant definition. + NODE_TYPE_ALIAS, ///< Type alias (typedef). + NODE_IF, ///< If statement. + NODE_WHILE, ///< While loop. + NODE_FOR, ///< For loop. + NODE_FOR_RANGE, ///< For-range loop (iterator). + NODE_LOOP, ///< Infinite loop. + NODE_REPEAT, ///< Repeat loop (n times). + NODE_UNLESS, ///< Unless statement (if !cond). + NODE_GUARD, ///< Guard clause (if !cond return). + NODE_BREAK, ///< Break statement. + NODE_CONTINUE, ///< Continue statement. + NODE_MATCH, ///< Match statement. + NODE_MATCH_CASE, ///< Case within match. + NODE_EXPR_BINARY, ///< Binary expression (a + b). + NODE_EXPR_UNARY, ///< Unary expression (!a). + NODE_EXPR_LITERAL, ///< Literal value. + NODE_EXPR_VAR, ///< Variable reference. + NODE_EXPR_CALL, ///< Function call. + NODE_EXPR_MEMBER, ///< Member access (a.b). + NODE_EXPR_INDEX, ///< Array index (a[b]). + NODE_EXPR_CAST, ///< Type cast. + NODE_EXPR_SIZEOF, ///< Sizeof expression. + NODE_EXPR_STRUCT_INIT, ///< Struct initializer. + NODE_EXPR_ARRAY_LITERAL,///< Array literal. + NODE_EXPR_SLICE, ///< Slice operation. + NODE_STRUCT, ///< Struct definition. + NODE_FIELD, ///< Struct field. + NODE_ENUM, ///< Enum definition. + NODE_ENUM_VARIANT, ///< Enum variant. + NODE_TRAIT, ///< Trait definition. + NODE_IMPL, ///< Impl block. + NODE_IMPL_TRAIT, ///< Trait implementation. + NODE_INCLUDE, ///< Include directive. + NODE_RAW_STMT, ///< Raw statement (transpiler bypass). + NODE_TEST, ///< Test block. + NODE_ASSERT, ///< Assert statement. + NODE_DEFER, ///< Defer statement. + NODE_DESTRUCT_VAR, ///< Destructuring declaration. + NODE_TERNARY, ///< Ternary expression (?:). + NODE_ASM, ///< Assembly block. + NODE_LAMBDA, ///< Lambda function. + NODE_PLUGIN, ///< Plugin invocation. + NODE_GOTO, ///< Goto statement. + NODE_LABEL, ///< Label. + NODE_DO_WHILE, ///< Do-while loop. + NODE_TYPEOF, ///< Typeof operator. + NODE_TRY, ///< Try statement (error handling). + NODE_REFLECTION, ///< Reflection info. + NODE_AWAIT, ///< Await expression. + NODE_REPL_PRINT, ///< Implicit print (REPL). + NODE_CUDA_LAUNCH, ///< CUDA kernel launch (<<<...>>>). + NODE_VA_START, ///< va_start intrinsic. + NODE_VA_END, ///< va_end intrinsic. + NODE_VA_COPY, ///< va_copy intrinsic. + NODE_VA_ARG ///< va_arg intrinsic. } NodeType; // ** AST Node Structure ** diff --git a/src/codegen/codegen.h b/src/codegen/codegen.h index ec77817..9d32110 100644 --- a/src/codegen/codegen.h +++ b/src/codegen/codegen.h @@ -50,11 +50,13 @@ void emit_auto_type(ParserContext *ctx, ASTNode *init_expr, Token t, FILE *out); char *codegen_type_to_string(Type *t); void emit_func_signature(FILE *out, ASTNode *func, const char *name_override); char *strip_template_suffix(const char *name); -// duplicate decl removed: char *strip_template_suffix(const char *name); int emit_move_invalidation(ParserContext *ctx, ASTNode *node, FILE *out); void codegen_expression_with_move(ParserContext *ctx, ASTNode *node, FILE *out); // Declaration emission (codegen_decl.c). +/** + * @brief Emits the standard preamble (includes, macros) to the output file. + */ void emit_preamble(ParserContext *ctx, FILE *out); void emit_includes_and_aliases(ASTNode *node, FILE *out); void emit_type_aliases(ASTNode *node, FILE *out); @@ -66,6 +68,10 @@ void emit_globals(ParserContext *ctx, ASTNode *node, FILE *out); void emit_lambda_defs(ParserContext *ctx, FILE *out); void emit_protos(ASTNode *node, FILE *out); void emit_impl_vtables(ParserContext *ctx, FILE *out); + +/** + * @brief Emits test runner and test cases if testing is enabled. + */ int emit_tests_and_runner(ParserContext *ctx, ASTNode *node, FILE *out); void print_type_defs(ParserContext *ctx, FILE *out, ASTNode *nodes); diff --git a/src/parser/parser.h b/src/parser/parser.h index 807f37d..4dd5cf5 100644 --- a/src/parser/parser.h +++ b/src/parser/parser.h @@ -6,20 +6,24 @@ #include "zprep.h" // Operator precedence for expression parsing + +/** + * @brief Operator precedence for expression parsing. + */ typedef enum { - PREC_NONE, - PREC_ASSIGNMENT, - PREC_TERNARY, - PREC_OR, - PREC_AND, - PREC_EQUALITY, - PREC_COMPARISON, - PREC_TERM, - PREC_FACTOR, - PREC_UNARY, - PREC_CALL, - PREC_PRIMARY + PREC_NONE, ///< No precedence. + PREC_ASSIGNMENT,///< Assignment operators. + PREC_TERNARY, ///< Ternary operator. + PREC_OR, ///< Logical OR. + PREC_AND, ///< Logical AND. + PREC_EQUALITY, ///< Equality operators. + PREC_COMPARISON,///< Comparison operators. + PREC_TERM, ///< Addition and subtraction. + PREC_FACTOR, ///< Multiplication and division. + PREC_UNARY, ///< Unary operators. + PREC_CALL, ///< Function calls. + PREC_PRIMARY ///< Primary expressions. } Precedence; // Main entry points @@ -27,6 +31,9 @@ typedef enum struct ParserContext; typedef struct ParserContext ParserContext; +/** + * @brief Parses a program. + */ ASTNode *parse_program(ParserContext *ctx, Lexer *l); extern ParserContext *g_parser_ctx; @@ -347,44 +354,136 @@ typedef struct TypeUsage } TypeUsage; // Type validation prototypes + +/** + * @brief Registers a type usage. + */ void register_type_usage(ParserContext *ctx, const char *name, Token t); + +/** + * @brief Validates types. + */ int validate_types(ParserContext *ctx); // Token helpers + +/** + * @brief Duplicates a token. + */ char *token_strdup(Token t); + +/** + * @brief Checks if a token matches a string. + */ int is_token(Token t, const char *s); + +/** + * @brief Expects a token of a specific type. + */ Token expect(Lexer *l, TokenType type, const char *msg); + +/** + * @brief Skips comments in the lexer. + */ void skip_comments(Lexer *l); + +/** + * @brief Consumes tokens until a semicolon is found. + */ char *consume_until_semicolon(Lexer *l); + +/** + * @brief Consumes and rewrites tokens. + */ char *consume_and_rewrite(ParserContext *ctx, Lexer *l); // C reserved word warnings + +/** + * @brief Checks if a name is a C reserved word. + */ int is_c_reserved_word(const char *name); + +/** + * @brief Warns about a C reserved word. + */ void warn_c_reserved_word(Token t, const char *name); // ZenSymbol table + +/** + * @brief Enters a new scope (pushes to scope stack). + */ void enter_scope(ParserContext *ctx); + +/** + * @brief Exits the current scope (pops from scope stack). + */ void exit_scope(ParserContext *ctx); + +/** + * @brief Adds a symbol to the current scope. + */ void add_symbol(ParserContext *ctx, const char *n, const char *t, Type *type_info); + +/** + * @brief Adds a symbol with definition token location. + */ void add_symbol_with_token(ParserContext *ctx, const char *n, const char *t, Type *type_info, Token tok); + +/** + * @brief Finds a symbol's type information. + */ Type *find_symbol_type_info(ParserContext *ctx, const char *n); + +/** + * @brief Finds a symbol's type. + */ char *find_symbol_type(ParserContext *ctx, const char *n); + +/** + * @brief Finds a symbol's entry. + */ ZenSymbol *find_symbol_entry(ParserContext *ctx, const char *n); + +/** + * @brief Finds a symbol in all scopes. + */ ZenSymbol *find_symbol_in_all(ParserContext *ctx, const char *n); char *find_similar_symbol(ParserContext *ctx, const char *name); // Function registry + +/** + * @brief Registers a function. + */ void register_func(ParserContext *ctx, const char *name, int count, char **defaults, Type **arg_types, Type *ret_type, int is_varargs, int is_async, Token decl_token); + +/** + * @brief Registers a function template. + */ void register_func_template(ParserContext *ctx, const char *name, const char *param, ASTNode *node); + +/** + * @brief Finds a function template. + */ GenericFuncTemplate *find_func_template(ParserContext *ctx, const char *name); // Generic/template helpers +/** + * @brief Registers a known generic type parameter. + */ void register_generic(ParserContext *ctx, char *name); + +/** + * @brief Checks if a name is a known generic parameter. + */ int is_known_generic(ParserContext *ctx, char *name); + void register_impl_template(ParserContext *ctx, const char *sname, const char *param, ASTNode *node); void add_to_struct_list(ParserContext *ctx, ASTNode *node); @@ -394,135 +493,474 @@ void add_to_impl_list(ParserContext *ctx, ASTNode *node); void add_to_global_list(ParserContext *ctx, ASTNode *node); void register_builtins(ParserContext *ctx); void add_instantiated_func(ParserContext *ctx, ASTNode *fn); + +/** + * @brief Instantiates a generic struct/function. + */ void instantiate_generic(ParserContext *ctx, const char *name, const char *concrete_type, const char *unmangled_type, Token t); + +/** + * @brief Instantiates a multi-parameter generic. + */ void instantiate_generic_multi(ParserContext *ctx, const char *name, char **args, int arg_count, Token t); + +/** + * @brief Sanitizes a mangled name for use in codegen. + */ char *sanitize_mangled_name(const char *name); + +/** + * @brief Registers a type alias. + */ void register_type_alias(ParserContext *ctx, const char *alias, const char *original); + +/** + * @brief Finds a type alias. + */ const char *find_type_alias(ParserContext *ctx, const char *alias); + +/** + * @brief Registers an implementation. + */ void register_impl(ParserContext *ctx, const char *trait, const char *strct); + +/** + * @brief Checks if a type implements a trait. + */ int check_impl(ParserContext *ctx, const char *trait, const char *strct); + +/** + * @brief Registers a template. + */ void register_template(ParserContext *ctx, const char *name, ASTNode *node); + +/** + * @brief Registers a deprecated function. + */ void register_deprecated_func(ParserContext *ctx, const char *name, const char *reason); + +/** + * @brief Finds a deprecated function. + */ DeprecatedFunc *find_deprecated_func(ParserContext *ctx, const char *name); + +/** + * @brief Parses a single parameter arrow lambda. + */ ASTNode *parse_arrow_lambda_single(ParserContext *ctx, Lexer *l, char *param_name); + +/** + * @brief Parses a multi-parameter arrow lambda. + */ ASTNode *parse_arrow_lambda_multi(ParserContext *ctx, Lexer *l, char **param_names, int num_params); // Utils + +/** + * @brief Parses and converts arguments. + */ char *parse_and_convert_args(ParserContext *ctx, Lexer *l, char ***defaults_out, int *count_out, Type ***types_out, char ***names_out, int *is_varargs_out); + +/** + * @brief Checks if a file has been imported. + */ int is_file_imported(ParserContext *ctx, const char *path); + +/** + * @brief Marks a file as imported. + */ void mark_file_imported(ParserContext *ctx, const char *path); + +/** + * @brief Registers a plugin. + */ void register_plugin(ParserContext *ctx, const char *name, const char *alias); + +/** + * @brief Resolves a plugin by name or alias. + */ const char *resolve_plugin(ParserContext *ctx, const char *name_or_alias); + +/** + * @brief Prints type definitions to a file. + */ void print_type_defs(ParserContext *ctx, FILE *out, ASTNode *nodes); // String manipulation + +/** + * @brief Replaces a substring in a string. + */ char *replace_in_string(const char *src, const char *old_w, const char *new_w); + +/** + * @brief Replaces a type string in a string. + */ char *replace_type_str(const char *src, const char *param, const char *concrete, const char *old_struct, const char *new_struct); + +/** + * @brief Replaces a type formal in a type. + */ Type *replace_type_formal(Type *t, const char *p, const char *c, const char *os, const char *ns); + +/** + * @brief Copies an AST node and replaces its type parameters. + */ ASTNode *copy_ast_replacing(ASTNode *n, const char *p, const char *c, const char *os, const char *ns); char *extract_module_name(const char *path); // Enum helpers +/** + * @brief Registers an enum variant. + */ void register_enum_variant(ParserContext *ctx, const char *ename, const char *vname, int tag); + +/** + * @brief Finds an enum variant. + */ EnumVariantReg *find_enum_variant(ParserContext *ctx, const char *vname); // Lambda helpers +/** + * @brief Registers a lambda. + */ void register_lambda(ParserContext *ctx, ASTNode *node); + +/** + * @brief Analyzes lambda captures. + */ void analyze_lambda_captures(ParserContext *ctx, ASTNode *lambda); // Type registration +/** + * @brief Registers a slice type. + */ void register_slice(ParserContext *ctx, const char *type); + +/** + * @brief Registers a tuple type. + */ void register_tuple(ParserContext *ctx, const char *sig); // Struct lookup +/** + * @brief Finds a struct definition. + */ ASTNode *find_struct_def(ParserContext *ctx, const char *name); + +/** + * @brief Registers a struct definition. + */ void register_struct_def(ParserContext *ctx, const char *name, ASTNode *node); // Module system +/** + * @brief Finds a module. + */ Module *find_module(ParserContext *ctx, const char *alias); + +/** + * @brief Registers a module. + */ void register_module(ParserContext *ctx, const char *alias, const char *path); + +/** + * @brief Registers a selective import. + */ void register_selective_import(ParserContext *ctx, const char *symbol, const char *alias, const char *source_module); + +/** + * @brief Finds a selective import. + */ SelectiveImport *find_selective_import(ParserContext *ctx, const char *name); // Type Aliases +/** + * @brief Registers a type alias. + */ void register_type_alias(ParserContext *ctx, const char *alias, const char *original); + +/** + * @brief Finds a type alias. + */ const char *find_type_alias(ParserContext *ctx, const char *alias); // External symbol tracking (C interop) +/** + * @brief Registers an external symbol. + */ void register_extern_symbol(ParserContext *ctx, const char *name); + +/** + * @brief Checks if a symbol is external. + */ int is_extern_symbol(ParserContext *ctx, const char *name); + +/** + * @brief Checks if a symbol should suppress an undefined warning. + */ int should_suppress_undef_warning(ParserContext *ctx, const char *name); // Initialization +/** + * @brief Initializes built-in types and symbols. + */ void init_builtins(); // Expression rewriting +/** + * @brief Rewrites expression methods. + */ char *rewrite_expr_methods(ParserContext *ctx, char *raw); + +/** + * @brief Processes a formatted string. + */ char *process_fstring(ParserContext *ctx, const char *content, char ***used_syms, int *count); + +/** + * @brief Instantiates a function template. + */ char *instantiate_function_template(ParserContext *ctx, const char *name, const char *concrete_type, const char *unmangled_type); + +/** + * @brief Finds a function. + */ FuncSig *find_func(ParserContext *ctx, const char *name); +/** + * @brief Parses a type formal. + */ Type *parse_type_formal(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a type. + */ char *parse_type(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a type base. + */ Type *parse_type_base(ParserContext *ctx, Lexer *l); +/** + * @brief Parses an expression. + */ ASTNode *parse_expression(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses an expression with minimum precedence. + */ ASTNode *parse_expr_prec(ParserContext *ctx, Lexer *l, Precedence min_prec); + +/** + * @brief Parses a primary expression (literal, variable, grouping). + */ ASTNode *parse_primary(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a lambda. + */ ASTNode *parse_lambda(ParserContext *ctx, Lexer *l); -// parse_arrow_lambda_single/multi already declared above + +/** + * @brief Parses a condition. + */ char *parse_condition_raw(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses an array literal. + */ char *parse_array_literal(ParserContext *ctx, Lexer *l, const char *st); + +/** + * @brief Parses a tuple literal. + */ char *parse_tuple_literal(ParserContext *ctx, Lexer *l, const char *tn); + +/** + * @brief Parses an embed. + */ ASTNode *parse_embed(ParserContext *ctx, Lexer *l); +/** + * @brief Parses a macro call. + */ ASTNode *parse_macro_call(ParserContext *ctx, Lexer *l, char *name); + +/** + * @brief Parses a statement. + */ ASTNode *parse_statement(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a block of statements { ... }. + */ ASTNode *parse_block(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses an if statement. + */ ASTNode *parse_if(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a while loop. + */ ASTNode *parse_while(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a for loop. + */ ASTNode *parse_for(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a loop. + */ ASTNode *parse_loop(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a repeat loop. + */ ASTNode *parse_repeat(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses an unless statement. + */ ASTNode *parse_unless(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a guard statement. + */ ASTNode *parse_guard(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a match statement. + */ ASTNode *parse_match(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a return statement. + */ ASTNode *parse_return(ParserContext *ctx, Lexer *l); +/** + * @brief Processes a formatted string. + */ char *process_printf_sugar(ParserContext *ctx, const char *content, int newline, const char *target, char ***used_syms, int *count, int check_symbols); + +/** + * @brief Parses an assert statement. + */ ASTNode *parse_assert(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a defer statement. + */ ASTNode *parse_defer(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses an asm statement. + */ ASTNode *parse_asm(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a plugin statement. + */ ASTNode *parse_plugin(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a variable declaration. + */ ASTNode *parse_var_decl(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a def statement. + */ ASTNode *parse_def(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a type alias. + */ ASTNode *parse_type_alias(ParserContext *ctx, Lexer *l); +/** + * @brief Parses a function definition. + */ ASTNode *parse_function(ParserContext *ctx, Lexer *l, int is_async); + +/** + * @brief Parses a struct definition. + */ ASTNode *parse_struct(ParserContext *ctx, Lexer *l, int is_union); + +/** + * @brief Parses an enum definition. + */ ASTNode *parse_enum(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a trait definition. + */ ASTNode *parse_trait(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses an implementation. + */ ASTNode *parse_impl(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses an implementation trait. + */ ASTNode *parse_impl_trait(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a test definition. + */ ASTNode *parse_test(ParserContext *ctx, Lexer *l); // Move semantics helpers + +/** + * @brief Checks if a type is copyable. + */ int is_type_copy(ParserContext *ctx, Type *t); + +/** + * @brief Checks if a type is copyable. + */ void check_move_usage(ParserContext *ctx, ASTNode *node, Token t); + +/** + * @brief Parses an include statement. + */ ASTNode *parse_include(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses an import statement. + */ ASTNode *parse_import(ParserContext *ctx, Lexer *l); + +/** + * @brief Parses a comptime statement. + */ ASTNode *parse_comptime(ParserContext *ctx, Lexer *l); +/** + * @brief Patches self arguments in a function. + */ char *patch_self_args(const char *args, const char *struct_name); +/** + * @brief Main loop to parse top-level nodes in a file. + */ ASTNode *parse_program_nodes(ParserContext *ctx, Lexer *l); #endif // PARSER_H diff --git a/src/zprep.h b/src/zprep.h index 0fcd76f..9f82706 100644 --- a/src/zprep.h +++ b/src/zprep.h @@ -11,7 +11,7 @@ #ifdef __COSMOPOLITAN__ #include <cosmo.h> -#define z_is_windows() IsWindows() +#define z_is_windows() IsWindows() ///< Check if running on Windows. #else #ifdef _WIN32 #define z_is_windows() 1 @@ -25,31 +25,31 @@ #ifndef PATH_MAX #define PATH_MAX 260 #endif -#define realpath(N, R) _fullpath((R), (N), PATH_MAX) +#define realpath(N, R) _fullpath((R), (N), PATH_MAX) ///< Get absolute path. #endif // **ZEN VERSION** #ifndef ZEN_VERSION -#define ZEN_VERSION "0.1.0" +#define ZEN_VERSION "0.1.0" ///< Zen-C version. #endif // ** ANSI COLORS ** -#define COLOR_RESET "\033[0m" -#define COLOR_RED "\033[1;31m" -#define COLOR_GREEN "\033[1;32m" -#define COLOR_YELLOW "\033[1;33m" -#define COLOR_BLUE "\033[1;34m" -#define COLOR_CYAN "\033[1;36m" -#define COLOR_BOLD "\033[1m" +#define COLOR_RESET "\033[0m" ///< Reset color. +#define COLOR_RED "\033[1;31m" ///< Red color. +#define COLOR_GREEN "\033[1;32m" ///< Green color. +#define COLOR_YELLOW "\033[1;33m" ///< Yellow color. +#define COLOR_BLUE "\033[1;34m" ///< Blue color. +#define COLOR_CYAN "\033[1;36m" ///< Cyan color. +#define COLOR_BOLD "\033[1m" ///< Bold text. // ** MEMORY OVERRIDES (Arena) ** -#define free(ptr) ((void)0) -#define malloc(sz) xmalloc(sz) -#define realloc(p, s) xrealloc(p, s) -#define calloc(n, s) xcalloc(n, s) +#define free(ptr) ((void)0) ///< Free memory. +#define malloc(sz) xmalloc(sz) ///< Allocate memory. +#define realloc(p, s) xrealloc(p, s) ///< Reallocate memory. +#define calloc(n, s) xcalloc(n, s) ///< Allocate and zero memory. // ** GLOBAL STATE ** -extern char *g_current_filename; +extern char *g_current_filename; ///< Current filename. /** * @brief Token types for the Lexer. @@ -134,24 +134,69 @@ typedef struct int col; ///< Current column number. } Lexer; +/** + * @brief Initialize the lexer. + */ void lexer_init(Lexer *l, const char *src); + +/** + * @brief Get the next token. + */ Token lexer_next(Lexer *l); + +/** + * @brief Get the next token without advancing. + */ Token lexer_peek(Lexer *l); + +/** + * @brief Get the next token without advancing (2 look ahead). + */ Token lexer_peek2(Lexer *l); +/** + * @brief Register a trait. + */ void register_trait(const char *name); + +/** + * @brief Check if a name is a trait. + */ int is_trait(const char *name); -// Arena and memory. +/** + * @brief Allocate memory. + */ void *xmalloc(size_t size); + +/** + * @brief Reallocate memory. + */ void *xrealloc(void *ptr, size_t new_size); + +/** + * @brief Allocate and zero memory. + */ void *xcalloc(size_t n, size_t size); + +/** + * @brief Duplicate a string. + */ char *xstrdup(const char *s); -// Error reporting. +/** + * @brief Error reporting. + */ void zpanic(const char *fmt, ...); + +/** + * @brief Error reporting with token location. + */ void zpanic_at(Token t, const char *fmt, ...); +/** + * @brief Load a file. + */ char *load_file(const char *filename); // ** Buffer Size Constants ** @@ -166,36 +211,131 @@ extern int g_warning_count; struct ParserContext; +/** + * @brief Scan build directives. + */ void scan_build_directives(struct ParserContext *ctx, const char *src); + +/** + * @brief Calculate Levenshtein distance. + */ int levenshtein(const char *s1, const char *s2); + +/** + * @brief Error reporting with suggestion. + */ void zpanic_with_suggestion(Token t, const char *msg, const char *suggestion); // Specific error types. + +/** + * @brief Error reporting for undefined function. + */ void error_undefined_function(Token t, const char *func_name, const char *suggestion); + +/** + * @brief Error reporting for wrong argument count. + */ void error_wrong_arg_count(Token t, const char *func_name, int expected, int got); + +/** + * @brief Error reporting for undefined field. + */ void error_undefined_field(Token t, const char *struct_name, const char *field_name, const char *suggestion); + +/** + * @brief Error reporting for type expected. + */ void error_type_expected(Token t, const char *expected, const char *got); + +/** + * @brief Error reporting for cannot index. + */ void error_cannot_index(Token t, const char *type_name); // Warning system. + +/** + * @brief Warning reporting. + */ void zwarn(const char *fmt, ...); + +/** + * @brief Warning reporting with token location. + */ void zwarn_at(Token t, const char *fmt, ...); // Specific warnings. + +/** + * @brief Warning reporting for unused variable. + */ void warn_unused_variable(Token t, const char *var_name); + +/** + * @brief Warning reporting for unused parameter. + */ void warn_unused_parameter(Token t, const char *param_name, const char *func_name); + +/** + * @brief Warning reporting for shadowing. + */ void warn_shadowing(Token t, const char *var_name); + +/** + * @brief Warning reporting for unreachable code. + */ void warn_unreachable_code(Token t); + +/** + * @brief Warning reporting for implicit conversion. + */ void warn_implicit_conversion(Token t, const char *from_type, const char *to_type); + +/** + * @brief Warning reporting for narrowing conversion. + */ void warn_narrowing_conversion(Token t, const char *from_type, const char *to_type); + +/** + * @brief Warning reporting for missing return. + */ void warn_missing_return(Token t, const char *func_name); + +/** + * @brief Warning reporting for comparison always true. + */ void warn_comparison_always_true(Token t, const char *reason); + +/** + * @brief Warning reporting for comparison always false. + */ void warn_comparison_always_false(Token t, const char *reason); + +/** + * @brief Warning reporting for division by zero. + */ void warn_division_by_zero(Token t); + +/** + * @brief Warning reporting for integer overflow. + */ void warn_integer_overflow(Token t, const char *type_name, long long value); + +/** + * @brief Warning reporting for array bounds. + */ void warn_array_bounds(Token t, int index, int size); + +/** + * @brief Warning reporting for format string. + */ void warn_format_string(Token t, int arg_num, const char *expected, const char *got); + +/** + * @brief Warning reporting for null pointer. + */ void warn_null_pointer(Token t, const char *expr); /** @@ -231,6 +371,10 @@ extern char g_link_flags[]; extern char g_cflags[]; struct ParserContext; + +/** + * @brief Scan build directives. + */ void scan_build_directives(struct ParserContext *ctx, const char *src); #endif |
