diff options
author | Mike Buland <mbuland@penny-arcade.com> | 2019-08-07 17:16:48 -0700 |
---|---|---|
committer | Mike Buland <mbuland@penny-arcade.com> | 2019-08-07 17:16:48 -0700 |
commit | 89d6ca2b2f560273e7aa0f0dec4b49cce3c94177 (patch) | |
tree | 7c091027f0adf9920ce4ff32b8d2fb1694519552 | |
parent | ba255eb660c79766dde69c50192fb3183d6bd778 (diff) | |
download | libneural-89d6ca2b2f560273e7aa0f0dec4b49cce3c94177.tar.gz libneural-89d6ca2b2f560273e7aa0f0dec4b49cce3c94177.tar.bz2 libneural-89d6ca2b2f560273e7aa0f0dec4b49cce3c94177.tar.xz libneural-89d6ca2b2f560273e7aa0f0dec4b49cce3c94177.zip |
Spec language implemented.
Diffstat (limited to '')
-rw-r--r-- | src/network.h | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/src/network.h b/src/network.h index e364459..50906a8 100644 --- a/src/network.h +++ b/src/network.h | |||
@@ -2,8 +2,13 @@ | |||
2 | #define NEURAL_NETWORK_H | 2 | #define NEURAL_NETWORK_H |
3 | 3 | ||
4 | #include "neural/node.h" | 4 | #include "neural/node.h" |
5 | #include "neural/column.h" | ||
6 | #include "neural/container.h" | ||
7 | #include "neural/row.h" | ||
8 | #include "neural/neuron.h" | ||
5 | 9 | ||
6 | #include <bu/string.h> | 10 | #include <bu/string.h> |
11 | #include <bu/exceptionbase.h> | ||
7 | 12 | ||
8 | namespace Neural | 13 | namespace Neural |
9 | { | 14 | { |
@@ -24,9 +29,198 @@ namespace Neural | |||
24 | static Network<sigtype> *fromStr( const Bu::String &sCode ) | 29 | static Network<sigtype> *fromStr( const Bu::String &sCode ) |
25 | { | 30 | { |
26 | Network<sigtype> *pNet = new Network<sigtype>(); | 31 | Network<sigtype> *pNet = new Network<sigtype>(); |
32 | Bu::String::const_iterator i = sCode.begin(); | ||
33 | pNet->pRoot = parseNode( i ); | ||
27 | return pNet; | 34 | return pNet; |
28 | } | 35 | } |
29 | 36 | ||
37 | Node<sigtype> *getRoot() | ||
38 | { | ||
39 | return pRoot; | ||
40 | } | ||
41 | |||
42 | private: | ||
43 | // Parser code | ||
44 | enum TokenType | ||
45 | { | ||
46 | tokRow, | ||
47 | tokColumn, | ||
48 | tokNeuron, | ||
49 | tokOpenParen, | ||
50 | tokCloseParen, | ||
51 | tokNumber, | ||
52 | tokComma, | ||
53 | tokEndOfInput | ||
54 | }; | ||
55 | |||
56 | static TokenType nextToken( Bu::String::const_iterator &i, | ||
57 | int32_t &iValue ) | ||
58 | { | ||
59 | Bu::String sTmp; | ||
60 | for(; i; i++ ) | ||
61 | { | ||
62 | switch( *i ) | ||
63 | { | ||
64 | case ' ': | ||
65 | case '\t': | ||
66 | case '\n': | ||
67 | case '\r': | ||
68 | continue; | ||
69 | |||
70 | case ',': | ||
71 | i++; | ||
72 | //Bu::println("Token: tokComma"); | ||
73 | return tokComma; | ||
74 | |||
75 | case '(': | ||
76 | i++; | ||
77 | //Bu::println("Token: tokOpenParen"); | ||
78 | return tokOpenParen; | ||
79 | |||
80 | case ')': | ||
81 | i++; | ||
82 | //Bu::println("Token: tokCloseParen"); | ||
83 | return tokCloseParen; | ||
84 | |||
85 | default: | ||
86 | if( (*i >= 'a' && *i <= 'z') || | ||
87 | (*i >= 'A' && *i <= 'Z') ) | ||
88 | { | ||
89 | for(; i; i++ ) | ||
90 | { | ||
91 | if( (*i >= 'a' && *i <= 'z') || | ||
92 | (*i >= 'A' && *i <= 'Z') ) | ||
93 | { | ||
94 | sTmp += *i; | ||
95 | } | ||
96 | else | ||
97 | { | ||
98 | //Bu::println("Read: '%1'").arg( sTmp ); | ||
99 | sTmp = sTmp.toLower(); | ||
100 | if( sTmp == "row" ) | ||
101 | return tokRow; | ||
102 | if( sTmp == "column" ) | ||
103 | return tokColumn; | ||
104 | if( sTmp == "neuron" ) | ||
105 | return tokNeuron; | ||
106 | throw Bu::ExceptionBase( Bu::String( | ||
107 | "Invalid token discovered in input: %1" | ||
108 | ).arg( sTmp ).end().getStr() ); | ||
109 | |||
110 | } | ||
111 | } | ||
112 | } | ||
113 | else if( *i >= '0' && *i <= '9' ) | ||
114 | { | ||
115 | for(; i; i++ ) | ||
116 | { | ||
117 | if( *i >= '0' && *i <= '9' ) | ||
118 | { | ||
119 | sTmp += *i; | ||
120 | } | ||
121 | else | ||
122 | { | ||
123 | //Bu::println("Read: '%1'").arg( sTmp ); | ||
124 | iValue = strtol( sTmp.getStr(), NULL, 10 ); | ||
125 | return tokNumber; | ||
126 | } | ||
127 | } | ||
128 | } | ||
129 | break; | ||
130 | } | ||
131 | } | ||
132 | return tokEndOfInput; | ||
133 | } | ||
134 | |||
135 | static Node<sigtype> *parseNode( Bu::String::const_iterator &i ) | ||
136 | { | ||
137 | int iValue; | ||
138 | TokenType eTok = nextToken( i, iValue ); | ||
139 | Node<sigtype> *pNode = NULL; | ||
140 | switch( eTok ) | ||
141 | { | ||
142 | case tokColumn: | ||
143 | pNode = new Column<sigtype>(); | ||
144 | parseNodeParams( (Column<sigtype> *)pNode, i ); | ||
145 | break; | ||
146 | |||
147 | case tokRow: | ||
148 | pNode = new Row<sigtype>(); | ||
149 | parseNodeParams( (Row<sigtype> *)pNode, i ); | ||
150 | break; | ||
151 | |||
152 | case tokNeuron: | ||
153 | pNode = new Neuron<sigtype>(); | ||
154 | break; | ||
155 | |||
156 | default: | ||
157 | throw Bu::ExceptionBase( | ||
158 | "Unexpecetd token parsing neural net specifacion."); | ||
159 | } | ||
160 | return pNode; | ||
161 | } | ||
162 | |||
163 | static void parseNodeParams( Container<sigtype> *pParent, | ||
164 | Bu::String::const_iterator &i ) | ||
165 | { | ||
166 | int iValue; | ||
167 | TokenType eTok = nextToken( i, iValue ); | ||
168 | if( eTok != tokOpenParen ) | ||
169 | { | ||
170 | throw Bu::ExceptionBase( | ||
171 | "Unexpected token, expected open paren." | ||
172 | ); | ||
173 | } | ||
174 | for(;;) | ||
175 | { | ||
176 | eTok = nextToken( i, iValue ); | ||
177 | switch( eTok ) | ||
178 | { | ||
179 | case tokColumn: | ||
180 | { | ||
181 | Column<sigtype> *pNode = new Column<sigtype>(); | ||
182 | parseNodeParams( pNode, i ); | ||
183 | pParent->addNode( pNode ); | ||
184 | } | ||
185 | break; | ||
186 | |||
187 | case tokRow: | ||
188 | { | ||
189 | Row<sigtype> *pNode = new Row<sigtype>(); | ||
190 | parseNodeParams( pNode, i ); | ||
191 | pParent->addNode( pNode ); | ||
192 | } | ||
193 | break; | ||
194 | |||
195 | case tokNeuron: | ||
196 | pParent->addNode( new Neuron<sigtype>() ); | ||
197 | break; | ||
198 | |||
199 | case tokNumber: | ||
200 | for( int j = 0; j < iValue; j++ ) | ||
201 | { | ||
202 | pParent->addNode( new Neuron<sigtype>() ); | ||
203 | } | ||
204 | break; | ||
205 | |||
206 | default: | ||
207 | throw Bu::ExceptionBase( | ||
208 | "Unexpecetd token parsing neural net specifacion."); | ||
209 | } | ||
210 | |||
211 | eTok = nextToken( i, iValue ); | ||
212 | if( eTok == tokCloseParen ) | ||
213 | return; | ||
214 | else if( eTok == tokComma ) | ||
215 | continue; | ||
216 | else | ||
217 | throw new Bu::ExceptionBase( | ||
218 | "Unexpected token parsing neural net specification," | ||
219 | " expected comma or close paren."); | ||
220 | } | ||
221 | |||
222 | } | ||
223 | |||
30 | private: | 224 | private: |
31 | Node<sigtype> *pRoot; | 225 | Node<sigtype> *pRoot; |
32 | }; | 226 | }; |