Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public static object ToType(this Value value, string type, bool isArray)
        {
            if (isArray)
            {
                if (value.IsArray)
                {
                    value = value.SourceArray;
                    var array = (Array)value;
                    return(array.Values.Select(v => v.ToType(type, false)).ToArray());
                }

                var newArray = new Array(Runtime.State.FieldPattern.Split(value.Text));
                return(ToType(newArray, type, true));
            }

            return(type switch
            {
                "system.int32" => value.Number,
                "system.int64" => value.Number,
                "system.int16" => value.Number,
                "system.byte" => value.Number,
                "system.double" => value.Number,
                "system.single" => value.Number,
                "system.boolean" => value.IsTrue,
                "system.string" => value.Text,
                _ => throw $"Didn't understand type {type}".Throws()
            });
Ejemplo n.º 3
0
 public Runtime(string text = "", IFileCache fileCache = null)
 {
     Sys                   = new Sys();
     stack                 = new ValueStack();
     blockManager          = new BlockManager();
     printBuffer           = new Buffer();
     patternManager        = new PatternManager();
     Regions               = new RegionManager(Sys, text);
     expressionManager     = new ExpressionManager();
     FieldPattern          = (Pattern)(STRING_BEGIN_PATTERN + "+" + STRING_END_PATTERN);
     RecordPattern         = (Pattern)(STRING_BEGIN_PATTERN + "'`r`n' | '`r' | '`n'" + STRING_END_PATTERN);
     FieldSeparator        = (String)" ";
     RecordSeparator       = (String)"\r\n";
     seed                  = DateTime.Now.Millisecond;
     random                = new Random(seed);
     valueStacks           = new Stack <ValueStack>();
     FileCache             = fileCache ?? new StandardFileCache();
     takeArray             = new Array();
     defaultParameterNames = new Stack <DefaultParameterNames>();
     defaultParameterNames.Push(new DefaultParameterNames(true));
     buffer                   = new Buffer();
     ArgumentDepth            = 0;
     invokeables              = new Hash <string, IInvokeable>();
     extenders                = new Hash <string, InvokeableReference>();
     Parser.InClassDefinition = false;
     consoleManager           = new ConsoleManager();
     indent                   = "";
 }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        public void SetValues(Block actualArguments, Block executable, bool resolveArguments)
        {
            Runtime.State.SetLocal(Runtime.VAR_ARGUMENTS, actualArguments);
            Array values = resolveArguments ? actualArguments.ToActualArguments() : actualArguments.ToDefinedArguments();
            int   min    = Math.Min(variableNames.Count, values.Length);

            for (var i = 0; i < min; i++)
            {
                Value value = values[i];
                Runtime.State.SetLocal(variableNames[i], value.AssignmentValue());
            }
            Runtime.State.SetLocal(Runtime.VAR_BLOCK, executable ?? new NullBlock());

            Array variables = values;

            if (resolveArguments)
            {
                variables = actualArguments.ToDefinedArguments();
            }

            for (var i = 0; i < min; i++)
            {
                Value value = variables[i];
                Runtime.State.SetLocal(Runtime.VAR_INDIRECT + i, value.IsVariable ? ((Variable)value).Name : "");
            }
        }
Ejemplo n.º 6
0
 protected static Value tabs(Value[] values)
 {
     return(values.Length switch
     {
         0 => "\t",
         _ => "\t".Repeat(values[0].Int)
     });
Ejemplo n.º 7
0
 void findMissing(Hash <string, string> missing, string[] messages, Func <string, bool> func, string interfaceName)
 {
     foreach (string message in messages.Where(m => !func(m)))
     {
         missing[message] = interfaceName;
     }
 }
Ejemplo n.º 8
0
        protected static Value put(Value[] values)
        {
            foreach (var value in values)
            {
                State.ConsoleManager.Put(ValueAsString(value));
            }

            return(null);
        }
Ejemplo n.º 9
0
        static Array fromValue(Value source, int count)
        {
            var array = new Array();

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

            return(array);
        }
Ejemplo n.º 10
0
 public ParallelZip(Array arrayOfArrays)
 {
     this.arrayOfArrays = arrayOfArrays;
     index     = -1;
     maxLength = -1;
     foreach (var value in this.arrayOfArrays.Select(item => item.Value))
     {
         value.IsArray.Must().BeTrue().OrThrow(LOCATION, () => $"Item {value} in array of arrays not an array");
         var array = (Array)value.SourceArray;
         maxLength = Max(maxLength, array.Length);
     }
 }
Ejemplo n.º 11
0
 public ParallelZip(Array arrayOfArrays)
 {
     this.arrayOfArrays = arrayOfArrays;
     index     = -1;
     maxLength = -1;
     foreach (var value in this.arrayOfArrays.Select(item => item.Value))
     {
         Assert(value.IsArray, LOCATION, $"Item {value} in array of arrays not an array");
         var array = (Array)value.SourceArray;
         maxLength = Max(maxLength, array.Length);
     }
 }
Ejemplo n.º 12
0
        bool compareArrayNone(Array x, Array y)
        {
            var length = Math.Min(x.Length, y.Length);

            for (var i = 0; i < length; i++)
            {
                if (Compare(x[i].Compare(y[i])))
                {
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 13
0
        public static string FixText(string text, string input, ref string needle, ref char[] needleChars)
        {
            if (State.IgnoreCase)
            {
                text  = text.ToLower();
                input = input.ToLower();
            }

            needle ??= Expand(text);

            needleChars ??= needle.ToCharArray();

            return(input);
        }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
0
        bool compareArrayOne(Value x, Array y)
        {
            var found = false;

            foreach (var _ in y.Where(item => Compare(x.Compare(item.Value))))
            {
                if (found)
                {
                    return(false);
                }
                found = true;
            }
            return(found);
        }
Ejemplo n.º 16
0
        bool compareArrayOne(Array x, Value y)
        {
            var found = false;

            foreach (var _ in x.Where(item => Compare(item.Value.Compare(y))))
            {
                if (found)
                {
                    return(false);
                }
                found = true;
            }
            return(found);
        }
Ejemplo n.º 17
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);
        }
Ejemplo n.º 18
0
        protected static Value peek(Value[] values)
        {
            switch (values.Length)
            {
            case 1:
                State.ConsoleManager.ConsolePrintln(ValueAsString(values[0]));
                return(values[0]);

            case 2:
                State.ConsoleManager.ConsolePrintln(ValueAsString(values[0]));
                return(values[1]);

            default:
                return(NilValue);
            }
        }
Ejemplo n.º 19
0
        public static object ToType(this Value value, string type, bool isArray)
        {
            if (isArray)
            {
                if (value.IsArray)
                {
                    value = value.SourceArray;
                    var array = (Array)value;
                    return(array.Values.Select(v => v.ToType(type, false)).ToArray());
                }

                var newArray = new Array(Runtime.State.FieldPattern.Split(value.Text));
                return(ToType(newArray, type, true));
            }

            switch (type)
            {
            case "system.int32":
                return((int)value.Number);

            case "system.int64":
                return((long)value.Number);

            case "system.int16":
                return((short)value.Number);

            case "system.byte":
                return((byte)value.Number);

            case "system.double":
                return(value.Number);

            case "system.single":
                return((float)value.Number);

            case "system.boolean":
                return(value.IsTrue);

            case "system.string":
                return(value.Text);

            default:
                throw $"Didn't understand type {type}".Throws();
            }
        }
Ejemplo n.º 20
0
        protected static Value write(Value[] values)
        {
            switch (values.Length)
            {
            case 0:
                return(null);

            case 1:
                var asString = ValueAsRep(values[0]);
                State.ConsoleManager.ConsolePrint(asString);
                return(null);

            default:
                var text = values.Select(ValueAsRep).ToString("");
                State.ConsoleManager.ConsolePrint(text);
                return(null);
            }
        }
Ejemplo n.º 21
0
        bool compareArrayOne(Array x, Array y)
        {
            var found  = false;
            var length = Math.Min(x.Length, y.Length);

            for (var i = 0; i < length; i++)
            {
                if (!Compare(x[i].Compare(y[i])))
                {
                    continue;
                }
                if (found)
                {
                    return(false);
                }
                found = true;
            }
            return(found);
        }
Ejemplo n.º 22
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);
        }
Ejemplo n.º 23
0
        public override Value Evaluate()
        {
            var stack    = State.Stack;
            var value    = stack.Pop(true, LOCATION);
            var keyValue = stack.Pop(false, LOCATION);

            if (keyValue.IsArray && value.IsArray)
            {
                var keys      = (Array)keyValue.Resolve().SourceArray;
                var values    = (Array)value.SourceArray;
                var maxLength = Math.Max(keys.Length, values.Length);
                var array     = new Array();
                for (var i = 0; i < maxLength; i++)
                {
                    array[keys[i].Text] = values[i];
                }
                return(array);
            }
            var key = keyValue.Text;

            return(new KeyedValue(key, value));
        }
Ejemplo n.º 24
0
 public static void SetMultipleParameters(Parameters parameters, Array array)
 {
     Assign.FromFieldsLocal(array, parameters);
 }
Ejemplo n.º 25
0
 public void SetBreakOnParameters(Array array, string key, int index)
 {
     Regions.SetLocal(names.ArrayVariable, array);
     Regions.SetLocal(names.KeyVariable, key);
     Regions.SetLocal(names.IndexVariable, index);
 }
Ejemplo n.º 26
0
        public static Block ConvertBlocks(Parameters parameters, Block finalValue, Block checkExpresion, Block invariant,
                                          Block increment, Array test = null)
        {
            var builder = new CodeBuilder();

            builder.Define(VAR_ACCUM);
            builder.Assign();
            builder.Parenthesize(finalValue);
            builder.End();

            builder.Push();

            builder.Push();
            builder.Exit();
            var ifThen = builder.Pop();

            builder.If(checkExpresion, ifThen);

            builder.Variable(VAR_ACCUM);
            builder.Assign();
            builder.Parenthesize(invariant);
            builder.End();

            var setup          = true;
            var parameterIndex = 0;

            foreach (var verb in increment)
            {
                if (setup)
                {
                    var length = parameters.VariableNames.Length;
                    Assert(parameterIndex < length, LOCATION, "No more parameters");
                    builder.Define(VAR_MANGLE + parameters[parameterIndex++].Name);
                    builder.Assign();
                    setup = false;
                }
                if (verb is AppendToArray)
                {
                    builder.End();
                    setup = true;
                }
                else
                {
                    builder.Verb(verb);
                }
            }

            builder.End();

            foreach (var variableName in parameters.VariableNames)
            {
                builder.Variable(variableName);
                builder.Assign();
                builder.Variable(VAR_MANGLE + variableName);
                builder.End();
            }

            var actions = builder.Pop();

            builder.Push();
            if (test == null)
            {
                builder.Value(true);
            }
            else
            {
                builder.Variable(parameters[0].Name);
                builder.Verb(new When());
                builder.Value(test);
            }
            var whileCondition = builder.Pop();

            builder.While(whileCondition, actions);
            builder.End();

            builder.Return();
            if (finalValue.Count == 1)
            {
                if (NameFromVariable(finalValue[0]).IsSome)
                {
                    builder.Parenthesize(finalValue);
                }
                else
                {
                    builder.Variable(VAR_ACCUM);
                }
            }
            else
            {
                builder.Variable(VAR_ACCUM);
            }
            builder.End();

            return(builder.Block);
        }
Ejemplo n.º 27
0
        public override Verb CreateVerb(string[] tokens)
        {
            var isAbstract     = tokens[2].IsNotEmpty();
            var type           = tokens[3].Trim();
            var className      = tokens[4];
            var parameterBegin = tokens[5];

            ClassName         = className;
            EnumerationValue  = -1;
            CurrentVisibility = Object.VisibilityType.Public;

            Color(position, tokens[1].Length, Whitespaces);
            Color(tokens[2].Length, KeyWords);
            Color(tokens[3].Length, KeyWords);
            Color(className.Length, Types);
            Color(parameterBegin.Length, Structures);

            Parameters parameters;
            int        index;

            if (parameterBegin == "(" && type != "module")
            {
                Color(parameterBegin.Length, Structures);
                index = position + length;
                var parametersParser = new ParametersParser();
                var parsed           = parametersParser.Parse(source, index);
                if (!parsed.If(out parameters, out index))
                {
                    return(null);
                }

                if (type == "enum")
                {
                    parameters.Unshift(new Parameter("name"));
                    parameters.Unshift(new Parameter("value"));
                }
            }
            else if (type == "enum")
            {
                parameters = new Parameters(new[]
                {
                    new Parameter("value"),
                    new Parameter("name")
                });
                index = position + length;
            }
            else
            {
                parameters = new Parameters();
                index      = position + length;
            }

            (var superClass, var superParameters, var traits, var newIndex) = Ancestors(source, index);
            index = newIndex;

            var endParser = new EndParser();

            if (endParser.Scan(source, index))
            {
                index = endParser.Position;
            }

            Block objectBlock;

            InClassDefinition      = true;
            EnumerationMappingCode = new CodeBuilder();
            try
            {
                if (type == "enum")
                {
                    addEnumerationSupport(className);
                }
                LockedDown = type == "view";
                if (GetBlock(source, index, true, InClass).If(out var block, out newIndex))
                {
                    objectBlock = block;
                    index       = newIndex;
                    if (type == "enum")
                    {
                        addEnumerationInstanceSupport(ref objectBlock);
                    }
                }
                else
                {
                    objectBlock = new Block();
                }
            }
            finally
            {
                InClassDefinition = false;
                if (EnumerationMappingCode != null)
                {
                    AddStaticBlock(EnumerationMappingCode.Block);
                }
                EnumerationMappingCode = null;
                LockedDown             = false;
            }

            var checker = new InheritanceChecker(className, objectBlock, parameters, superClass, isAbstract, traits);
            var passes  = checker.Passes();

            Assert(passes.IsSuccessful, LOCATION, () => passes.Exception.Message);

            var cls = new Class(parameters, objectBlock, GetStaticBlock(), superClass, traits, superParameters,
                                type == "view");

            CompilerState.RegisterClass(ClassName, cls);
            result.Value     = cls;
            overridePosition = index;
            ClassName        = "";
            switch (type)
            {
            case "module":
                return(new CreateModule(className, cls, true)
                {
                    Index = position
                });

            case "extend":
                return(new CreateExtender(className, cls));

            default:
                var verb = new CreateClass(className, cls)
                {
                    HelperFunctions = HelperFunctions,
                    HelperBlock     = HelperBlock,
                    Index           = position
                };
                HelperFunctions = null;
                HelperBlock     = null;
                return(verb);
            }
        }
Ejemplo n.º 28
0
        public static object[] GetArguments(this Value[] arguments, string[] types)
        {
            var matcher = new Matcher();

            return(types.Select((t, i) => t.GetArgument(arguments[i], matcher)).ToArray());
        }
Ejemplo n.º 29
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);
        }
Ejemplo n.º 30
0
 public static object[] GetArguments(this Value[] arguments) => arguments.Select(a => a.GetArgument()).ToArray();