I\'m not saying I want to create a whole new compiler that\'s completely indepen
ID: 645818 • Letter: I
Question
I'm not saying I want to create a whole new compiler that's completely independent. I'm using C# Windows Forms and I want users to be able to write filtering syntax in a textbox withing my managed application in a basic query language, for example:
inCategory(Animals)
where(animal.age > 40)
take animal;
It's basically a simplifed version of LINQ. But I want this logic to be enclosed in some separate class, so I can say something like this:
var dataQuery = FilterCompiler.Compile(filterTextBox.Text);
dataQuery.Execute();
I'm not saying 'gimee the code', but some psuedocode / basic logic structure for this 'compiler' would be nice since I am kind of clueless about this subject. Like for example,
What sort of text analysis would this FilterCompiler need to do?
What would be the type of dataQuery?
Would this require any knowledge of compiler grammar?
Would I need to use any unmanaged code?
How can I keep the compiling as independent as possible so that it does not depend too much on what type of data I have? Currently I have a List, but things change, I might have a database with the information in it soon.
Explanation / Answer
Unfortunately its not really that simple, you have a few options...
Coco/R
If you aren't going to do a complex language and only support a few custom keywords, I would suggest something like Coco/R. I have used this several times in the past to create parsers for all kinds of projects.
The jist of it would be that you create a grammar file that describes your language and it allows you to plug in your own functions when certain tokens are found. This may be a little more difficult because it appears that your sample language understands the concepts of Objects and Coco/R actually creates classes that you compile. This means you are going to have to build these sort of things in to the parser and/or use interfaces. You are also doing arithmetic which means you are going to have to write an expression evaluator.
Compile with .NET classes
Another option is to not actually create your own syntax but use something like .NET's built in classes to compile code. This saves you a lot of work (read ton of work) but you lose the flexibility of creating your own simplistic language. More information can be found here.
Reflection.Emit
Another option which would probably be the most complex and not really worth it would be to write IL using reflection and then compile that code to a dynamic function and execute it. You would have a lot of trail and error making sure that you generate valid IL that doesn't just crash and throw up some strange errors that are nearly impossible to track down. There are a lot of benefits from this tho, the user could write complex code that could interact with your own objects (like a real scripting language) and would be easy to extend once the framework was built. More info can be found here
If I personally had to choose one it would come down to what I needed it for. If I was developing this for my company I would probably go with Reflection.Emit and if I just needed something to work right now and fast I would go with using the .NET libraries to just compile C# code to a app domain that I then would just execute. If the language only had a small subset of features I would probably go with Coco/R, but I have a lot of experience with it and there would be no learning curve there for me.
Sorry for the terrible formatting, I'm still rather new to MD.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.