Beispiel #1
0
        public void ArrayDeclaration()
        {
            var src    = @"new [1; 2; 1 + 2]";
            var result = Expr.Array(
                Expr.Int(1),
                Expr.Int(2),
                Expr.Add(Expr.Int(1), Expr.Int(2))
                );

            TestParser(src, result);
        }
Beispiel #2
0
        public void Algebraic3()
        {
            var src = @"
type TestType
    Small of int
    Large of int
fun part:TestType (x:int) ->
    if x > 100 then
        (new Large x) as TestType
    else
        new Small x


var a = part 10
new [ a is TestType; a is Small; a is Large ]";

            var result = new NodeBase[]
            {
                Expr.Type(
                    "TestType",
                    Expr.Label("Small", "int"),
                    Expr.Label("Large", "int")
                    ),

                Expr.Fun(
                    "part",
                    "TestType",
                    new [] { Expr.Arg("x", "int") },
                    Expr.If(
                        Expr.Greater(Expr.Get("x"), Expr.Int(100)),
                        Expr.Block(
                            Expr.Cast(
                                Expr.New("Large", Expr.Get("x")),
                                "TestType"
                                )
                            ),
                        Expr.Block(
                            Expr.New("Small", Expr.Get("x"))
                            )
                        )
                    ),

                Expr.Var("a", Expr.Invoke("part", Expr.Int(10))),
                Expr.Array(
                    Expr.Is(Expr.Get("a"), "TestType"),
                    Expr.Is(Expr.Get("a"), "Small"),
                    Expr.Is(Expr.Get("a"), "Large")
                    )
            };

            TestParser(src, result);
        }
Beispiel #3
0
        protected override NodeBase expand(Context ctx, bool mustReturn)
        {
            if (_Wrapper.IsPartiallyApplied)
            {
                // (expr) _ a b _
                // is transformed into
                // (pa0:T1 pa1:T2) -> (expr) (pa0) (a) (b) (pa1)
                var argDefs  = new List <FunctionArgument>();
                var argExprs = new List <NodeBase>();
                for (var idx = 0; idx < _ArgTypes.Length; idx++)
                {
                    if (_ArgTypes[idx] == typeof(UnspecifiedType))
                    {
                        var argName = ctx.Unique.AnonymousArgName();
                        argDefs.Add(Expr.Arg(argName, _Wrapper.ArgumentTypes[idx].FullName));
                        argExprs.Add(Expr.Get(argName));
                    }
                    else
                    {
                        argExprs.Add(Arguments[idx]);
                    }
                }

                return(Expr.Lambda(argDefs, recreateSelfWithArgs(argExprs)));
            }

            if (_Wrapper.IsVariadic)
            {
                var srcTypes = _ArgTypes;
                var dstTypes = _Wrapper.ArgumentTypes;
                var lastDst  = dstTypes[dstTypes.Length - 1];
                var lastSrc  = srcTypes[srcTypes.Length - 1];

                // compress items into an array:
                //     fx a b c d
                // becomes
                //     fx a b (new[ c as X; d as X ])
                if (dstTypes.Length > srcTypes.Length || lastDst != lastSrc)
                {
                    var elemType   = lastDst.GetElementType();
                    var simpleArgs = Arguments.Take(dstTypes.Length - 1);
                    var combined   = Expr.Array(Arguments.Skip(dstTypes.Length - 1).Select(x => Expr.Cast(x, elemType)).ToArray());
                    return(recreateSelfWithArgs(simpleArgs.Union(new[] { combined })));
                }
            }

            return(base.expand(ctx, mustReturn));
        }
Beispiel #4
0
        public void LinqCall()
        {
            var src    = @"new [1; 2].Where (x:int -> x > 1)";
            var result = Expr.Invoke(
                Expr.Array(Expr.Int(1), Expr.Int(2)),
                "Where",
                Expr.Lambda(
                    new [] { Expr.Arg("x", "int") },
                    Expr.Greater(
                        Expr.Get("x"),
                        Expr.Int(1)
                        )
                    )
                );

            TestParser(src, result);
        }
