Click here to Skip to main content
15,885,244 members
Articles / Programming Languages / C#

Script Engine Implemented by C# and Regular Expression

Rate me:
Please Sign up or sign in to vote.
4.60/5 (4 votes)
21 Nov 2009CPOL2 min read 24.8K   853   24   2
Script engine to execute script codes, which is built by C# and regular expression

Introduction

We are going to build a "script engine" to execute user input scripts and get the returned value in some complex business applications. Always the user knows little about how to write complex script like JavaScript.

Another requirement may be that the script contains some built-in functions. The functions work differently in different applications. We want the script engine to execute different functions in your different applications or projects.

Also, the script engine should be able to operate on external data. The script's execution caller then can get the data updated by executing the script.

The script engine won't to be as powerful as some like JavaScript engine for web browsers. I built it just to implement some simple script lines. So it's small, cute and hope to have very good performance.

Background

Basically, a script language has operators, data types and functions.

The script engine here supports external functions to be ported to a runtime, just like a plug and play device.

Not only functions can be plug in, the data (in dictionary or data table) can be plug in as well.

Using the Code

It's easy to evaluate a value from a simple script line.

Step 1: Create a context, where you can plug external functions or data.

Step 2: Create a new script processor with context.

Step 3: Evaluate a script line.

C#
ScriptContext context = new ScriptContext();
DictionaryExternalValueProvider evp = new DictionaryExternalValueProvider();
evp.AddValue("dt", DateTime.Today);
evp.AddValue("dt2", DateTime.Today.AddDays(1));
evp.AddValue("dt3", DateTime.Today.AddDays(-1));
evp.AddValue("v", "abc");
evp.AddValue("regex", @"^\d+$");
evp.AddValue("i", 10);
evp.AddValue("f", 10.5);
context.AddPortal(evp);
ScriptProcessor processor = new ScriptProcessor(context);
try
{
    Console.WriteLine(processor.Evaluate<bool>("IsToday(@dt2) && 
	IsToday(@dt) || DoesMatchRegex(\"38883\", @regex);"));
    Console.WriteLine(processor.Evaluate<bool>("!IsToday(@dt) || IsToday(@dt);"));
    Console.WriteLine(processor.Evaluate<bool>("1 >=2 && IsToday(@dt);"));
    Console.WriteLine(processor.Evaluate<bool>("@i >=20;"));
    Console.WriteLine(processor.Evaluate<bool>("@f >=10.4;"));
    Console.WriteLine(processor.Evaluate<bool>("IsAfterToday(@dt3);"));
    Console.WriteLine(processor.Evaluate<bool>("IsBeforeToday(@dt3);"));
    Console.WriteLine(processor.Evaluate<bool>("DoesContainWord(@v, \"d\");"));
    Console.WriteLine(processor.Evaluate<bool>("2!=2 || 1==2;"));
}
catch (Exception e)
{
    Console.WriteLine("Exception: " + e.Message);
}

Points of Interest

Class hierarchy: Everything in script is an "Expression". All script elements inherit from Expression. If you add a new element to the engine, just create a new class inheriting from Expression.

All expressions have got a "parser" class to do parse. The regular expression is used in each parser.

The script code will be parsed to be a "tree view" expressions. The root is an "Expression" and it may have one or more sub expressions. When executing, all sub expressions will be evaluated and a value (of course, an expression as well) will be returned.

History

Initial version at 2009-11-20, built with Visual Studio 2005, based on .NET fx 2.0.
If you have any questions or advice, you are welcome to send emails to me.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Totaldata Web Data Mining(extractweb.com)
China China
Just develop simple softwares. C# is my favorite prgramming languare, though I hope to create a new one...

Comments and Discussions

 
QuestionWhy another script engine ? Pin
Nicolas Penin22-Nov-09 23:16
Nicolas Penin22-Nov-09 23:16 
AnswerRe: Why another script engine ? Pin
Zhang Wangqiu24-Nov-09 16:07
Zhang Wangqiu24-Nov-09 16:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.