コード例 #1
0
ファイル: FunctionTest.cs プロジェクト: mrcsparker/Crispy
        public void ShouldBeAbleToCreateASimpleFunction()
        {
            const string text = @"
import System.Console as console
import System.Math as math
import System.Collections as collections

// Prints output
defun print(str) {
    console.WriteLine(str)
}

defun princ(str) {
    console.Write(str)
}

defun array() {
    new collections.ArrayList()      
}

var x = array()

x.add('xxx')
x.add(2)
x.add('abc')

print(x[0])

x[0] = 'yyy'

var add2 = lambda(x, y) {
    x + y
}

add2(3, 4)

defun map(fn, a) {

    var i = 0

    loop {
        a[i] = fn(a[i])

        i = i + 1

        if (i == a.count) {
            break
        }     
    }
}

var a = array();
a.add(1)
a.add(2)
a.add(3)

map(lambda(x) { x * 2 }, a);

defun printArr(a) {
    i = 0

    loop {

        print(a[i])

        i = i + 1

        if (i == a.count) {
            break(a)
        }        
    }
}

var x = printArr(a)
x

defun foo() {
    15
}

var y = foo();

";

            const string text5 = @"

import System.Dynamic as dynamic
import Newtonsoft.Json as json

defun objn() {
    new dynamic.ExpandoObject()
}

var foo = objn();
foo.Bar = 'something'

var out = json.JsonConvert.SerializeObject(foo);
";

            string   dllPath = typeof(object).Assembly.Location;
            Assembly asm     = Assembly.LoadFile(dllPath);

            string   dynamicObjectPath = typeof(System.Dynamic.ExpandoObject).Assembly.Location;
            Assembly dynamicObjectAsm  = Assembly.LoadFile(dynamicObjectPath);

            string   jsonObjectPath = typeof(Newtonsoft.Json.JsonConvert).Assembly.Location;
            Assembly jsonAsm        = Assembly.LoadFile(jsonObjectPath);

            Console.WriteLine(asm);

            var Crispy = new Crispy(new[] { asm, dynamicObjectAsm, jsonAsm });
            var output = Crispy.ExecuteExpr(text, new ExpandoObject());

            Console.WriteLine(output);
        }
コード例 #2
0
ファイル: CompilerTest.cs プロジェクト: mrcsparker/Crispy
        public void TestAdditiveNode()
        {
            var result0 = (int)_Crispy.ExecuteExpr("100 + 2", _scope);

            Assert.AreEqual(102, result0);

            var result1 = (double)_Crispy.ExecuteExpr("100.1 + 100.2", _scope);

            Assert.AreEqual(200.3, result1);

            var result2 = (int)_Crispy.ExecuteExpr("1-1", _scope);

            Assert.AreEqual(0, result2);
        }
コード例 #3
0
 public object Exec(string text)
 {
     return(_Crispy.ExecuteExpr(text, new ExpandoObject()));
 }