-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadder3.bismuth
executable file
·78 lines (63 loc) · 1.69 KB
/
adder3.bismuth
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
extern func printf(str s, ...) : int;
define program :: c : Channel<-int> {
var addStream := exec BinaryCounter, s1, s2 := exec toBinary, printer := exec toDecimal;
s1.send(120);
s2.send(75);
addStream.send(s1);
addStream.send(s2);
printer.send(addStream);
c.send(0);
}
define func XOR (boolean a, boolean b) : boolean {
return (a && !b) || (!a && b);
}
define BinaryCounter :: c : Channel<+Channel<!+boolean>; +Channel<!+boolean>;?-boolean> {
var i1 := c.recv(), i2 := c.recv();
boolean carry := false;
# Only one or the other of the accepts will end up running
accept(i1) {
boolean val := i1.recv();
boolean looped2, val2 := false;
acceptWhile(i2, !looped2) {val2 := i2.recv(); looped2 := true;}
boolean xor := XOR(val, val2);
boolean sum := XOR(xor, carry);
boolean car := (xor && carry) || (val && val2);
more(c);
c.send(sum);
carry := car;
}
accept(i2) {
boolean val := i2.recv();
more(c);
c.send(XOR(val, carry));
carry := val && carry;
}
if carry {
more(c);
c.send(carry);
}
weaken(c);
}
define toBinary :: c : Channel<+int;?-boolean> {
int n := c.recv();
while n > 0 {
more(c)
if n % 2 == 1 {
c.send(true);
} else {
c.send(false);
}
n := n / 2;
}
weaken(c);
}
define toDecimal :: c : Channel<+Channel<!+boolean>> {
var a := c.recv(), dec_val := 0, base := 1;
accept(a) {
if a.recv() {
dec_val := dec_val + base;
}
base := base * 2;
}
printf("%u\n", dec_val);
}