Click here to Skip to main content
Click here to Skip to main content

Tagged as

Script Engine Implemented by C# and Regular Expression

, 21 Nov 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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)

Share

About the Author

Zhang Wangqiu
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 ? Pinmemberneon-np23-Nov-09 0:16 
Hi,
 
Why would need to do something like this while Jint can do this for you and even much more ?
AnswerRe: Why another script engine ? PinmemberZhang Wangqiu24-Nov-09 17:07 

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

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

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