Exemple #1
0
        public override void Compile(Emitter.Emitter emitter)
        {
            var type = Expression.GetExpressionType(emitter);
              if (type.IsAnyOf("int", "float", "complex"))
              {
            if(type == "complex")
            {
              // simple case: the complex is a constant
              if(Expression is ComplexNode)
              {
            (Expression as ComplexNode).Imaginary *= -1;
            Expression.Compile(emitter);
              }
              // complex case: complex is an expression result
              else
              {
            Expression.Compile(emitter);

            // tmp = ...
            var tmpVar = emitter.CurrentMethod.Scope.Introduce("complex", emitter.FindType("complex").Type);
            emitter.EmitSaveVariable(tmpVar);

            // tmp.real
            emitter.EmitLoadVariableAddress(tmpVar);
            emitter.EmitCall(emitter.AssemblyImport(typeof(SN.Complex).GetMethod("get_Real")));

            // tmp.imaginary * -1
            emitter.EmitLoadVariableAddress(tmpVar);
            emitter.EmitCall(emitter.AssemblyImport(typeof(SN.Complex).GetMethod("get_Imaginary")));
            emitter.EmitLoadFloat(-1);
            emitter.EmitMul();

            // new complex
            emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float"));
              }
            }
            else
            {
              // multiply by -1
              Expression.Compile(emitter);
              if (type == "int")
            emitter.EmitLoadInt(-1);
              else
            emitter.EmitLoadFloat(-1);
              emitter.EmitMul();
            }
              }
              else
            Error(String.Format(Resources.errInvertType, type));
        }
Exemple #2
0
        public override void Compile(Emitter.Emitter emitter)
        {
            // check if constructor exists
              TypeNode type = null;
              try
              {
            type = emitter.FindType(Name);
              }
              catch
              {
            Error(String.Format(Resources.errTypeNotFound, Name));
              }

              // check if type is an enum?
              if (type.Enum)
              {
            // the constructor in an enum is pseudo-private
            // so that it can only be called from another method
            // of the same enum type
            if(emitter.CurrentMethod == null || emitter.CurrentMethod.Owner != type)
              Error(String.Format(Resources.errConstructEnum, Name));
              }

              MethodNode method = null;
              var signature = GetSignature(emitter);
              try
              {
            method = emitter.FindMethod(Name, ".ctor", signature);
              }
              catch
              {
            Error(String.Format(Resources.errConstructorNotFound, Name, signature.Join(" ")));
              }

              // parameters
              for (int idx = 0; idx < Parameters.Count; idx++)
              {
            Parameters[idx].Compile(emitter);
            emitter.EmitUpcastBasicType(Parameters[idx].GetExpressionType(emitter), method.Parameters[idx].Type.Signature);
              }

              emitter.EmitNewObj(method);
        }
        public override void Compile(Emitter.Emitter emitter)
        {
            var currType = emitter.CurrentType;
              var currMethod = emitter.CurrentMethod;
              emitter.CurrentType = emitter.FindType(PlannerID);

              // convert the IdentifierGet to IdentifierInvoke with two faux arguments
              AppendActionParameters(emitter);

              // extract closured variables
              var detector = new Utils.ClosureDetector();
              Closures = detector.Process(Action, emitter);
              DeclareClosuredVariables(emitter);

              // generate closured code for emitter
              CompileBody(emitter);

              emitter.CurrentType = currType;
              emitter.CurrentMethod = currMethod;

              CompileInitiation(emitter);
        }
