~repos /plum
git clone https://pyrossh.dev/repos/plum.git
A statically typed, imperative programming language inspired by rust, python
f9f376cc
—
pyrossh 1 year ago
update
- readme.md +66 -46
- std/json.mi +9 -0
readme.md
CHANGED
|
@@ -1,25 +1,23 @@
|
|
|
1
1
|
# 👾 Pacos Programming Language
|
|
2
2
|
|
|
3
|
-
- A statically typed, imperative programming language inspired by rust, koka
|
|
3
|
+
- A statically typed, imperative programming language inspired by rust, koka
|
|
4
|
-
- The compiler users the tree-sitter parser so has out of the box syntax highlighting support for helix and zed editor
|
|
4
|
+
- The compiler users the tree-sitter parser so has out of the box syntax highlighting support for helix and zed editor
|
|
5
|
+
- Plans to be compiled to WASM
|
|
5
6
|
|
|
6
|
-
Here is some sample code, please enjoy
|
|
7
|
+
Here is some sample code, please enjoy
|
|
7
8
|
|
|
8
|
-
```
|
|
9
|
+
```rs
|
|
9
10
|
module lambda
|
|
10
11
|
|
|
11
12
|
import std/list
|
|
12
13
|
import std/math
|
|
13
14
|
import std/http
|
|
14
15
|
|
|
15
|
-
const START_YEAR = 2101
|
|
16
|
-
const
|
|
16
|
+
const DELTA = 11
|
|
17
|
-
const NAME = "Gleam"
|
|
18
|
-
const SIZE = 100
|
|
19
17
|
|
|
20
|
-
fn sum(a: int, b: int): int = a + b
|
|
18
|
+
fn sum(a: int, b: int): int = a + b + DELTA
|
|
21
19
|
|
|
22
|
-
fn
|
|
20
|
+
fn sumAll(series: list[int]): int =
|
|
23
21
|
series.reduce(0, |v| v + 1)
|
|
24
22
|
|
|
25
23
|
fn fib(n: int): int =
|
|
@@ -38,33 +36,29 @@ fn factorial(n: int): int =
|
|
|
38
36
|
a -> 1
|
|
39
37
|
_ -> n * factorial(n - 1)
|
|
40
38
|
|
|
41
|
-
fn firstItem(l: list[int]): int
|
|
39
|
+
fn firstItem(l: list[int]): option[int] =
|
|
42
40
|
l[0]
|
|
43
41
|
|
|
44
|
-
fn firstItem(l: list[int]): int
|
|
42
|
+
fn firstItem(l: list[int]): option[int] =
|
|
45
43
|
match l
|
|
46
|
-
[] ->
|
|
44
|
+
[] -> none
|
|
47
45
|
[head, ...rest] -> head
|
|
48
46
|
|
|
49
|
-
fn firstItem(l: list[int]): int? =
|
|
50
|
-
if l.size > 0
|
|
51
|
-
l[0]
|
|
52
|
-
else
|
|
53
|
-
nil
|
|
54
|
-
|
|
55
47
|
fn toCelsius(f: float): float =
|
|
56
48
|
(f - 32) * (5 / 9)
|
|
57
49
|
|
|
58
|
-
record Cat
|
|
50
|
+
record Cat(
|
|
59
51
|
name: str
|
|
60
52
|
age: int
|
|
61
53
|
)
|
|
62
54
|
|
|
55
|
+
// Additional Cat Contructor which is just a static function on type Cat
|
|
63
56
|
fn Cat.withName(name: str): Cat =
|
|
64
57
|
Cat(name: name, age: 0)
|
|
65
58
|
|
|
59
|
+
// A simple method
|
|
66
60
|
fn (c: Cat) fullname(): str =
|
|
67
|
-
c.name + c.age.
|
|
61
|
+
c.name + c.age.toStr()
|
|
68
62
|
|
|
69
63
|
fn (c: Cat) talk() =
|
|
70
64
|
printLn("cat ${c.name} says meow")
|
|
@@ -76,7 +70,7 @@ enum Temperature =
|
|
|
76
70
|
| celsius(float)
|
|
77
71
|
| fahrenheit(float)
|
|
78
72
|
|
|
79
|
-
fn (s: Temperature)
|
|
73
|
+
fn (s: Temperature) toStr(): str =
|
|
80
74
|
match s
|
|
81
75
|
celsius(t) && t > 30 -> "${t}C is above 30 celsius"
|
|
82
76
|
celsius(t) -> "${t}C is below 30 celsius"
|
|
@@ -85,36 +79,39 @@ fn (s: Temperature) to_str(): str =
|
|
|
85
79
|
|
|
86
80
|
type MapCallback = fn(v: a): v
|
|
87
81
|
|
|
88
|
-
trait Comparable(
|
|
82
|
+
trait Comparable[A: Compare](
|
|
89
83
|
fn compare(left: A, right: A): bool
|
|
90
84
|
)
|
|
91
85
|
|
|
92
|
-
trait
|
|
86
|
+
trait Stringable(
|
|
93
|
-
fn
|
|
87
|
+
fn toStr(): str
|
|
94
88
|
)
|
|
95
89
|
|
|
96
|
-
fn connect_db(conn_url: str): DB! =
|
|
97
|
-
db := postgres_connect(conn_url)?
|
|
98
|
-
db.exec("select 1")?
|
|
99
|
-
|
|
100
|
-
fn db_select(conn_url: str): void! =
|
|
101
|
-
|
|
90
|
+
record DB(
|
|
102
|
-
db.exec("select 1")?
|
|
103
|
-
|
|
104
|
-
|
|
91
|
+
conn_url: str
|
|
92
|
+
)
|
|
105
93
|
|
|
94
|
+
#[error]
|
|
106
|
-
|
|
95
|
+
enum DatabaseError =
|
|
107
|
-
|
|
96
|
+
NotOnline(conn_url: str)
|
|
108
97
|
RowReadFailure(query: str)
|
|
109
98
|
|
|
110
|
-
fn
|
|
99
|
+
fn newDB(conn_url: str) result[DB, DatabaseError] =
|
|
111
100
|
db := DB(conn_url: str)
|
|
112
|
-
online := db.
|
|
101
|
+
online := db.status()
|
|
113
102
|
if !online
|
|
114
|
-
|
|
103
|
+
err(NotOnline(conn_url))
|
|
115
104
|
else
|
|
116
105
|
db
|
|
117
106
|
|
|
107
|
+
fn (d: DB) status(conn_url: str): bool =
|
|
108
|
+
res := d.exec("select 1")
|
|
109
|
+
if res == None
|
|
110
|
+
false
|
|
111
|
+
else
|
|
112
|
+
true
|
|
113
|
+
|
|
114
|
+
// Testing
|
|
118
115
|
test("talks") |t|
|
|
119
116
|
c := Cat(name: "123", age: 1)
|
|
120
117
|
c.talk()
|
|
@@ -123,12 +120,12 @@ test("fullname") |t|
|
|
|
123
120
|
Cat("rabby", 21).fullname() == "rabby21"
|
|
124
121
|
c2 := Cat(...c, age: c.age + 1)
|
|
125
122
|
|
|
126
|
-
test("
|
|
123
|
+
test("ToStr") |t|
|
|
127
124
|
items := [Cat("Molly", 9), Cat("Fenton", 6)]
|
|
128
125
|
.retain(|p| p.name.size > 5)
|
|
129
126
|
.map(|p| describe(p))
|
|
130
127
|
.each(|d| printLn(d))
|
|
131
|
-
assert items[0].
|
|
128
|
+
assert items[0].toStr() == "Cat<Fenton, 6>"
|
|
132
129
|
|
|
133
130
|
bench("1231") |n|
|
|
134
131
|
for i := range n
|
|
@@ -140,7 +137,7 @@ bench("1231") |n|
|
|
|
140
137
|
**Keywords**
|
|
141
138
|
|
|
142
139
|
```rs
|
|
143
|
-
for,while,if,else
|
|
140
|
+
for,while,if,else,record,type,enum,fn,assert,match
|
|
144
141
|
```
|
|
145
142
|
|
|
146
143
|
### Types
|
|
@@ -299,6 +296,11 @@ c := some(Car(wheels: 2))
|
|
|
299
296
|
match c
|
|
300
297
|
none -> print("no car")
|
|
301
298
|
some(car) -> car.wheels
|
|
299
|
+
|
|
300
|
+
if Some(car) = c
|
|
301
|
+
printLn("Hello ${car.wheels}")
|
|
302
|
+
else
|
|
303
|
+
printLn("nothing")
|
|
302
304
|
```
|
|
303
305
|
|
|
304
306
|
## result
|
|
@@ -359,10 +361,28 @@ fn main(): result[int] =
|
|
|
359
361
|
err(e) -> printLn("generic error ${e.msg()}")
|
|
360
362
|
ok(0)
|
|
361
363
|
|
|
362
|
-
|
|
364
|
+
fn connect_db(conn_url: str): result[DB] =
|
|
365
|
+
db := postgres_connect(conn_url)?
|
|
363
|
-
|
|
366
|
+
db.exec("select 1")?
|
|
367
|
+
|
|
368
|
+
record DB(conn_url: str)
|
|
369
|
+
|
|
370
|
+
#[error]
|
|
371
|
+
enum DatabaseError
|
|
372
|
+
NotOnline(conn_url: str)
|
|
373
|
+
RowReadFailure(query: str)
|
|
374
|
+
|
|
375
|
+
fn newDB(conn_url: str) result[DB, DatabaseError] =
|
|
376
|
+
db := DB(conn_url: str)
|
|
377
|
+
online := db.check()?
|
|
378
|
+
if !online
|
|
379
|
+
err(DatabaseError(conn_url))
|
|
364
|
-
else
|
|
380
|
+
else
|
|
381
|
+
db
|
|
382
|
+
|
|
383
|
+
fn db_select(conn_url: str): result[unit, DatabaseError] =
|
|
384
|
+
db := newDB(conn_url)
|
|
365
|
-
|
|
385
|
+
db.exec("select 1")?
|
|
366
386
|
```
|
|
367
387
|
|
|
368
388
|
**constants**
|
std/json.mi
CHANGED
|
@@ -2,6 +2,15 @@ module std/json
|
|
|
2
2
|
|
|
3
3
|
type Json = Map[str, Json] | List[Json] | int | float | str | bool | nil
|
|
4
4
|
|
|
5
|
+
enum Json =
|
|
6
|
+
| Object(Map[str, Json])
|
|
7
|
+
| Array(List[Json])
|
|
8
|
+
| Int(int)
|
|
9
|
+
| Float(float)
|
|
10
|
+
| String(str)
|
|
11
|
+
| Bool(bool)
|
|
12
|
+
| Nil
|
|
13
|
+
|
|
5
14
|
fn Json.parse(src: str | Buffer | IO): Json | ParseError =
|
|
6
15
|
JsonParser.withSrc(src).parse()
|
|
7
16
|
|