Add comprehensive Zig syntax test coverage

- Add support for error sets and error union types
- Implement for loops with index and payload syntax
- Add defer, break, continue, and switch statements
- Support unary expressions and array indexing
- Add unreachable expression and test declarations
- Extend AST with new type expressions (array, error union)
- Update formatter to handle all new syntax elements
- Fix formatting for switch prongs, payloads, and blocks
This commit is contained in:
2025-06-05 20:44:49 -05:00
parent 50b38254ab
commit 258b3c8e9b
4 changed files with 529 additions and 7 deletions

View File

@@ -73,6 +73,11 @@ func If(cond Expr, then, els Stmt) *IfStmt {
return &IfStmt{Cond: cond, Then: then, Else: els}
}
// IfWithPayload creates an if statement with a payload.
func IfWithPayload(cond Expr, payload *Payload, then, els Stmt) *IfStmt {
return &IfStmt{Cond: cond, Payload: payload, Then: then, Else: els}
}
// NewBlock creates a block of statements.
func NewBlock(stmts ...Stmt) *Block {
return &Block{Stmts: stmts}
@@ -134,3 +139,93 @@ func OptionalType(base TypeExpr) *PrefixTypeExpr {
func PointerType(base TypeExpr) *PrefixTypeExpr {
return &PrefixTypeExpr{Op: "*", Base: base}
}
// ArrayType creates an array type ([N]T).
func ArrayType(size Expr, elem TypeExpr) *ArrayTypeExpr {
return &ArrayTypeExpr{Size: size, Elem: elem}
}
// ErrorSet creates an error set declaration.
func ErrorSet(names ...string) *ErrorSetDecl {
return &ErrorSetDecl{Names: names}
}
// ErrorUnionType creates an error union type (E!T).
func ErrorUnionType(errSet, typ TypeExpr) *ErrorUnionTypeExpr {
return &ErrorUnionTypeExpr{ErrSet: errSet, Type: typ}
}
// DeferStmt creates a defer statement.
func Defer(stmt Stmt) *DeferStmt {
return &DeferStmt{Stmt: stmt}
}
// ForLoop creates a for loop statement.
func ForLoop(args []ForArg, payload *Payload, body Stmt, els Stmt) *LoopStmt {
return &LoopStmt{
Kind: "for",
Prefix: &ForPrefix{Args: args, Payload: payload},
Body: body,
Else: els,
}
}
// ForArg creates a for loop argument.
func ForArgExpr(expr Expr) ForArg {
return ForArg{Expr: expr}
}
// Payload creates a payload for control flow.
func PayloadNames(names []string, pointers []bool) *Payload {
return &Payload{Names: names, Pointers: pointers}
}
// SwitchStmt creates a switch statement.
func Switch(cond Expr, prongs ...*SwitchProng) *SwitchStmt {
return &SwitchStmt{Cond: cond, Prongs: prongs}
}
// SwitchProng creates a switch prong.
func Prong(cases []*SwitchCase, payload *Payload, expr Expr) *SwitchProng {
return &SwitchProng{Cases: cases, Payload: payload, Expr: expr}
}
// SwitchCase creates a switch case.
func Case(expr Expr, to Expr) *SwitchCase {
return &SwitchCase{Expr: expr, To: to}
}
// ElseCase creates an else case for a switch.
func ElseCase() *SwitchCase {
return &SwitchCase{IsElse: true}
}
// BreakStmt creates a break statement.
func Break(label string, value Expr) *BreakStmt {
return &BreakStmt{Label: label, Value: value}
}
// ContinueStmt creates a continue statement.
func Continue(label string) *ContinueStmt {
return &ContinueStmt{Label: label}
}
// UnaryExpr creates a unary expression.
func Unary(op string, expr Expr) *UnaryExpr {
return &UnaryExpr{Op: op, Expr: expr}
}
// IndexExpr creates an index expression.
func Index(receiver, index Expr) *IndexExpr {
return &IndexExpr{Receiver: receiver, Index: index}
}
// UnreachableExpr creates an unreachable expression.
func Unreachable() *UnreachableExpr {
return &UnreachableExpr{}
}
// TestDecl creates a test declaration.
func Test(name string, block *Block) *TestDecl {
return &TestDecl{Name: name, Block: block}
}