Exemple #4
0
        public override void Compile(Emitter.Emitter emitter)
        {
            var currType = emitter.CurrentType;
              var currMethod = emitter.CurrentMethod;
              emitter.CurrentType = emitter.FindType(EmitterID);

              // extract closured variables
              var detector = new Utils.ClosureDetector();
              ActionClosures = detector.Process(Action, emitter);
              if (Condition != null)
            ConditionClosures = detector.Process(Condition, emitter);
              MergeClosures();
              DeclareClosuredVariables(emitter);

              // generate closured code for emitter
              CompileBody(emitter);

              emitter.CurrentType = currType;
              emitter.CurrentMethod = currMethod;

              CompileInitiation(emitter);
        }
        /// <summary>
        /// Create fields in the emitter type to save local variables intos
        /// </summary>
        private void DeclareClosuredVariables(Emitter.Emitter emitter)
        {
            var flowSimType = emitter.FindType(PlannerID);

              foreach (var curr in Closures)
              {
            var fld = emitter.CreateField(flowSimType, "_" + curr.Key, new SignatureNode(curr.Value));
            emitter.PrepareField(fld);
              }
        }
        /// <summary>
        /// Generate code to setup the currently defined emitter
        /// </summary>
        /// <param name="emitter"></param>
        private void CompileInitiation(Emitter.Emitter emitter)
        {
            var flowSimType = emitter.FindType(PlannerID);
              var tmpVar = emitter.CurrentMethod.Scope.Introduce(PlannerID, flowSimType.Type);

              // tmp = new flowsimN()
              emitter.EmitNewObj(emitter.FindMethod(PlannerID, ".ctor"));
              emitter.EmitSaveVariable(tmpVar);

              SaveClosuredVariables(emitter, tmpVar);

              // call FlowSimulation.Start(planner)
              emitter.EmitLoadVariable(tmpVar);
              var method = emitter.AssemblyImport(typeof(FlowSimulation).GetMethod("Start", new[] { typeof(Planner) }));
              emitter.EmitCall(method);
        }
Exemple #7
0
        /// <summary>
        /// Resolve the identifier meaning
        /// </summary>
        public void Resolve(Emitter.Emitter emitter)
        {
            // already resolved?
              if (Kind != IdentifierKind.Unresolved) return;

              // atmark?
              if (AtmarkPrefix)
              {
            if (emitter.CurrentType == null)
              Error(Resources.errFieldOutsideType);

            OwnerType = emitter.CurrentType.Name;

            var field = emitter.FindField(emitter.CurrentType.Name, Name);
            if (field != null)
              Kind = field.Static ? IdentifierKind.StaticField : IdentifierKind.Field;
            else
              Error(String.Format(Resources.errFieldNotFound, Name, emitter.CurrentType.Name));

            // additional check: dynamic field from static method
            if (emitter.CurrentMethod.Static && !field.Static)
              Error(String.Format(Resources.errDynamicFromStatic, field.Name));
              }

              // other prefix?
              else if (TypePrefix != null || ExpressionPrefix != null)
              {
            OwnerType = (TypePrefix != null ? TypePrefix.Data : ExpressionPrefix.GetExpressionType(emitter));

            if (OwnerType == "null")
              Error(Resources.errNullAccessor);

            if(OwnerType.EndsWith("[]") && Name == "size")
            {
              Kind = IdentifierKind.SizeProperty;
              return;
            }

            // check class existence
            emitter.FindType(OwnerType);

            // field ?
            try
            {
              emitter.FindField(OwnerType, Name);
              Kind = (TypePrefix != null ? IdentifierKind.StaticField : IdentifierKind.Field);
              return;
            }
            catch { }

            // method ?!
            MethodNode method = null;
            try
            {
              method = emitter.FindMethod(OwnerType, Name);
            }
            catch
            {
              Error(String.Format(Resources.errTypeIdentifierUnresolved, OwnerType, Name));
            }

            if (ExpressionPrefix == null && !method.Static)
              Error(String.Format(Resources.errNonStaticMethod, Name));

            Kind = (TypePrefix != null ? IdentifierKind.StaticMethod : IdentifierKind.Method);
              }

              else
              {
            MethodNode method = null;

            // local variable
            if (emitter.CurrentMethod.Scope.Exists(Name))
            {
              Kind = IdentifierKind.Variable;
              return;
            }

            // parameter
            if(emitter.CurrentMethod.Parameters.Contains(Name))
            {
              Kind = IdentifierKind.Parameter;
              return;
            }

            // search for a method
            try
            {
              method = emitter.FindMethod(emitter.CurrentType != null ? emitter.CurrentType.Name : "", true, Name);
            }
            catch
            {
              Error(String.Format(Resources.errIdentifierUnresolved, Name));
            }

            OwnerType = method.Owner.Name;
            if(method.Static)
              Kind = IdentifierKind.StaticMethod;
            else
            {
              // additional check for invoking a dynamic method from static context
              if (emitter.CurrentMethod == null || emitter.CurrentMethod.Static)
            Error(String.Format(Resources.errDynamicFromStatic, Name));

              Kind = IdentifierKind.Method;
            }
              }
        }
