none
Parsing/Interpreting a small defined set of user commands into VB.Net code RRS feed

  • Question

  • Not sure really how to explain what I'm looking for help on. But here goes:


    I have a small set of commands that I want to let my user select and then I will create a function to implement it. For instance, in our software, we have three main loops. One for the Channel being tested, One for Temperature and one for Pressure. We also have a few commands that we would like to expose. 


    Here is my proposed list of Keywords:

    CHANNEL_LOOP

    TEMP_LOOP

    PRESSURE_LOOP

    NEXT_CHANNEL

    NEXT_TEMP

    NEXT_PRESSURE

    SET_TEMP

    SET_PRESSURE

    DELAY

    COMPARE


    So now I will create a  small User-Defined function using these keywords:


    CHANNEL_LOOP

        TEMP_LOOP

            SET_TEMP

            PRESSURE_LOOP

                SET_PRESSURE

                DELAY

                MEASURE

                COMPARE

            NEXT_PRESSURE

        NEXT_TEMP

    NEXT_CHANNEL


    After reading this list, somehow  I need to convert it to VB.Net code that looks something like this:


    For iChannel = 1 to Number OfChannels

        For Each Temperature  In TemperatureList

            SetTemperature(Temperature)

            For Each Pressure In PressureList

                SetPressure(Pressure)

                PressureDelay

                Value = ReadValueFromChannel

                CompareToTolerances(Value)

            Next Pressure

        Next Temperature

    Next iChannel


    I would love to convert this on the fly as the User-Defined function is read in,  if possible.


    Any ideas?


    Thanks,

    Tim


    Tim

    Wednesday, June 19, 2019 6:18 PM

All replies

  • Hi

    Seems like you are talking about interfacing with some external hardware/receiver/something.

    Is that the case?


    Regards Les, Livingston, Scotland

    Wednesday, June 19, 2019 6:24 PM
  • Yes. But that is totally irrelevant to what I am asking.


    Tim

    Wednesday, June 19, 2019 6:31 PM
  • Hello,

    What you may want to look at is code generation using tt templates. In short you write a script from your logic 

    https://github.com/olegsych/T4Toolbox

    This for example is how Entity Framework 6 creates classes for databases, here you are generating classes from logic.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, June 19, 2019 7:13 PM
    Moderator
  • Yes. But that is totally irrelevant to what I am asking.


    Tim

    Hi

    OK, I will ignore anything outwith the code window.

    Here is some code that *may* be close to what you mentioned.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim Coms As New List(Of String)
    	Dim WithEvents ListBox1 As New ListBox
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		Coms.AddRange({"CHANNEL_LOOP", "TEMP_LOOP", "PRESSURE_LOOP", "NEXT_CHANNEL", "NEXT_TEMP", "NEXT_PRESSURE", "SET_TEMP", "SET_PRESSURE", "DELAY"})
    		With ListBox1
    			.Location = New Point(10, 10)
    			.Size = New Size(200, ClientSize.Height - 30)
    			.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Bottom
    			.Font = New Font(ListBox1.Font.FontFamily, 12)
    			.DataSource = Coms
    		End With
    		Controls.Add(ListBox1)
    	End Sub
    	Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    		Dim s As String = DirectCast(sender, ListBox).SelectedItem.ToString
    		Select Case s
    			Case "CHANNEL_LOOP"
    				' code here
    			Case "TEMP_LOOP"
    				' code here
    			Case "PRESSURE_LOOP"
    				' code here
    			Case "PRESSURE_LOOP"
    				' code here
    			Case "NEXT_CHANNEL"
    				' code here
    			Case "NEXT_TEMP"
    				' code here
    			Case "NEXT_PRESSURE"
    				' code here
    			Case "SET_TEMP"
    				' code here
    			Case "SET_PRESSURE"
    				' code here
    			Case "DELAY"
    				' code here
    		End Select
    		MessageBox.Show("Your selected Command = " & s)
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    Wednesday, June 19, 2019 7:46 PM