Add function/struct definition parsing and program entry point

- ast.rs: Param, FieldDef, FuncDef, StructDef, TopLevelDef,
  TopLevelDefKind, Program
- parser.rs: parse_program, parse_top_level_def, parse_func_def,
  parse_struct_def with param/field list helpers;
  synchronize_top_level for recovery; 14 new tests (76 total)
- main.rs: parse source file as a Program and print the AST
This commit is contained in:
2026-03-10 18:03:56 +01:00
parent d556a54541
commit 546dc119d0
3 changed files with 393 additions and 35 deletions

View File

@@ -201,3 +201,64 @@ pub enum StmtKind {
/// Error placeholder — emitted during recovery so the parent can continue.
Error,
}
// ── Top-level definitions ──────────────────────────────────────────────────────
/// A function parameter: `[mut] name : type`.
#[derive(Debug, Clone)]
pub struct Param {
pub mutable: bool,
pub name: String,
pub name_span: Span,
pub ty: Type,
}
/// A struct definition field: `name : type`.
///
/// Named `FieldDef` to distinguish from `StructField`, which is a
/// field in a struct *literal expression*.
#[derive(Debug, Clone)]
pub struct FieldDef {
pub name: String,
pub name_span: Span,
pub ty: Type,
}
/// `fn name ( params ) [ -> type ] block`
#[derive(Debug, Clone)]
pub struct FuncDef {
pub name: String,
pub name_span: Span,
pub params: Vec<Param>,
pub ret_ty: Option<Type>,
pub body: Block,
}
/// `struct name { fields }`
#[derive(Debug, Clone)]
pub struct StructDef {
pub name: String,
pub name_span: Span,
pub fields: Vec<FieldDef>,
}
#[derive(Debug, Clone)]
pub struct TopLevelDef {
pub kind: TopLevelDefKind,
pub span: Span,
}
#[derive(Debug, Clone)]
pub enum TopLevelDefKind {
Func(FuncDef),
Struct(StructDef),
/// Error placeholder for recovery.
Error,
}
/// The root of the AST — a sequence of top-level definitions.
#[derive(Debug, Clone)]
pub struct Program {
pub defs: Vec<TopLevelDef>,
pub span: Span,
}