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:
@@ -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,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user