Click here to Skip to main content
13,350,029 members (46,322 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads


137 bookmarked
Posted 13 Jul 2007

Conscript: An embeddable, compiled scripting language for .NET

, 5 Sep 2008
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;


        #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;
                // 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

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

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

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

            catch (Exception exception)
                // context setup or execution error
                    += "\r\nRuntime error: " + exception;


        #region Public Methods

        public MainForm()

        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>();
            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];
            return null;


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.


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


About the Author

Colin Vella
Software Developer (Senior)
Malta Malta
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 5 Sep 2008
Article Copyright 2007 by Colin Vella
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid