Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
VB
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:
VB
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
Posted
Updated 3-Oct-15 18:30pm
v2
Comments
Ralf Meier 4-Oct-15 11:32am    
I'm not sure if I understood you right.
You have (for example) a custom Button which has a Property "Commands" and here you want to add commands from your application which could be used user-defined. This commands could be something like "switch to another form" or set a Variable to a value or whatever ...
Am I right so far ?

If not please give an example for what you want to do ...
iProgramIt 4-Oct-15 19:46pm    
No. Commands is a separate class. It is standalone.
Ralf Meier 5-Oct-15 0:16am    
Then ... branch to the part "if not ..." of my comment.
Maciej Los 5-Oct-15 14:05pm    
Agree!
Maciej Los 5-Oct-15 14:10pm    
Your question is unclear! No one is able to read in you mind or direct from your screen. Please, be more specific and provide more details. These pieces of code won't help unless you...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900