0

I am searching for a Script-Language for my product, which will allow easy customisation.

I have built my own small XML-Based language, but i'm not really happy with this solution:

<ScrArp>
    <StaticClass Name="Root">
        <Method Return="Void" Name="Main">
            <Call Method="Print" Text="Hello World" />

            <Declare Type="SampleClass" Name="myInstance">
                <Set>
                    <NewInstance Type="SimpleClass" />
                </Set>
            </Declare>

            <Call Text="Hello 123">
                <Instance Name="myInstance">
                    <!-- <Instance Name="mySample2..."> -->
                    <Method Name="TestMethod" />
                    <!-- </Instance> -->
                </Instance>
            </Call>
        </Method>
    </StaticClass>

    <Class Name="SampleClass">
        <Method Return="Void" Name="TestMethod">
            <Parameter>
                <Declare Type="String" Name="Text" />
            <Parameter>
            <Call Method="Print">
                <Text>
                    <Get Name="Text" />
                </Text>
            </Call>
        </Method>

        <Method Return="String" Name="Method2">
            <Declare Type="String" Name="cReturnValue" />

            <Set>
                <Instance Name="cReturnValue" />
                <Value>
                    <Call>
                        <Instance Name="cReturnValue" />
                    </Call>
                <Value>
            </Set>

            <Return>
                <Instance Name="cReturnValue" />
            <Return>
        </Method>
    </Class>
</ScrArp>

I have used Lua and IronPython before, but i don't like Lua that much and as far as i know IronPython is only community supported.

Now my Question. Which is the best solution for scripting in C#? - IronPython - Lua - My Own Small Language?

Thank's!

Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
BendEg
  • 20,098
  • 17
  • 57
  • 131
  • What's wrong with C# ? have a read of http://stackoverflow.com/questions/3540231/implementing-a-simple-xml-based-scripting-language-for-an-xna-game – Paul Zahra Sep 07 '13 at 08:16

3 Answers3

6

If you want to go the hard way, I would recommend spending the next 6 months or so trying to write your own "compiler". If you want an easier way, I would recommend you offer scripting in actual C# language and use CodeDOM to compile and execute.

In order to properly use the generated/compiled assemblies I recommend you force the usage of interfaces by the end users and use Dependency Injection in your application.

EDIT :

Following your edit on the question. To be honest I have no clue about neither Lua or IronPython, but something I know for sure: you should NEVER EVER try reinventing the wheel. DON'T try writing your own language. I do remember spending 3 months trying to write my own small ORM (SQL Generator for Dapper) to end up by suddenly discovering ServiceStack ORMLite which was waaaaay more better and offered higher performance than my code.

As I previously explained I would really recommend using C# (since you are also using it in your application).

Moslem Ben Dhaou
  • 6,897
  • 8
  • 62
  • 93
  • I also thought about the CodeDOM solution. But than i had to restart my application, if i want to change some thing in my script. – BendEg Sep 07 '13 at 08:16
  • No you don't. If you are using dependency injection the generated assemblies will not be part of your application (not hard coded dependencies exist), therefore you can regenerate them and simply reuse them as long as them still implement the interface you are using (like you have a new assembly) – Moslem Ben Dhaou Sep 07 '13 at 08:19
  • "DON'T try writing your own language". If everyone listened to that advice, where would we be? – Mark Walsh Sep 07 '13 at 09:36
  • @MarkWalsh The calendar says 2013 not 1960s. What about not reinventing the wheel? – Moslem Ben Dhaou Sep 07 '13 at 11:26
  • Its terrible advice, without innovation and people having a go at writing their own language we wouldn't have languages like Ruby. You might not of had luck with your ventures but others certainly have. – Mark Walsh Sep 07 '13 at 13:40
  • 2
    @MarkWalsh: The question is not looking for a way to achieve something not existent yet or so. This is looking for which solution is the best to implement within a specific application. Again, reinventing the wheel will still lead you to a wheel. We are not discussing the Mars expedition vehicle. – Moslem Ben Dhaou Sep 07 '13 at 14:07
1

You could use C# for adding functionality to your application while compiling the code or evaluating expressions from within your application. This can be accomplished using the .NET framework, without relying on community-supported components, but there are community-supported libraries that you can learn from if you choose to write your own code.

Here's an article on how to compile from your application: http://www.codeproject.com/Articles/9019/Compiling-and-Executing-Code-at-Runtime

There's the C# Eval library available for evaluating expressions at runtime as well: http://csharp-eval.com/HowTo.php

1

You can use PascalScript and maXbox4 #maxbox4 Studio to build your own types and syntax. maXbox is a script tool engine, compiler and source lib all in one exe to design and code your scripts in a shellbook! Pure Code for Object Scripting. Principle is simplicity and reduce to the max. The App is “out of the box” (self containment) and needs no installation nor registration.

Pascal Script is a scripting language based on the programming language Delphi/Pascal that facilitates automated runtime control over scriptable applications and server software. It is implemented by a free scripting engine that includes a compiler and an interpreter for byte code.