0

Environment

  • Windows 8.1 64bit
  • Microsoft Visual Studio
  • C#

What I'm trying to do

To implement a mini language in C#

My job in my company is to make a software which automates evaluation of the performance of company products. The evaluation involves opening valves, flowing chemicals, fetching signals from sensors, calculating some values, etc...

The issue is when a user wants to change the procedure and parameters like how long the reaction time is, the order of opening valves, whether reaction conduit is flushed out or not, the programmer has to change the source code and build another software every time the user requires it.

So I'm trying to implement a mini language in C#. A user can organize a procedure of measurement with the easy-to-understand language. The user writes a code in a txt file, my software reads it, parse each line of code, extract commands and parameters, call corresponding method in the software.

For example, a user writes a line of code like open_valve(3), my software reads the code and call an internal method send_commands_to_ADconverter(VALVE, 3) which sends appropriate commands to a target valve via an A/D converter connected to the computer. This is my conception.

However, due to lack of my programming skill, I have no idea where to start. What technology is available? What are the keywords to achieve the goal? How can I achieve this objective? Any help would be appreciated. Thanks.

Edit 1

c# - Adding scripting functionality to .NET applications - Stack Overflow doesn't refer to accessing external devices like sensors, valves via A/D converter which is crucial for my purpose, so it is unclear this is a duplicate question of the link above.

Community
  • 1
  • 1
dixhom
  • 2,419
  • 4
  • 20
  • 36
  • 1
    I do not think you need to implement mini language in C# for that. I see you need to a system where you can take user input and based on that perform a set of operation. If the user also needs to tell "how" to perform the operation, this can also be performed without complicating things. For example: you can have delegate as a parameter in your method and that delegate can be passed on from outside. – Ankit Vijay Mar 27 '16 at 09:29
  • 3
    It seems that what you're looking for is making your application scriptable. See [here](http://stackoverflow.com/q/260/162671) and [here](http://stackoverflow.com/q/462311/162671) (you can find more by using your favorite search engine). – Nasreddine Mar 27 '16 at 09:38
  • 1
    As for your edit, it doesn't matter what kind of interface you expose to the script - that's all up to you. The script simply calls your methods. – Luaan Mar 27 '16 at 10:12
  • 1
    .Net can integrate with VBScript/JScript directly, you can pass a class to the context of the script when you run it allowing users to call whatever methods you like, [Example](http://stackoverflow.com/questions/5715943/how-to-execute-vbscript-command-within-textbox-from-c-sharp-application). – Alex K. Mar 27 '16 at 16:13

4 Answers4

2

In order to create a language you need a "parser" of some sort. You will need to define a "grammar". Parsing your "progam" via the grammar will result in a structure that you can then call methods in your code that implement each feature of your language.

You are on a big learning curve here :) lots of strange things like EBNF. You will probably see lots of references to things like Gold and ANTLR. These are hugely capable but involve things like "compiler compilers" and add a level of complexity that can be confusing and require extra steps in you build pipeline.

Here are a couple of libraries I've used that allow you to define you grammar in c#.

Irony is very good (I've used it for various projects) but hasn't been maintained for a while.

Eto.Parse is more recent. It also has built in parsers that allow you to create a Grammer by parsing BNF. This is very cool

AndyPook
  • 2,762
  • 20
  • 23
  • With stack-oriented computing, powerful languages can be written without grammar, without parser. In the example the programmer gave, the user would type "3 open-valve", which would be split into "3" and then "open-valve." When the software reads "3", after seeing it matches nothing, it tries turning it into an int, and putting it on the stack. Then when it reads "open-valve," it pops the 3 off the stack, and calls open_valve(3). No grammer, no parser. Just linear reading. – LionKimbro Oct 08 '19 at 00:26
1

If I understand, your goal is to parse a syntax written by your user and take a decision with what he typed. I suggest you to use regular expression to match and split the user input.

Veler
  • 161
  • 2
  • 13
1

There are several scripting languages which can be run directly from C#.

As your users doesn't seem to have programming knowledge it might help to use a verbose language like VBScript.

To run VBScript you can use the Scripting Control. See this question for samples: Use Microsoft Scripting Control to evaluate 'If' expressions (via c#)

IIRC the script control must be run as a 32bit application.

Community
  • 1
  • 1
jgauffin
  • 99,844
  • 45
  • 235
  • 372
1

Well, the easiest option would be to use what you already have - C#.

You can compile C# code on the fly, and it's very easy and cheap with the new compiler, Roslyn. Then all you need to do is define your interface so that it's easy to use for the users, and put that code in a method you can call from your code. This allows you to easily support all the various development tools for C# as well, including Visual Studio/VS Code. In the simplest form, all you need to do is make your "script" implement some base class that has all the methods you want the users to have - there's certainly better approaches, but this one is very simple, which sounds like something you're looking for.

Of course, this only makes sense if you can trust your users. Making safely sand-boxed code in C# is a lot more complicated (though certainly possible), and you'd probably be better off using some ready scripting solution.

Luaan
  • 62,244
  • 7
  • 97
  • 116