Created
September 7, 2016 16:51
-
-
Save gbluma/59fc74a824600c71883abdd15c484e8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open syntax chips; | |
// the most basic integer summing device | |
chip sum | |
connector pins | |
pin i1: %< int | |
pin i2: %< int | |
pin r1: %> int | |
{ | |
while true do | |
write(pins.r1, | |
pins.i1.read + pins.i2.read | |
); | |
done | |
} | |
// the most basic integer duplicating device | |
chip dup | |
connector pins | |
pin i1 : %< int | |
pin r1 : %> int | |
pin r2 : %> int | |
{ | |
var n: int; | |
while true do | |
n = read(pins.i1); | |
write(pins.r1, n); | |
write(pins.r2, n); | |
done | |
} | |
// the most basic integer printing device | |
// we artificially cap this at 40 iterations | |
chip display | |
connector pins | |
pin i1: %< int | |
{ | |
var n =0; | |
while n <= 40 do | |
println$ read(pins.i1); | |
n = n + 1; | |
done | |
} | |
// a chip to kick off the processing, | |
// it provides the initial values at the start | |
chip initializer | |
connector pins | |
pin fn0 : %> int | |
pin fn1 : %> int | |
{ | |
write(pins.fn0, 0); | |
write(pins.fn1, 1); | |
} | |
// create some copies of the stock duplicators | |
// because we need two of them | |
device dupFn = dup; | |
device dupFn1 = dup; | |
// connect everything together. | |
// believe it or not, this is the heart of the fibonacci | |
// this corresponds to the diagram above. | |
circuit | |
connect dupFn.r1, display.i1 | |
connect dupFn.r2, sum.i1 | |
connect dupFn1.r1, sum.i2 | |
connect dupFn1.r2, dupFn.i1 | |
connect sum.r1, dupFn1.i1 | |
// introduce our initial values to the circuit | |
connect initializer.fn0, dupFn.i1 | |
connect initializer.fn1, dupFn1.i1 | |
endcircuit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ flx -c --static -o fib fib.flx | |
$ time ./fib | |
0 | |
1 | |
1 | |
2 | |
3 | |
5 | |
8 | |
13 | |
21 | |
34 | |
55 | |
89 | |
144 | |
233 | |
377 | |
610 | |
987 | |
1597 | |
2584 | |
4181 | |
6765 | |
10946 | |
17711 | |
28657 | |
46368 | |
75025 | |
121393 | |
196418 | |
317811 | |
514229 | |
832040 | |
1346269 | |
2178309 | |
3524578 | |
5702887 | |
9227465 | |
14930352 | |
24157817 | |
39088169 | |
63245986 | |
102334155 | |
real 0m0.004s | |
user 0m0.002s | |
sys 0m0.001s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment