Hosting a scheme script in C# .net

Mar 22, 2009 at 11:25 AM
Edited Mar 22, 2009 at 3:06 PM
Hi all,

I am trying to run scheme as a script file for some text file manipulation in C# window application. I have 2 questions and hope that someone can point me in the right direction
1) Is it possible for ironscheme to open and evaluate a external scheme file, such as a PLT Scheme format in .scm?
2) Is it possible for ironscheme in C# to input a argument for the scheme codes? E.g. i want a specific argument to be enter in scheme, is it possible to pass in from C#?

            IronScheme.Runtime.Builtins.Load("~/ironscheme.boot.pp");
            ScriptDomainManager mgr = ScriptDomainManager.CurrentManager;
            IronSchemeLanguageProvider slp = new IronSchemeLanguageProvider(mgr);
            ScriptEngine se = slp.GetEngine();

            String file = @"C:\Documents and Settings\razer\Desktop\test.txt";
            StreamReader sr = new StreamReader(file);
            string text = sr.ReadToEnd();
            textBox2.Text = se.Evaluate(string.Format(text)).ToString();

Is it possible in what i am trying to do?

Thanks in advance
Coordinator
Mar 22, 2009 at 8:30 PM
Edited Mar 22, 2009 at 8:31 PM
Hi

1. If the code was written in portable (case-sensitive) R5RS scheme it should be no problem. It would simply require an import  at the top of the file.

Example:

(import (rnrs))

... rest of the file ...

2. The best way to do this is by exposing a scheme function.

Example:

In Scheme (evaluate this, the foo function is created in the interaction environment):
(define (foo x)
  (let ((x (string-append x "\n")))
    (display x)
    x))

In C#:
ICallable foo = se.Evaluate( "foo") as ICallable;
object result = foo.Call("hello world");
Console.Write(result);

The above will print "hello world" twice.

Hope this helps (i didn't test it but it should work).

Cheers

leppie
Mar 23, 2009 at 8:29 AM
Hi,

Thanks for replying my question. However i encounter a error when ever i reach the object result = foo.Call("hello world");

Error message is a NullReferenceException was unhandled. It seems that the foo reference is not set to an instance of an object...

Sorry for the trouble.
Coordinator
Mar 23, 2009 at 11:27 AM
No trouble. I will try it tonite from home and see what the problem is.

Cheers

leppie

From: razer_1

Hi,

Thanks for replying my question. However i encounter a error when ever i reach the object result = foo.Call("hello world");

Error message is a NullReferenceException was unhandled. It seems that the foo reference is not set to an instance of an object...

Sorry for the trouble.

Coordinator
Mar 23, 2009 at 5:09 PM
Ok, I worked it out, your ordering was a bit wrong  :) Here is a working sample.  I will checking the sample project too.

      var slp = new IronSchemeLanguageProvider(ScriptDomainManager.CurrentManager);
      IronScheme.Runtime.Builtins.Load("~/ironscheme.boot.pp");

      ScriptEngine se = slp.GetEngine();

      se.Evaluate("(define (foo x) x)");

      var foo = se.Evaluate("foo") as ICallable;

      var result = foo.Call("hello world");

      Console.WriteLine(result);
Coordinator
Mar 23, 2009 at 9:01 PM
I have improved/simplified loading.  Here is the code from the latest sample (you will require the latest checkin, probably in an hour).

class Program
{
  static void Main(string[] args)
  {
    var slp = new IronSchemeLanguageProvider();
    var se = slp.GetEngine();

    se.Evaluate(@"
(define (foo x)
  (let ((x (string-append x ""\n"")))
    (display x)
    x))
");

    var foo = se.Evaluate("foo") as ICallable;
    var result = foo.Call("hello world");
    Console.Write(result);

    // this should become quite funky in C# 4.0  :)
    var bar = se.Evaluate("(lambda x (for-each display (reverse x))(newline))") as ICallable;
    bar.Call(1, 2, 3, 4, 5);
    
    Console.ReadLine();
  }
}