Click here to Skip to main content
11,496,146 members (454 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
Add your own
alternative version

Conscript: An embeddable, compiled scripting language for .NET

, 5 Sep 2008 CPOL 92K 987 137
An API for enhancing any .NET application with a scripting language
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
conscript_api_and_demo_source.zip
Conscript_API_&_Demo_Source
Conscript
ConscriptDemo
Conscript.ico
Properties
Settings.settings
vssver2.scc
Resources
Conscript.ico
vssver2.scc
vssver2.scc
Collections
vssver2.scc
Compiler
vssver2.scc
Conscript.doxyfile
Properties
vssver2.scc
Resources
Conscript.ico
vssver2.scc
Runtime
vssver2.scc
vssver2.scc
SampleScripts
BubbleSort.cns
Dungeon.cns
Factorial.cns
HelloWorld.cns
vssver2.scc
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)

Share

About the Author

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

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