11using System ;
22using System . Collections . Generic ;
3+ using System . Data ;
34using System . IO ;
45
56namespace CustomShell
@@ -8,6 +9,8 @@ class ScriptInterpreter
89 {
910 MainController main = MainController . controller ;
1011
12+ DataTable dt = new DataTable ( ) ;
13+
1114 string [ ] lines ;
1215 string [ ] file ;
1316 List < NUM > nums = new List < NUM > ( ) ;
@@ -45,14 +48,14 @@ private struct GOTO
4548 private struct IF
4649 {
4750 public string statement ;
51+ public string name ;
4852 public int line ;
4953 public int ? endLine ;
50- public int index ;
5154 }
5255
5356 private struct ENDIF
5457 {
55- public int index ;
58+ public string name ;
5659 public int line ;
5760 public int ? startLine ;
5861 }
@@ -66,7 +69,7 @@ private void ReadScriptFile(string filePath)
6669 {
6770 filePath = main . GetPathType ( filePath ) ;
6871
69- if ( ! filePath . EndsWith ( ".srp " ) )
72+ if ( ! filePath . EndsWith ( ".rls " ) )
7073 return ;
7174
7275 lines = File . ReadAllLines ( filePath ) ;
@@ -79,8 +82,6 @@ private void CreateTokens()
7982 if ( ! lines [ 0 ] . Equals ( "[SCRIPT]" ) )
8083 return ;
8184
82- int ifIndex = 0 ;
83- int endifIndex = 0 ;
8485 for ( int i = 0 ; i < lines . Length ; ++ i )
8586 {
8687 if ( string . IsNullOrWhiteSpace ( lines [ i ] ) ) //Skip all empty lines
@@ -112,12 +113,12 @@ private void CreateTokens()
112113 {
113114 IF iF ;
114115 iF . line = i ;
115- iF . statement = string . Concat ( tokens [ 1 ] , " " , tokens [ 2 ] , " " , tokens [ 3 ] ) ;
116- iF . index = ifIndex ;
116+ iF . name = tokens [ 1 ] ;
117+ iF . statement = string . Concat ( tokens [ 3 ] , " " , tokens [ 4 ] , " " , tokens [ 5 ] ) ;
117118 iF . endLine = null ;
118119 for ( int x = 0 ; x < endifs . Count ; ++ x ) //Check if an if statement can be paired to an end line
119120 {
120- if ( iF . index == endifs [ x ] . index )
121+ if ( iF . name == endifs [ x ] . name )
121122 iF . endLine = endifs [ x ] . line ;
122123 }
123124
@@ -127,20 +128,21 @@ private void CreateTokens()
127128 else if ( lines [ i ] . Contains ( "endif" ) )
128129 {
129130 ENDIF endif ;
131+ endif . name = tokens [ 1 ] ;
130132 endif . line = i ;
131- endif . index = endifIndex ;
132133 endif . startLine = null ;
133134 for ( int x = 0 ; x < ifs . Count ; ++ x )
134135 {
135- if ( endif . index == ifs [ x ] . index )
136+ if ( tokens [ 1 ] == ifs [ x ] . name )
136137 {
137138 endif . startLine = ifs [ x ] . line ;
138- ifs [ x ] = new IF { endLine = endif . line , index = ifs [ x ] . index , line = ifs [ x ] . line , statement = ifs [ x ] . statement } ; //Create a new copy of the if struct with a modified end line
139+ ifs [ x ] = new IF { endLine = endif . line , line = ifs [ x ] . line , name = ifs [ x ] . name , statement = ifs [ x ] . statement } ; //Create a new copy of the if struct with a modified end line
139140 }
140141 }
141142 endifs . Add ( endif ) ;
142143 }
143144 }
145+ HandleComments ( ) ;
144146 InterpretTokens ( ) ;
145147 }
146148
@@ -153,6 +155,7 @@ private void InterpretTokens()
153155
154156 lines [ i ] = lines [ i ] . Trim ( ) ;
155157 string [ ] tokens = lines [ i ] . Split ( ) ;
158+ HandleComments ( ) ;
156159
157160 for ( int j = 0 ; j < tokens . Length ; ++ j )
158161 {
@@ -168,7 +171,7 @@ private void InterpretTokens()
168171
169172 lines [ i ] = string . Join ( " " , tokens ) ; //Insert the modified string back into the main file array
170173
171- if ( lines [ i ] . Contains ( "goto" ) )
174+ if ( lines [ i ] . StartsWith ( "goto" ) )
172175 {
173176 for ( int x = 0 ; x < labels . Count ; ++ x )
174177 {
@@ -182,17 +185,23 @@ private void InterpretTokens()
182185 {
183186 for ( int z = 0 ; z < ifs . Count ; ++ z )
184187 {
185- if ( ifs [ z ] . line == i )
188+ if ( ifs [ z ] . line == i && ifs [ z ] . name == tokens [ 1 ] )
186189 {
187- i = ( int ) ifs [ z ] . endLine - 1 ;
190+ int l = GetEndifIndex ( tokens [ 1 ] ) ;
191+ i = endifs [ l ] . line ;
188192 continue ;
189193 }
190194 }
191195 }
192196 }
193- else if ( lines [ i ] . Contains ( "[END]" ) )
197+ else if ( tokens [ 0 ] == "print" )
198+ {
199+ string output = string . Join ( " " , tokens , 1 , tokens . Length - 1 ) ;
200+ main . AddTextToConsole ( output ) ;
201+ }
202+ else if ( lines [ i ] . StartsWith ( "[END]" ) )
194203 return ;
195- else if ( ! lines [ i ] . Contains ( "NUM" ) && ! lines [ i ] . Contains ( "label" ) && ! lines [ i ] . Contains ( "[SCRIPT]" ) && ! lines [ i ] . Contains ( "endif" ) && LineContainsVariable ( lines [ i ] ) == false ) //If line doesn't contain a keyword, run it as a command
204+ else if ( ! lines [ i ] . StartsWith ( "NUM" ) && ! lines [ i ] . StartsWith ( "label" ) && ! lines [ i ] . StartsWith ( "[SCRIPT]" ) && ! lines [ i ] . Contains ( "endif" ) && ! lines [ i ] . StartsWith ( "print ") && LineContainsVariable ( lines [ i ] ) == false ) //If line doesn't contain a keyword, run it as a command
196205 main . RunCommand ( lines [ i ] , true ) ;
197206
198207 CheckForVariableChange ( tokens ) ;
@@ -212,34 +221,53 @@ private bool LineContainsVariable(string line)
212221 return containsVar ;
213222 }
214223
224+ private void HandleComments ( )
225+ {
226+ for ( int i = 0 ; i < lines . Length ; i ++ )
227+ {
228+ if ( lines [ i ] . Contains ( "#" ) )
229+ {
230+ int commentIndex = 0 ;
231+ commentIndex = lines [ i ] . IndexOf ( "#" ) ;
232+
233+ if ( commentIndex == 0 )
234+ file [ i ] = "" ;
235+ else if ( commentIndex > 0 )
236+ file [ i ] = file [ i ] . Substring ( 0 , commentIndex ) ;
237+
238+ file . CopyTo ( lines , 0 ) ;
239+ }
240+ }
241+ }
242+
215243 private bool isValidStatement ( string [ ] statement )
216244 {
217245 bool valid = false ;
218246
219- switch ( statement [ 2 ] )
247+ switch ( statement [ 4 ] )
220248 {
221249 case "==" :
222- if ( statement [ 1 ] == statement [ 3 ] )
250+ if ( statement [ 3 ] == statement [ 5 ] )
223251 valid = true ;
224252 break ;
225253 case "<" :
226- if ( Convert . ToSingle ( statement [ 1 ] ) < Convert . ToSingle ( statement [ 3 ] ) )
254+ if ( Convert . ToSingle ( statement [ 3 ] ) < Convert . ToSingle ( statement [ 5 ] ) )
227255 valid = true ;
228256 break ;
229257 case "<=" :
230- if ( Convert . ToSingle ( statement [ 1 ] ) <= Convert . ToSingle ( statement [ 3 ] ) )
258+ if ( Convert . ToSingle ( statement [ 3 ] ) <= Convert . ToSingle ( statement [ 5 ] ) )
231259 valid = true ;
232260 break ;
233261 case ">" :
234- if ( Convert . ToSingle ( statement [ 1 ] ) > Convert . ToSingle ( statement [ 3 ] ) )
262+ if ( Convert . ToSingle ( statement [ 3 ] ) > Convert . ToSingle ( statement [ 5 ] ) )
235263 valid = true ;
236264 break ;
237265 case ">=" :
238- if ( Convert . ToSingle ( statement [ 1 ] ) >= Convert . ToSingle ( statement [ 3 ] ) )
266+ if ( Convert . ToSingle ( statement [ 3 ] ) >= Convert . ToSingle ( statement [ 5 ] ) )
239267 valid = true ;
240268 break ;
241269 case "!=" :
242- if ( statement [ 1 ] != statement [ 3 ] )
270+ if ( statement [ 3 ] != statement [ 5 ] )
243271 valid = true ;
244272 break ;
245273 default :
@@ -251,51 +279,48 @@ private bool isValidStatement(string[] statement)
251279
252280 private void CheckForVariableChange ( string [ ] tokens )
253281 {
254- for ( int l = 0 ; l < tokens . Length ; ++ l ) //Look for lines where a NUM variable changes in value, only +, -, ++, --
282+ if ( ! IsNumVar ( tokens [ 0 ] ) )
283+ return ;
284+
285+ int numIndex = GetNumIndex ( tokens [ 0 ] ) ;
286+
287+ //Check for var++ and var--
288+ if ( IsNumVar ( tokens [ 0 ] ) && tokens . Length == 1 && tokens [ 0 ] . EndsWith ( "++" ) )
255289 {
256- for ( int j = 0 ; j < nums . Count ; ++ j )
257- {
258- if ( tokens [ j ] . StartsWith ( nums [ j ] . name ) )
259- {
260- if ( tokens . Length >= 2 )
261- {
262- if ( tokens [ j + 1 ] == "+" )
263- {
264- ChangeNUMValue ( nums [ j ] , j , true , Convert . ToSingle ( tokens [ 2 ] ) ) ;
265- }
266- else if ( tokens [ j + 1 ] == "-" )
267- {
268- ChangeNUMValue ( nums [ j ] , j , false , Convert . ToSingle ( tokens [ 2 ] ) ) ;
269- }
270- else if ( tokens [ j + 1 ] == "=" )
271- {
272- ChangeNUMValue ( nums [ j ] , j , false , Convert . ToSingle ( tokens [ 2 ] ) ) ;
273- }
274- return ;
275- }
276- else if ( tokens . Length == 1 )
277- {
278- if ( tokens [ j ] . EndsWith ( "++" ) )
279- {
280- ChangeNUMValue ( nums [ j ] , j , true , 1.0f ) ;
281- }
282- else if ( tokens [ j ] . EndsWith ( "--" ) )
283- {
284- ChangeNUMValue ( nums [ j ] , j , false , 1.0f ) ;
285- }
286- return ;
287- }
288- }
289- }
290+ ChangeNUMValue ( nums [ numIndex ] , numIndex , true , 1.0f ) ;
291+ return ;
292+ }
293+ else if ( IsNumVar ( tokens [ 0 ] ) && tokens . Length == 1 && tokens [ 0 ] . EndsWith ( "--" ) )
294+ {
295+ ChangeNUMValue ( nums [ numIndex ] , numIndex , false , 1.0f ) ;
296+ return ;
297+ }
298+
299+ for ( int i = 0 ; i < tokens . Length ; ++ i ) //Skip first two tokens since they are not part of the new value
300+ {
301+ if ( IsNumVar ( tokens [ i ] ) )
302+ tokens [ i ] = nums [ GetNumIndex ( tokens [ i ] ) ] . value . ToString ( ) ;
290303 }
304+
305+ string equation = string . Empty ;
306+
307+ if ( tokens [ 1 ] == "=" )
308+ equation = string . Join ( " " , tokens , 2 , tokens . Length - 2 ) ;
309+ else
310+ equation = string . Join ( " " , tokens ) ;
311+
312+ float answer = Convert . ToSingle ( dt . Compute ( equation , "" ) ) ;
313+ nums [ numIndex ] = new NUM { name = nums [ numIndex ] . name , value = answer } ;
314+
315+ file . CopyTo ( lines , 0 ) ; //Reset the lines array to default
291316 }
292317
293318 private bool IsNumVar ( string name )
294319 {
295320 bool isVar = false ;
296321 for ( int i = 0 ; i < nums . Count ; ++ i )
297322 {
298- if ( name == nums [ i ] . name )
323+ if ( name == nums [ i ] . name || name == nums [ i ] . name + "++" || name == nums [ i ] . name + "--" )
299324 isVar = true ;
300325 }
301326 return isVar ;
@@ -305,14 +330,35 @@ private float GetNumValue(string name)
305330 {
306331 for ( int i = 0 ; i < nums . Count ; ++ i )
307332 {
308- if ( name == nums [ i ] . name )
309- {
333+ if ( name == nums [ i ] . name || name == nums [ i ] . name + "++" || name == nums [ i ] . name + "--" )
310334 return nums [ i ] . value ;
311- }
312335 }
313336 return 0.0f ; //This should never be reached since a check for IsNumVar should always have happened before calling this
314337 }
315338
339+ private int GetNumIndex ( string name )
340+ {
341+ int index = 0 ;
342+ for ( int i = 0 ; i < nums . Count ; ++ i )
343+ {
344+ if ( name == nums [ i ] . name )
345+ index = i ;
346+ }
347+
348+ return index ;
349+ }
350+
351+ private int GetEndifIndex ( string name )
352+ {
353+ int index = 0 ;
354+ for ( int i = 0 ; i < endifs . Count ; ++ i )
355+ {
356+ if ( endifs [ i ] . name == name )
357+ index = i ;
358+ }
359+ return index ;
360+ }
361+
316362 private void ChangeNUMValue ( NUM number , int index , bool add , float change )
317363 {
318364 float newValue = 0.0f ;
0 commit comments