Exemple #1
0
        public static Array ReplacePlaceholders(Block block)
        {
            var array = new Array();

            foreach (var verb in block.AsAdded)
            {
                Value value;
                switch (verb)
                {
                case Push push:
                    value = push.Value;
                    break;

                case PushArrayLiteral pushArrayLiteral:
                    value = pushArrayLiteral.Array;
                    break;

                default:
                    continue;
                }

                if (value is Variable variable)
                {
                    var name = variable.Name;
                    value = name == "_" ? (Value) new Any() : new Placeholder(name);
                }
                array.Add(value);
            }

            return(array);
        }
Exemple #2
0
        static Array FromFields(INSGenerator generator, Parameters parameters, bool readOnly, bool setting, bool overriding, Func <Value, Value> map)
        {
            var iterator           = new NSIterator(generator);
            var array              = new Array();
            var start              = 0;
            var actualParameters   = parameters.GetParameters();
            var length             = actualParameters.Length;
            var assignments        = new Hash <string, Value>();
            var assignedParameters = new Hash <string, Parameter>();

            for (var i = start; i < length; i++)
            {
                start = i;
                var parameter = actualParameters[i];
                var value     = iterator.Next();
                if (value.IsNil)
                {
                    break;
                }

                assignments[parameter.Name]        = value;
                assignedParameters[parameter.Name] = parameter;
                array[parameter.Name] = value;
            }

            if (start <= length - 1)
            {
                var innerArray = new Array {
                    array[parameters[start].Name]
                };
                for (var i = start; i < MAX_LOOP; i++)
                {
                    var value = iterator.Next();
                    if (value.IsNil)
                    {
                        break;
                    }

                    innerArray.Add(value);
                }

                if (innerArray.Length > 0)
                {
                    var value = map(innerArray);
                    assignments[parameters[start].Name]        = value;
                    assignedParameters[parameters[start].Name] = parameters[start];
                    array[parameters[start].Name] = value;
                }
            }

            foreach (var(key, value) in assignments)
            {
                if (assignedParameters.If(key, out var parameter))
                {
                    parameter.Assign(value, readOnly, setting, overriding);
                }
            }

            return(array);
        }
Exemple #3
0
        static Array fromValue(Value source, int count)
        {
            var array = new Array();

            for (var i = 0; i < count; i++)
            {
                array.Add(source);
            }

            return(array);
        }
Exemple #4
0
        static Array fromValue(Value source, INSGenerator count)
        {
            var countSource = (Array)count.Array();
            var array       = new Array();
            var outerCount  = countSource[0].Int;
            var innerCount  = countSource[1].Int;

            for (var i = 0; i < outerCount; i++)
            {
                array.Add(fromValue(source, innerCount));
            }

            return(array);
        }
Exemple #5
0
        public Value Next()
        {
            if (++index >= maxLength)
            {
                return(new Nil());
            }
            var newArray = new Array();

            foreach (var array in arrayOfArrays.Select(item => (Array)item.Value))
            {
                newArray.Add(array[index]);
            }
            return(newArray);
        }
Exemple #6
0
        public static Array ToArray(INSGenerator generator)
        {
            var array    = new Array();
            var iterator = new NSIterator(generator);

            iterator.Reset();
            for (var i = 0; i < MAX_ARRAY; i++)
            {
                var next = iterator.Next();
                if (next.IsNil)
                {
                    break;
                }

                array.Add(next);
            }

            return(array);
        }
