Click here to Skip to main content
13,512,061 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


24 bookmarked
Posted 19 Nov 2009

Script Engine Implemented by C# and Regular Expression

, 21 Nov 2009
Rate this:
Please Sign up or sign in to vote.
Script engine to execute script codes, which is built by C# and regular expression


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.


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.

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);
ScriptProcessor processor = new ScriptProcessor(context);
    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>("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.


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.


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


About the Author

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

You may also be interested in...


Comments and Discussions

QuestionWhy another script engine ? Pin
neon-np22-Nov-09 23:16
memberneon-np22-Nov-09 23:16 
AnswerRe: Why another script engine ? Pin
Zhang Wangqiu24-Nov-09 16:07
memberZhang 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180417.1 | Last Updated 22 Nov 2009
Article Copyright 2009 by Zhang Wangqiu
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid