-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguage_syntax.rain
215 lines (140 loc) · 3.48 KB
/
language_syntax.rain
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//// Language Syntax
// This is file for showcasing the syntax of my programming language aka. Rain.
// Syntax is inspired from many languages, heavily Jai, Javascript, C++, and Wren.
//
// Enjoy!
/// Main function, everything starts from here:
main :: () {
println("Hello world!");
}
/// Comments:
// In rain we accept only line comments: `// comment`
/// Variables:
// Mutuable variable
x := 5;
// Constant
y :: 6;
// Specify variable like this:
foo: int = 65;
bar := float(3); // will be turned into 3.0
// You can initliaze more variables in same line:
x := 5, y := 5;
/// Functions:
hello :: (name) {
println("Hello ${name}!");
}
hello("Sky"); // > "Hello Sky!"
// squared2 :: (x) {
// return x ** 2;
// }
// In Rain, same as in Rust, if you want to just straight up return the value (with calculations or not), just write it, and forget the return keyword:
squared :: (x) {
x ** 2
}
// This could be useful if you want just to return a value
// and that you don't want to run anything before that return statement
println("The squared of 5 is: ${squared(5)}") // > "The squared of 5 is: 25"
// Optional arguments need a default value (like you can see on y argument), and if you want you want specify what return value will be, just add the variable type after parantheses:
squared_by :: (x, y := 2) int {
return x ** y;
}
/// Arrays:
numbers := [0, 1, 2, 3, 4];
names : string[5] = [];
names[0] = "Evry";
names[1] = "Daniel";
names[2] = "Chandler";
// etc...
/// Objects:
z :: object {
"foo": 1,
"bar": x
"baz": 2
}
/// Structs:
Vector2 :: struct
{
x: int,
y: int,
}
a := Vector2 { 6, 5 };
println(a.x); // > 6
println(a); // > Vector2 { x: 6, y: 5 }
// Functions for structs:
Vector2: magnitude: () {
return int(sqrt(this.x * this.x + this.y * this.y));
}
println(a.magnitude()); // > 7.8
/// Fibers:
// Create a fiber like this:
foo :: fiber {
return "Fiber called";
}
// And call it:
println(foo.call()); // > "Fiber called!"
// Yielding:
bar :: fiber {
println("passed to yield: {foo.call()}");
}
foo.yield("hello, world!"); // > "passed to yield: hello, world!"
/// Operators:
// `::`:
// Initializator or `::` can be used to create a function, a struct or an constant that like a struct and function can't be modified after creation.
// You can specify the constants' variable type before the second `:`:
x: int : 5
// It's possible to initialize a function inside of a struct by giving it the function name after the first colon (`:`), and after that
StructName : FuncName : () {
// do something...
}
// `:=`:
// The second initializator (`:=`) also known as walrus operator, creates a modifiable variable:
x := 5;
// You can specify the variable type before the `=` operator:
x: int = 5;
x: int[] = [];
/// Import and Modules
// in file.rain:
pub foo :: () {
println("foo");
}
// in main.rain file:
#import "file.rain"
main :: () {
foo();
}
/// Condtional statements
// If statmenet
x := 5;
y := 3;
if (x == y + 2) {
println("This is true!");
} else {
println("This is false");
}
// for statement
// for can be used to loop trought arrays:
number := [0, 1, 2, 3, 4, 5];
for num in numbers {
println(num);
}
names := ["Sam", "Peter"]
for i, name in names {
println("{i}) {name}");
// > 0) Sam
// 1) Peter
}
// range numbers
for i in 0 .. 5 {
print(i);
}
// > 01234
// or condtional looping, also known as `while` loop in other languages
sum := 0
i := 0
for i <= 100 {
sum += i;
i++;
}
println(sum); // > 5050
/// References and heap
x := 6;