Exemple #1
0
        public override Verb CreateVerb(string[] tokens)
        {
            if (!InClassDefinition)
            {
                return(null);
            }

            Color(position, tokens[1].Length, KeyWords);
            var name = tokens[2];

            Color(name.Length, Variables);
            var parameterLength = tokens[3].Length;

            Color(parameterLength, Structures);
            var index      = position + length;
            var parameters = new Parameters();

            if (parameterLength > 0)
            {
                var parametersParser = new ParametersParser();
                var newIndex         = parametersParser.Parse(source, index);
                if (!newIndex.If(out parameters, out index))
                {
                    return(null);
                }
            }

            overridePosition = index;

            var builder = new CodeBuilder();

            if (parameters.Length == 0)
            {
                var mangledName = MangledName(name);
                var cls         = new Class(parameters, objectBlock(name), new Block(), ClassName, new string[0], new Parameters(), false);
                var verb        = new CreateClass(mangledName, cls);
                builder.Verb(verb);
                builder.End();
                builder.Push();
                builder.FunctionInvoke(mangledName);
                builder.AssignToNewField(true, name, builder.Pop(true), global: true);
                builder.End();
            }
            else
            {
                var cls  = new Class(parameters, objectBlock(name), new Block(), ClassName, new string[0], new Parameters(), false);
                var verb = new CreateClass(name, cls);
                builder.Verb(verb);
            }
            if (HelperBlock == null)
            {
                HelperBlock = new Block();
            }
            foreach (var verb in builder.Block.AsAdded)
            {
                HelperBlock.Add(verb);
            }

            return(new NullOp());
        }
Exemple #2
0
        public override Verb CreateVerb(string[] tokens)
        {
            var variable = tokens[2];

            Color(position, tokens[1].Length, KeyWords);
            Color(variable.Length, Variables);
            Color(tokens[3].Length, Structures);

            var index = NextPosition;

            if (GetExpression(source, index, LoopWhile()).If(out var initialization, out index))
            {
                var builder = new CodeBuilder();
                builder.AssignToNewField(false, variable, initialization);
                var block = builder.Block;
                block.Expression = false;
                initialization   = block;
                if (parser.Scan(source, index, "^ /(' '*) /('while' | 'until') /b"))
                {
                    var direction = parser.Tokens[2];
                    var isWhile   = direction == "while";
                    parser.Colorize(Whitespaces, KeyWords);
                    index = parser.Position;
                    if (GetExpression(source, index, LoopThen()).If(out var condition, out index))
                    {
                        condition.Expression = false;
                        if (GetExpression(source, index, LoopEnd()).If(out var increment, out index))
                        {
                            increment.Expression = false;
                            builder.Clear();
                            builder.Variable(variable);
                            builder.Assign();
                            builder.Inline(increment);
                            increment = builder.Block;
                            if (GetOneOrMultipleBlock(source, index).If(out var body, out index))
                            {
                                overridePosition = index;
                                return(new Loop(initialization, isWhile, condition, increment, body)
                                {
                                    Index = position
                                });
                            }
                        }
                    }
                }
            }

            return(null);
        }
Exemple #3
0
        public override Value Evaluate(Arguments arguments, Value instance = null, bool register = true, bool setArguments = true)
        {
            if (parameters.Length <= 1)
            {
                return(base.Evaluate(arguments, instance, register, setArguments));
            }

            var allParameters  = parameters.GetParameters();
            var firstParameter = allParameters[0];
            var blocks         = arguments.Blocks;
            var passedValue    = blocks.Length > 0 ? blocks[0] : firstParameter.DefaultValue;

            passedValue.Expression = true;
            var builder = new CodeBuilder();

            builder.AssignToNewField(true, firstParameter.Name, passedValue);
            builder.End();
            for (var i = 1; i < allParameters.Length; i++)
            {
                var parameter = allParameters[i];
                builder.Parameter(parameter);
            }

            builder.Inline(block);
            var curryingLambda = builder.CurryingLambda();

            if (blocks.Length > 1)
            {
                var newArguments = new Arguments();
                for (var i = 1; i < blocks.Length; i++)
                {
                    var value = blocks[i].Evaluate();
                    if (value is IInvokable invokeable)
                    {
                        value = invokeable.Invoke(new Arguments());
                    }

                    newArguments.AddArgument(value);
                }

                return(curryingLambda.Evaluate(newArguments, instance, register, setArguments));
            }

            return(curryingLambda);
        }
