Exemplo n.º 1
0
        static void GenerateEndMethodImpl(CodeClass c, MethodInfo endProcessMethod, string name, MethodInfo mi)
        {
            CodeMethod  m = c.ImplementMethod(mi);
            CodeBuilder b = m.CodeBuilder;

            ParameterInfo [] pinfos = mi.GetParameters();

            ParameterInfo         p = pinfos [0];
            CodeArgumentReference asyncResultRef = m.GetArg(0);

            CodeVariableDeclaration paramsDecl = new CodeVariableDeclaration(typeof(object []), "parameters");

            b.CurrentBlock.Add(paramsDecl);
            CodeVariableReference paramsRef = paramsDecl.Variable;

            b.Assign(paramsRef,
                     new CodeNewArray(typeof(object), new CodeLiteral(pinfos.Length - 1)));

            /*
             * for (int i = 0; i < pinfos.Length - 2; i++) {
             *      ParameterInfo par = pinfos [i];
             *      if (!par.IsOut)
             *              b.Assign (
             *                      new CodeArrayItem (paramsRef, new CodeLiteral (i)),
             *                      new CodeCast (typeof (object),
             *                              new CodeArgumentReference (par.ParameterType, par.Position + 1, "arg" + i)));
             * }
             */
#if USE_OD_REFERENCE_IN_PROXY
            CodePropertyReference argMethodInfo = GetOperationMethod(m, b, name, "EndMethod");
#else
            CodeMethodCall argMethodInfo = new CodeMethodCall(typeof(MethodBase), "GetCurrentMethod");
#endif
            CodeLiteral argOperName = new CodeLiteral(name);

            CodeVariableReference retValue = null;
            if (mi.ReturnType == typeof(void))
            {
                b.Call(m.GetThis(), endProcessMethod, argMethodInfo, argOperName, paramsRef, asyncResultRef);
            }
            else
            {
                CodeVariableDeclaration retValueDecl = new CodeVariableDeclaration(mi.ReturnType, "retValue");
                b.CurrentBlock.Add(retValueDecl);
                retValue = retValueDecl.Variable;
                b.Assign(retValue,
                         new CodeCast(mi.ReturnType,
                                      b.CallFunc(m.GetThis(), endProcessMethod, argMethodInfo, argOperName, paramsRef, asyncResultRef)));
            }
            // FIXME: fill out parameters
            if (retValue != null)
            {
                b.Return(retValue);
            }
        }
