~repos /plum
git clone https://pyrossh.dev/repos/plum.git
A statically typed, imperative programming language inspired by rust, python
2a44b7e0
—
Peter John 1 year ago
more syntax
- sample.ks +266 -5
- scratch.hs +10 -10
- std/json.mi +13 -9
- tree-sitter-kestrel/test/corpus/fn.txt +1 -1
- tree-sitter-kestrel/test/corpus/match.txt +76 -0
sample.ks
CHANGED
|
@@ -4,7 +4,6 @@ import std.{List, Map, Math, Random}
|
|
|
4
4
|
|
|
5
5
|
Int.random()
|
|
6
6
|
Float.random()
|
|
7
|
-
|
|
8
7
|
Float.PI
|
|
9
8
|
|
|
10
9
|
trait BinaryOp<T> {
|
|
@@ -84,11 +83,11 @@ record Cat(
|
|
|
84
83
|
printLn("called deinit")
|
|
85
84
|
}
|
|
86
85
|
|
|
87
|
-
fn fullname()
|
|
86
|
+
fn fullname() -> Str {
|
|
88
87
|
return "${self.name} ${self.age}"
|
|
89
88
|
}
|
|
90
89
|
|
|
91
|
-
override fn toStr()
|
|
90
|
+
override fn toStr() -> Str {
|
|
92
91
|
return "Cat<${fullname()}, ${age}>"
|
|
93
92
|
}
|
|
94
93
|
}
|
|
@@ -188,9 +187,13 @@ fn mkPerson(name: Str, age: Int) -> Result<Person, ValidationError> = result {
|
|
|
188
187
|
Person(validName(name).bind(), validAge(age).bind())
|
|
189
188
|
}
|
|
190
189
|
|
|
191
|
-
@
|
|
190
|
+
@handler("GET", "/posts")
|
|
191
|
+
fn getPosts() -> List<Post> {
|
|
192
|
+
return listOf(Post(id: 123))
|
|
193
|
+
}
|
|
194
|
+
|
|
192
195
|
fn handler(req: Request) -> Response {
|
|
193
|
-
Response()
|
|
196
|
+
return Response()
|
|
194
197
|
.status(2)
|
|
195
198
|
.body("213")
|
|
196
199
|
.headers("1" => "2")
|
|
@@ -213,3 +216,261 @@ fn main(args: List[String]) {
|
|
|
213
216
|
)
|
|
214
217
|
Greeter(...g, name: "123")
|
|
215
218
|
}
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
fn main() {
|
|
222
|
+
val a = (1 * 5) + (2 + 3)
|
|
223
|
+
match a {
|
|
224
|
+
case a < b => {
|
|
225
|
+
printLn(a != b)
|
|
226
|
+
}
|
|
227
|
+
case a > 9 => {
|
|
228
|
+
printLn(a == 9)
|
|
229
|
+
}
|
|
230
|
+
case a < 9 => {
|
|
231
|
+
printLn(b == 0)
|
|
232
|
+
}
|
|
233
|
+
case _ => {
|
|
234
|
+
printLn(a == 9)
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
val list = listOf<Shape>(Circle(123), Square(20))
|
|
239
|
+
|
|
240
|
+
match list.get(0) {
|
|
241
|
+
case Circle(c): {
|
|
242
|
+
printLn("${c}")
|
|
243
|
+
}
|
|
244
|
+
case Square(a): {
|
|
245
|
+
printLn("${a}")
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
birds = 3
|
|
253
|
+
iguanas = 2
|
|
254
|
+
total = addAndStringify birds iguanas
|
|
255
|
+
|
|
256
|
+
main() =
|
|
257
|
+
Stdout.line! "There are $(total) animals."
|
|
258
|
+
|
|
259
|
+
addAndStringify(num1: Int, num2: Int) -> Str =
|
|
260
|
+
(num1 + num2) |> Int.toStr()
|
|
261
|
+
|
|
262
|
+
addAndStringify = \num1: Int, num2: Int -> Str
|
|
263
|
+
Num.toStr (num1 + num2)
|
|
264
|
+
|
|
265
|
+
val user = User(1, 2, 3)
|
|
266
|
+
|
|
267
|
+
params = {"one" => 1, "two" => 2, "three" => 3}
|
|
268
|
+
params.each do |k,v|
|
|
269
|
+
puts("#{k}=#{v}")
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
lib LibM
|
|
273
|
+
fun sqrt(x: LibC::Double) -> LibC::Double
|
|
274
|
+
fun pow(x: LibC::Double, y: LibC::Double) -> LibC::Double
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
primitive Int : Comparable, ToStr
|
|
278
|
+
fn abs() -> Float
|
|
279
|
+
fn ceil() -> Float
|
|
280
|
+
fn floor() -> Float
|
|
281
|
+
fn round() -> Float
|
|
282
|
+
fn trunc() -> Float
|
|
283
|
+
fun log() -> Float
|
|
284
|
+
fun log2() -> Float
|
|
285
|
+
fun log10() -> Float
|
|
286
|
+
fun logb() -> Float
|
|
287
|
+
fun pow(y: Float) -> Float
|
|
288
|
+
LibM.pow(self, y)
|
|
289
|
+
end
|
|
290
|
+
fun pow(y: Int) -> Float
|
|
291
|
+
pow(y.toFloat())
|
|
292
|
+
end
|
|
293
|
+
fun sqrt() -> Float
|
|
294
|
+
LibM.sqrt(self)
|
|
295
|
+
end
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
primitive Str : Comparable, ToStr
|
|
299
|
+
val data: Buffer
|
|
300
|
+
|
|
301
|
+
fun get(i: Int) -> Char
|
|
302
|
+
return data.get(i)
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
fun contains(search: Str) -> Bool
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
fun indexOf(sub: Str) -> Int
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
fun test(pattern: Regex) -> Bool
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
fun search(key: Str) -> (Int, Bool)
|
|
315
|
+
val (low, mid, high) = (0, 0, n.numItems)
|
|
316
|
+
while low < high do
|
|
317
|
+
mid = (low + high) / 2
|
|
318
|
+
val cmp = key > n.items[mid].key
|
|
319
|
+
low = cmp > 0 ? mid + 1 : low
|
|
320
|
+
high = cmp < 0 ? mid : high
|
|
321
|
+
if cmp == 0 then
|
|
322
|
+
return (mid, True)
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
return (low, False)
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
fun startsWith(search: str) -> Bool
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
fun endsWith(search: Str) -> Bool
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
fun concat(other: Str) -> Str
|
|
335
|
+
s + other
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
fun toStr() -> Str
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
fun matchPattern(pattern: Regex) -> []Str
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
fun matchAll(pattern: Regex) -> []Str
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
fun padStart(sub: Str, count: Int) -> Str
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
fun padEnd(sub: Str, count: Int) -> Str
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
fun repeat(count: Int) -> Str
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
fun replace(pattern: Regex, sub: Str) -> Str
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
fun replaceAll(pattern: Regex, sub: Str) -> Str
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
fun search(pattern: Regex) -> Str
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
fun slice(start: Int, e: Int) -> Str
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
fun split(separator: Str, limit: Int) -> []Str
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
fun sub(start: Int, e: Int) -> Str
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
fun toLower() -> Str
|
|
375
|
+
end
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
record Response(a): Option(a, _)
|
|
379
|
+
val body: Buffer = emptyBuffer()
|
|
380
|
+
val headers: Map(Str, Str) = emptyMap()
|
|
381
|
+
val status: Int = 0
|
|
382
|
+
|
|
383
|
+
fun header(k: Str, v: Str) -> Response
|
|
384
|
+
headers.add(kv)
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
fun body(buf: Buffer) -> Response
|
|
388
|
+
body = buf
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
fun status(v: Int) -> Response
|
|
392
|
+
status = v
|
|
393
|
+
end
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
User::create()
|
|
397
|
+
.name("John Doe")
|
|
398
|
+
.email("john@example.com")
|
|
399
|
+
.todo(Todo::create().title("Make pizza"))
|
|
400
|
+
.todo(Todo::create().title("Finish Toasty"))
|
|
401
|
+
.todo(Todo::create().title("Sleep"))
|
|
402
|
+
|
|
403
|
+
trait Result(a, b) permits Ok(a), Err(b)
|
|
404
|
+
fn ok() -> a
|
|
405
|
+
fn err() -> b
|
|
406
|
+
fn okOrElse(default: a) -> a
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
tuple Ok(a): Result(a, _)
|
|
410
|
+
fn ok() -> a = self.0
|
|
411
|
+
fn err() -> fail("called 'Result.err()' on an 'Ok' value")
|
|
412
|
+
fn okOrElse(default: a) -> a = self.0
|
|
413
|
+
fn map(cb: fn(a) -> b) -> Result(b) = Ok(cb(self.0))
|
|
414
|
+
|
|
415
|
+
tuple Err(a): Result(_, a)
|
|
416
|
+
fn get() -> a = fail("called 'Result.get()' on an 'Err' value")
|
|
417
|
+
fn err() -> self.1
|
|
418
|
+
fn okOrElse(default: a) -> a = default
|
|
419
|
+
fn mapErr(cb: fn(a) -> b) -> Result(_, b) = Err(cb(self.1))
|
|
420
|
+
|
|
421
|
+
trait Option(a) permits Some(a), None
|
|
422
|
+
fn some() -> a
|
|
423
|
+
fn someOrElse(default: a) -> a
|
|
424
|
+
fn map(cb: fn(a) -> b) -> Option(b)
|
|
425
|
+
|
|
426
|
+
tuple Some(a): Option(a)
|
|
427
|
+
fn some() -> a = self.0
|
|
428
|
+
fn someOrElse(default: a) -> a = self.0
|
|
429
|
+
fn map(cb: fn(a) -> b) -> Option(b) = Some(cb(self.0))
|
|
430
|
+
|
|
431
|
+
tuple None(a): Option(_)
|
|
432
|
+
fn some() -> a = fail("called 'Option.some()' on an 'Some' value")
|
|
433
|
+
fn someOrElse(default: a) -> a = default
|
|
434
|
+
fn stoplightColor(something: Int) -> Color =
|
|
435
|
+
if something > 0 then
|
|
436
|
+
Red
|
|
437
|
+
else if something == 0 then
|
|
438
|
+
Yellow
|
|
439
|
+
else
|
|
440
|
+
Green
|
|
441
|
+
|
|
442
|
+
fn stoplightStr(stoplightColor: Color) -> Str =
|
|
443
|
+
when stoplightColor is
|
|
444
|
+
Red -> "red"
|
|
445
|
+
Green -> "green"
|
|
446
|
+
Yellow -> "yellow"
|
|
447
|
+
end
|
|
448
|
+
|
|
449
|
+
fn dddd(data: Option(Str)) -> Str =
|
|
450
|
+
if Some(v) = data then
|
|
451
|
+
v.subString(0, 3)
|
|
452
|
+
else if a == b then
|
|
453
|
+
"123"
|
|
454
|
+
else
|
|
455
|
+
"1231"
|
|
456
|
+
|
|
457
|
+
fn dddd(data: Option(Str)) -> Str =
|
|
458
|
+
if Some(v) = data then
|
|
459
|
+
v.subString(0, 3)
|
|
460
|
+
else if a == b then
|
|
461
|
+
"123"
|
|
462
|
+
else
|
|
463
|
+
if a < 100 then
|
|
464
|
+
printLn("Data")
|
|
465
|
+
else
|
|
466
|
+
println("No Data")
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
names = ["Sam", "Lee", "Ari"]
|
|
470
|
+
names |> List.append("Jess")
|
|
471
|
+
names |> List.map(\num -> num * 2)
|
|
472
|
+
List.map([1, 2, 3], \num -> num * 2)
|
|
473
|
+
|
|
474
|
+
range data, 1, 2, do k,v
|
|
475
|
+
puts(k, v)
|
|
476
|
+
end
|
scratch.hs
CHANGED
|
@@ -2,17 +2,17 @@ data.price?.take_if(|a| a != "")
|
|
|
2
2
|
math.sqrt(math.pow(math.mod(1 + 2, 3), 2))
|
|
3
3
|
|
|
4
4
|
if v == None
|
|
5
|
-
|
|
5
|
+
error.Error
|
|
6
6
|
else
|
|
7
|
-
|
|
7
|
+
c * 20
|
|
8
8
|
|
|
9
|
-
if v == None
|
|
9
|
+
if v == None then
|
|
10
|
-
|
|
10
|
+
error.Error
|
|
11
11
|
else
|
|
12
|
-
|
|
12
|
+
Some(c) := v
|
|
13
|
-
|
|
13
|
+
c * 20
|
|
14
14
|
|
|
15
|
-
match v
|
|
15
|
+
match v is
|
|
16
|
-
|
|
16
|
+
None -> return error.Error
|
|
17
|
-
|
|
17
|
+
Some(c) ->
|
|
18
|
-
|
|
18
|
+
return c * 20
|
std/json.mi
CHANGED
|
@@ -5,7 +5,7 @@ trait Json permits JsonStr, JsonBool, JsonNull, JsonFloat, JsonInt, JsonObject,
|
|
|
5
5
|
fn parse() -> T
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
tuple JsonObject(Map[str, Json]) {
|
|
8
|
+
tuple JsonObject(Map[str, Json]): Json {
|
|
9
9
|
type = JsonObject
|
|
10
10
|
|
|
11
11
|
fn parse() -> JsonObject {
|
|
@@ -14,27 +14,27 @@ tuple JsonObject(Map[str, Json]) {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
|
|
17
|
-
tuple JsonArray(List[Json]) {
|
|
17
|
+
tuple JsonArray(List[Json]): Json {
|
|
18
18
|
fn parse() {}
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
tuple JsonInt(Int) {
|
|
21
|
+
tuple JsonInt(Int): Json {
|
|
22
22
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
|
|
26
|
-
tuple JsonFloat(Float) {
|
|
26
|
+
tuple JsonFloat(Float): Json {
|
|
27
27
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
|
|
31
|
-
tuple JsonString(Str) {
|
|
31
|
+
tuple JsonString(Str): Json {
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
tuple JsonBool(Bool) {
|
|
34
|
+
tuple JsonBool(Bool): Json {
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
tuple JsonNull {
|
|
37
|
+
tuple JsonNull: Json {
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
fn Json.parse(src: str | Buffer | IO): Json | ParseError =
|
|
@@ -56,11 +56,15 @@ record JsonParser(
|
|
|
56
56
|
fn JsonParser.withSrc(src: str | Buffer | IO): JsonParser =
|
|
57
57
|
JsonParser(_buf = src)
|
|
58
58
|
|
|
59
|
-
fn
|
|
59
|
+
fn isSpace(c: U32): Bool => (c == ' ') or ((c >= '\t') and (c <= '\r'))
|
|
60
60
|
|
|
61
|
-
fn
|
|
61
|
+
fn isDelim(c: U32): Bool =>
|
|
62
62
|
(c == ',') or (c == '}') or (c == ':') or (c == ']') or (_is_space(c)) or (c == 0)
|
|
63
63
|
|
|
64
|
+
fn isDigit(c: U32) -> Bool {
|
|
65
|
+
return (c >= '0') && (c <= '9')
|
|
66
|
+
}
|
|
67
|
+
|
|
64
68
|
fn _is_digit(c: U32): Bool => (c >= '0') and (c <= '9')
|
|
65
69
|
|
|
66
70
|
fn (p: Parser) _err(msg: str) => printLn(msg)
|
tree-sitter-kestrel/test/corpus/fn.txt
CHANGED
|
@@ -151,7 +151,7 @@ fn fetchAllPosts(req: Request) -> Response {
|
|
|
151
151
|
fn - generics
|
|
152
152
|
================================================================================
|
|
153
153
|
|
|
154
|
-
|
|
154
|
+
add[T: Int, Q: String + Compare](a: List[T], b: List[Q]) -> List[T] {
|
|
155
155
|
}
|
|
156
156
|
|
|
157
157
|
--------------------------------------------------------------------------------
|
tree-sitter-kestrel/test/corpus/match.txt
CHANGED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
================================================================================
|
|
2
|
+
statement - match
|
|
3
|
+
================================================================================
|
|
4
|
+
|
|
5
|
+
fn main() {
|
|
6
|
+
val a = (1 * 5) + (2 + 3)
|
|
7
|
+
match a {
|
|
8
|
+
case a < b => {
|
|
9
|
+
printLn(a != b)
|
|
10
|
+
}
|
|
11
|
+
case a > 9 => {
|
|
12
|
+
printLn(a == 9)
|
|
13
|
+
}
|
|
14
|
+
case a < 9 => {
|
|
15
|
+
printLn(b == 0)
|
|
16
|
+
}
|
|
17
|
+
case _ => {
|
|
18
|
+
printLn(a == 9)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
--------------------------------------------------------------------------------
|
|
24
|
+
|
|
25
|
+
(source
|
|
26
|
+
(test
|
|
27
|
+
(string
|
|
28
|
+
(string_start)
|
|
29
|
+
(string_end))
|
|
30
|
+
(lambda
|
|
31
|
+
(identifier)
|
|
32
|
+
(body
|
|
33
|
+
(assignment_statement
|
|
34
|
+
(identifier)
|
|
35
|
+
(binary_operator
|
|
36
|
+
(parenthesized_expression
|
|
37
|
+
(binary_operator
|
|
38
|
+
(integer)
|
|
39
|
+
(integer)))
|
|
40
|
+
(parenthesized_expression
|
|
41
|
+
(binary_operator
|
|
42
|
+
(integer)
|
|
43
|
+
(integer)))))
|
|
44
|
+
(if_statement
|
|
45
|
+
(comparison_operator
|
|
46
|
+
(identifier)
|
|
47
|
+
(identifier))
|
|
48
|
+
(block
|
|
49
|
+
(assert_statement
|
|
50
|
+
(comparison_operator
|
|
51
|
+
(identifier)
|
|
52
|
+
(identifier))))
|
|
53
|
+
(else_if_clause
|
|
54
|
+
(comparison_operator
|
|
55
|
+
(identifier)
|
|
56
|
+
(integer))
|
|
57
|
+
(block
|
|
58
|
+
(assert_statement
|
|
59
|
+
(comparison_operator
|
|
60
|
+
(identifier)
|
|
61
|
+
(integer)))))
|
|
62
|
+
(else_if_clause
|
|
63
|
+
(comparison_operator
|
|
64
|
+
(identifier)
|
|
65
|
+
(integer))
|
|
66
|
+
(block
|
|
67
|
+
(assert_statement
|
|
68
|
+
(comparison_operator
|
|
69
|
+
(identifier)
|
|
70
|
+
(integer)))))
|
|
71
|
+
(else_clause
|
|
72
|
+
(block
|
|
73
|
+
(assert_statement
|
|
74
|
+
(comparison_operator
|
|
75
|
+
(identifier)
|
|
76
|
+
(integer))))))))))
|