Exemple #7
0
        static Array FromFields(INSGenerator generator, Parameters parameters, bool readOnly, bool setting,
                                bool _override, Func <Value, Value> map)
        {
            var iterator           = new NSIterator(generator);
            var array              = new Array();
            var start              = 0;
            var actuals            = parameters.GetParameters();
            var length             = actuals.Length;
            var assignments        = new Hash <string, Value>();
            var assignedParameters = new Hash <string, Parameter>();

            for (var i = start; i < length; i++)
            {
                start = i;
                var parameter = actuals[i];
                var value     = iterator.Next();
                if (value.IsNil)
                {
                    break;
                }

                assignments[parameter.Name]        = value;
                assignedParameters[parameter.Name] = parameter;
                array[parameter.Name] = value;
            }

            if (start <= length - 1)
            {
                var innerArray = new Array {
                    array[parameters[start].Name]
                };
                for (var i = start; i < MAX_LOOP; i++)
                {
                    var value = iterator.Next();
                    if (value.IsNil)
                    {
                        break;
                    }

                    innerArray.Add(value);
                }

                if (innerArray.Length > 0)
                {
                    var value = map(innerArray);
                    assignments[parameters[start].Name]        = value;
                    assignedParameters[parameters[start].Name] = parameters[start];
                    array[parameters[start].Name] = value;
                }
            }

            // ReSharper disable once LoopCanBePartlyConvertedToQuery
            foreach (var item in assignments)
            {
                if (assignedParameters.If(item.Key, out var parameter))
                {
                    parameter.Assign(item.Value, readOnly, setting, _override);
                }
            }

            return(array);
        }
        public static (Block, Block) GetComparisand(Block block)
        {
            var   variable      = new Variable();
            var   builder       = new CodeBuilder();
            var   bindingName   = "";
            var   bindNextValue = false;
            var   iffing        = false;
            Block condition     = null;

            foreach (var verb in block.AsAdded)
            {
                if (iffing)
                {
                    condition.Add(verb);
                    continue;
                }

                Value     value;
                string    name;
                Arguments arguments;
                switch (verb)
                {
                case Bind _:
                    builder.PopLastVerb();
                    bindingName   = variable?.Name ?? "_";
                    bindNextValue = true;
                    break;

                case If _:
                    iffing    = true;
                    condition = new Block();
                    break;

                case ToList toList:
                    var list      = new List();
                    var listBlock = toList.Block;
                    foreach (var listVerb in listBlock.AsAdded)
                    {
                        switch (listVerb)
                        {
                        case Push listPush:
                            value = listPush.Value;
                            break;

                        case PushArrayLiteral pushArrayLiteral:
                            value = pushArrayLiteral.Array;
                            break;

                        default:
                            continue;
                        }

                        if (value is Variable listVariable)
                        {
                            name  = listVariable.Name;
                            value = evaluateVariable(name);
                        }
                        list = list.Add(value);
                    }

                    builder.Value(list);
                    break;

                case SendMessage sendMessage:
                    var lastVerb = builder.Last;
                    if (lastVerb is Push push1 && push1.Value is Placeholder placeholder)
                    {
                        builder.PopLastVerb();
                        builder.Variable(placeholder.Text);
                    }
                    arguments = sendMessage.Arguments;
                    arguments = convertArguments(arguments);
                    builder.SendMessage(sendMessage.Message, arguments);
                    break;

                case Or or:
                    var expression = or.Expression;
                    (var expressionBlock, _) = GetComparisand(expression);
                    builder.Verb(new AppendToAlternation());
                    builder.Inline(expressionBlock);
                    break;

                case CreateRecord createRecord:
                    var members    = createRecord.Members;
                    var newMembers = new Hash <string, Thunk>();
                    foreach (var item in members)
                    {
                        (var thunkBlock, _)  = GetComparisand(item.Value.Block);
                        newMembers[item.Key] = new Thunk(thunkBlock, item.Value.Region);
                    }

                    builder.Verb(new CreateRecord(newMembers, createRecord.FieldName));
                    break;

                case Push push:
                    value = push.Value;

                    switch (value)
                    {
                    case Variable variable1:
                        name  = variable1.Name;
                        value = evaluateVariable(name);
                        break;

                    case Array array:
                        var newArray = new Array();
                        foreach (var item in array)
                        {
                            if (item.Value is Variable variable1)
                            {
                                name  = variable1.Name;
                                value = evaluateVariable(name);
                            }
                            else
                            {
                                value = item.Value;
                            }
                            newArray.Add(value);
                        }

                        value = newArray;
                        break;

                    case Block pushedBlock:
                        (var retrievedBlock, _) = GetComparisand(pushedBlock);
                        value = retrievedBlock;
                        break;
                    }

                    if (bindNextValue)
                    {
                        builder.Verb(new BindValue(bindingName, value));
                        bindNextValue = false;
                    }
                    else
                    {
                        builder.Value(value);
                    }
                    break;

                case PushSome pushSome:
                    var someBlock = pushSome.Expression;
                    (someBlock, _) = GetComparisand(someBlock);
                    builder.Verb(new PushSome(someBlock));
                    break;

                case FunctionInvoke functionInvoke:
                    var functionName = functionInvoke.FunctionName;
                    arguments = functionInvoke.Arguments;
                    arguments = convertArguments(arguments);
                    builder.FunctionInvoke(functionName, arguments);
                    break;

                default:
                    builder.Verb(verb);
                    break;
                }
            }

            if (condition != null)
            {
                condition.AutoRegister = false;
            }
            return(builder.Block, condition);
        }