Click here to Skip to main content
15,893,814 members
Articles / Programming Languages / F#

Embedded Scripting using F#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
22 Aug 2011CPOL6 min read 52.8K   1K   19  
This article shows how to compile and run F# code during runtime.
/*
    Copyright (C) 2011 Vladimir Ivanovskiy <vivanovsky@gmail.com>.
    All rights reserved. 
    You can use this software under the terms of The Code Project Open License (CPOL) 1.02.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;


namespace FSCompileTestForm
{
    public partial class MainForm : Form
    {
        private StdStreamRedirector.Redirector m_r = null;
        private List<string> m_references;
        private string m_code;
        private string[] m_lineSeparators = { "\r\n" };


        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Action<string> aOut = (text => AppendStdStreamText(text, Color.Black));
            Action<string> aErr = (text => AppendStdStreamText(text, Color.DarkRed));
            m_r = StdStreamRedirector.Init(aOut, aErr);
        }

        private void AppendStdStreamText(string text, Color c)
        {
            if(this.InvokeRequired)
            {
                MethodInvoker del = delegate
                {
                    AppendStdStreamText(text, c);
                };
                this.Invoke(del);
                return;
            }
            AppendColoredText(text, c);
            m_outputTB.AppendText("\n");
        }
        private void AppendColoredText(string text, Color c)
        {
            int l1 = m_outputTB.TextLength;
            m_outputTB.AppendText(text);
            int l2 = m_outputTB.TextLength;
            m_outputTB.SelectionStart = l1;
            m_outputTB.SelectionLength = l2 - l1;
            m_outputTB.SelectionColor = c;
        }

        private List<string> GetReferences()
        {
            List<string> ls = new List<string>(
                m_referencesTB.Text.Split(m_lineSeparators, 
                    StringSplitOptions.RemoveEmptyEntries).AsEnumerable());
            return ls;
        }
        private void RunBtn_Click(object sender, EventArgs e)
        {
            m_references = GetReferences();
            m_code = m_codeTB.Text;
            m_runBtn.Enabled = false;
            m_bw.RunWorkerAsync();
        }

        private void ClearBtn_Click(object sender, EventArgs e)
        {
            m_outputTB.Clear();
        }

        private void WorkerDoWork(object sender, DoWorkEventArgs e)
        {
            FSExecutor.CompileAndExecute(m_codeTB.Text, m_references);   
        }

        private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            m_runBtn.Enabled = true;
        }
    }
}

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)
United States United States
My professional carrier started in 1997. I have broad range of interests including database and web development, compiler tools and technologies. I am experienced in C++, C#/F# and Java. I am watching mobile development with great attention.

Comments and Discussions