Exemple #8
0
        public override void Compile(Emitter.Emitter emitter)
        {
            var fromType = Expression.GetExpressionType(emitter);
              var toType = ToType;
              var simpleTypes = new[] { "bool", "int", "float", "long", "complex" };

              if (emitter.ResolveType(toType) == null)
            Error(String.Format(Resources.errTypeNotFound, toType));

              // compile the expression itself
              Expression.Compile(emitter);

              // idiotic case?
              if (fromType == toType) return;

              if(fromType == "null")
              {
            if(toType.IsAnyOf(simpleTypes))
              Error(String.Format(Resources.errInvalidNullCast, toType));

            // no actual casting is required
              }
              // cast simple types
              else if(fromType.IsAnyOf(simpleTypes))
              {
            if (toType.IsAnyOf(simpleTypes))
            {
              switch(toType)
              {
            case "bool": emitter.EmitConvertToBool(); break;
            case "int": emitter.EmitConvertToInt(); break;
            case "float": emitter.EmitConvertToFloat(); break;
            default: throw new NotImplementedException();
              }
            }
            else
              Error(String.Format(Resources.errInvalidCast, fromType, toType));
              }
              else
              {
            // complex type to simple type: search for a conversion method
            if(toType.IsAnyOf(simpleTypes))
            {
              // to_b, to_f, to_i
              string methodName = "to_" + toType[0];
              MethodNode converter = null;
              try
              {
            converter = emitter.FindMethod(fromType, methodName);
              }
              catch
              {
            Error(String.Format(Resources.errInvalidCast, fromType, toType));
              }

              // call the method
              emitter.EmitCall(converter);
            }
            else
            {
              if (fromType.Contains("[]") || toType.Contains("[]"))
            Error(Resources.errCastArray);

              if(!emitter.TypeCastable(fromType, toType))
            Error(String.Format(Resources.errInvalidCast, fromType, toType));

              var type = emitter.FindType(toType);
              emitter.EmitCast(type.Type);
            }
              }
        }
Exemple #9
0
        private void Resolve(Emitter.Emitter emitter)
        {
            // already resolved?
              if (Kind != IdentifierKind.Unresolved) return;

              // atmark?
              if (AtmarkPrefix)
              {
            OwnerType = emitter.CurrentType.Name;

            var field = emitter.FindField(emitter.CurrentType.Name, Name);
            if (field != null)
              Kind = field.Static ? IdentifierKind.StaticField : IdentifierKind.Field;
            else
              Error(String.Format(Resources.errFieldNotFound, Name, emitter.CurrentType.Name));

            // additional check: dynamic field from static method
            if (emitter.CurrentMethod.Static && !field.Static)
              Error(String.Format(Resources.errDynamicFromStatic, field.Name));

            IdentifierType = field.Type.Signature;
              }

              // other prefix?
              else if (TypePrefix != null || ExpressionPrefix != null)
              {
            OwnerType = (TypePrefix != null ? TypePrefix.Data : ExpressionPrefix.GetExpressionType(emitter));

            // check class existence
            var type = emitter.FindType(OwnerType);
            if (type == null)
              Error(String.Format(Resources.errTypeNotFound, OwnerType), TypePrefix);

            // field ?
            var field = emitter.FindField(OwnerType, Name);
            Kind = (TypePrefix != null ? IdentifierKind.StaticField : IdentifierKind.Field);
            IdentifierType = field.Type.Signature;
              }

              else
              {
            // local variable
            if (emitter.CurrentMethod.Scope.Exists(Name))
            {
              Kind = IdentifierKind.Variable;
              IdentifierType = emitter.CurrentMethod.Scope.Find(Name).Type;
            }

            // parameter
            else if (emitter.CurrentMethod.Parameters.Contains(Name))
            {
              Kind = IdentifierKind.Parameter;
              IdentifierType = emitter.CurrentMethod.Parameters[Name].Type.Signature;
            }

            // no luck at all
            else
              Error(String.Format(Resources.errIdentifierUnresolved, Name));
              }
        }
