Skip to content

Commit 3c96133

Browse files
authored
Add proposed syntax for new programming language
This file introduces a new syntax for a programming language, covering multiple assignment, arrays, functions, enums, structs, interfaces, methods, error handling, loops, optional types, and ternary operators. Signed-off-by: Fuad Hasan <[email protected]>
1 parent f9bdcd9 commit 3c96133

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

proposed_syntax.fer.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Multiple assignment
2+
let a, b, c := 1, 2, 4;
3+
4+
// Arrays
5+
let arr: [10]i32 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; // Fixed-size
6+
let dynArr: []i32 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; // Dynamic-size
7+
8+
// Functions
9+
fn getPi() -> f32 {
10+
return 3.14;
11+
}
12+
13+
// Compile-time constant
14+
const pi := comptime getPi();
15+
16+
// enum
17+
type Color enum {
18+
Red,
19+
Green,
20+
Blue
21+
};
22+
23+
// Structs
24+
type Circle struct {
25+
.radius: f32,
26+
.area: f32,
27+
.color: Color
28+
};
29+
30+
// Interfaces
31+
type Shape interface {
32+
fn area(self) -> f32,
33+
fn perimeter(self) -> f32
34+
};
35+
36+
37+
// Methods
38+
fn (c &Circle) area() -> f32 {
39+
return c.area;
40+
}
41+
fn (c &Circle) perimeter() -> f32 {
42+
return 2.0 * pi * c.radius;
43+
}
44+
45+
// Struct literal
46+
const circle1 := Circle {
47+
.radius = 5.0,
48+
.area = pi * 5.0 * 5.0,
49+
.color = Color::Red
50+
};
51+
52+
// Result type & error handling
53+
fn fetch(url: str) -> Result ! str {
54+
if url == "https://example.com/api/data" {
55+
return Result{.data = "Fetched Data"};
56+
} else {
57+
return "Network Error"!;
58+
}
59+
}
60+
61+
// Catch with default value
62+
let res := fetch("https://example.com/api/data") catch err {
63+
log("Error fetching data: " + err);
64+
with "default data";
65+
};
66+
67+
// Loops
68+
for i, v in 0..10:2 {
69+
log("Current index: " + i);
70+
log("Current value: " + v);
71+
}
72+
73+
while a < 10 {
74+
a += 1;
75+
}
76+
77+
// Optional types
78+
let mayExist: i32?;
79+
80+
// Statement-only `when`
81+
when mayExist {
82+
null => log("mayExist is null"),
83+
_ => log("mayExist has a value: " + mayExist)
84+
}
85+
86+
// Optional unwrapping in `if`
87+
if mayExist == null {
88+
log("mayExist is null");
89+
} else {
90+
log("mayExist has a value: " + mayExist);
91+
}
92+
93+
// Ternary / Elvis operators
94+
let value := mayExist ? mayExist : 42; // Standard ternary
95+
let value2 := mayExist ?: 42; // Elvis operator

0 commit comments

Comments
 (0)