0

I'm currently investigating how to create and execute a script that is parsed from a file.

We are using XML to provide commands to the app and I would need to parse the string and convert it into blocks of code to execute. It would be simple logic commands to start with "if x > y then z". An example of the string is:

#VCurrentTime > #VUWTTimeStamp + 20

My question is 2 parts.

How could I achieve this? I have come across roslyn and a few other questions on here (Evaluating string “3*(4+2)” yield int 18, Is it possible to compile and execute new code at runtime in .NET?) but i appear to run into the same issues. How to discern and get the appropriate values from the string i.e how to convert "#VCurrentTime" to equal the local variable.

NinjaArekku
  • 159
  • 10
  • 1
    > `How could I achieve this?` -- By writing an interpreter. – dialer Apr 24 '20 at 07:31
  • You know, you can actually integrate C#-Script into your app. (Among other scripting languages.) Some references: https://visualstudiomagazine.com/articles/2011/11/16/the-roslyn-scripting-api.aspx, https://github.com/dotnet/roslyn/wiki/Scripting-API-Samples, https://docs.microsoft.com/en-us/archive/msdn-magazine/2016/january/essential-net-csharp-scripting, https://docs.microsoft.com/en-us/archive/blogs/cdndevs/adding-c-scripting-to-your-development-arsenal-part-1 – Fildor Apr 24 '20 at 07:32
  • @dialer Apologies. I havent any experince in an interpreter. Maybe I'm overthinking it. I could brute force it with ifs and switches? but surely there would be a better way? – NinjaArekku Apr 24 '20 at 07:39
  • @Fildor This is my current thought. I'm still planning how the app will hang together so I maybe able to convince the other guys to change the custom script to make it easier on my end. – NinjaArekku Apr 24 '20 at 07:39
  • 1
    Yes, do not reinvent the wheel. I used that scripting in order to customize the behavior of an application on a "per customer" basis. Worked like a charm. You have familiar syntax, you can interact appscript, you can use all the libraries, you can save a precompiled "instance" so it's faster and only recompile if the source file changes ... it's fantastic. You only need to invest some learning - which will be way less than inventing all this on your own (_and getting it right_). – Fildor Apr 24 '20 at 07:41
  • It's really a shame, I don't have more recent literature on it. It's been a while since I used it actively. But I am sure you will find what you need based on the links above. – Fildor Apr 24 '20 at 07:46
  • Alternatively, there are various solution to integrate languages like Lua, Python, ... I just like the Idea of using C# for the scripts, too. – Fildor Apr 24 '20 at 07:57

0 Answers0