public override void Compile(CompileContext context)
        {
            Type procType = typeof(ProcessBase);
            ILGenerator il = context.ILGenerator;

            EmitDebug("Preparing to sync now...", context);

            il.Emit(OpCodes.Ldarg_0); //this, to call the Sync

            //Create methodcall sync object
            il.Emit(OpCodes.Ldstr, this.Name);
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Newobj, typeof(MethodCallAction).GetConstructor(new Type[] { typeof(string), typeof(ProcessBase)}));

            //Call sync
            il.Emit(OpCodes.Call, SyncMethod);
            context.MarkSequencePoint(this.LexicalInfo);
            //Now compile the actual method call
            MethodCallExpr.Compile(context);
        }
        public override void Compile(CompileContext context)
        {
            Type procType = typeof(ProcessBase);
            ILGenerator il = context.ILGenerator;

            EmitDebug("Preparing to sync now...", context);
            LocalBuilder syncObject = il.DeclareLocal(typeof(ChannelSyncAction));
            il.Emit(OpCodes.Ldarg_0); //this
            il.Emit(OpCodes.Ldstr, Name);
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldc_I4, _children.Count);
            il.Emit(OpCodes.Ldc_I4_1);
            il.Emit(OpCodes.Newobj, typeof(ChannelSyncAction).GetConstructor(new Type[] { typeof(string), typeof(ProcessBase), typeof(int), typeof(bool) }));
            il.Emit(OpCodes.Stloc, syncObject);
            il.Emit(OpCodes.Ldloc, syncObject);
            il.Emit(OpCodes.Call, SyncMethod);
            context.MarkSequencePoint(this.LexicalInfo);

            //Save values to variables
            for (int i = 0; i < _children.Count; i++) {
                Variable var = (Variable)_children[i];

                if (context.Options.Optimize && !var.IsUsed) {
                    continue;
                }
                //Get the value to assign to it...
                il.Emit(OpCodes.Ldloc, syncObject);
                il.Emit(OpCodes.Ldc_I4, i);
                il.Emit(OpCodes.Call, typeof(ChannelSyncAction).GetMethod("GetValue"));
                //...and assign it
                LocalBuilder local = context.Type.GetLocal(var.Name);
                if (local == null) { //Inactions can be defining occurrences, so just create it...
                    local = context.ILGenerator.DeclareLocal(typeof(object));
                    if (context.Options.Debug) {
                        local.SetLocalSymInfo(var.Name);
                    }
                    context.Type.Locals.Add(var.Name, local);
                }
                il.Emit(OpCodes.Stloc, local);
            }
        }
 public override void Compile(CompileContext context)
 {
     foreach (Expression exp in this.Expressions) {
         exp.Compile(context);
         if (exp is ArithmeticExpression) {
             context.ILGenerator.Emit(OpCodes.Box, typeof(Int32));
         }
     }
     context.MarkSequencePoint(this.LexicalInfo);
     EmitRunProcess(context, context.GetType(this.FullName), false, LexicalInfo, false);
 }
        public override void Compile(CompileContext context)
        {
            Type procType = typeof(ProcessBase);
            ILGenerator il = context.ILGenerator;

            EmitDebug("Preparing to sync now...", context);
            LocalBuilder syncObject = il.DeclareLocal(typeof(KLAIMAction));

            il.Emit(OpCodes.Ldarg_0); //this
            il.Emit(OpCodes.Ldarg_0); //this
            il.Emit(OpCodes.Ldstr, this.ToString());
            il.Emit(OpCodes.Newobj, typeof(KLAIMAction).GetConstructors()[0]);
            il.Emit(OpCodes.Call, SyncMethod);

            //..and here we actually do something...
            context.MarkSequencePoint(this.LexicalInfo);

            if (this.At is PLRString && ((PLRString)this.At).Value == "Screen") {
                CompileOutAtScreen(context);
                return;
            }

            LocalBuilder loc = il.DeclareLocal(typeof(Locality));
            LocalBuilder arr = il.DeclareLocal(typeof(object[]));

            if (this.At is Variable && ((Variable)this.At).Name == "self") {
                il.Emit(OpCodes.Ldstr, this.Locality);
            } else {
                this.At.Compile(context);
            }
            il.Emit(OpCodes.Call, typeof(Net).GetMethod("GetLocality"));
            il.Emit(OpCodes.Stloc, loc);

            il.Emit(OpCodes.Ldc_I4, this.ChildNodes.Count-1);
            il.Emit(OpCodes.Newarr, typeof(object));
            il.Emit(OpCodes.Stloc, arr);

            for (int i = 1; i < this.ChildNodes.Count; i++) {
                Node node = this.ChildNodes[i];
                il.Emit(OpCodes.Ldloc, arr);
                il.Emit(OpCodes.Ldc_I4, i-1);
                node.Compile(context);
                if (node is ArithmeticExpression) {
                    il.Emit(OpCodes.Box, typeof(int));
                }
                il.Emit(OpCodes.Stelem_Ref);
            }

            //Create the Tuple from the array
            LocalBuilder tuple = il.DeclareLocal(typeof(Tuple));
            il.Emit(OpCodes.Ldloc, arr);
            il.Emit(OpCodes.Newobj, typeof(Tuple).GetConstructor(new Type[] { typeof(object[]) }));
            il.Emit(OpCodes.Stloc, tuple);

            il.Emit(OpCodes.Ldloc, loc);
            il.Emit(OpCodes.Ldloc, tuple);
            il.Emit(OpCodes.Call, typeof(Locality).GetMethod("Out"));

            //Now lets print out the net for fun...

            il.EmitWriteLine("************** CURRENT TUPLES **************");
            il.Emit(OpCodes.Call, typeof(Net).GetMethod("Display"));
            il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new Type[] {typeof(string)}));
        }
 public override void Compile(CompileContext context)
 {
     if (context.Options.Optimize && !ElseBranch.IsUsed) {
         IfBranch.Compile(context);
         return;
     } else if (context.Options.Optimize && !IfBranch.IsUsed) {
         ElseBranch.Compile(context);
         return;
     }
     Label elseStart = context.ILGenerator.DefineLabel();
     Label elseEnd = context.ILGenerator.DefineLabel();
     context.MarkSequencePoint(Expression.LexicalInfo);
     this.Expression.Compile(context);
     context.ILGenerator.Emit(OpCodes.Brfalse, elseStart);
     IfBranch.Compile(context);
     context.ILGenerator.Emit(OpCodes.Br, elseEnd);
     context.ILGenerator.MarkLabel(elseStart);
     ElseBranch.Compile(context);
     context.ILGenerator.MarkLabel(elseEnd);
     if (context.Options.Debug && ElseBranch.LexicalInfo.EndLine != 0) {
         LexicalInfo l = ElseBranch.LexicalInfo;
         context.ILGenerator.MarkSequencePoint(context.DebugWriter, l.EndLine, l.EndColumn + 1, l.EndLine, l.EndColumn + 1);
     }
 }
        public override void Compile(CompileContext context)
        {
            Type procType = typeof(ProcessBase);
            ILGenerator il = context.ILGenerator;

            EmitDebug("Preparing to sync now...", context);
            LocalBuilder syncObject = il.DeclareLocal(typeof(KLAIMAction));

            il.Emit(OpCodes.Ldarg_0); //this
            il.Emit(OpCodes.Ldarg_0); //this
            il.Emit(OpCodes.Ldstr, this.ToString());
            il.Emit(OpCodes.Newobj, typeof(KLAIMAction).GetConstructors()[0]);
            il.Emit(OpCodes.Call, SyncMethod);

            //..and here we actually do something...
            context.MarkSequencePoint(this.LexicalInfo);

            LocalBuilder loc = il.DeclareLocal(typeof(Locality));
            LocalBuilder arr = il.DeclareLocal(typeof(object[]));
            LocalBuilder tuple = il.DeclareLocal(typeof(Tuple));

            if (this.At is Variable && ((Variable)this.At).Name == "self") {
                il.Emit(OpCodes.Ldstr, this.Locality);
            } else {
                this.At.Compile(context);
            }
            il.Emit(OpCodes.Call, typeof(Net).GetMethod("GetLocality"));
            il.Emit(OpCodes.Stloc, loc);

            il.Emit(OpCodes.Ldc_I4, this.ChildNodes.Count - 1);
            il.Emit(OpCodes.Newarr, typeof(object));
            il.Emit(OpCodes.Stloc, arr);

            for (int i = 1; i < this.ChildNodes.Count; i++) {
                Node node = this.ChildNodes[i];
                il.Emit(OpCodes.Ldloc, arr);
                il.Emit(OpCodes.Ldc_I4, i - 1);
                node.Compile(context);
                if (node is ArithmeticExpression) {
                    il.Emit(OpCodes.Box, typeof(int));
                }
                il.Emit(OpCodes.Stelem_Ref);
            }

            il.Emit(OpCodes.Ldloc, loc);
            il.Emit(OpCodes.Ldloc, arr);
            il.Emit(OpCodes.Call, typeof(Locality).GetMethod(_methodName));

            //We might be blocked at this location for a very long time...

            il.Emit(OpCodes.Stloc, tuple);
            //Now lets bind our variables... if they are used in the process
            for (int i = 1; i < this.ChildNodes.Count; i++) {
                if (_children[i] is VariableBinding) {
                    VariableBinding var = (VariableBinding)_children[i];
                    if (context.Options.Optimize && !var.IsUsed) {
                        continue;
                    }

                    //Get the value to assign to it...
                    il.Emit(OpCodes.Ldloc, tuple);
                    il.Emit(OpCodes.Ldc_I4, i - 1);
                    il.Emit(OpCodes.Call, typeof(Tuple).GetMethod("GetValueAt"));

                    //...and assign it
                    LocalBuilder bindLoc = context.Type.GetLocal(var.Name);
                    if (bindLoc == null) {
                        bindLoc = il.DeclareLocal(typeof(object));
                        context.Type.Locals.Add(var.Name, bindLoc);
                        if (context.Options.Debug) {
                            bindLoc.SetLocalSymInfo(var.Name);
                        }
                    }
                    il.Emit(OpCodes.Stloc, bindLoc);
                }
            }

            //Ok, we got past the input action. If our parent is not null then we are a replicated
            //process and should notify our parent that we have really started.
            Label noParent = il.DefineLabel();
            il.Emit(OpCodes.Ldarg_0); //Load the "this" pointer
            il.Emit(OpCodes.Call, typeof(ProcessBase).GetMethod("get_Parent"));
            il.Emit(OpCodes.Brfalse, noParent);
            {
                il.Emit(OpCodes.Ldarg_0); //Load the "this" pointer
                il.Emit(OpCodes.Call, typeof(ProcessBase).GetMethod("get_Parent"));
                il.Emit(OpCodes.Call, typeof(ProcessBase).GetMethod("get_Thread")); //find our parent thread
                il.Emit(OpCodes.Call, typeof(Thread).GetMethod("Resume"));

                //Now we've woken up our parent. Set the Parent property to null, so it doesn't get passed
                //on to the rest of this process, KLAIM has no need for the Parent and passing it on might
                //lead to later in actions spawning extra instances of the replicated process.
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldnull);
                il.Emit(OpCodes.Call, typeof(ProcessBase).GetMethod("set_Parent"));
            }
            il.MarkLabel(noParent);

            //Now lets print out the net for fun...

            il.EmitWriteLine("************** CURRENT TUPLES **************");
            il.Emit(OpCodes.Call, typeof(Net).GetMethod("Display"));
            il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new Type[] { typeof(string) }));
            //TODO: Notify possible replicated process
        }
        public override void Compile(CompileContext context)
        {
            Type procType = typeof(ProcessBase);
            ILGenerator il = context.ILGenerator;

            EmitDebug("Preparing to sync now...", context);
            LocalBuilder syncObject = il.DeclareLocal(typeof(ChannelSyncAction));

            il.Emit(OpCodes.Ldarg_0); //this
            il.Emit(OpCodes.Ldstr, Name);
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldc_I4, _children.Count);
            il.Emit(OpCodes.Ldc_I4_0);
            il.Emit(OpCodes.Newobj, typeof(ChannelSyncAction).GetConstructor(new Type[] { typeof(string), typeof(ProcessBase), typeof(int), typeof(bool) }));
            il.Emit(OpCodes.Stloc, syncObject);

            //Now put the result of any expression we have into the sync objects
            foreach (Expression exp in _children) {
                il.Emit(OpCodes.Ldloc, syncObject);
                exp.Compile(context);
                if (exp is ArithmeticExpression || exp is MethodCallExpression) {
                    il.Emit(OpCodes.Box, typeof(int));
                }
                il.Emit(OpCodes.Call, typeof(ChannelSyncAction).GetMethod("AddValue"));
            }

            il.Emit(OpCodes.Ldloc, syncObject);
            il.Emit(OpCodes.Call, SyncMethod);
            //Do nothing here after. In an action class that actually does something we would
            //compile it here.
            context.MarkSequencePoint(this.LexicalInfo);
        }
 public override void Compile(CompileContext context)
 {
     context.MarkSequencePoint(LexicalInfo);
     EmitDebug("Turned into 0", context);
 }