Integrate a Lua editor into C# program - c#

I'm doing a POC (Proof of concept) for a little open source project, to integrate Lua into c# looks pretty straight forward, but for this project we want to go a little further. We want to be able to edit Lua scripts from within our C# program (WPF client but we can go for winforms if that what it takes), the IDE needs basic code completion and that we can add our C# objects and methods (The ones you will be able to reach from the Lua scripts while running) so they will be auto completed.. Are they such a API available for C#?
edit: Schollii wanted some example code that i want code completion for
while true do
Mouse:setY(TrackIR:getPitch())
end
where Mouse and TrackIR are c# objects accessible from Lua script

If you want to integrate a Lua editor with syntax highlighting C#, IMO the easiest way is to go for the Scintilla control, which is available in C# through ScintillaNET. It already contains highlighter for the Lua language, and is relatively easy to embed and use.
For interfacing Lua with C#, you can use LuaInterface and back. See this question for documentation links.
Code completion will not be an easy task if you require completion beyond the standard library, which can be described in a simple dictionary. With Lua being a dynamic language, it is not trivial to deduce the contents of a variable before actually running the code up to a given point - it is not impossible, take a look at lua-inspect which is available for SciTE and ViM and allows code inspection (but it's not perfect).
If you want to try code in a running environment - i.e. you have a Lua state running with modules loaded and variables set - you can get inspired by Advanced Readline Support patch at LuaPowerPatches and adapt it to your enviroment in C#. It supports among other:
Completion of keywords and global variable names.
Recursive and metatable-aware completion of variable names.
Context sensitive delimiter completion.

It seems you just want an embeddable editor that can have code completion and syntax highlighting, preferably WPF based. It doesn't need to explicitly support Lua.
Have a look at Avalon Edit. Here is an article about it
You will have to implement the highlighting file and completions yourself within the framework it provides - but that looks easy enough.

I am not aware of any such libs. Some tool you will likely need/find useful are Luainterface and some open-source Lua editors that provide code comppletion such as blua and Scite. Note that code completion comes in different flavors, each with pros/cons in terms of power, ease of implementation, etc. For instance if you just want to provide a list of possible words that correspond to objects in global Lua namespace then you don't need much at all; if OTOH you want code completion to work with table fields and objects and various levels of nesting, work will be much increased.

Related

How to implement a mini language?

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.
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#.
https://github.com/picoe/Eto.Parse
https://irony.codeplex.com/
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
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.
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.
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.

How to compile a string xna

Basicly, I want to create code that I can compile from a STRING that I will be loading from a file.
Is that even possible?
I did my research and I couldn't find any answers.
Reading your follow-up comment, it sounds like what you would be looking for would be a scripting language rather than compiling C# code dynamically. A scripting language allows you to pass in and execute code as a string, while also allowing to tie in your current execution to that code. For instance, you can pass in the character's current health to the scripting language, and then read back the character's current state (dead? immune? etc). The two most popular languages to do scripting for games are Lua and Python. Both of these languages have .NET libraries/bindings to allow them to be used within C#.
It has been a bit of time since I have set up my own project in C# with Lua, but I believe this tutorial looks very similar to what I was using, and therefore how I answered my question (ie, being able to closely bind them). However, searching Google for "lua C# binding" or "python .NET binding" will give you lots of libraries and tutorials. Which one you ultimately decide upon really comes down to which of those two languages (though there are bound to be many other scripting languages out there as well) you feel more comfortable with using.
I suppose, as EZI said above, it would be possible to compile C# code dynamically. But from your question, it sounds like you'd be looking for a scripting language instead.

What is the best script language for Unity3D

I am starting with game dev using Unity3d and I can see there's 3 supported languages for creating scripts: C#, unityscript and Boo...
although, coming from a web-design past I am obviously used with javascript, but after few attempts I could notice Unity3d doesn't really have full support to the script language and some elements like new Date().valueOf() and some other statements within methods such as the attribute arguments and many other won't work properly, also it seems my file can't have a wrapper method that envolves all the other methods like:
(function (scope) {
function Start() {
...
}
...
}(this));
and when using something like new Date().valueOf() which is valid in JS I get:
MissingMethodException: Method not found: 'System.DateTime.valueOf'. Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.ProduceExtensionDispatcher ()
So perhaps the compiler turns that initial 'unityscript' code into Boo language? so perhaps Boo is the right 'native' choice and maybe unityscript itself isn't the best way to go when developing unity3d apps?
I was thinking on a language that has full support to all known native classes and methods which will work without problem?
Sorry for any errors mentioned above and please let me know what you think.
Unity doesn't use 'real' Javascript per se. See:
http://forum.unity3d.com/threads/1117-Javascript-Version
UnityScript is based on javascript 2.0. There are a few things missing (switch statements, etc.), but they get half the speed of C++, which is way faster than Mozilla.
Once you get used to it, developing in 'JS' for unity is very very fast and flexible...but of course, if you're going to develop native components, or interface to anything in C, you'll need to use C# (in the end, C# is not difficult to learn).
If you want to be a developer in Unity and build a career on it, learn C# and use it - don't waste your time with UnityScript. You can use C# outside of Unity for programming. If your main career is already web development (or will be), then continue using javascript because ultimately C# and UnityScript have the same functionality but C# is much more widely used for programing games and applications.
The majority of the assets on the store that I have used are C# or offer both JS and C#. Mixing between the languages creates serious dependency problems because in order to use a class in either language it has to already have been processed by the Unity script loading order. To use a Javascript defined class in C#, that script file must have been processed prior to the C# script file. If the javscript class then later needs something from the C# class, you would have to find ways around it because its simply no longer possible from the javascript file. Unity has ways to define the script order.
I thought that I would warn you about mixing C# with UnityScript because if you don't focus on one or the other, you will run into this problem. Ultimately, C# is the middle ground between ambiguous languages like VB and JavaScript, and pedantic languages like C++ (I have a C/C++ background of about 10 years).
Your title "What is the best xxx" leads to opinionated answers, but I am trying to give the best advice possible without being subjective. C# will allow you to continue, even if Unity were to go away a year from now. UnityScript/JavaScript would only allow you to move into being a web developer. A C# developer can easily move into UnityScript/JavaScript, but the reverse is much harder (not impossible, just more difficult).
You should use c#. It is popular, mature, native to .NET. c# is what big teams use. Learning c# will be useful in the future. JS in Unity3D is not real JS as already mentioned and nobody heard of Boo.
JS is the most popular language for use with Unity. But the language is implemented in Mono, and so any restrictions in that implementation are going to affect Unity scripts too.
I don't know how the Mono implementation of JS works, but judging from the error message, it uses datatypes initially defined for Boo, at least. However, that doesn't mean that JS is "turned into Boo". Both are compiled into the same bytecode, which is JIT'ed and run by the Mono runtime. So neither language is "more native" than the other.
Checkout advantages and disadvantages of using c# and Java script from here.
C# is little bit faster than JavaScript and you get extra features provided by C#. You can get peoples opinion from here.
Developing with c# has advantage of Visual Studio 2010. It provides better code completion feature than Mono develop.
In JavaScript only, variables can have an unspecified type. This only occurs if you do not assign a value while declaring the variable.
Performance is slower with dynamically typed variables, and you can run into casting problems. Iif this is a concern, use #pragma strict.

Can I execute a single C# statement as a script?

I have seen online how C# allows you to compile code on the fly and I think that is a really awesome thing. I need to use this functionality for my project. But here is the problem. I'm not trying to compile a whole function or new program dynamically. I just need to call a single statement to create or delete stuff on the fly.
Is there a way C# can compile and/or run a single code statement and allow that statement to act on variables and objects in the current program's scope?
Thanks
You could compile C# using Microsoft.CSharp.CSharpCodeProvider, but that gets really complicated if you want to do it correctly, since you need to load your code in a separate App Domain to prevent memory leaks.
I'd suggest using IronPython or some other DLR language: http://www.codeplex.com/wikipage?ProjectName=IronPython
Some sample here, not sure how up-to-date it is but the idea's pretty much the same: http://www.voidspace.org.uk/ironpython/dlr_hosting.shtml
To give you an alternative rather than using C# as a scripting language, have a look at Conscript [1].
Conscript is a compiled scripting language for .NET.
[1]: http://www.codeproject.com/KB/cs/Conscript.aspx
"I just need to call a single statement to create or delete stuff on the fly." Statements like these make me shudder down to my bones. What are you trying to accomplish here, really? You want to have your user write C# statements and have your program execute them within its AppDomain? Not only is this an immense security risk, it is also a terrible user experience.
Furthermore, C# is not a scripting language. If you try to shoehorn it into being one, You're Gonna Have A Bad Time (TM).
You can, while using the debugger. At a breakpoint, just type some code into the Immediate Window in VS, and viola.
One of the best debugging features there are!
Scripting static languages comes trade-offs. There are scope and security concerns to consider, but they can be controlled.
If you're looking to execute static code (i.e. C#) from within a managed run time, I'd recommend starting with Mono. The Mono team has made strides creating a safe environment to compile JIT code into native code at run time.
Start with their official post on the subject.
It depends on what you are trying to do ... if you are looking to use it as an embedded scripting language within another application, then my answer doesn't apply, but if you just want to execute random C# statements (or programs if you like) and save them as scripts, LinqPad is awesome for that.

Adding scripting functionality to .NET applications

I have a little game written in C#. It uses a database as back-end. It's
a trading card game, and I wanted to implement the function of the cards as a script.
What I mean is that I essentially have an interface, ICard, which a card class implements (public class Card056: ICard) and which contains a function that is called by the game.
Now, to make the thing maintainable/moddable, I would like to have the class for each card as source code in the database and essentially compile it on first use. So when I have to add/change a card, I'll just add it to the database and tell my application to refresh, without needing any assembly deployment (especially since we would be talking about 1 assembly per card which means hundreds of assemblies).
Is that possible? Register a class from a source file and then instantiate it, etc.
ICard Cards[current] = new MyGame.CardLibrary.Card056();
Cards[current].OnEnterPlay(ref currentGameState);
The language is C# but extra bonus if it's possible to write the script in any .NET language.
Oleg Shilo's C# Script solution (at The Code Project) really is a great introduction to providing script abilities in your application.
A different approach would be to consider a language that is specifically built for scripting, such as IronRuby, IronPython, or Lua.
IronPython and IronRuby are both available today.
For a guide to embedding IronPython read
How to embed IronPython script support in your existing app in 10 easy steps.
Lua is a scripting language commonly used in games. There is a Lua compiler for .NET, available from CodePlex -- http://www.codeplex.com/Nua
That codebase is a great read if you want to learn about building a compiler in .NET.
A different angle altogether is to try PowerShell. There are numerous examples of embedding PowerShell into an application -- here's a thorough project on the topic:
Powershell Tunnel
You might be able to use IronRuby for that.
Otherwise I'd suggest you have a directory where you place precompiled assemblies. Then you could have a reference in the DB to the assembly and class, and use reflection to load the proper assemblies at runtime.
If you really want to compile at run-time you could use the CodeDOM, then you could use reflection to load the dynamic assembly. Microsoft documentation article which might help.
If you don't want to use the DLR you can use Boo (which has an interpreter) or you could consider the Script.NET (S#) project on CodePlex. With the Boo solution you can choose between compiled scripts or using the interpreter, and Boo makes a nice scripting language, has a flexible syntax and an extensible language via its open compiler architecture. Script.NET looks nice too, though, and you could easily extend that language as well as its an open source project and uses a very friendly Compiler Generator (Irony.net).
You could use any of the DLR languages, which provide a way to really easily host your own scripting platform. However, you don't have to use a scripting language for this. You could use C# and compile it with the C# code provider. As long as you load it in its own AppDomain, you can load and unload it to your heart's content.
I'd suggest using LuaInterface as it has fully implemented Lua where it appears that Nua is not complete and likely does not implement some very useful functionality (coroutines, etc).
If you want to use some of the outside prepacked Lua modules, I'd suggest using something along the lines of 1.5.x as opposed to the 2.x series that builds fully managed code and cannot expose the necessary C API.
I'm using LuaInterface1.3 + Lua 5.0 for a NET 1.1 application.
The issue with Boo is that every time you parse/compile/eval your code on the fly, it creates a set of boo classes so you will get memory leaks.
Lua in the other hand, does not do that, so it's very very stable and works wonderful (I can pass objects from C# to Lua and backwards).
So far I haven't put it in PROD yet, but seems very promising.
I did have memory leaks issues in PROD using LuaInterface + Lua 5.0, therefore I used Lua 5.2 and linked directly into C# with DllImport. The memory leaks were inside the LuaInterface library.
Lua 5.2: from http://luabinaries.sourceforge.net and http://sourceforge.net/projects/luabinaries/files/5.2/Windows%20Libraries/Dynamic/lua-5.2_Win32_dll7_lib.zip/download
Once I did this, all my memory leaks were gone and the application was very stable.
The main application that my division sells does something very similar to provide client customisations (which means that I can't post any source). We have a C# application that loads dynamic VB.NET scripts (although any .NET language could be easily supported - VB was chosen because the customisation team came from an ASP background).
Using .NET's CodeDom we compile the scripts from the database, using the VB CodeDomProvider (annoyingly it defaults to .NET 2, if you want to support 3.5 features you need to pass a dictionary with "CompilerVersion" = "v3.5" to its constructor). Use the CodeDomProvider.CompileAssemblyFromSource method to compile it (you can pass settings to force it to compile in memory only.
This would result in hundreds of assemblies in memory, but you could put all the dynamic classes' code together into a single assembly, and recompile the whole lot when any change. This has the advantage that you could add a flag to compile on disk with a PDB for when you're testing, allowing you to debug through the dynamic code.
Yes, I thought about that, but I soon figured out that another Domain-Specific-Language (DSL) would be a bit too much.
Essentially, they need to interact with my gamestate in possibly unpredictable ways. For example, a card could have a rule "When this cards enter play, all your undead minions gain +3 attack against flying enemies, except when the enemy is blessed". As trading card games are turn based, the GameState Manager will fire OnStageX events and let the cards modify other cards or the GameState in whatever way the card needs.
If I try to create a DSL, I have to implement a rather large feature set and possibly constantly update it, which shifts the maintenance work to another part without actually removing it.
That's why I wanted to stay with a "real" .NET language to essentially be able to just fire the event and let the card manipulate the gamestate in whatever way (within the limits of the code access security).
The next version of .NET (5.0?) has had a lot of talk about opening the "compiler as a service" which would make things like direct script evaluation possible.

Categories