Exemplo n.º 2
0
        static void GenerateBeginMethodImpl(CodeClass c, MethodInfo beginProcessMethod, string name, MethodInfo mi)
        {
            CodeMethod  m = c.ImplementMethod(mi);
            CodeBuilder b = m.CodeBuilder;

            // object [] parameters = new object [x];
            // parameters [0] = arg1;
            // parameters [1] = arg2;
            // ...
            // (return) BeginProcess (Contract.Operations [operName].BeginMethod, operName, parameters, asyncCallback, userState);
            ParameterInfo []        pinfos     = mi.GetParameters();
            CodeVariableDeclaration paramsDecl = new CodeVariableDeclaration(typeof(object []), "parameters");

            b.CurrentBlock.Add(paramsDecl);
            CodeVariableReference paramsRef = paramsDecl.Variable;

            b.Assign(paramsRef,
                     new CodeNewArray(typeof(object), new CodeLiteral(pinfos.Length - 2)));
            for (int i = 0; i < pinfos.Length - 2; i++)
            {
                ParameterInfo par = pinfos [i];
                if (!par.IsOut)
                {
                    b.Assign(
                        new CodeArrayItem(paramsRef, new CodeLiteral(i)),
                        new CodeCast(typeof(object), m.GetArg(i)));
                }
            }
#if USE_OD_REFERENCE_IN_PROXY
            CodePropertyReference argMethodInfo = GetOperationMethod(m, b, name, "BeginMethod");
#else
            CodeMethodCall argMethodInfo = new CodeMethodCall(typeof(MethodBase), "GetCurrentMethod");
#endif
            CodeLiteral argOperName = new CodeLiteral(name);

            ParameterInfo         p           = pinfos [pinfos.Length - 2];
            CodeArgumentReference callbackRef = new CodeArgumentReference(typeof(AsyncCallback), p.Position + 1, p.Name);
            p = pinfos [pinfos.Length - 1];
            CodeArgumentReference stateRef = new CodeArgumentReference(typeof(object), p.Position + 1, p.Name);

            CodeVariableDeclaration retValueDecl = new CodeVariableDeclaration(mi.ReturnType, "retValue");
            b.CurrentBlock.Add(retValueDecl);
            CodeVariableReference retValue = retValueDecl.Variable;
            b.Assign(retValue,
                     new CodeCast(mi.ReturnType,
                                  b.CallFunc(m.GetThis(), beginProcessMethod, argMethodInfo, argOperName, paramsRef, callbackRef, stateRef)));

            b.Return(retValue);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        protected virtual Lambda createPartial(List <ParameterValue> values)
        {
            var builder = new CodeBuilder();

            foreach (var parameterValue in values)
            {
                var parameterName = parameterValue.Name;
                if (parameterValue.Value.Type == ValueType.Any)
                {
                    builder.Parameter(parameterName);
                    builder.VariableAsArgument(parameterName);
                }
                else
                {
                    builder.Define(parameterName);
                    builder.Assign();
                    builder.Value(parameterValue.Value);
                    builder.End();
                }
            }

            builder.Inline(block);
            return(builder.Lambda());
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
0
        static void GenerateMethodImpl(CodeClass c, MethodInfo processMethod, string name, MethodInfo mi)
        {
            CodeMethod  m = c.ImplementMethod(mi);
            CodeBuilder b = m.CodeBuilder;

            // object [] parameters = new object [x];
            // parameters [0] = arg1;
            // parameters [1] = arg2;
            // ...
            // (return) Process (Contract.Operations [operName].SyncMethod, operName, parameters);
            ParameterInfo []        pinfos     = mi.GetParameters();
            CodeVariableDeclaration paramsDecl = new CodeVariableDeclaration(typeof(object []), "parameters");

            b.CurrentBlock.Add(paramsDecl);
            CodeVariableReference paramsRef = paramsDecl.Variable;

            b.Assign(paramsRef,
                     new CodeNewArray(typeof(object), new CodeLiteral(pinfos.Length)));
            for (int i = 0; i < pinfos.Length; i++)
            {
                ParameterInfo par = pinfos [i];
                if (!par.IsOut)
                {
                    b.Assign(
                        new CodeArrayItem(paramsRef, new CodeLiteral(i)),
                        new CodeCast(typeof(object),
                                     new CodeArgumentReference(par.ParameterType, par.Position + 1, "arg" + i)));
                }
            }
#if USE_OD_REFERENCE_IN_PROXY
            CodePropertyReference argMethodInfo = GetOperationMethod(m, b, name, "SyncMethod");
#else
            CodeMethodCall argMethodInfo = new CodeMethodCall(typeof(MethodBase), "GetCurrentMethod");
#endif
            CodeLiteral           argOperName = new CodeLiteral(name);
            CodeVariableReference retValue    = null;
            if (mi.ReturnType == typeof(void))
            {
                b.Call(m.GetThis(), processMethod, argMethodInfo, argOperName, paramsRef);
            }
            else
            {
                CodeVariableDeclaration retValueDecl = new CodeVariableDeclaration(mi.ReturnType, "retValue");
                b.CurrentBlock.Add(retValueDecl);
                retValue = retValueDecl.Variable;
                b.Assign(retValue,
                         new CodeCast(mi.ReturnType,
                                      b.CallFunc(m.GetThis(), processMethod, argMethodInfo, argOperName, paramsRef)));
            }
            for (int i = 0; i < pinfos.Length; i++)
            {
                ParameterInfo par = pinfos [i];
                if (par.IsOut || par.ParameterType.IsByRef)
                {
                    b.Assign(
                        new CodeArgumentReference(par.ParameterType, par.Position + 1, "arg" + i),
                        new CodeCast(par.ParameterType.GetElementType(),
                                     new CodeArrayItem(paramsRef, new CodeLiteral(i))));
                }
            }
            if (retValue != null)
            {
                b.Return(retValue);
            }
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        void modifyBlocks()
        {
/*			var builder = new CodeBuilder();
*                       var finalBuilder = new CodeBuilder();
*                       initialize(builder);
*                       initialize(finalBuilder);
*                       var count = block.Count;
*                       var foundArguments = false;
*                       foreach (var verb in block)
*                       {
*                               Invoke invoke;
*                               if (!verb.IsA(out invoke))
*                                       continue;
*
*                               builder.Parenthesize(trimArgumentBlock(invoke.Arguments.ArgumentsBlock));
*                               finalBuilder.Parenthesize(finalValue);
*                               foundArguments = true;
*                               break;
*                       }
*                       Runtime.Assert(foundArguments, LOCATION, "Couldn't determine function call");
*                       builder.End();
*                       finalBuilder.End();
*                       var i = 0;
*                       while (i < count)
*                       {
*                               var verb = block[i];
*                               Push push;
*                               if (verb.IsA(out push))
*                               {
*                                       var value = push.Value;
*                                       Variable variable;
*                                       if (value.IsA(out variable))
*                                       {
*                                               var name = variable.Name;
*                                               if (name == variableName)
*                                               {
*                                                       addAccumulator(builder);
*                                                       addAccumulator(finalBuilder);
*                                                       i++;
*                                                       continue;
*                                               }
*                                               if (i + 1 < count && block[i + 1] is Invoke)
*                                               {
*                                                       addAccumulator(builder);
*                                                       addAccumulator(finalBuilder);
*                                                       i += 2;
*                                                       continue;
*                                               }
*                                       }
*                               }
*                               addVerb(builder, verb);
*                               addVerb(finalBuilder, verb);
*                               i++;
*                       }
*                       addComma(builder);
*                       addComma(finalBuilder);
*                       addValue(builder);
*                       addValue(finalBuilder);
*                       modBlock = builder.Block;
*                       modFinalValue = finalBuilder.Block;*/
            var builder = new CodeBuilder();

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

            var actions = new CodeBuilder();

            actions.Variable(VAR_ACCUMULATOR);
            actions.Assign();

            var foundArguments = false;

            foreach (var verb in block)
            {
                Invoke invoke;
                if (!verb.IsA(out invoke))
                {
                    continue;
                }

                builder.Parenthesize(trimArgumentBlock(invoke.Arguments.ArgumentsBlock));
                finalBuilder.Parenthesize(finalValue);
                foundArguments = true;
                break;
            }
            Runtime.Assert(foundArguments, LOCATION, "Couldn't determine function call");
        }
Exemplo n.º 10
0
 static void initialize(CodeBuilder builder)
 {
     builder.Define(VAR_VALUE);
     builder.Assign();
 }