Click here to Skip to main content
15,887,928 members
Articles / Containers / Virtual Machine

Conscript: An embeddable, compiled scripting language for .NET

Rate me:
Please Sign up or sign in to vote.
4.97/5 (58 votes)
5 Sep 2008CPOL15 min read 158.9K   1.4K   141  
An API for enhancing any .NET application with a scripting language
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Conscript;
using Conscript.Compiler;
using Conscript.Runtime;

namespace ConscriptDemo
{
    internal partial class MainForm
        : Form
        , ScriptLoader
        , HostFunctionHandler
    {
        #region Private Variables

        // script manager
        ScriptManager m_scriptManager;

        // reference to default script loader
        ScriptLoader m_scriptLoaderDefault;

        #endregion

        #region Private Methods

        private void MainForm_Load(object objectSender, EventArgs eventArgs)
        {
            // initialise script manager
            m_scriptManager = new ScriptManager();

            // keep reference to default loader for include's
            m_scriptLoaderDefault = m_scriptManager.Loader;

            // assign self as script loader
            m_scriptManager.Loader = this;

            // define Print host function for script output
            HostFunctionPrototype hostFunctionPrototype
                = new HostFunctionPrototype((Type)null, "Print", (Type)null);

            // register host function
            m_scriptManager.RegisterHostFunction(hostFunctionPrototype, this);
        }

        private void m_btnRunScript_Click(object objectSender, EventArgs eventArgs)
        {
            m_txtScriptOutput.Text = "";

            Script script = null;
            try
            {
                // attempt to compile script object
                script = new Script(m_scriptManager, null);

                // build successful
                m_txtBuildOutput.Text = "Build succeeded\r\n";

                // display byte code
                foreach (ScriptInstruction scriptInstruction
                    in script.Executable.Instructions)
                    m_txtBuildOutput.Text += "\r\n" + scriptInstruction;
            }
            catch (Exception exception)
            {
                // build failed
                m_txtBuildOutput.Text = "Build failed\r\n";
                m_txtBuildOutput.Text += exception;

                // stop here
                return;
            }

            try
            {
                // create execution context (assuming main function with 
                // no parameters defined)
                ScriptContext scriptContext = new ScriptContext(script);

                // execute script context till termination
                while (!scriptContext.Terminated)
                    scriptContext.Execute();

                // display termination message
                m_txtScriptOutput.Text
                    += "\r\nScript terminated";

            }
            catch (Exception exception)
            {
                // context setup or execution error
                m_txtScriptOutput.Text
                    += "\r\nRuntime error: " + exception;
            }
        }

        #endregion

        #region Public Methods

        public MainForm()
        {
            InitializeComponent();
        }

        public List<String> LoadScript(String strResourceName)
        {
            // if resource name specified, use default disk script loader
            if (strResourceName != null)
                return m_scriptLoaderDefault.LoadScript(strResourceName);

            // null specified - load from text box
            String strSource = m_txtSource.Text;
            strSource = strSource.Replace("\r\n", "\r");
            List<String> listSourceLines = new List<string>();
            listSourceLines.AddRange(strSource.Split('\r'));
            return listSourceLines;
        }

        // implements Print host function
        public object OnHostFunctionCall(String strFunctionName, List<object> listParameters)
        {
            // note: function name can be ignored as only one defined

            // write output to script output text box
            m_txtScriptOutput.Text += "\r\n" + listParameters[0];
            m_txtScriptOutput.Refresh();
            return null;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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)
Malta Malta
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions