Skip to content

Commit aa01ca5

Browse files
authored
Merge pull request #137 from Defelo/push-wtnqukuqukpw
add more builtins
2 parents ec03f91 + 471ac6b commit aa01ca5

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

src/par/builtin/Builtin.par

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// Nat
22

33
dec Nat.Add : [Nat, Nat] Nat
4+
dec Nat.Sub : [Nat, Int] Nat
45
dec Nat.Mul : [Nat, Nat] Nat
56
dec Nat.Div : [Nat, Nat] Nat
67
dec Nat.Mod : [Nat, Nat] Nat
@@ -307,6 +308,7 @@ dec Os.TraverseDir : [Os.Path] Result<Os.Error, recursive either {
307308
.dir(Os.Path, self) self,
308309
}>
309310

311+
dec Os.Env : List<(Bytes) Bytes>
310312

311313
/// Url
312314

@@ -366,6 +368,7 @@ type Map<k, v> = iterative choice {
366368
}
367369

368370
dec Map.String : [type v] [List<(String) box v>] Map<String, v>
371+
dec Map.Bytes : [type v] [List<(Bytes) box v>] Map<Bytes, v>
369372
dec Map.Int : [type v] [List<(Int) box v>] Map<Int, v>
370373
dec Map.Nat : [type v] [List<(Nat) box v>] Map<Nat, v>
371374

@@ -382,6 +385,7 @@ type BoxMap<k, v> = iterative box choice {
382385
}
383386

384387
dec BoxMap.String : [type v] [List<(String) box v>] BoxMap<String, v>
388+
dec BoxMap.Bytes : [type v] [List<(Bytes) box v>] BoxMap<Bytes, v>
385389
dec BoxMap.Int : [type v] [List<(Int) box v>] BoxMap<Int, v>
386390
dec BoxMap.Nat : [type v] [List<(Nat) box v>] BoxMap<Nat, v>
387391

src/par/builtin/boxmap.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ pub fn external_module() -> Module<Arc<process::Expression<()>>> {
3737
),
3838
|handle| Box::pin(boxmap_new(handle, Handle::string, Handle::provide_string)),
3939
),
40+
// BoxMap.Bytes : [type v] [List<(Bytes) box v>] BoxMap<Bytes, v>
41+
Definition::external(
42+
"Bytes",
43+
Type::forall(
44+
"v",
45+
Type::function(
46+
Type::name(
47+
Some("List"),
48+
"List",
49+
vec![Type::pair(Type::bytes(), Type::box_(Type::var("v")))],
50+
),
51+
Type::name(None, "BoxMap", vec![Type::bytes(), Type::var("v")]),
52+
),
53+
),
54+
|handle| Box::pin(boxmap_new(handle, Handle::bytes, Handle::provide_bytes)),
55+
),
4056
// BoxMap.Int : [type v] [List<(Int) box v>] BoxMap<Int, v>
4157
Definition::external(
4258
"Int",

src/par/builtin/map.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ pub fn external_module() -> Module<Arc<process::Expression<()>>> {
3434
),
3535
|handle| Box::pin(map_new(handle, Handle::string, Handle::provide_string)),
3636
),
37+
// Map.Bytes : [type v] [List<(Bytes) box v>] Map<Bytes, v>
38+
Definition::external(
39+
"Bytes",
40+
Type::forall(
41+
"v",
42+
Type::function(
43+
Type::name(
44+
Some("List"),
45+
"List",
46+
vec![Type::pair(Type::bytes(), Type::box_(Type::var("v")))],
47+
),
48+
Type::name(None, "Map", vec![Type::bytes(), Type::var("v")]),
49+
),
50+
),
51+
|handle| Box::pin(map_new(handle, Handle::bytes, Handle::provide_bytes)),
52+
),
3753
// Map.Int : [type v] [List<(Int) box v>] Map<Int, v>
3854
Definition::external(
3955
"Int",

src/par/builtin/nat.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ pub fn external_module() -> Module<Arc<process::Expression<()>>> {
2222
Type::function(Type::nat(), Type::function(Type::nat(), Type::nat())),
2323
|handle| Box::pin(nat_add(handle)),
2424
),
25+
Definition::external(
26+
"Sub",
27+
Type::function(Type::nat(), Type::function(Type::int(), Type::nat())),
28+
|handle| Box::pin(nat_sub(handle)),
29+
),
2530
Definition::external(
2631
"Mul",
2732
Type::function(Type::nat(), Type::function(Type::nat(), Type::nat())),
@@ -136,6 +141,12 @@ async fn nat_add(mut handle: Handle) {
136141
handle.provide_nat(x + y);
137142
}
138143

144+
async fn nat_sub(mut handle: Handle) {
145+
let x = handle.receive().nat().await;
146+
let y = handle.receive().int().await;
147+
handle.provide_nat((x - y).max(0.into()));
148+
}
149+
139150
async fn nat_mul(mut handle: Handle) {
140151
let x = handle.receive().nat().await;
141152
let y = handle.receive().nat().await;

src/par/builtin/os.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ pub fn external_module() -> Module<std::sync::Arc<process::Expression<()>>> {
156156
),
157157
|handle| Box::pin(os_traverse_dir(handle)),
158158
),
159+
Definition::external(
160+
"Env",
161+
Type::name(
162+
Some("List"),
163+
"List",
164+
vec![Type::pair(Type::bytes(), Type::bytes())],
165+
),
166+
|handle| Box::pin(os_env(handle)),
167+
),
159168
],
160169
}
161170
}
@@ -565,6 +574,17 @@ async fn os_traverse_dir(mut handle: Handle) {
565574
}
566575
}
567576

577+
async fn os_env(mut handle: Handle) {
578+
for (name, value) in std::env::vars_os() {
579+
handle.signal(literal!("item"));
580+
let mut pair = handle.send();
581+
pair.send().provide_bytes(os_to_bytes(&name));
582+
pair.provide_bytes(os_to_bytes(&value));
583+
}
584+
handle.signal(literal!("end"));
585+
handle.break_();
586+
}
587+
568588
async fn pathbuf_from_os_path(mut handle: Handle) -> PathBuf {
569589
handle.signal(literal!("absolute"));
570590
let path_bytes = handle.bytes().await;

0 commit comments

Comments
 (0)