Hi guys,
I have a code which allows me to use "commands" to do things. I do not use the efficient lexer, parser, tokeniser, but my own method. I would like to integrate multiline commands, maybe making a different class called "Script" which uses the class "Command"?
The class command is:
Public Class Command
Private _cmd As String
Private _args As String()
Public DeclaredVariables As New Dictionary(Of String, String)
Private Function Split(ByVal expression As String, ByVal delimiter As String, ByVal qualifier As String, ByVal ignoreCase As Boolean) As String()
Dim _QualifierState As Boolean = False
Dim _StartIndex As Integer = 0
Dim _Values As New System.Collections.ArrayList
For _CharIndex As Integer = 0 To expression.Length - 1
If Not qualifier Is Nothing _
AndAlso String.Compare(expression.Substring(_CharIndex, qualifier.Length), qualifier, ignoreCase) = 0 Then
_QualifierState = Not _QualifierState
ElseIf Not _QualifierState _
AndAlso Not delimiter Is Nothing _
AndAlso String.Compare(expression.Substring(_CharIndex, delimiter.Length), delimiter, ignoreCase) = 0 Then
_Values.Add(expression.Substring(_StartIndex, _CharIndex - _StartIndex))
_StartIndex = _CharIndex + 1
End If
Next
If _StartIndex < expression.Length Then
_Values.Add(expression.Substring(_StartIndex, _
expression.Length - _StartIndex))
Dim _returnValues(_Values.Count - 1) As String
_Values.CopyTo(_returnValues)
Return _returnValues
Else : Return Nothing
End If
End Function
Public Property Arguments As String()
Get
Return _args
End Get
Set(ByVal value As String())
_args = value
End Set
End Property
Public Function OverrideConstructor(ByVal Command As String) As Boolean
Try
_cmd = Command
CMD = Command
Return True
Catch ex As Exception
Return False
End Try
End Function
Public ReadOnly Property CommandName As String
Get
Return CMD.Split(" ")(0)
End Get
End Property
Public Property CMD As String
Get
Return _cmd
End Get
Set(ByVal value As String)
Dim args(_cmd.Split(" ").Count - 1) As String
Dim i As Integer = 0
For Each arg As String In Split(_cmd, " ", """", True)
If i > 0 Then
If arg.StartsWith("""") And arg.EndsWith("""") Then
Dim removal1 As String = arg.Remove(0, 1)
args(i - 1) = removal1.Remove(arg.Length - 2)
Else
args(i - 1) = arg
End If
End If
i += 1
Next
_args = args
_cmd = value
End Set
End Property
Public Sub New(ByVal xcmd As String)
_cmd = xcmd
CMD = xcmd
End Sub
Public Sub New()
End Sub
Public Function ActivateCommand()
Select Case CommandName.ToLower
Case "write"
Dim writArg As String = Arguments(0)
For Each DictItm In DeclaredVariables
writArg = writArg.Replace("[" & DictItm.Key & "]", DictItm.Value)
Next
Return writArg
Case "decl"
DeclaredVariables.Add(Arguments(0), Arguments(1))
Return Nothing
Case Else
Return "Command not found"
End Select
End Function
End Class
With the integration of Multiline commands, I would like to do loops. (Just For i = 0 to 10 sort of thing). So for loops and multiline commands. I have no idea where to start. I know I should be using a parser & tokenizer, but this one is a simple basic starting point.
Thanks,
iProgramIt
[EDIT]
Usage is as follows:
Public Class MainForm
Dim cmd As New Command()
Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
cmd.OverrideConstructor(TextBox1.Text)
If cmd.ActivateCommand IsNot Nothing Then
TextBox2.Text &= vbNewLine & "> " & cmd.ActivateCommand()
End If
TextBox1.Clear()
End If
End Sub
End Class