Menu

[r7]: / GCGB / GCBSubroutine.vb  Maximize  Restore  History

Download this file

298 lines (246 with data), 7.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
'
' Created by SharpDevelop.
' User: Hugh
' Date: 23/01/2010
' Time: 4:10 PM
'
' To change this template use Tools | Options | Coding | Edit Standard Headers.
'
Imports System
Imports System.Collections
Namespace Great_Cow_Graphical_BASIC
Public Class SubParam
Public Dim Name As String
Public Dim Description As String
Public Dim DataType As String
Public Dim DataDir As String
Public Dim DefValue As String
Public Dim DataArray As Boolean
Public Dim CurValue As String
Public Sub New()
Description = ""
End Sub
Public Sub New (ByVal InData As String, ForceIn As Boolean)
Dim LoopMore As Boolean
InData = InData.Trim
DefValue = ""
If InData.IndexOf("=") <> -1 Then
DefValue = InData.Substring(InData.IndexOf("=") + 1).Trim
InData = InData.Substring(0, InData.IndexOf("=")).Trim
End If
If ForceIn Then
DataDir = "I"
Else
DataDir = "IO"
End If
Do
LoopMore = False
If InData.ToLower.StartsWith("in ") Then
DataDir = "I"
InData = InData.Substring(3)
LoopMore = True
ElseIf InData.ToLower.StartsWith("out ") Then
DataDir = "O"
InData = InData.Substring(4)
LoopMore = True
ElseIf InData.ToLower.StartsWith("optional ") Then
InData = InData.Substring(9)
LoopMore = True
End If
Loop While LoopMore
DataArray = False
If InData.IndexOf("()") <> -1 Then
DataArray = True
InData = InData.Replace("()", "")
End If
If InData.ToLower.IndexOf(" as ") <> -1 Then
DataType = InData.Substring(InData.ToLower.IndexOf(" as ") + 4).Trim.ToLower
InData = InData.Substring(0, InData.ToLower.IndexOf(" as ")).Trim
ElseIf InData.IndexOf("$") <> -1 Then
DataType = "string"
InData = InData.Replace("$", "")
Else
DataType = "byte"
End If
Name = InData
Description = ""
End Sub
Public Function GetCode (Optional OmitDirection As Boolean = False) As String
Dim OutVal As String
OutVal = Name
If DataArray Then
OutVal = OutVal + "()"
End If
If DataType <> "byte" Then
OutVal = OutVal + " As " + DataType
End If
If Not OmitDirection Then
If DataDir = "I" Then
OutVal = "In " + OutVal
ElseIf DataDir = "O" Then
OutVal = "Out " + OutVal
End If
End If
If DefValue <> "" Then
OutVal = "Optional " + OutVal + " = " + DefValue
End If
Return OutVal
End Function
End Class
Public Class GCBSubroutine
'Stores a subroutine
'Sub name
Private Dim pName As String
'Description
Public Dim Description As String
'Hide in subroutine list?
Public Dim HideSub As Boolean
'Sub return type (used for functions)
Public Dim ReturnType As String
'Parameter list
'List of SubParams
Public Dim Parameters As ArrayList
'Code listing
'List of Strings
Public Dim Lines As ArrayList
Public Sub New(Name As String)
pName = Name
Description = ""
Parameters = New ArrayList
Lines = New ArrayList
End Sub
Public Property Name
Get
Return pName
End Get
Set
pName = Value
End Set
End Property
'Parameter list conversion
Public Property StartCode
'Generate parameter string
Get
Dim OutVal As String
'Routine name
If ReturnType = "" Then
OutVal = "Sub " + pName
Else
OutVal = "Function " + pName
End If
'Parameters
If Parameters.Count > 0 Then
Dim isFirstParam As Boolean = True
Dim thisParam As SubParam
For Each thisParam In Parameters
If isFirstParam Then
isFirstParam = False
OutVal += " ("
Else
OutVal += ", "
End If
OutVal += thisParam.GetCode(ReturnType <> "")
Next
OutVal += ")"
End If
If ReturnType <> "" Then
If ReturnType.Trim.ToLower <> "byte" Then
OutVal += " As " + ReturnType
End If
End If
Return OutVal
End Get
'Parse parameter string
Set
Dim SubTemp, ParamTemp, NewName As String
Dim ForceIn As Boolean = False
Parameters = New ArrayList
'Format: {sub | function} name [(param1 [as type][, param2 [as type][, ...]])] [as type] [#nr]
SubTemp = Value
Dim oldTemp As String = SubTemp
If SubTemp.ToLower.IndexOf("#nr") <> -1 Then
SubTemp = SubTemp.Substring(0, SubTemp.ToLower.IndexOf("#nr")).Trim
ForceIn = True
End If
'Get sub/function
If SubTemp.ToLower.StartsWith("sub ") Then
ReturnType = ""
SubTemp = SubTemp.Substring(4).Trim
Else If SubTemp.ToLower.StartsWith("function ") Then
ReturnType = "byte"
SubTemp = SubTemp.Substring(9).Trim
ForceIn = True 'Functions cannot return values in params
End If
'Remove parameters from name
If SubTemp.IndexOf("(") <> -1 Then
Dim FindStart, FindEnd, CurrLevel As Integer
CurrLevel = 0
FindStart = SubTemp.IndexOf("(")
For FindEnd = FindStart To SubTemp.Length - 1
If SubTemp.Substring(FindEnd, 1) = "(" Then CurrLevel += 1
If SubTemp.Substring(FindEnd, 1) = ")" Then CurrLevel -= 1
If CurrLevel = 0 Then
ParamTemp = SubTemp.Substring(FindStart + 1, FindEnd - FindStart - 1)
SubTemp = Subtemp.Substring(0, FindStart) + SubTemp.Substring(FindEnd + 1)
Exit For
End If
Next
If CurrLevel <> 0 Then
'Bracket mismatch
ParamTemp = SubTemp.Substring(FindStart + 1)
SubTemp = Subtemp.Substring(0, FindStart)
End If
End If
'Get Type
If SubTemp.ToLower.IndexOf(" as ") <> - 1 Then
ReturnType = SubTemp.Substring(SubTemp.ToLower.IndexOf(" as ") + 4).ToLower
SubTemp = SubTemp.Substring(0, SubTemp.ToLower.IndexOf(" as "))
End If
pName = SubTemp.Trim
'Get parameters
If ParamTemp <> "" Then
Dim paramList() As String = ParamTemp.Split(",")
Dim thisParam As String
For Each thisParam In paramList
Parameters.Add(New SubParam(thisParam, ForceIn))
Next
End If
End Set
End Property
Public ReadOnly Property EndCode As String
Get
If ReturnType = "" Then
Return "End Sub"
Else
Return "End Function"
End If
End Get
End Property
Public ReadOnly Property IsFunction As Boolean
Get
Return ReturnType <> ""
End Get
End Property
Public ReadOnly Property IsSub As Boolean
Get
Return ReturnType = ""
End Get
End Property
Public Shared Function FromCode(CodeIn As String, Optional DocIn As DocSection = Nothing) As GCBSubroutine
'Return a new subroutine from a line of code
Dim outSub As New GCBSubroutine("")
outSub.StartCode = CodeIn
'Add documentation (if present)
If Not DocIn Is Nothing Then
outSub.Description = DocIn.Description
outSub.HideSub = DocIn.HideItem
Dim currParam As SubParam
Dim paramDesc As String
For Each currParam In outSub.Parameters
currParam.Description = DocIn.Params.GetValue(currParam.Name)
Next
End If
Return outSub
End Function
End Class
End Namespace
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.