-1

Okay, so I am working on a small scripting language using a VB Console Application.

I want the user to input "say('something')" and it calls the function I made named "say", is there a way to call the function and still use the following code:

Module Module1

Sub say(sayline)
    Console.WriteLine(sayline)
End Sub


Sub Main()

    Dim cmd As String
    Console.WriteLine(">")
    Do
        Console.Write("")
        cmd = Console.ReadLine()
        If cmd IsNot Nothing Then cmd
    Loop While cmd IsNot Nothing



End Sub

End Module
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
ZyDevs
  • 43
  • 1
  • 8
  • What this line suppose to do? `If cmd IsNot Nothing Then cmd` – ilans May 28 '15 at 06:20
  • That line not even compile – Fabio May 28 '15 at 08:07
  • Check this: [http://stackoverflow.com/questions/7898310/using-regex-to-balance-match-parenthesis](http://stackoverflow.com/questions/7898310/using-regex-to-balance-match-parenthesis) – Fabio May 28 '15 at 08:20

2 Answers2

1

No, you cannot just call a method from user's string. You need to interpret the entered data.

First, you need to split your method name and arguments so that entered "say('something')" will transform to say and something. Remember that user can enter wrong data and you need to check if this call is correct - it's all about syntactic and lexical analysis. I hope you understand how to do this because it is pretty difficult.

Then, you need to check if you have a method called say. In case of plain and simple structure, switch construction will be enough. If your have such method, then pass something argument to this method. Else, output something like "unknown method".

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
0

If you wanted to call the method say upon typing the word say(something) and display the word something, then you can just have a certain condition that if the user types the word say within the input then call say method else, do whatever you want to do under else portion. Parse the input and omit the word say from the input and display it then.

You can have your code this way for example. (I just copied your code and added some codes to meet what you wanted... in my understanding)

Module Module1
  Sub say(ByVal sayline)
    Console.WriteLine(sayline)
  End Sub

  Sub Main()
    Dim cmd As String
    Do
        Console.Write("> ")
        cmd = Console.ReadLine()
        Try
            If cmd IsNot Nothing And cmd.Substring(0, 3).ToUpper().Equals("SAY") Then
                say(parseInput(cmd))
            End If
        Catch ex As Exception
            Console.WriteLine("message here")
        End Try
    Loop While cmd IsNot Nothing
  End Sub

  Function parseInput(ByVal cmd As String) As String
    Dim input As String = ""
    For index As Integer = 3 To cmd.Length - 1
        If Char.IsLetter(cmd) Then
            input += cmd.Substring(index, 1)
        Else
            input = input
        End If
    Next
    Return input
  End Function
End Module
MAC
  • 676
  • 3
  • 13
  • 32