Exemple #10
0
        /// <summary>
        /// Generate code to setup the currently defined emitter
        /// </summary>
        /// <param name="emitter"></param>
        private void CompileInitiation(Emitter.Emitter emitter)
        {
            var emitterType = emitter.FindType(EmitterID);
              var tmpVar = emitter.CurrentMethod.Scope.Introduce(EmitterID, emitterType.Type);

              // tmp = new emitterN()
              emitter.EmitNewObj(emitter.FindMethod(EmitterID, ".ctor"));
              emitter.EmitSaveVariable(tmpVar);

              // step
              if (Step != null)
              {
            // validate step
            var stepType = Step.GetExpressionType(emitter);
            if (!stepType.IsAnyOf("int", "float"))
              Error(Resources.errEmitStepExpected);

            // tmp.Step = step
            emitter.EmitLoadVariable(tmpVar);
            Step.Compile(emitter);
            if (stepType == "int")
              emitter.EmitConvertToFloat();
            emitter.EmitSaveField(emitter.FindField(EmitterID, "step"));
              }

              // distribution
              if (Distribution != null)
              {
            // validate distr
            if (Distribution.GetExpressionType(emitter) != "distr")
              Error(Resources.errEmitDistributionExpected);

            // tmp.Distr = distr
            emitter.EmitLoadVariable(tmpVar);
            Distribution.Compile(emitter);
            emitter.EmitSaveField(emitter.FindField(EmitterID, "distr"));
              }

              // limit
              if (Limit != null)
              {
            // validate distr
            if (Limit.GetExpressionType(emitter) != "int")
              Error(Resources.errEmitLimitExpected);

            // tmp.Distr = distr
            emitter.EmitLoadVariable(tmpVar);
            Limit.Compile(emitter);
            emitter.EmitSaveField(emitter.FindField(EmitterID, "limit"));
              }

              SaveClosuredVariables(emitter, tmpVar);

              // register emitter in the system
              emitter.EmitLoadVariable(tmpVar);
              var registerMethod = emitter.AssemblyImport(typeof(Simulation).GetMethod("RegisterEmitter", new[] { typeof(EventEmitter) }));
              emitter.EmitCall(registerMethod);
        }
        public void Resolve(Emitter.Emitter emitter)
        {
            // build signature
              var signature = GetSignature(emitter);
              MethodNode method;

              // type prefix ?
              if(TypePrefix != null)
              {
            OwnerType = TypePrefix.Data;
            Static = true;

            // check class existence
            emitter.FindType(OwnerType);
            method = emitter.FindMethod(TypePrefix.Data, Name, signature);

            if (!method.Static)
              Error(String.Format(Resources.errNonStaticMethod, Name));
              }

              // expression prefix ?
              else if(ExpressionPrefix != null)
              {
            OwnerType = ExpressionPrefix.GetExpressionType(emitter);
            if (OwnerType == "null")
              Error(Resources.errNullAccessor);

            emitter.FindMethod(OwnerType, Name, signature);
              }

              // local or visible method ?
              else
              {
            // try to find method in local type, if there's one
            var tmpOwner = emitter.CurrentType != null ? emitter.CurrentType.Name : "";
            method = emitter.FindMethod(tmpOwner, true, Name, signature);

            OwnerType = method.Owner.Name;
            Static = method.Static;
              }
        }