示例#1
0
文件: Builtin.cs 项目: wj32/Lambda
        public Expression CreateList(IEnumerable<Expression> list)
        {
            BuiltinList head = null;
            BuiltinList tail = null;

            foreach (var item in list)
            {
                if (head == null)
                {
                    head = new BuiltinList { First = item, Second = null };
                    tail = head;
                }
                else
                {
                    tail.Second = new BuiltinList { First = item, Second = null };
                    tail = tail.Second;
                }
            }

            return CreateList(head);
        }
示例#2
0
文件: Builtin.cs 项目: wj32/Lambda
 public Expression CreateList(BuiltinList head)
 {
     return new FreeSymbolExpression { Name = "_List", Tag = head, Display = _ListDisplay };
 }
示例#3
0
文件: Evaluator.cs 项目: wj32/Lambda
        public static bool RecognizeList(Expression expression, out BuiltinList value)
        {
            BuiltinList head = null;
            BuiltinList tail = null;
            Expression current = expression;

            // \x,y,z.y is Null
            // \f.f <FIRST> <SECOND> is Pair

            value = null;

            while (true)
            {
                if (current is AbstractionExpression)
                {
                    var abs1 = (AbstractionExpression)current;

                    if (abs1.Right is ApplicationExpression)
                    {
                        var app1 = (ApplicationExpression)abs1.Right;

                        if (app1.Left is ApplicationExpression)
                        {
                            var app2 = (ApplicationExpression)app1.Left;

                            if (app2.Left is BoundSymbolExpression)
                            {
                                var bound = (BoundSymbolExpression)app2.Left;

                                if (bound.Symbol == abs1.Left)
                                {
                                    // Pair

                                    if (head == null)
                                    {
                                        head = new BuiltinList { First = app2.Right, Second = null };
                                        tail = head;
                                    }
                                    else
                                    {
                                        tail.Second = new BuiltinList { First = app2.Right, Second = null };
                                        tail = tail.Second;
                                    }

                                    current = app1.Right;
                                    continue;
                                }
                            }
                        }
                    }
                    else if (abs1.Right is AbstractionExpression)
                    {
                        var abs2 = (AbstractionExpression)abs1.Right;

                        if (abs2.Right is AbstractionExpression)
                        {
                            var abs3 = (AbstractionExpression)abs2.Right;

                            if (abs3.Right is BoundSymbolExpression)
                            {
                                var bound = (BoundSymbolExpression)abs3.Right;

                                if (bound.Symbol == abs2.Left)
                                {
                                    // Null
                                    value = head;
                                    return true;
                                }
                            }
                        }
                    }
                }

                return false;
            }
        }