Enumerate valid Brainf**k programs Golunar/Unary is a way to encode all valid Brainfuck programs, but it is not an enumeration, since most natural numbers do not correspond to a valid program.
For the purpose of this challenge, assume a doubly infinite tape and no comments, i.e., a Brainfuck program is valid if and only if it consists solely of the characters &lt;&gt;+-.,[] and all left and right brackets match.
For example, the empty program, ,[+][-]., [&gt;+&lt;[--].] and +[+[+][+[+]+]+]+. are valid Brainfuck programs, while ][, and a[] are not.
Task
Write a program or function that accepts a valid Brainfuck program as input and returns a natural number (1, 2, 3, …), with the following constraints:

The generated output must be different for all valid Brainfuck programs.

For every natural number n, there must be a valid Brainfuck program that, when provided as input, generates the output n.


Additional rules

Given a Brainfuck program of 100 or less bytes, your program or function must finish within one minute.
This means that you cannot iterate over all valid Brainfuck programs until you match the input.

Standard code-golf rules apply.
Python 3, 443 158 155 154 134 131 128 124 117 116 115 bytes



c=d=C=D=0
for e in input():v='[&lt;&gt;,.-+]'.find(e);d=d*8+v;c+=c&lt;0&lt;6&lt;v;c-=d&gt;1&gt;v;C,D=(c,C+1,d,D)[v&gt;6::2]
print(-~D*8**C)


Several bytes thanks to Sp3000 and Mitch Schwartz :D

How this works:

This maps all valid BF programs into all possible, valid or invalid, BF programs, that don't start with a [, in a one-to-one ratio. After that, the new program is simply converted into octal.

Here is the mapping formula:


Separate a BF program into 3 parts. The first part is the largest prefix consisting of only [ characters. The third part is the largest postfix consisting of only ] characters. The second part is the middle.
Dispose of the first part. These can be recomputed later.
Remove all ] brackets in the third part that match [ brackets in the second part. These can also be recomputed later.
Concatenate the second and third parts together.


If you don't understand this explanation, you can find an extended explanation in chat starting here.

For reference, here are the first 20 programs:

1 : 
2 : &lt;
3 : &gt;
4 : ,
5 : .
6 : -
7 : +
8 : []
9 : &lt;[]
10 : &lt;&lt;
11 : &lt;&gt;
12 : &lt;,
13 : &lt;.
14 : &lt;-
15 : &lt;+
16 : [&lt;]
17 : &gt;[]
18 : &gt;&lt;
19 : &gt;&gt;
20 : &gt;,


Here are the first 1000 programs: http://pastebin.com/qykBWhmD
Here is the program I used to generate them: http://ideone.com/e8oTVl

Here is Hello, World!:

&gt;&gt;&gt; ++++++++[&gt;++++[&gt;++&gt;+++&gt;+++&gt;+&lt;&lt;&lt;&lt;-]&gt;+&gt;+&gt;-&gt;&gt;+[&lt;]&lt;-]&gt;&gt;.&gt;---.+++++++..+++.&gt;&gt;.&lt;-.&lt;.+++.------.--------.&gt;&gt;+.&gt;++.
457711481836430915510337664562435564418569135809989841510260388418118348571803953323858180392373