Exemple #4
0
        static void addEnumerationSupport(string className)
        {
            var staticCode = new CodeBuilder();

            var builder = new CodeBuilder();

            builder.Push();
            builder.Verb(new PushArrayLiteral(new Array()));
            var expression = builder.Pop(true);

            builder.AssignToNewField(true, "valueToName", expression);

            builder.Push();
            builder.Verb(new PushArrayLiteral(new Array()));
            expression = builder.Pop(true);
            builder.AssignToNewField(true, "nameToValue", expression);
            //builder.Setter("valueToName", SetterName("default"), new NotMatched<Verb>(), new Failure("No such value").Pushed);

            /*         builder.Define("valueToName", Protected);
             *       builder.Assign();
             *       builder.Verb(new PushArrayLiteral(new Array()));
             *       builder.End();
             *       builder.Variable("valueToName");
             *       builder.SendMessage("default");
             *       builder.Assign();
             *       builder.Error("No such value");
             *       builder.End();
             *
             *       builder.Define("nameToValue", Protected);
             *       builder.Assign();
             *       builder.Verb(new PushArrayLiteral(new Array()));
             *       builder.End();
             *       builder.Variable("nameToValue");
             *       builder.SendMessage("default");
             *       builder.Assign();
             *       builder.Error("No such name");
             *       builder.End();*/

            staticCode.Inline(builder);

            var arguments = new CodeBuilder();

            builder = new CodeBuilder();
            builder.Parameter("value");
            builder.Define("name");
            builder.Assign();
            builder.Variable("valueToName");
            builder.Indexer("value");
            builder.End();
            builder.Return();
            builder.Variable(className);
            arguments.VariableAsArgument("value");
            arguments.VariableAsArgument("name");
            builder.Invoke(arguments.Arguments);
            builder.End();

            staticCode.Function("fromValue", builder);

            arguments = new CodeBuilder();
            builder   = new CodeBuilder();
            builder.Parameter("name");
            builder.Define("value");
            builder.Assign();
            builder.Variable("nameToValue");
            builder.Indexer("name");
            builder.End();
            builder.Return();
            builder.Variable(className);
            arguments.VariableAsArgument("value");
            arguments.VariableAsArgument("name");
            builder.Invoke(arguments.Arguments);
            builder.End();

            staticCode.Function("fromName", builder);

            builder = new CodeBuilder();
            builder.Return();
            builder.Variable("valueToName");
            builder.End();

            staticCode.Function("names", builder);

            AddStaticBlock(staticCode.Block);
        }
Exemple #5
0
        protected static void addEnumerationSupport(string className)
        {
            var staticCode = new CodeBuilder();

            var builder = new CodeBuilder();

            builder.Push();
            builder.Verb(new PushArrayLiteral(new Array()));
            var expression = builder.Pop(true);

            builder.AssignToNewField(true, "valueToName", expression);

            builder.Push();
            builder.Verb(new PushArrayLiteral(new Array()));
            expression = builder.Pop(true);
            builder.AssignToNewField(true, "nameToValue", expression);

            staticCode.Inline(builder);

            var arguments = new CodeBuilder();

            builder = new CodeBuilder();
            builder.Parameter("value");
            builder.Define("name");
            builder.Assign();
            builder.Variable("valueToName");
            builder.Indexer("value");
            builder.End();
            builder.Return();
            builder.Variable(className);
            arguments.VariableAsArgument("value");
            arguments.VariableAsArgument("name");
            builder.Invoke(arguments.Arguments);
            builder.End();

            staticCode.Function("fromValue", builder);

            arguments = new CodeBuilder();
            builder   = new CodeBuilder();
            builder.Parameter("name");
            builder.Define("value");
            builder.Assign();
            builder.Variable("nameToValue");
            builder.Indexer("name");
            builder.End();
            builder.Return();
            builder.Variable(className);
            arguments.VariableAsArgument("value");
            arguments.VariableAsArgument("name");
            builder.Invoke(arguments.Arguments);
            builder.End();

            staticCode.Function("fromName", builder);

            builder = new CodeBuilder();
            builder.Return();
            builder.Variable("valueToName");
            builder.End();

            staticCode.Function("names", builder);

            AddStaticBlock(staticCode.Block);
        }