Debian Codegraph @

terps/frankendrift/FrankenDrift.Adrift/clsVariable.vb

raw
1 Imports FrankenDrift.Glue
2
3 Public Class clsVariable
4 Inherits clsItem
5
6 Private sName As String
7 Private iIntValue(0) As Integer
8 Private sStringValue(0) As String
9 Private iLength As Integer = 1
10
11 Public Enum VariableTypeEnum
12 Numeric
13 Text
14 End Enum
15 Private eVariableType As VariableTypeEnum
16
17 Public Property Name() As String
18 Get
19 Return sName
20 End Get
21 Set(ByVal Value As String)
22 sName = Value
23 End Set
24 End Property
25
26 Public Overrides ReadOnly Property Clone() As clsItem
27 Get
28 Return CType(Me.MemberwiseClone, clsVariable)
29 End Get
30 End Property
31
32 Public Property Length() As Integer
33 Get
34 Return iLength
35 End Get
36 Set(ByVal value As Integer)
37 If Type = VariableTypeEnum.Numeric Then
38 ReDim iIntValue(value - 1)
39 ReDim sStringValue(0)
40 sStringValue(0) = ""
41 Else
42 ReDim sStringValue(value - 1)
43 Erase iIntValue
44 End If
45 iLength = value
46 End Set
47 End Property
48
49
50 Public Property IntValue(Optional ByVal iIndex As Integer = 1) As Integer
51 Get
52 If iIndex > 0 AndAlso iIntValue IsNot Nothing AndAlso iIndex < iIntValue.Length + 1 Then
53 Return iIntValue(iIndex - 1) ' 1 - based indices
54 Else
55 If iIntValue IsNot Nothing Then
56 ErrMsg("Attempting to read index " & iIndex & " outside bounds of array of variable " & Me.Name & IIf(iIntValue.Length > 1, "(" & iIntValue.Length & ")", "").ToString)
57 Else
58 ErrMsg("Attempting to read index " & iIndex & " outside bounds of array of variable (0)")
59 End If
60 Return 0
61 End If
62 End Get
63 Set(ByVal Value As Integer)
64 If iIndex > 0 AndAlso iIndex < iIntValue.Length + 1 Then
65 iIntValue(iIndex - 1) = Value
66 Else
67 ErrMsg("Attempting to set index " & iIndex & " outside bounds of array of variable " & Me.Name & IIf(iIntValue.Length > 1, "(" & iIntValue.Length & ")", "").ToString)
68 End If
69 End Set
70 End Property
71
72
73 Friend Property StringValue(Optional ByVal iIndex As Integer = 1) As String
74 Get
75 If iIndex > 0 AndAlso iIndex < sStringValue.Length + 1 Then
76 Return sStringValue(iIndex - 1)
77 Else
78 ErrMsg("Attempting to read index " & iIndex & " outside bounds of array of variable " & Me.Name & IIf(sStringValue.Length > 1, "(" & sStringValue.Length & ")", "").ToString)
79 Return ""
80 End If
81 End Get
82 Set(ByVal Value As String)
83 If iIndex > 0 AndAlso iIndex < sStringValue.Length + 1 Then
84 sStringValue(iIndex - 1) = Value
85 ElseIf iIndex <= 0 Then
86 ErrMsg("Attempting to set index " & iIndex & " outside bounds of array of variable " & Me.Name & IIf(sStringValue.Length > 1, "(" & sStringValue.Length & ")", "").ToString & ". ADRIFT arrays start at 1, not 0.")
87 Else
88 ErrMsg("Attempting to set index " & iIndex & " outside bounds of array of variable " & Me.Name & IIf(sStringValue.Length > 1, "(" & sStringValue.Length & ")", "").ToString)
89 End If
90 End Set
91 End Property
92
93 Public Property Type() As VariableTypeEnum
94 Get
95 Return eVariableType
96 End Get
97 Set(ByVal Value As VariableTypeEnum)
98 eVariableType = Value
99 Select Case Value
100 Case VariableTypeEnum.Numeric
101 ReDim iIntValue(0)
102 ReDim sStringValue(0)
103 sStringValue(0) = ""
104 Case VariableTypeEnum.Text
105 ReDim sStringValue(0)
106 Erase iIntValue
107 End Select
108 End Set
109 End Property
110
111
112 #Region "Expressions"
113
114 Structure TokenType
115 Dim Token As String
116 Dim Value As String
117 Dim Left As Integer
118 Dim Right As Integer
119 End Structure
120 Private Token() As TokenType
121 Private iToken As Integer
122
123 Private sParseString As String
124 Private sParseToken As String
125
126
127 Private Function GetToken(ByRef text As String) As String
128
129 Dim n As Integer
130
131 GetToken = Nothing
132
133 Select Case sLeft(text, 1)
134 Case "*", "/", "+", "-", "^", "(", ")", ","
135 GetToken = sLeft(text, 1)
136 sParseString = sRight(sParseString, sParseString.Length - 1)
137 Exit Function
138 Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" '0 To 9
139 n = 1
140 Dim bDP As Boolean = False
141 While IsNumeric(sMid(text, n, 1)) OrElse (Not bDP AndAlso sMid(text, n, 1) = ".")
142 If sMid(text, n, 1) = "." Then bDP = True
143 n = n + 1
144 End While
145 n = n - 1
146 GetToken = sLeft(text, n)
147 sParseString = sRight(sParseString, sParseString.Length - n)
148 Case Chr(34)
149 If sInstr(2, text, Chr(34)) > 0 Then
150 GetToken = "vlu" & sMid(text, 2, sInstr(2, text, Chr(34)) - 2)
151 sParseString = sRight(sParseString, sParseString.Length - GetToken.Length + 1)
152 End If
153 Case "'"
154 If sInstr(2, text, "'") > 0 Then
155 GetToken = "vlu" & sMid(text, 2, sInstr(2, text, "'") - 2)
156 sParseString = sRight(sParseString, sParseString.Length - GetToken.Length + 1)
157 End If
158 Case "%"
159 For Each var As clsVariable In Adventure.htblVariables.Values
160 If var.Length > 1 AndAlso text.ToLower.StartsWith("%" & var.Name.ToLower & "[") Then
161 Dim sArgs As String = GetFunctionArgs(text.Substring(InStr(text.ToLower, "%" & var.Name.ToLower & "[") + var.Name.Length))
162 GetToken = "var-" & var.Key & "[" & ReplaceFunctions(sArgs) & "]"
163 sParseString = sParseString.Replace("%" & var.Name & "[" & sArgs & "]%", "")
164 Exit Function
165 End If
166 If sLeft(text, var.Name.Length + 2) = "%" & var.Name & "%" Then
167 GetToken = "var-" & var.Key
168 sParseString = sRight(sParseString, sParseString.Length - var.Name.Length - 2)
169 Exit Function
170 End If
171 Next
172 If sLeft(text.ToLower, 6) = "%loop%" Then
173 GetToken = "lop"
174 sParseString = sRight(sParseString, sParseString.Length - 6)
175 End If
176 If sLeft(text.ToLower, 10) = "%maxscore%" Then
177 GetToken = "mxs"
178 sParseString = sRight(sParseString, sParseString.Length - 10)
179 End If
180 If sLeft(text.ToLower, 8) = "%number%" Then
181 GetToken = "num"
182 sParseString = sRight(sParseString, sParseString.Length - 8)
183 End If
184 If sLeft(text.ToLower, 7) = "%score%" Then
185 GetToken = "sco"
186 sParseString = sRight(sParseString, sParseString.Length - 7)
187 End If
188 If sLeft(text.ToLower, 6) = "%time%" Then
189 GetToken = "tim"
190 sParseString = sRight(sParseString, sParseString.Length - 6)
191 End If
192 If sLeft(text.ToLower, 7) = "%turns%" Then
193 GetToken = "trn"
194 sParseString = sRight(sParseString, sParseString.Length - 7)
195 End If
196 If sLeft(text.ToLower, 9) = "%version%" Then
197 GetToken = "ver"
198 sParseString = sRight(sParseString, sParseString.Length - 9)
199 End If
200 If sLeft(text.ToLower, 6) = "%text%" Then
201 GetToken = "txt"
202 sParseString = sRight(sParseString, sParseString.Length - 6)
203 End If
204 If sLeft(text.ToLower, 8) = "%player%" Then
205 GetToken = "ply"
206 sParseString = sRight(sParseString, sParseString.Length - 8)
207 End If
208 For Each fn As String In FunctionNames()
209 If LCase(text).StartsWith("%" & fn.ToLower & "[") AndAlso text.ToLower.Contains("]%") Then
210 Dim sArgs As String = GetFunctionArgs(text.Substring(InStr(text.ToLower, "%" & fn & "[") + fn.Length + 1))
211 GetToken = "fun-%" & fn & "[" & sArgs & "]%"
212 sParseString = sParseString.Replace(GetToken.Substring(4), "")
213 Exit Function
214 End If
215 Next
216 For Each ref As String In ReferenceNames()
217 If sLeft(text.ToLower, ref.Length) = ref.ToLower Then
218 GetToken = "ref-" & ref
219 sParseString = sRight(sParseString, sParseString.Length - ref.Length)
220 Exit For
221 End If
222 Next
223
224 Case "|"
225 If sLeft(text, 2) = "||" Then
226 GetToken = "OR"
227 sParseString = sRight(sParseString, sParseString.Length - 2)
228 Exit Function
229 End If
230 GetToken = "OR"
231 sParseString = sRight(sParseString, sParseString.Length - 1)
232 Case "&"
233 If sLeft(text, 2) = "&&" Then
234 GetToken = "AND"
235 sParseString = sRight(sParseString, sParseString.Length - 2)
236 Exit Function
237 End If
238 GetToken = "AND"
239 sParseString = sRight(sParseString, sParseString.Length - 1)
240 Case "="
241 If sLeft(text, 2) = "==" Then
242 GetToken = "EQ"
243 sParseString = sRight(sParseString, sParseString.Length - 2)
244 Exit Function
245 End If
246 GetToken = "EQ"
247 sParseString = sRight(sParseString, sParseString.Length - 1)
248 Case "<"
249 If sLeft(text, 2) = "<>" Then
250 GetToken = "NE"
251 sParseString = sRight(sParseString, sParseString.Length - 2)
252 Exit Function
253 End If
254 If sLeft(text, 2) = "<=" Then
255 GetToken = "LE"
256 sParseString = sRight(sParseString, sParseString.Length - 2)
257 Exit Function
258 End If
259 GetToken = "LT"
260 sParseString = sRight(sParseString, sParseString.Length - 1)
261 Case ">"
262 If sLeft(text, 2) = ">=" Then
263 GetToken = "GE"
264 sParseString = sRight(sParseString, sParseString.Length - 2)
265 Exit Function
266 End If
267 GetToken = "GT"
268 sParseString = sRight(sParseString, sParseString.Length - 1)
269 Case "!"
270 If sLeft(text, 2) = "!=" Then
271 GetToken = "NE"
272 sParseString = sRight(sParseString, sParseString.Length - 2)
273 Exit Function
274 End If
275 GetToken = "!"
276 sParseString = sRight(sParseString, sParseString.Length - 1)
277 Case "a", "A"
278 If LCase(sLeft(text, 3)) = "and" Then
279 GetToken = "AND"
280 sParseString = sRight(sParseString, sParseString.Length - 3)
281 Exit Function
282 End If
283 If LCase(sLeft(text, 3)) = "abs" Then
284 GetToken = "abs"
285 sParseString = sRight(sParseString, sParseString.Length - 3)
286 Exit Function
287 End If
288 Case "e", "E"
289 If LCase(sLeft(text, 6)) = "either" Then
290 GetToken = "either"
291 sParseString = sRight(sParseString, sParseString.Length - 6)
292 Exit Function
293 End If
294 Case "i", "I"
295 If LCase(sLeft(text, 2)) = "if" Then
296 GetToken = "if"
297 sParseString = sRight(sParseString, sParseString.Length - 2)
298 Exit Function
299 End If
300 If LCase(sLeft(text, 5)) = "instr" Then
301 GetToken = "ist"
302 sParseString = sRight(sParseString, sParseString.Length - 5)
303 Exit Function
304 End If
305 Case "l", "L"
306 If LCase(sLeft(text, 5)) = "lower" Or LCase(sLeft(text, 5)) = "lcase" Then
307 GetToken = "lwr"
308 sParseString = sRight(sParseString, sParseString.Length - 5)
309 Exit Function
310 End If
311 If LCase(sLeft(text, 4)) = "left" Then
312 GetToken = "lft"
313 sParseString = sRight(sParseString, sParseString.Length - 4)
314 Exit Function
315 End If
316 If LCase(sLeft(text, 3)) = "len" Then
317 GetToken = "len"
318 sParseString = sRight(sParseString, sParseString.Length - 3)
319 Exit Function
320 End If
321 Case "m", "M"
322 If LCase(sLeft(text, 3)) = "max" Then
323 GetToken = "max"
324 sParseString = sRight(sParseString, sParseString.Length - 3)
325 Exit Function
326 End If
327 If LCase(sLeft(text, 3)) = "min" Then
328 GetToken = "min"
329 sParseString = sRight(sParseString, sParseString.Length - 3)
330 Exit Function
331 End If
332 If LCase(sLeft(text, 3)) = "mod" Then
333 GetToken = "mod"
334 sParseString = sRight(sParseString, sParseString.Length - 3)
335 Exit Function
336 End If
337 If LCase(sLeft(text, 3)) = "mid" Then
338 GetToken = "mid"
339 sParseString = sRight(sParseString, sParseString.Length - 3)
340 Exit Function
341 End If
342 Case "o", "O"
343 If LCase(sLeft(text, 2)) = "or" Then
344 GetToken = "OR"
345 sParseString = sRight(sParseString, sParseString.Length - 2)
346 Exit Function
347 End If
348 If LCase(sLeft(text, 5)) = "oneof" Then
349 GetToken = "oneof"
350 sParseString = sRight(sParseString, sParseString.Length - 5)
351 Exit Function
352 End If
353 Case "p", "P"
354 If LCase(sLeft(text, 6)) = "proper" Or LCase(sLeft(text, 5)) = "pcase" Then
355 GetToken = "ppr"
356 sParseString = sRight(sParseString, sParseString.Length - 5)
357 Exit Function
358 End If
359 Case "r", "R"
360 If LCase(sLeft(text, 4)) = "rand" Then
361 GetToken = "rand"
362 sParseString = sRight(sParseString, sParseString.Length - 4)
363 Exit Function
364 End If
365 If LCase(sLeft(text, 7)) = "replace" Then
366 GetToken = "replace"
367 sParseString = sRight(sParseString, sParseString.Length - 7)
368 Exit Function
369 End If
370 If LCase(sLeft(text, 5)) = "right" Then
371 GetToken = "rgt"
372 sParseString = sRight(sParseString, sParseString.Length - 5)
373 Exit Function
374 End If
375 Case "s", "S"
376 If LCase(sLeft(text, 3)) = "str" Then
377 GetToken = "str"
378 sParseString = sRight(sParseString, sParseString.Length - 3)
379 Exit Function
380 End If
381 Case "u", "U"
382 If LCase(sLeft(text, 5)) = "upper" Or LCase(sLeft(text, 5)) = "ucase" Then
383 GetToken = "upr"
384 sParseString = sRight(sParseString, sParseString.Length - 5)
385 Exit Function
386 End If
387 If LCase(sLeft(text, 5)) = "urand" Then
388 GetToken = "urand"
389 sParseString = sRight(sParseString, sParseString.Length - 5)
390 Exit Function
391 End If
392 Case "v", "V"
393 If LCase(sLeft(text, 3)) = "val" Then
394 GetToken = "val"
395 sParseString = sRight(sParseString, sParseString.Length - 3)
396 Exit Function
397 End If
398 Case Else
399
400 End Select
401
402 If GetToken Is Nothing Then GetToken = ""
403 If GetToken = "" Then
404 ' Treat the token like a string
405 For i As Integer = 0 To sParseString.Length - 1
406 Select Case sParseString(i)
407 Case "A"c To "Z"c, "a"c To "z"c, "0"c To "9"c
408 GetToken &= sParseString(i)
409 Case Else
410 sParseString = sRight(sParseString, sParseString.Length - GetToken.Length)
411 GetToken = "vlu" & GetToken
412 Exit Function
413 End Select
414 Next
415 End If
416 End Function
417
418
419 Private Sub PrintTokens(Optional ByVal CurrentToken As TokenType = Nothing)
420 Dim iToken As Integer = GetFirstToken() '0
421 While iToken <> -1
422 iToken = PrintToken(iToken, CurrentToken)
423 End While
424 Debug.WriteLine("")
425 End Sub
426
427 Private Function PrintToken(ByVal iToken As Integer, Optional ByVal CurrentToken As TokenType = Nothing) As Integer
428 With Token(iToken)
429 Dim bCurrent As Boolean = .Left = CurrentToken.Left AndAlso .Right = CurrentToken.Right AndAlso .Token = CurrentToken.Token
430 Debug.Write("[" & If(bCurrent, "#", "").ToString & iToken & If(bCurrent, "#", "").ToString & ": " & .Token & "/" & .Value & "] ")
431 Return .Right
432 End With
433 End Function
434
435 Private Function GetFirstToken() As Integer
436 For i As Integer = Token.Length - 1 To 0 Step -1
437 If Token(i).Left = -1 Then Return i
438 Next
439 End Function
440
441 Private Function NoRepeatRandom(ByVal iMin As Integer, ByVal iMax As Integer) As Integer
442 Dim sDictKey As String = iMin & "-" & iMax
443
444 If Not Adventure.dictRandValues.ContainsKey(sDictKey) Then Adventure.dictRandValues.Add(sDictKey, New List(Of Integer))
445
446 Dim lValues As List(Of Integer) = Adventure.dictRandValues(sDictKey)
447 If lValues.Count = 0 Then
448 ' Create a new random list of values
449 For i As Integer = iMin To iMax
450 lValues.Insert(Random(lValues.Count), i)
451 Next
452 End If
453
454 ' Pick a random element from the list
455 Dim iIndex As Integer = Random(lValues.Count - 1)
456 NoRepeatRandom = lValues(iIndex)
457 lValues.RemoveAt(iIndex)
458 End Function
459
460 Public Sub SetToExpression(ByVal sExpression As String, Optional ByVal iIndex As Integer = 1, Optional ByVal bThrowExceptionOnBadExpression As Boolean = False)
461 Try
462 sExpression = sExpression.Replace("%Loop%", iIndex.ToString)
463 sExpression = ReplaceFunctions(sExpression, True)
464 ' Otherwise IF(%object%="Object1", "a", "b") becomes IF(Object1="Object1"...
465 ' But it is needed so we can evaluate expressions like %object%.Weight+%object%.Children.Weight
466
467 ' Remove spaces
468 sExpression = sExpression.Replace("\""", "~@&#~")
469
470 sParseString = StripRedundantSpaces(sExpression) ' Replace(expression, " ", "")
471 Dim sPrevious As String = "972398"
472
473 ' Set up initial tokens
474 iToken = 0
475 While sParseString <> "" AndAlso sParseString <> sPrevious
476 sPrevious = sParseString
477 ReDim Preserve Token(iToken + 1)
478 If iToken > 0 Then Token(iToken - 1).Right = iToken
479 With Token(iToken)
480 .Token = GetToken(sParseString)
481 If .Token IsNot Nothing Then .Token = .Token.Replace("~@&#~", """")
482 .Value = "0"
483 .Left = -1
484 .Right = -1
485 If iToken > 0 Then .Left = iToken - 1
486 End With
487 iToken = iToken + 1
488 End While
489
490 Dim n As Integer
491
492 If iToken = 0 Then Exit Sub
493
494 ' Set initial tokens to normal ones
495 For n = 0 To iToken - 1
496 With Token(n)
497 If IsNumeric(.Token) Then
498 .Value = .Token
499 .Token = "expr"
500 Else
501 Select Case .Token
502 Case "*", "/", "+", "-", "^", "mod"
503 .Value = .Token
504 .Token = "op"
505 Case "("
506 .Value = .Token
507 .Token = "lp"
508 Case ")"
509 .Value = .Token
510 .Token = "rp"
511 Case ","
512 .Value = .Token
513 .Token = "comma"
514 Case "txt"
515 .Value = ""
516 For Each ref As clsNewReference In UserSession.NewReferences
517 If ref.ReferenceType = ReferencesType.Text Then
518 If ref.Items.Count = 1 Then
519 If ref.Items(0).MatchingPossibilities.Count = 1 Then
520 .Value = ref.Items(0).MatchingPossibilities(0)
521 End If
522 End If
523 End If
524 Next
525 .Token = "expr"
526 Case "min", "max", "if", "rand", "either", "abs", "upr", "lwr", "ppr", "rgt", "lft", "mid", "ist", "len", "val", "str", "replace", "oneof", "urand"
527 .Value = .Token
528 .Token = "funct"
529 Case "EQ", "GT", "LT", "LE", "GE", "NE"
530 .Value = .Token
531 .Token = "TESTOP"
532 Case "AND", "OR"
533 .Value = .Token
534 .Token = "LOGIC"
535 Case "lop"
536 .Value = iIndex.ToString
537 .Token = "expr"
538 Case "num"
539 .Value = ""
540 For Each ref As clsNewReference In UserSession.NewReferences
541 If ref.ReferenceType = ReferencesType.Number Then
542 If ref.Items.Count = 1 Then
543 If ref.Items(0).MatchingPossibilities.Count = 1 Then
544 .Value = ref.Items(0).MatchingPossibilities(0)
545 End If
546 End If
547 End If
548 Next
549 .Token = "expr"
550 Case "ver"
551 .Value = Application.ProductVersion
552 Dim sVer As String() = Application.ProductVersion.Split("."c)
553 .Value = sVer(0) & CInt(sVer(1)).ToString("00") & CInt(sVer(2)).ToString("0000")
554 .Token = "expr"
555 Case "sco"
556 .Value = Adventure.Score.ToString
557 .Token = "expr"
558 Case "mxs"
559 .Value = Adventure.MaxScore.ToString
560 .Token = "expr"
561 Case "ply"
562 .Value = Adventure.Player.Name
563 .Token = "expr"
564 Case "trn"
565 .Value = Adventure.Turns.ToString
566 .Token = "expr"
567 Case "tim"
568 .Value = "0"
569 .Token = "expr"
570 Case Else
571 If sLeft(.Token, 3) = "var" Then
572 Dim sVar As String = .Token.Replace("var-", "")
573 Dim iVarIndex As Integer = 1
574 If sVar.Contains("[") Then
575 Dim sIndex As String = sVar.Substring(InStr(sVar, "["))
576 sIndex = sLeft(sIndex, sIndex.Length - 1)
577 iVarIndex = SafeInt(sIndex)
578 sVar = sVar.Replace("[" & sIndex & "]", "")
579 End If
580 Dim var As clsVariable = Adventure.htblVariables(sVar)
581 If var.Type = VariableTypeEnum.Text Then
582 .Value = var.StringValue(iVarIndex).ToString
583 Else
584 .Value = var.IntValue(iVarIndex).ToString
585 End If
586 .Token = "expr"
587 ElseIf sLeft(.Token, 3) = "vlu" Then
588 .Value = sRight(.Token, Len(.Token) - 3)
589 .Token = "expr"
590 ElseIf sLeft(.Token, 3) = "prv" Then
591 .Value = ReplaceFunctions(.Token.Substring(4))
592 .Token = "expr"
593 ElseIf sLeft(.Token, 3) = "pin" Then
594 .Value = ReplaceFunctions(.Token.Substring(4))
595 .Token = "expr"
596 ElseIf sLeft(.Token, 3) = "pch" Then
597 .Value = ReplaceFunctions(.Token.Substring(4))
598 .Token = "expr"
599 ElseIf sLeft(.Token, 3) = "ref" Then
600 .Value = ReplaceFunctions(.Token.Substring(4))
601 .Token = "expr"
602 ElseIf sLeft(.Token, 4) = "fun-" Then
603 .Value = ReplaceFunctions(.Token.Substring(4))
604 .Token = "expr"
605 Else
606 Throw New Exception("Bad token: " & .Token)
607 End If
608 End Select
609 End If
610 End With
611 Next n
612
613 Dim lefttoken As TokenType, optoken As TokenType, righttoken As TokenType
614 Dim currtoken As TokenType, lptoken As TokenType, rptoken As TokenType, functoken As TokenType
615 Dim op2token As TokenType, right2token As TokenType
616 Dim changed As Boolean
617 Dim badexp As Boolean
618 Dim run As Integer
619
620 currtoken = Token(GetFirstToken)
621 run = 1
622
623 ' Do while more than one token
624 While Not (currtoken.Left = -1 And currtoken.Right = -1)
625 changed = False
626
627 ' If at least two tokens
628 If currtoken.Right <> -1 Then
629 ' If at least three tokens
630 If Token(currtoken.Right).Right <> -1 Then
631
632 If Token(Token(currtoken.Right).Right).Right <> -1 Then
633 ' Four tokens - for abs(a)
634 functoken = currtoken
635 lptoken = Token(currtoken.Right)
636 lefttoken = Token(lptoken.Right)
637 rptoken = Token(lefttoken.Right)
638
639 If functoken.Token = "funct" And lptoken.Token = "lp" And
640 lefttoken.Token = "expr" And rptoken.Token = "rp" Then
641 Select Case functoken.Value
642 Case "abs"
643 addtoken("expr", Math.Abs(CInt(Val(lefttoken.Value))).ToString, functoken.Left, rptoken.Right)
644 Case "upr"
645 addtoken("expr", UCase(lefttoken.Value), functoken.Left, rptoken.Right)
646 Case "lwr"
647 addtoken("expr", LCase(lefttoken.Value), functoken.Left, rptoken.Right)
648 Case "ppr"
649 addtoken("expr", ToProper(lefttoken.Value), functoken.Left, rptoken.Right)
650 Case "len"
651 addtoken("expr", lefttoken.Value.Length.ToString, functoken.Left, rptoken.Right)
652 Case "val"
653 addtoken("expr", SafeInt(Val(lefttoken.Value)).ToString, functoken.Left, rptoken.Right)
654 Case "str"
655 addtoken("expr", Str(lefttoken.Value), functoken.Left, rptoken.Right)
656 Case "rand"
657 addtoken("expr", Random(0, SafeInt(lefttoken.Value)).ToString, functoken.Left, rptoken.Right)
658 Case "urand"
659 addtoken("expr", NoRepeatRandom(0, SafeInt(lefttoken.Value)).ToString, functoken.Left, rptoken.Right)
660 End Select
661 If functoken.Left <> -1 Then Token(functoken.Left).Right = iToken - 1
662 If rptoken.Right <> -1 Then Token(rptoken.Right).Left = iToken - 1
663 changed = True
664 End If
665
666 If Token(Token(Token(currtoken.Right).Right).Right).Right <> -1 Then
667 If Token(Token(Token(Token(currtoken.Right).Right).Right).Right).Right <> -1 Then
668
669 If Token(Token(Token(Token(Token(currtoken.Right).Right).Right).Right).Right).Right <> -1 Then
670 If Token(Token(Token(Token(Token(Token(currtoken.Right).Right).Right).Right).Right).Right).Right <> -1 Then
671 ' Eight tokens - for if(test, a, b)
672 functoken = currtoken
673 lptoken = Token(currtoken.Right)
674 lefttoken = Token(lptoken.Right)
675 optoken = Token(lefttoken.Right)
676 righttoken = Token(optoken.Right)
677 op2token = Token(righttoken.Right)
678 right2token = Token(op2token.Right)
679 rptoken = Token(right2token.Right)
680
681 If functoken.Token = "funct" AndAlso lptoken.Token = "lp" AndAlso (lefttoken.Token = "test" OrElse lefttoken.Token = "expr") _
682 AndAlso optoken.Token = "comma" AndAlso righttoken.Token = "expr" AndAlso op2token.Token = "comma" _
683 AndAlso right2token.Token = "expr" AndAlso rptoken.Token = "rp" Then
684
685 Select Case functoken.Value
686 Case "if"
687 If Val(lefttoken.Value) > 0 Then ' True
688 addtoken("expr", righttoken.Value, functoken.Left, rptoken.Right)
689 Else ' False
690 addtoken("expr", right2token.Value, functoken.Left, rptoken.Right)
691 End If
692 End Select
693 If functoken.Left <> -1 Then Token(functoken.Left).Right = iToken - 1
694 If rptoken.Right <> -1 Then Token(rptoken.Right).Left = iToken - 1
695 changed = True
696 End If
697
698 If functoken.Token = "funct" And lptoken.Token = "lp" And lefttoken.Token = "expr" _
699 And optoken.Token = "comma" And righttoken.Token = "expr" And op2token.Token = "comma" _
700 And right2token.Token = "expr" And rptoken.Token = "rp" Then
701
702 Select Case functoken.Value
703 Case "mid"
704 addtoken("expr", sMid(lefttoken.Value, CInt(Val(righttoken.Value)), CInt(Val(right2token.Value))), functoken.Left, rptoken.Right)
705 Case "replace"
706 addtoken("expr", Replace(lefttoken.Value, righttoken.Value, right2token.Value), functoken.Left, rptoken.Right)
707 End Select
708 If functoken.Left <> -1 Then Token(functoken.Left).Right = iToken - 1
709 If rptoken.Right <> -1 Then Token(rptoken.Right).Left = iToken - 1
710 changed = True
711 End If
712
713 End If
714 End If
715
716 ' Six tokens - for funct(a,b)
717 functoken = currtoken
718 lptoken = Token(currtoken.Right)
719 lefttoken = Token(lptoken.Right)
720 optoken = Token(lefttoken.Right)
721 righttoken = Token(optoken.Right)
722 rptoken = Token(righttoken.Right)
723
724 If functoken.Token = "funct" And lptoken.Token = "lp" And lefttoken.Token = "expr" _
725 And optoken.Token = "comma" And righttoken.Token = "expr" And rptoken.Token = "rp" Then
726 Select Case functoken.Value
727 Case "max"
728 addtoken("expr", Math.Max(CInt(Val(lefttoken.Value)), CInt(Val(righttoken.Value))).ToString, functoken.Left, rptoken.Right)
729 Case "min"
730 addtoken("expr", Math.Min(CInt(Val(lefttoken.Value)), CInt(Val(righttoken.Value))).ToString, functoken.Left, rptoken.Right)
731 Case "either"
732 If Random(1) = 1 Then
733 addtoken("expr", lefttoken.Value, functoken.Left, rptoken.Right)
734 Else
735 addtoken("expr", righttoken.Value, functoken.Left, rptoken.Right)
736 End If
737 Case "rand"
738 addtoken("expr", Random(SafeInt(lefttoken.Value), SafeInt(righttoken.Value)).ToString, functoken.Left, rptoken.Right)
739 Case "urand"
740 addtoken("expr", NoRepeatRandom(SafeInt(lefttoken.Value), SafeInt(righttoken.Value)).ToString, functoken.Left, rptoken.Right)
741 Case "lft"
742 addtoken("expr", sLeft(lefttoken.Value, CInt(Val(righttoken.Value))), functoken.Left, rptoken.Right)
743 Case "rgt"
744 addtoken("expr", sRight(lefttoken.Value, CInt(Val(righttoken.Value))), functoken.Left, rptoken.Right)
745 Case "ist"
746 addtoken("expr", sInstr(lefttoken.Value, righttoken.Value).ToString, functoken.Left, rptoken.Right)
747 End Select
748 If functoken.Left <> -1 Then Token(functoken.Left).Right = iToken - 1
749 If rptoken.Right <> -1 Then Token(rptoken.Right).Left = iToken - 1
750 changed = True
751 End If
752
753
754
755 If functoken.Token = "funct" And lptoken.Token = "lp" AndAlso lefttoken.Token = "expr" AndAlso functoken.Value = "oneof" Then
756 ' Can have any number of arguments: oneof([expr,])
757
758 Dim iLength As Integer = 1
759 Dim oToken As TokenType = lefttoken
760
761 While oToken.Right <> -1
762 Dim oNext As TokenType = Token(oToken.Right)
763
764 Select Case oNext.Token
765 Case "rp"
766 ' Great, we reached the end of the function. Let's evaluate
767 rptoken = oNext
768 Dim iOneOfIndex As Integer = Random(iLength - 1) + 1
769 Dim oRandom As TokenType = functoken
770 For i As Integer = 1 To iOneOfIndex
771 oRandom = Token(Token(oRandom.Right).Right)
772 Next
773 addtoken("expr", oRandom.Value, functoken.Left, rptoken.Right)
774 If functoken.Left <> -1 Then Token(functoken.Left).Right = iToken - 1
775 If rptoken.Right <> -1 Then Token(rptoken.Right).Left = iToken - 1
776 changed = True
777 Exit While
778 Case "comma"
779 Dim oExpr As TokenType = Token(oNext.Right)
780 If oExpr.Token = "expr" Then
781 iLength += 1
782 oToken = Token(oNext.Right)
783 Else
784 ' Not what we were expecting
785 Exit While
786 End If
787 Case Else
788 ' Ok, not what we were expecting
789 Exit While
790 End Select
791 End While
792
793 End If
794
795
796 End If
797 End If
798 End If
799
800 lefttoken = currtoken
801 optoken = Token(currtoken.Right)
802 righttoken = Token(optoken.Right)
803
804 ' expr op expr
805 If lefttoken.Token = "expr" And optoken.Token = "op" And righttoken.Token = "expr" Then
806 Select Case optoken.Value
807 Case "^"
808 addtoken("expr", (Val(lefttoken.Value) ^ Val(righttoken.Value)).ToString, lefttoken.Left, righttoken.Right)
809 Case "*"
810 addtoken("expr", (Val(lefttoken.Value) * Val(righttoken.Value)).ToString, lefttoken.Left, righttoken.Right)
811 Case "/"
812 addtoken("expr", (Math.Round((Val(lefttoken.Value) / Val(righttoken.Value)), MidpointRounding.AwayFromZero)).ToString, lefttoken.Left, righttoken.Right)
813 Case "+"
814 If IsNumeric(lefttoken.Value) And IsNumeric(righttoken.Value) Then
815 If run = 2 Then addtoken("expr", (Val(lefttoken.Value) + Val(righttoken.Value)).ToString, lefttoken.Left, righttoken.Right)
816 Else
817 addtoken("expr", lefttoken.Value & righttoken.Value, lefttoken.Left, righttoken.Right)
818 run = 2
819 End If
820 Case "-"
821 If run = 2 Then addtoken("expr", (Val(lefttoken.Value) - Val(righttoken.Value)).ToString, lefttoken.Left, righttoken.Right)
822 Case "mod"
823 If run = 2 Then addtoken("expr", (Val(lefttoken.Value) Mod Val(righttoken.Value)).ToString, lefttoken.Left, righttoken.Right)
824
825 End Select
826 If run = 2 Or optoken.Value = "*" Or optoken.Value = "/" Then
827 If lefttoken.Left <> -1 Then Token(lefttoken.Left).Right = iToken - 1
828 If righttoken.Right <> -1 Then Token(righttoken.Right).Left = iToken - 1
829 changed = True
830 End If
831 End If
832
833 If lefttoken.Token = "test" And optoken.Token = "LOGIC" And righttoken.Token = "test" Then
834 Select Case optoken.Value
835 Case "AND"
836 If Val(lefttoken.Value) > 0 And Val(righttoken.Value) > 0 Then
837 addtoken("test", "1", lefttoken.Left, righttoken.Right)
838 Else
839 addtoken("test", "0", lefttoken.Left, righttoken.Right)
840 End If
841 Case "OR"
842 If Val(lefttoken.Value) > 0 Or Val(righttoken.Value) > 0 Then
843 addtoken("test", "1", lefttoken.Left, righttoken.Right)
844 Else
845 addtoken("test", "0", lefttoken.Left, righttoken.Right)
846 End If
847 End Select
848 If lefttoken.Left <> -1 Then Token(lefttoken.Left).Right = iToken - 1
849 If righttoken.Right <> -1 Then Token(righttoken.Right).Left = iToken - 1
850 changed = True
851 End If
852
853 ' expr TESTOP expr
854 ' Do this on run 3 in case we have expr TESTOP expr OP expr, to ensure the expr OP expr reduces to expr first
855 If lefttoken.Token = "expr" And optoken.Token = "TESTOP" And righttoken.Token = "expr" And run = 3 Then
856 Select Case optoken.Value
857 Case "EQ"
858 If IsNumeric(lefttoken.Value) AndAlso IsNumeric(righttoken.Value) Then
859 If Val(lefttoken.Value) = Val(righttoken.Value) Then
860 addtoken("test", "1", lefttoken.Left, righttoken.Right)
861 Else
862 addtoken("test", "0", lefttoken.Left, righttoken.Right)
863 End If
864 Else
865 If lefttoken.Value = righttoken.Value Then
866 addtoken("test", "1", lefttoken.Left, righttoken.Right)
867 Else
868 addtoken("test", "0", lefttoken.Left, righttoken.Right)
869 End If
870 End If
871 Case "NE"
872 If IsNumeric(lefttoken.Value) AndAlso IsNumeric(righttoken.Value) Then
873 If Val(lefttoken.Value) <> Val(righttoken.Value) Then
874 addtoken("test", "1", lefttoken.Left, righttoken.Right)
875 Else
876 addtoken("test", "0", lefttoken.Left, righttoken.Right)
877 End If
878 Else
879 If lefttoken.Value <> righttoken.Value Then
880 addtoken("test", "1", lefttoken.Left, righttoken.Right)
881 Else
882 addtoken("test", "0", lefttoken.Left, righttoken.Right)
883 End If
884 End If
885 Case "GT"
886 If Val(lefttoken.Value) > Val(righttoken.Value) Then
887 addtoken("test", "1", lefttoken.Left, righttoken.Right)
888 Else
889 addtoken("test", "0", lefttoken.Left, righttoken.Right)
890 End If
891 Case "LT"
892 If Val(lefttoken.Value) < Val(righttoken.Value) Then
893 addtoken("test", "1", lefttoken.Left, righttoken.Right)
894 Else
895 addtoken("test", "0", lefttoken.Left, righttoken.Right)
896 End If
897 Case "GE"
898 If Val(lefttoken.Value) >= Val(righttoken.Value) Then
899 addtoken("test", "1", lefttoken.Left, righttoken.Right)
900 Else
901 addtoken("test", "0", lefttoken.Left, righttoken.Right)
902 End If
903 Case "LE"
904 If Val(lefttoken.Value) <= Val(righttoken.Value) Then
905 addtoken("test", "1", lefttoken.Left, righttoken.Right)
906 Else
907 addtoken("test", "0", lefttoken.Left, righttoken.Right)
908 End If
909 End Select
910 If lefttoken.Left <> -1 Then Token(lefttoken.Left).Right = iToken - 1
911 If righttoken.Right <> -1 Then Token(righttoken.Right).Left = iToken - 1
912 changed = True
913 End If
914
915 ' Concatenate text, e.g. "one" & "two"
916 ' Need to be careful that it evaluates late so we don't get: expr = expr AND expr = expr reduced to expr = expr = expr
917 ' Logic should be the last thing to resolve
918 ' Should we somehow check that each expression is a string?
919 If lefttoken.Token = "expr" And optoken.Value = "AND" And righttoken.Token = "expr" And run = 3 Then
920 addtoken("expr", lefttoken.Value & righttoken.Value, lefttoken.Left, righttoken.Right)
921 If lefttoken.Left <> -1 Then Token(lefttoken.Left).Right = iToken - 1
922 If righttoken.Right <> -1 Then Token(righttoken.Right).Left = iToken - 1
923 changed = True
924 End If
925
926 ' ( expr )
927 If lefttoken.Token = "lp" And optoken.Token = "expr" And righttoken.Token = "rp" Then
928 If IsNumeric(optoken.Value) Then
929 addtoken("expr", Val(optoken.Value).ToString, lefttoken.Left, righttoken.Right)
930 Else
931 addtoken("expr", optoken.Value, lefttoken.Left, righttoken.Right)
932 End If
933
934 If lefttoken.Left <> -1 Then Token(lefttoken.Left).Right = iToken - 1
935 If righttoken.Right <> -1 Then Token(righttoken.Right).Left = iToken - 1
936 changed = True
937 End If
938
939 ' ( test )
940 If lefttoken.Token = "lp" And optoken.Token = "test" And righttoken.Token = "rp" Then
941 If IsNumeric(optoken.Value) Then
942 addtoken("test", Val(optoken.Value).ToString, lefttoken.Left, righttoken.Right)
943 Else
944 addtoken("test", optoken.Value, lefttoken.Left, righttoken.Right)
945 End If
946
947 If lefttoken.Left <> -1 Then Token(lefttoken.Left).Right = iToken - 1
948 If righttoken.Right <> -1 Then Token(righttoken.Right).Left = iToken - 1
949 changed = True
950 End If
951
952 End If
953
954 ' Only two tokens
955 optoken = currtoken
956 righttoken = Token(optoken.Right)
957
958 ' op expr
959 If optoken.Token = "op" And righttoken.Token = "expr" Then
960 Select Case optoken.Value
961 Case "-"
962 If run = 2 Then addtoken("expr", (-Val(righttoken.Value)).ToString, optoken.Left, righttoken.Right)
963 Case "+" ' Not really expected, but I guess +X = X
964 If run = 2 Then addtoken("expr", (Val(righttoken.Value)).ToString, optoken.Left, righttoken.Right)
965 End Select
966 If run = 2 Then
967 If optoken.Left <> -1 Then Token(optoken.Left).Right = iToken - 1
968 If righttoken.Right <> -1 Then Token(righttoken.Right).Left = iToken - 1
969 changed = True
970 End If
971
972 End If
973
974 End If
975
976 If changed Then
977 ' Move pointer to far left
978 currtoken = Token(iToken - 1)
979 Dim iBombOut As Integer = 0
980 While currtoken.Left <> -1
981 currtoken = Token(currtoken.Left)
982 iBombOut += 1
983 If iBombOut = 5000 Then
984 badexp = False
985 GoTo nxt
986 End If
987 End While
988 badexp = False
989 run = 1
990 Else
991 If currtoken.Right = -1 Then
992 If badexp Then
993 If run = 1 Then
994 run = 2
995 currtoken = Token(iToken - 1)
996 Dim iBombOut As Integer = 0
997 While currtoken.Left <> -1
998 currtoken = Token(currtoken.Left)
999 iBombOut += 1
1000 If iBombOut = 5000 Then
1001 badexp = False
1002 GoTo nxt
1003 End If
1004 End While
1005 badexp = False
1006 GoTo nxt
1007 ElseIf run = 2 Then
1008 run = 3
1009 currtoken = Token(iToken - 1)
1010 Dim iBombOut As Integer = 0
1011 While currtoken.Left <> -1
1012 currtoken = Token(currtoken.Left)
1013 iBombOut += 1
1014 If iBombOut = 5000 Then
1015 badexp = False
1016 GoTo nxt
1017 End If
1018 End While
1019 badexp = False
1020 GoTo nxt
1021 Else
1022 If bThrowExceptionOnBadExpression Then
1023 Throw New Exception("Bad expression: " & sExpression)
1024 Else
1025 ErrMsg("Bad Expression: " & sExpression)
1026 End If
1027 Exit Sub
1028 End If
1029 End If
1030 badexp = True
1031 Else
1032 ' Move token one to right
1033 currtoken = Token(currtoken.Right)
1034 End If
1035 End If
1036
1037 nxt:
1038 End While
1039
1040 If Type = VariableTypeEnum.Numeric Then
1041 IntValue(iIndex) = SafeInt(Val(Token(iToken - 1).Value))
1042 'StringValue(iIndex) = IntValue(iIndex).ToString ' Used to evaluate datatype
1043 ' But we can't set indexed string value because it won't exist, only the int array...
1044 Else
1045 StringValue(iIndex) = Token(iToken - 1).Value
1046 End If
1047
1048 Catch ex As Exception
1049 If bThrowExceptionOnBadExpression Then Throw New Exception("Error in SetToExpression for variable " & Me.Name, ex)
1050 End Try
1051
1052 End Sub
1053
1054 Private Sub addtoken(ByVal tokenv As String, ByVal value As String, ByVal left As Integer, ByVal right As Integer)
1055
1056 ReDim Preserve Token(iToken + 1)
1057
1058 With Token(iToken)
1059 .Token = tokenv
1060 .Value = value
1061 .Left = left
1062 .Right = right
1063 End With
1064 iToken = iToken + 1
1065
1066 End Sub
1067
1068 Private Function StripRedundantSpaces(ByVal strExpression As String) As String
1069
1070 Dim bolOkToStrip As Boolean
1071 bolOkToStrip = True
1072 Dim strChunk As String
1073 Dim intPos As Integer
1074
1075 StripRedundantSpaces = ""
1076
1077 While strExpression <> ""
1078 intPos = sInstr(1, strExpression, Chr(34))
1079 If intPos > 0 Then
1080 strChunk = sLeft(strExpression, intPos)
1081 strExpression = sRight(strExpression, Len(strExpression) - intPos)
1082 If bolOkToStrip Then strChunk = Replace(strChunk, " ", "").Replace(vbCrLf, "").Replace(vbLf, "")
1083 bolOkToStrip = Not bolOkToStrip
1084 Else
1085 strChunk = strExpression
1086 If bolOkToStrip Then strChunk = Replace(strChunk, " ", "").Replace(vbCrLf, "").Replace(vbLf, "")
1087 strExpression = ""
1088 End If
1089
1090 Debug.Print("Chunk: " & strChunk & " - " & bolOkToStrip)
1091 StripRedundantSpaces = StripRedundantSpaces & strChunk
1092 End While
1093 End Function
1094
1095
1096 #End Region
1097
1098 Public Overrides ReadOnly Property CommonName() As String
1099 Get
1100 Return Name
1101 End Get
1102 End Property
1103
1104
1105 Friend Overrides ReadOnly Property AllDescriptions() As System.Collections.Generic.List(Of SharedModule.Description)
1106 Get
1107 Dim all As New Generic.List(Of Description)
1108 Return all
1109 End Get
1110 End Property
1111
1112
1113 Friend Overrides Function FindStringLocal(sSearchString As String, Optional sReplace As String = Nothing, Optional bFindAll As Boolean = True, Optional ByRef iReplacements As Integer = 0) As Object
1114 Dim iCount As Integer = iReplacements
1115 iReplacements += MyBase.FindStringInStringProperty(Me.sName, sSearchString, sReplace, bFindAll)
1116 Return iReplacements - iCount
1117 End Function
1118
1119 Public Overrides Function ReferencesKey(ByVal sKey As String) As Integer
1120 Dim iCount As Integer = 0
1121 For Each d As Description In AllDescriptions
1122 iCount += d.ReferencesKey(sKey)
1123 Next
1124 If Adventure.htblVariables.ContainsKey(sKey) AndAlso Me.StringValue.Contains("%" & Adventure.htblVariables(sKey).Name & "%") Then iCount += 1
1125
1126 Return iCount
1127 End Function
1128
1129 Public Overrides Function DeleteKey(ByVal sKey As String) As Boolean
1130 For Each d As Description In AllDescriptions
1131 If Not d.DeleteKey(sKey) Then Return False
1132 Next
1133 If Adventure.htblVariables.ContainsKey(sKey) AndAlso Me.StringValue.Contains("%" & Adventure.htblVariables(sKey).Name & "%") Then
1134 If Adventure.htblVariables(sKey).Type = VariableTypeEnum.Numeric Then
1135 StringValue = StringValue.Replace("%" & Adventure.htblVariables(sKey).Name & "%", "0")
1136 Else
1137 StringValue = StringValue.Replace("%" & Adventure.htblVariables(sKey).Name & "%", """""")
1138 End If
1139 End If
1140 Return True
1141 End Function
1142
1143 End Class