Skip to content

Commit 5416a7f

Browse files
committed
feat: Add array, field access, and index expressions to zig.zyn
- Add array literal support: [a, b, c] - Add field access expressions: obj.field - Add index expressions: arr[i] - Restructure primary expressions with postfix layer
1 parent c213d7a commit 5416a7f

1 file changed

Lines changed: 47 additions & 8 deletions

File tree

crates/zyn_peg/grammars/zig.zyn

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,21 +338,39 @@ unary_with_op = { unary_op ~ primary }
338338
]
339339
}
340340

341-
primary = { bool_literal | string_literal | integer_literal | call_expr | identifier_expr | paren_expr }
341+
primary = { postfix_expr }
342342
-> TypedExpression {
343343
"get_child": { "index": 0 }
344344
}
345345

346-
paren_expr = _{ "(" ~ expr ~ ")" }
346+
// Postfix expressions: base followed by call/field/index
347+
// For now, just handle simple cases - no chaining
348+
postfix_expr = { call_expr | field_expr | index_expr | atom }
349+
-> TypedExpression {
350+
"get_child": { "index": 0 }
351+
}
347352

348-
// Function call expression: identifier(args...)
349-
call_expr = { identifier ~ "(" ~ call_args? ~ ")" }
353+
// Function call: atom(args...)
354+
call_expr = { atom ~ "(" ~ call_args? ~ ")" }
350355
-> TypedExpression {
351356
"commands": [
352-
{ "define": "call", "args": {
353-
"callee": "$1",
354-
"args": "$2"
355-
}}
357+
{ "define": "call", "args": { "callee": "$1", "args": "$2" } }
358+
]
359+
}
360+
361+
// Field access: atom.field
362+
field_expr = { atom ~ "." ~ identifier }
363+
-> TypedExpression {
364+
"commands": [
365+
{ "define": "field_access", "args": { "object": "$1", "field": "$2" } }
366+
]
367+
}
368+
369+
// Index: atom[index]
370+
index_expr = { atom ~ "[" ~ expr ~ "]" }
371+
-> TypedExpression {
372+
"commands": [
373+
{ "define": "index", "args": { "object": "$1", "index": "$2" } }
356374
]
357375
}
358376

@@ -361,6 +379,27 @@ call_args = { expr ~ ("," ~ expr)* }
361379
"get_all_children": true
362380
}
363381

382+
// Atom: the base of postfix expressions
383+
atom = { array_literal | bool_literal | string_literal | integer_literal | identifier_expr | paren_expr }
384+
-> TypedExpression {
385+
"get_child": { "index": 0 }
386+
}
387+
388+
paren_expr = _{ "(" ~ expr ~ ")" }
389+
390+
// Array literal: [a, b, c]
391+
array_literal = { "[" ~ array_elements? ~ "]" }
392+
-> TypedExpression {
393+
"commands": [
394+
{ "define": "array_literal", "args": { "elements": "$1" } }
395+
]
396+
}
397+
398+
array_elements = { expr ~ ("," ~ expr)* }
399+
-> List {
400+
"get_all_children": true
401+
}
402+
364403
identifier_expr = { identifier }
365404
-> TypedExpression {
366405
"get_text": true,

0 commit comments

Comments
 (0)