Beispiel #5
0
        /// <summary>
        /// Returns the code to concatenate two arrays.
        /// </summary>
        private NodeBase ArrayExpand(Context ctx)
        {
            var type = Resolve(ctx);

            var tmpArray = ctx.Scope.DeclareImplicit(ctx, type, false);
            var tmpLeft  = ctx.Scope.DeclareImplicit(ctx, type, false);
            var tmpRight = ctx.Scope.DeclareImplicit(ctx, type, false);

            // a = <left>
            // b = <right>
            // c = new T[a.Length + b.Length]
            // Array.Copy(from: a, to: c, count: a.Length)
            // Array.Copy(from: b, startFrom: 0, to: c, startTo: a.Length, count: b.Length)
            return(Expr.Block(
                       Expr.Set(tmpLeft, LeftOperand),
                       Expr.Set(tmpRight, RightOperand),
                       Expr.Set(
                           tmpArray,
                           Expr.Array(
                               type.GetElementType(),
                               Expr.Add(
                                   Expr.GetMember(Expr.Get(tmpLeft), "Length"),
                                   Expr.GetMember(Expr.Get(tmpRight), "Length")
                                   )
                               )
                           ),
                       Expr.Invoke(
                           "System.Array",
                           "Copy",
                           Expr.Get(tmpLeft),
                           Expr.Get(tmpArray),
                           Expr.GetMember(Expr.Get(tmpLeft), "Length")
                           ),
                       Expr.Invoke(
                           "System.Array",
                           "Copy",
                           Expr.Get(tmpRight),
                           Expr.Int(0),
                           Expr.Get(tmpArray),
                           Expr.GetMember(Expr.Get(tmpLeft), "Length"),
                           Expr.GetMember(Expr.Get(tmpRight), "Length")
                           ),
                       Expr.Get(tmpArray)
                       ));
        }
Beispiel #6
0
        public void ManyParameters()
        {
            var src    = @"test 1337 true ""hello"" (new(13.37; new [1; 2]))";
            var result = Expr.Invoke(
                "test",
                Expr.Int(1337),
                Expr.True(),
                Expr.Str("hello"),
                Expr.Tuple(
                    Expr.Double(13.37),
                    Expr.Array(
                        Expr.Int(1),
                        Expr.Int(2)
                        )
                    )
                );

            TestParser(src, result);
        }
Beispiel #7
0
        public void SizedArray()
        {
            var src = @"
let x = new Tuple<string~,System.Uri>[5]
let y = new string[1 + 3]";

            var result = new[]
            {
                Expr.Let(
                    "x",
                    Expr.Array("Tuple<string~,System.Uri>", Expr.Int(5))
                    ),
                Expr.Let(
                    "y",
                    Expr.Array("string", Expr.Add(Expr.Int(1), Expr.Int(3)))
                    ),
            };

            TestParser(src, result);
        }
        /// <summary>
        /// Repeats an array.
        /// </summary>
        private NodeBase ArrayExpand(Context ctx)
        {
            var arrayType = LeftOperand.Resolve(ctx);
            var tmpLeft   = ctx.Scope.DeclareImplicit(ctx, arrayType, false);
            var tmpResult = ctx.Scope.DeclareImplicit(ctx, arrayType, false);
            var tmpRight  = ctx.Scope.DeclareImplicit(ctx, typeof(int), false);
            var tmpIndex  = ctx.Scope.DeclareImplicit(ctx, typeof(int), false);

            // a = <left>
            // b = right
            // result = new T[a.Length * b]
            // for idx in 0..a.Length-1 do
            //    Array::Copy(from: a; to: result; targetIndex: idx * a.Length)
            // result
            return(Expr.Block(
                       Expr.Set(tmpLeft, LeftOperand),
                       Expr.Set(
                           tmpRight,
                           Expr.Invoke(
                               "System.Math",
                               "Abs",
                               RightOperand
                               )
                           ),
                       Expr.Set(
                           tmpResult,
                           Expr.Array(
                               arrayType.GetElementType(),
                               Expr.Mult(
                                   Expr.GetMember(
                                       Expr.Get(tmpLeft),
                                       "Length"
                                       ),
                                   Expr.Get(tmpRight)
                                   )
                               )
                           ),
                       Expr.For(
                           tmpIndex,
                           Expr.Int(0),
                           Expr.Get(tmpRight),
                           Expr.Block(
                               Expr.Invoke(
                                   "System.Array",
                                   "Copy",
                                   Expr.Get(tmpLeft),
                                   Expr.Int(0),
                                   Expr.Get(tmpResult),
                                   Expr.Mult(
                                       Expr.Get(tmpIndex),
                                       Expr.GetMember(
                                           Expr.Get(tmpLeft),
                                           "Length"
                                           )
                                       ),
                                   Expr.GetMember(
                                       Expr.Get(tmpLeft),
                                       "Length"
                                       )
                                   )
                               )
                           ),
                       Expr.Get(tmpResult)
                       ));
        }