10

About 13 years ago I needed a way to script an application I was writing in Borland C++. I found a C interpreter and embedded that in my application. This has been working fine and I have quite a number of scripts.

I now need to rework the app and will do this in C#. Does anyone know of a C interpreter for C#? I have the sources for the original interpreter but I just need to save some time.

Edit Here is an extract from a script:

main()
{
   LwSet( "STUFE", "00.00" );
   LwSet( "STATUS", "Warten", "AUS" );
   do
   {
      LwSet( "DEBUG", "OFF" );
      LwSet( "FUNKTION", "Warten auf Start" );
      do
      {
         rc = LwGet( "Event" );
         if( rc == "QUIT" ) {
            ...

The LwSet/LwGet calls a functions which I registered with the interpreter and are features of my application.

Edit2

Before anyone feels the need to tell me what other language I should have used, here is my comment from below:

At the time, and considering the skills available it was not an annoying choice. Now, 13 years later, a lot of things look a whole lot different! I'm just stuck with a pile of quasi-C scripts which I would like to use without having to convert them all somehow.

Gabe
  • 84,912
  • 12
  • 139
  • 238
paul
  • 13,312
  • 23
  • 81
  • 144
  • What exactly is that interpreter doing? – Daniel Hilgarth Apr 07 '11 at 12:00
  • 1
    Using C as a scripting language for an application sounds a bit annoying. Couldn't you just C# itself? Or maybe Lua? – unwind Apr 07 '11 at 12:01
  • 3
    @unwind: maybe he has a lot of LEGACY CODE to support (a lot of C scripts already written/working on his/her application) – Pablo Santa Cruz Apr 07 '11 at 12:06
  • 2
    @unwind At the time, and considering the skills available it was not an annoying choice. Now, 13 years later, a lot of things look a whole lot different! I'm just stuck with a pile of quasi-C scripts which I would like to use without having to convert them all somehow. – paul Apr 07 '11 at 12:06
  • or TCL or python or JavaScript or pretty much anything but C – jk. Apr 07 '11 at 12:08
  • +1 I think this would be a wonderful thing to get going on the DLR! – Daren Thomas Apr 07 '11 at 12:15
  • That code snippet is pretty simple, perhaps a simple solution would be to use some regexes (or similar) to convert the c scripts to c# scripts. The example above is 99% c# compatible, all that needs changing is some imports and changing the main() declaration. – David Miani Apr 07 '11 at 12:18
  • 2
    Why do not to use old interpreter by interop? – zabulus Apr 07 '11 at 12:27
  • 1
    I really doubt that what you've got is a C interpreter. It is something "C-like". So, with all your legacy code, you've got no other choice but to use the very same interpreter via interop or porting manually it to C#. No other C interpreter will be able to replace that one. – SK-logic Apr 07 '11 at 12:47

4 Answers4

5

How about using the same existing C interpreter from C# via interop?

Justin
  • 84,773
  • 49
  • 224
  • 367
3

The excellent ANTLR parser generator has support for C#. Its examples include a C grammar for C#.

This obviously isn't a full interpreter, but the grammar, lexer and parser would make a good starting point.

Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
2

I don't know of any C interpreter for C# but there is a c# scripting engine that you might be able to use.

http://www.csscript.net/index.html

Alex Mendez
  • 5,120
  • 1
  • 25
  • 23
  • 2
    -1; sorry, question is pretty explicit - needs a C interpreter – Kieren Johnstone Apr 07 '11 at 12:14
  • 1
    +1, it's a valid answer. Close the question as "too localized" if SO users shouldn't be allowed to suggest a better approach. – Hans Passant Apr 07 '11 at 12:23
  • 2
    I agree with Kieren. The question made it clear he did not want to convert the scripts. – Jonas Van der Aa Apr 07 '11 at 12:23
  • I gave this answer in case he did not find what he was looking for, at least he has an option to go with. My answer did clearly state that I did not know of any C interpreter. – Alex Mendez Apr 07 '11 at 12:27
  • @Hans - indeed, at the time of posting he clearly says he has legacy C scripts he doesn't want to convert, and he's looking for a C interpreter. How can suggesting a C# scripting engine be an answer to that question? It's the answer to the question, "I want to add scripting/an interpreter to my app for purpose X", but not this one.. – Kieren Johnstone Apr 07 '11 at 12:28
  • I must say that Alex was the first to provide a *useful* suggestion :-) Even if it isn't exactly what I was looking for it is more helpful than saying how annoying C is! – paul Apr 07 '11 at 12:31
  • I agree that the question was very specific, but if he does not find an answer, are we going to leave him with at least an option. I am just trying to help. – Alex Mendez Apr 07 '11 at 12:36
  • 1
    The idea that SO should cater to the needs of *one* SO user is silly. This question is going to linked by Google for many years by other programmers that look for a scripting solution. If they can't see better advice then SO is not useful to them. – Hans Passant Apr 07 '11 at 12:38
  • I'd argue (and downvoted) for the same reason: yet another useless Google result for someone searching for "C interpreter written in C#"!. Also, something that's a 'sideways' suggestion should be a comment, not an answer, no? – Kieren Johnstone Apr 07 '11 at 13:55
  • I fear that the desire to have a "SO police" and clean up all the posts and topics may be getting a bit out of control. I, for instance, see no useful purpose of purging posts with slightly off-topic but useful answers. Besides, I was also looking for a C interpreter in .NET, and now I can think of many good uses to the CS-Script linked in the above post, for which I learned for the first time from this very same post. By the way (no offense, just out of curiosity), @KierenJohnstone, if you think this post has no place here, why did not you try flagging it instead of down-voting? – Ivaylo Slavov Apr 02 '15 at 07:35
2

The nearist think I'm aware of is: http://www.softintegration.com/

Added the following Link Is there an interpreter for C?

Regards Friedrich

Community
  • 1
  • 1
Friedrich
  • 5,916
  • 25
  • 45
  • I don't think you can easily embed this in a C# application. – Jonas Van der Aa Apr 07 '11 at 12:24
  • Hm interfacing to C is not to difficult in C# or is it? If interfacing is easy than I can not see the problems. At least I think it would be worth a try.... – Friedrich Apr 07 '11 at 12:54
  • AFAIK there's only COM interop, but I doubt their solution is COM compatible – Jonas Van der Aa Apr 07 '11 at 12:55
  • See http://www.softintegration.com/support/faq/embed.html And that seems to be not much more than passing a opaque pointer around and this works mostly unproblematic I've done that with C# and C shared libraries before. So it seems this is a very doable way of getting a C/C++ interpreter. Of course YMMV ... – Friedrich Apr 07 '11 at 13:47
  • I think it's a lot of work, but he might not have a choice. – Jonas Van der Aa Apr 07 '11 at 13:55
  • I don't think so, just see the first example. You compile it into an DLL and use the Pinvoke Services on this function. You just hand over either a file or a string and let it execute the main function for calling the Interpreter. But maybe the other link is more useful? Just call the tcc with a script via System and "let" it interpret whatever there is in the file ;-) – Friedrich Apr 07 '11 at 13:58