This project is read-only.
Project Description
j9Script is a recursive descent interpreter written in C#. The interpreter executes scripts using the j9Script language which is a simplified, case-insensitive, strongly typed ECMAScript-like language.

Object libraries can easily be added to integrate with your application.

How does it differ from JavaScript and ECMAScript?
  • The language is case-insensitive including keywords and identifiers.
  • Function declarations specify the datatype of each parameter.
  • Function overloading is supported.
  • Variables are valid in the scope they were created in and below.
  • The built-in classes closely following the .NET framework (Ex. A j9Script String exposes the same members as System.String).
  • The built-in primitive datatypes are: String, Number, Boolean, DateTime, TimeSpan, Array, ByteArray, Dictionary.
  • The built-in framework classes are: Math, FileSystem, OleDbConnection, OdbcConnection, DataTable, TextFileReader, TextFileReader, RegEx.
  • Custom types can be created using .NET languages.
  • Custom global functions can be created using .NET languages.
  • Supports built-in metadata for object discovery, documentation, and source code editor auto-completion.
  • There is currently no support for a switch statement.
  • Classes can not be created from script (only .NET).

Why another scripting language?
j9Script was born in 1996 as a recursive descent graduate school project written in C (now in C#). While JavaScript and C were the inspiration for the language syntax, j9Script is a simpler language to learn for beginners. Since 1996 j9Script has had several other code branches included in multiple commercial applications as well as a global enterprise application.

C# Examples

Simple Example:
string script = "return 10 + 5;";
IScriptObject result = ScriptEvaluator.EvaluateNow(script);

Example with exception handling:
    string script = "return 10 + 5;";
    IScriptObject result = ScriptEvaluator.EvaluateNow(txtScript.Text);
catch (ScriptEvaluatorException ex)
    Trace.Write(ex.Message + "\r\n" + ex.Position.ToString() + "\r\n\r\n" + "Script Snippet:\r\n" + ex.ScriptSnippet);
catch (Exception ex)

Example using predefined variables:
var vars = new ScriptVariableCollection();
vars["name"] = new ScriptString("John Hughes"); // the script changes this to "Sam".
vars["x"] = new ScriptNumber(19); // this does not change.
vars["b"] = ScriptBoolean.True; // after the script runs, notice how the datatype of this will change from Boolean to DateTime.

string script = "name = \"Sam\"; b = DateTime.Now;";

IScriptObject result = ScriptEvaluator.EvaluateNow(script, vars, null);

Example using custom classes:
var types = new ScriptObjectTypeInfoCollection();
types.Add(typeof(ScriptFibonacci), "Fibonacci");
types.Add(typeof(ScriptPerson), "Person");

ScriptEvaluator eval = new ScriptEvaluator(); // reusing an instance of ScriptEvaluator provides better performance.

string script1 = "s=\"\"; for (i=0; i<10; i++) s = s + Fibonacci.Calculate(i) + \", \"; return s;";
IScriptObject result1 = eval.Evaluate(script1, types);

string script2 = "p = new Person(\"Sam\", 10); p.Age = p.Age + 2; return p.GetInfo();";
IScriptObject result2 = eval.Evaluate(script2, types);

The source code includes an example project as well.

Last edited Sep 20, 2011 at 9:04 PM by JohnHughes, version 18