How to evaluate scheme code from C#
This is just a small example on how to do a basic integration of a small IronScheme evaluator in a C# project.
* Make a WPF App project in Visual Studio

* Edit the xaml to something simple:
<Window x:Class="WpfSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Simple Scheme Embedding" Height="320" Width="300">
<Grid>
<StackPanel>
<Label FontWeight="Bold">Scheme Result:</Label>
<TextBox x:Name="DisplayArea" Height="100" IsReadOnly="True"></TextBox>
<Label FontWeight="Bold">Input:</Label>
<TextBox x:Name="Input" Height="100"></TextBox>
<Button Click="Evaluate_Click">Evaluate</Button>
</StackPanel>
</Grid>
</Window>
* Now we need to handle the Evaluate_Click in the code-behind file, but let us get some scheme integration first.
* Go to the project references and browse to the ironscheme install directory and just add all the .dll files. Remember to include the ironscheme boot.dll and the Microsoft Scripting dll too.
* Let's also isolate the scheme-communication in a schemehandler. Remember that this example is meant to be really simple:
namespace WpfAndScheme
{
using IronScheme; // the extension methods are exported from this namespace
public class SchemeHandler
{
public object Evaluate(string input)
{
return input.Eval(); // calls IronScheme.RuntimeExtensions.Eval(string)
}
}
}
* The above code could also be directly embedded in the sample below.
* Now make the code-behind something like:
namespace WpfAndScheme
{
using System.Windows;
public partial class Window1 : Window
{
private SchemeHandler schemeHandler;
public Window1()
{
InitializeComponent();
schemeHandler = new SchemeHandler();
}
private void Evaluate_Click(object sender, RoutedEventArgs e)
{
DisplayArea.Text = schemeHandler.Evaluate(Input.Text).ToString();
}
}
}
* Now you should be able to build and run the app. Enter an expression in the input window, e.g (+ 1 2 3) and with some luck you should get 6 displayed in the output window.
