コード例 #1
0
ファイル: CompilerTest.cs プロジェクト: mrcsparker/Crispy
        public CompilerTest()
        {
            string   dllPath = typeof(object).Assembly.Location;
            Assembly asm     = Assembly.LoadFile(dllPath);

            _Crispy = new Crispy(new[] { asm });
        }
コード例 #2
0
        public void ShouldBeAbleToCallCompiler()
        {
            string   dllPath = typeof(object).Assembly.Location;
            Assembly asm     = Assembly.LoadFile(dllPath);

            var Crispy = new Crispy(new[] { asm });
            //Crispy.ExecuteCode('1');
        }
コード例 #3
0
        public InstanceObjectLoad()
        {
            string   dllPath = typeof(object).Assembly.Location;
            Assembly asm     = Assembly.LoadFile(dllPath);

            var metricsModel = MakeSimpleMetricsModel();

            _Crispy = new Crispy(new[] { asm }, new [] { metricsModel });
        }
コード例 #4
0
        public ExternalObjectInjectionTest()
        {
            string   dllPath = typeof(object).Assembly.Location;
            Assembly asm     = Assembly.LoadFile(dllPath);

            IEnumerable <Product> productList = GetProductList();

            _product = productList.First();

            _Crispy = new Crispy(new[] { asm }, new [] { _product });
        }
コード例 #5
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);
        }
コード例 #6
0
        // CrispyImport takes the runtime and module as context for the import.
        // It takes a list of names, what, that either identify a (possibly dotted
        // sequence) of names to fetch from Globals or a file name to load.  Variables
        // is a list of names to fetch from the final object that what indicates
        // and then set each name in module.  Renames is a list of names to add to
        // module instead of names.  If names is empty, then the name set in
        // module is the last name in what.  If renames is not empty, it must have
        // the same cardinality as names.
        //
        public static object CrispyImport(Crispy runtime, ExpandoObject module,
                                          string[] names, string[] nameAsArr)
        {
            var nameAs = nameAsArr [0];

            // Get object or file scope.
            object value = null;

            if (names.Length == 1)
            {
                string name = names[0];
                if (DynamicObjectHelpers.HasMember(runtime.Globals, name))
                {
                    value = DynamicObjectHelpers.GetMember(runtime.Globals, name);
                }
                else
                {
                    string f = (string)(DynamicObjectHelpers
                                        .GetMember(module, "__file__"));
                    f = Path.Combine(Path.GetDirectoryName(f), name + ".sympl");
                    if (File.Exists(f))
                    {
                        value = runtime.ExecuteFile(f);
                    }
                    else
                    {
                        throw new ArgumentException(
                                  "Import: can't find name in globals " +
                                  "or as file to load -- " + name + " " + f);
                    }
                }
            }
            else
            {
                // What has more than one name, must be Globals access.
                value = runtime.Globals;
                // For more correctness and generality, shouldn't assume all
                // globals are dynamic objects, or that a look up like foo.bar.baz
                // cascades through all dynamic objects.
                // Would need to manually create a CallSite here with Crispy's
                // GetMemberBinder, and think about a caching strategy per name.
                foreach (string name in names)
                {
                    value = DynamicObjectHelpers.GetMember(
                        (IDynamicMetaObjectProvider)value, name);
                }
            }

            if (nameAs != null)
            {
                DynamicObjectHelpers.SetMember(
                    (IDynamicMetaObjectProvider)module, nameAs,
                    value);
            }
            else
            {
                DynamicObjectHelpers.SetMember(
                    (IDynamicMetaObjectProvider)module, names[names.Length - 1],
                    value);
            }

            return(null);
        } // CrispyImport