1 module bf_analyzer;
2 
3 import bf_parser : RepeatedToken, BFTokenEnum;
4 
5 //struct AnalyserResult {
6 //	uint numberOfCells;
7 //}
8 /+ TODO fix the code below to actually return sane numbers
9   AKA make it loop-aware
10 uint countCells(const RepeatedToken[] programm) {
11 	int numberOfCells = 1;
12 
13 	foreach(rt;programm) with(BFTokenEnum) {
14 		if (rt.token == IncPtr) {
15 			numberOfCells += rt.count;
16 		} else if (rt.token == DecPtr) {
17 			numberOfCells -= rt.count;
18 		}
19 	}
20 
21 	assert(numberOfCells > 0);
22 	return numberOfCells;
23 }
24 +/
25 
26 const(int) maxNestingLevel(const RepeatedToken[] programm)
27 {
28     int max;
29     int currentNestingLevel;
30 
31     foreach (rt; programm)
32         with (BFTokenEnum)
33         {
34             if (rt.token == LoopBegin)
35             {
36                 currentNestingLevel += rt.count;
37                 if (currentNestingLevel > max)
38                 {
39                     max = currentNestingLevel;
40                 }
41             }
42             else if (rt.token == LoopEnd)
43             {
44                 currentNestingLevel -= rt.count;
45             }
46         }
47 
48     return max;
49 }
50 
51 const(bool) usesInput(const RepeatedToken[] programm) pure
52 {
53     foreach (rt; programm)
54         with (BFTokenEnum)
55         {
56             if (rt.token == InputVal)
57             {
58                 return true;
59             }
60         }
61 
62     return false;
63 }