Hi all,
this is my first post to this forum, so let me start by stating that I'm pretty impressed by the capabilities of the Cobra language, which for me combines the most favorable aspects of my preferred programming languages, Python and C#.
After playing around a little bit with the language's compiler/interpreter, I decided to try out cobra on a somewhat larger scale. I want to integrate the interpreter into an existing .NET application as an additional scripting language to allow product automation. Since Cobra uses C# as a backend, I would assume that this simply means to incorporate a reference to the Cobra runtime dll, instantiate a DOM CodeProvider for C#, and compile a little bit of C# code which reads the script and executes it in the Cobra runtime.
Actually, I'm stucked on the last one. Most likely, I assume, there is some kind of static method inside one of the Cobra classes which takes the script code as an input and would execute it, but I cannot find anything like that inside Cobra.Lang or the CobraLang source file. Maybe somebody could be of assistance here and point me into the right direction?
Best regards
Hendrik
Forums
Embedding Cobra as a scripting language.
3 posts
• Page 1 of 1
Re: Embedding Cobra as a scripting language.
Thanks for the comment and welcome to the group.
I haven't gotten around to embedding Cobra yet, so I can't give you the exact steps, but I'll try to help you along and we'll see how far we get.
Cobra is not interpreted so there is no run-time interpreter in CobraLang.dll. You'll want to make a new dll for embedding, let's call it CobraCompiler.dll. First, get the code. Then to make the DLL:
That's like building cobra.exe except you're making a DLL instead. (I guess technically you can reference the .exe as a library, but I tend to avoid that.)
You can reference the DLL in your hosting application and make use of the classes therein. You'll want to instantiate and use either CommandLine or Compiler. See the files CommandLine.cobra and Compiler.cobra. Also, the files CommandLine.cobra and TestifyRunner.cobra have examples of using the Compiler object.
I haven't bothered to put the classes in a namespace so there is some possibility that they could clash with your classes. We can address that in the future (or sooner if there is a clash).
Other future work of interest includes:
-- Implementing a "compiler provider" like C# and VB have.
-- An alternative to having Cobra generate C# would be to have the Cobra nodes that represent parsed Cobra code implement "exec" methods for the purpose of interpretation. This is no small task, however.
Let us know how far you get and what problems you encounter,
-Chuck
I haven't gotten around to embedding Cobra yet, so I can't give you the exact steps, but I'll try to help you along and we'll see how far we get.
Cobra is not interpreted so there is no run-time interpreter in CobraLang.dll. You'll want to make a new dll for embedding, let's call it CobraCompiler.dll. First, get the code. Then to make the DLL:
- Code: Select all
cd cobra-workspace
cd Source
comp -t:lib -sharp-args:"-out:CobraCompiler.dll"
That's like building cobra.exe except you're making a DLL instead. (I guess technically you can reference the .exe as a library, but I tend to avoid that.)
You can reference the DLL in your hosting application and make use of the classes therein. You'll want to instantiate and use either CommandLine or Compiler. See the files CommandLine.cobra and Compiler.cobra. Also, the files CommandLine.cobra and TestifyRunner.cobra have examples of using the Compiler object.
I haven't bothered to put the classes in a namespace so there is some possibility that they could clash with your classes. We can address that in the future (or sooner if there is a clash).
Other future work of interest includes:
-- Implementing a "compiler provider" like C# and VB have.
-- An alternative to having Cobra generate C# would be to have the Cobra nodes that represent parsed Cobra code implement "exec" methods for the purpose of interpretation. This is no small task, however.
Let us know how far you get and what problems you encounter,
-Chuck
- Charles
- Posts: 2515
- Location: Los Angeles, CA
3 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 21 guests