Skip to content

Commit 9410cb2

Browse files
committed
feat: add brace-style syntax for structs and abstracts in ZynML grammar
- Add brace-style struct definitions: struct Point { x: float, y: float } - Add brace-style abstract definitions with suffix support - Add struct_field_list and struct_field rules for comma-separated fields - Add expression-bodied method syntax for impl blocks (fn x(): expr) - Update stdlib (prelude, collections, tensor) to use brace-style structs - Update abstract_types example to use brace-style syntax - Expand e2e tests with syntax_styles module for new grammar features - All 123 e2e tests passing
1 parent fc4f304 commit 9410cb2

6 files changed

Lines changed: 450 additions & 84 deletions

File tree

crates/zynml/examples/abstract_types.zynml

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,31 @@ import prelude
55

66
// Abstract Duration type with suffix literals
77
// Wraps i64 but provides type-safe time operations
8-
abstract Duration(i64) with Suffixes("ms, s, m"):
9-
value: i64
8+
// Brace-style field definition (recommended for consistency)
9+
abstract Duration(i64) with Suffixes("ms, s, m") { value: i64 }
1010

1111
// Inherent methods for Duration
1212
impl Duration(i64) {
13-
// Constructor: create from milliseconds
14-
fn from_ms(v: i64) -> Duration:
15-
Duration { value: v }
13+
// Constructor: create from milliseconds (expression-bodied)
14+
fn from_ms(v: i64) -> Duration: Duration { value: v }
1615

1716
// Constructor: create from seconds
18-
fn from_s(v: i64) -> Duration:
19-
Duration { value: v * 1000 }
17+
fn from_s(v: i64) -> Duration: Duration { value: v * 1000 }
2018

2119
// Constructor: create from minutes
22-
fn from_m(v: i64) -> Duration:
23-
Duration { value: v * 60000 }
20+
fn from_m(v: i64) -> Duration: Duration { value: v * 60000 }
2421

2522
// Conversion: to milliseconds
26-
fn to_ms(self) -> i64:
27-
self.value
23+
fn to_ms(self) -> i64: self.value
2824

2925
// Conversion: to seconds (integer division)
30-
fn to_s(self) -> i64:
31-
self.value / 1000
26+
fn to_s(self) -> i64: self.value / 1000
3227

3328
// Conversion: to minutes
34-
fn to_m(self) -> i64:
35-
self.value / 60000
29+
fn to_m(self) -> i64: self.value / 60000
3630
}
3731

38-
fn main():
32+
fn main() {
3933
// Demonstrate Duration with suffix literals and method dispatch
4034

4135
// Test 1: milliseconds literal
@@ -60,3 +54,4 @@ fn main():
6054
let delay5 = Duration::from_s(10)
6155
println(delay5.to_ms()) // Expected: 10000
6256
println(delay5.to_s()) // Expected: 10
57+
}

0 commit comments

Comments
 (0)