Skip to content

Commit f895873

Browse files
committed
feat: Add comptime parameter support to Zig grammar
- Add `comptime` and `type` to keyword list - Add `type` as a primitive type for metatype expressions - Add `comptime_param` rule for comptime parameters: `comptime T: type` - Add `type_value` expression for passing types as values in calls - Update `fn_params` to support both comptime and regular parameters This enables generic functions with Zig-style comptime syntax: ```zig fn identity(comptime T: type, x: T) T { return x; } ```
1 parent 303837f commit f895873

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

crates/zyn_peg/grammars/zig.zyn

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,25 @@ fn_decl_no_params = { "fn" ~ identifier ~ "(" ~ ")" ~ type_expr ~ block }
120120
]
121121
}
122122

123-
fn_params = { fn_param ~ ("," ~ fn_param)* }
123+
fn_params = { fn_param_any ~ ("," ~ fn_param_any)* }
124124
-> List {
125125
"get_all_children": true
126126
}
127127

128+
// Match either comptime param or regular param
129+
fn_param_any = { comptime_param | fn_param }
130+
-> TypedParameter {
131+
"get_child": { "index": 0 }
132+
}
133+
134+
// Comptime parameter: comptime T: type
135+
comptime_param = { "comptime" ~ identifier ~ ":" ~ type_expr }
136+
-> TypedParameter {
137+
"commands": [
138+
{ "define": "type_param", "args": { "name": "$1", "type": "$2" } }
139+
]
140+
}
141+
128142
// Children (non-silent): identifier, type_expr
129143
fn_param = { identifier ~ ":" ~ type_expr }
130144
-> TypedParameter {
@@ -227,7 +241,7 @@ array_type = { "[" ~ integer_literal? ~ "]" ~ type_expr }
227241
]
228242
}
229243

230-
primitive_type = { "i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" | "f32" | "f64" | "bool" | "void" }
244+
primitive_type = { "i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" | "f32" | "f64" | "bool" | "void" | "type" }
231245
-> Type {
232246
"get_text": true,
233247
"define": "primitive_type",
@@ -481,11 +495,25 @@ call_args = { expr ~ ("," ~ expr)* }
481495
}
482496

483497
// Atom: the base of postfix expressions
484-
atom = { try_expr | struct_init | array_literal | bool_literal | string_literal | integer_literal | identifier_expr | paren_expr }
498+
atom = { try_expr | struct_init | array_literal | bool_literal | string_literal | integer_literal | type_value | identifier_expr | paren_expr }
485499
-> TypedExpression {
486500
"get_child": { "index": 0 }
487501
}
488502

503+
// Type as a value (for comptime type parameters)
504+
type_value = { type_expr_as_value }
505+
-> TypedExpression {
506+
"commands": [
507+
{ "define": "type_value", "args": { "type": "$1" } }
508+
]
509+
}
510+
511+
// Types that can be used as values in expressions (for comptime)
512+
type_expr_as_value = { primitive_type }
513+
-> Type {
514+
"get_child": { "index": 0 }
515+
}
516+
489517
// Struct instantiation: Point{ .x = 10, .y = 20 }
490518
struct_init = { identifier ~ "{" ~ struct_init_fields? ~ "}" }
491519
-> TypedExpression {
@@ -569,6 +597,7 @@ escape_seq = { "\\" ~ ("n" | "r" | "t" | "\\" | "\"" | "0") }
569597
keyword = @{
570598
("struct" | "enum" | "fn" | "const" | "var" | "if" | "else" | "while" | "for" |
571599
"return" | "break" | "continue" | "try" | "and" | "or" | "true" | "false" |
600+
"comptime" | "type" |
572601
"i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" | "f32" | "f64" | "bool" | "void")
573602
~ !(ASCII_ALPHANUMERIC | "_")
574603
}

0 commit comments

Comments
 (0)