-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall.klar
More file actions
247 lines (219 loc) · 4.67 KB
/
Copy pathall.klar
File metadata and controls
247 lines (219 loc) · 4.67 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/// This file uses all syntax features in Klar. This can be
/// used to test the parser and type checker.
/// TODO: Fix reference errors
// This is a line comment
/* This is a
block comment
*/
/* This
is /* a nested
block comment */ */
// Imports
//import klar.io // Standard library import
//import klar.io.{Readable, Writable}
//import klar.io.{Readable as MyName, Writable}
//import klar.*
//import system = klar.os
// Declarations
basicVar := "Hello"
basicVarWithAnnot: String := "World"
var1, var2 := 1
var3, var4 := 2, 3
var5, var6: Int := 4, 5
var7, var8: Int := 6
var9, var10, var11 := 7
type ReturnType = Nothing
func function() {}
func function(param: Int) -> ReturnType {}
func function2(label param: Int) = 1 + param
func function3(label1 param1, label2 param2: Int) -> Int {}
func withResult() -> Result<Int> {}
func alias = function
func Struct.alias = .method
// Assignment
basicVar += ", World!"
basicVar = "Klar is awesome!"
basicVar += " "
basicVar *= 3
// Compound assignments
_ = 1
_ := 1
_ += 1
_ -= 1
_ *= 1
_ /= 1
_ ^= 1
_ %= 1
// Type Declarations
type #Tag
type #Inherited
type #Tag2: Inherited
type Struct {
field: Int
field2, field3: String
fieldWithDefault: Int = 1
}
type Struct2: Inherited {}
type #Interface {
field: Int
field2, field3: String
method()
methodWithParam(param: String)
methodWithParam2(String)
methodWithLabelledParam(label param: String)
methodWithLabelledParam2(label _: String) -> ReturnType
methodWithReturn() -> Bool
}
type #Interface2: Inherited, Inherited2 {}
type Enum {
.some
.none
.withParams(String, Int)
.withLabelledParams(a: String, b: Int)
.withLabelledParams2(a, b: Int)
}
type GenericEnum<T> { .some(T) }
type Alias = String
// Types
type Union = Int | Float
type StringList = [String]
type StringMap = #{String: String}
type StringResult = Result<String>
type StringResult2 = Result<String, Error>
type StringResultOrCustomError = Result<String, CustomError>
type Outcome = Result<Nothing>
type Optional = Int?
type Tuple = (Int, String)
type Tuple2 = (k: Int, v: String)
type Tuple3 = (k, v: Int)
type _ = GenericEnum<Int>
type Function = func()
type Function2 = func(String)
type Function3 = func(a, b: Int) -> Nothing
type Function4 = func(a: Int, b: String)
// Attributes
@deprecated
func deprecatedFunc() {}
@external(.js, file: "#/myfunc.js", export: "default")
func myFunc()
@external(.js, file: "#/myfunc.js", export: "default")
@deprecated(since: v1.4.2)
func myFuncWithFallback() { crashout("Unsupported") }
@added(v1)
@deprecated(since: v1.4)
func _()
// Literals
function()
nilValue: Int? := nil
_ = (
// String
"Hello", 'Hello', "Hello {1 + 1}",
`Hello
World!`,
// Int
23333, -27272, (1 + 1),
0xfff, 0xc0fefe, 0xc0_fe_fe,
0b10101, 0b110_10,
// Float
0.18728, 0.172_228,
10e5, 0.272e3, 10e4_2, 10.42e6_2,
// Bool and nil
false, true, nilValue, Optional(nil),
// List
[], [1, 2, 3], [[1, 2, 3]..., whatever...],
// Map
#{}, #{a: 1, b: 2, "c": 3, ("Hello" + " World!"): 4},
#{
#{a: 1}..., // Rest
:b, // Shorthand
c: 1,
},
)
// Tuples
_ = (
(1, 2, 3),
(1,),
(1, 2),
)
// Operators
arithmetic := (
1 + 1, 2 - 1,
3 * 5, 12 / 4, 11 % 10,
2 ^ 3, -2 ^ 4 /* -16 */, (-2) ^ 4 /* 16 */,
)
logical := (
true && false, true || false,
!true,
1 + 1 == 2 || 1 * 3 > 6,
)
relational := (
1 == 1, 1 != 2,
1 < 2, 2 > 1,
1 <= 1, 2 >= 1,
// Chained comparisons
1 < 2 < 3,
1 >= 2 >= 3,
1 < 2 == 2 < 3,
)
x := 1
distributive := (
x == 3 or 4,
x != 1 and 5,
x == 3 or 4 or 5,
)
list := [1, 2, 3, 4, 5]
contains2 := 2 in list
excludes6 := 6 !in list
type Object { name: String }
object := Object(name: "John")
object = Object("John")
object = .("John")
// Index
object.name = "John"
map := #{'key1': #{'key2': 2, 'key3': 3}, 'key4': #{}}
_ = (
object.name.length,
map['key1']!!['key2'],
map.key1["key2"],
map['key1']!!.key2,
)
// List slicing
_ = (
list[1..<3],
list[1...],
list[1...3],
list[...6],
)
// Ranges
closedRange := 1...10 // 11 items
openRange := 1..<10 // 10 items
// Try
_ = try withResult()
// Assertion
optional: Int? := 1
result: Result<Int> := 2
print(optional!!, result!!)
// Casts
_ = [Int]()
_ = #{String: Int}()
// Lambdas
_ = (
func {},
func (x: Int) {},
func (x: Int) -> x,
func (x: Int = 1) -> x,
)
_: func(Int, Int) -> Int := func x, y -> x + y
// Enum literals
_: (Enum, Enum) := (Enum.some, .some)
// Go/Await
func fn() = 1
task := go fn()
task2 := go { print("Running in parallel") }
go {}
await task
await (task, task2)
_ = await task
// Pipeline
// Object pipeline
//lol