コード例 #1
0
        public override void Compile(Emitter.Emitter emitter)
        {
            var leftType = Left.GetExpressionType(emitter);
              var rightType = Right.GetExpressionType(emitter);

              var type = GetExpressionType(emitter);

              // subtract matrices
              if(type == "matrix")
              {
            Left.Compile(emitter);
            Right.Compile(emitter);

            var matrixType = typeof(MN.Matrix<double>);
            var method = emitter.AssemblyImport(matrixType.GetMethod("Subtract", new [] { matrixType } ));
            emitter.EmitCall(method);
              }

              // subtract dicts
              else if (type == "dict")
              {
            Left.Compile(emitter);
            Right.Compile(emitter);

            var dictType = typeof(Dict);
            var method = emitter.AssemblyImport(dictType.GetMethod("Subtract", new[] { dictType }));
            emitter.EmitCall(method);
              }

              // subtract complex numbers
              else if (type == "complex")
              {
            Left.Compile(emitter);
            if (leftType != "complex")
            {
              emitter.EmitUpcastBasicType(leftType, "float");
              emitter.EmitLoadFloat(0);
              emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float"));
            }

            Right.Compile(emitter);
            if (rightType != "complex")
            {
              emitter.EmitUpcastBasicType(rightType, "float");
              emitter.EmitLoadFloat(0);
              emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float"));
            }

            emitter.EmitCall(emitter.AssemblyImport(typeof(SN.Complex).GetMethod("op_Subtraction", new[] { typeof(SN.Complex), typeof(SN.Complex) })));
              }
              // add floating point numbers or integers
              else if (type.IsAnyOf("int", "float"))
              {
            Left.Compile(emitter);
            emitter.EmitUpcastBasicType(leftType, type);
            Right.Compile(emitter);
            emitter.EmitUpcastBasicType(rightType, type);
            emitter.EmitSub();
              }
        }
コード例 #2
0
ファイル: OperatorDivideNode.cs プロジェクト: menozz/mirelle
        public override void Compile(Emitter.Emitter emitter)
        {
            var leftType = Left.GetExpressionType(emitter);
              var rightType = Right.GetExpressionType(emitter);

              var type = GetExpressionType(emitter);

              // divide matrix by a number
              if(type == "matrix")
              {
            Left.Compile(emitter);

            // division is broken, therefore multiply by an inverse value
            emitter.EmitLoadFloat(1);
            Right.Compile(emitter);

            if (rightType != "float")
              emitter.EmitConvertToFloat();

            emitter.EmitDiv();

            var matrixType = typeof(MN.Matrix<double>);
            var method = emitter.AssemblyImport(matrixType.GetMethod("Multiply", new[] { typeof(double) }));
            emitter.EmitCall(method);
              }

              // divide complex numbers
              else if (type == "complex")
              {
            Left.Compile(emitter);
            if (leftType != "complex")
            {
              emitter.EmitUpcastBasicType(leftType, "float");
              emitter.EmitLoadFloat(0);
              emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float"));
            }

            Right.Compile(emitter);
            if (rightType != "complex")
            {
              emitter.EmitUpcastBasicType(rightType, "float");
              emitter.EmitLoadFloat(0);
              emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float"));
            }

            emitter.EmitCall(emitter.AssemblyImport(typeof(SN.Complex).GetMethod("op_Division", new[] { typeof(SN.Complex), typeof(SN.Complex) })));
              }

              // divide floating point numbers or integers
              else if (type.IsAnyOf("int", "float"))
              {
            Left.Compile(emitter);
            emitter.EmitUpcastBasicType(leftType, type);
            Right.Compile(emitter);
            emitter.EmitUpcastBasicType(rightType, type);
            emitter.EmitDiv();
              }
        }
コード例 #3
0
        public override void Compile(Emitter.Emitter emitter)
        {
            try
              {
            Resolve(emitter);
              }
              catch (CompilerException ex)
              {
            ex.AffixToLexem(Lexem);
            throw;
              }

              var method = emitter.FindMethod(OwnerType, Name, GetSignature(emitter));

              // load 'this'
              if (ExpressionPrefix != null)
            ExpressionPrefix.Compile(emitter);
              else if (!Static)
            emitter.EmitLoadThis();

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

              // invoke
              emitter.EmitCall(method);
        }
コード例 #4
0
ファイル: DictNode.cs プロジェクト: menozz/mirelle
        public override void Compile(Emitter.Emitter emitter)
        {
            // declare variables and methods
              var tmpVar = emitter.CurrentMethod.Scope.Introduce("dict", emitter.ResolveType("dict"));
              var ctor = emitter.AssemblyImport(typeof(MirelleStdlib.Dict).GetConstructor(new Type[] { }));
              var set = emitter.FindMethod("dict", "set", "string", "string");

              // var tmp = new dict
              emitter.EmitNewObj(ctor);
              emitter.EmitSaveVariable(tmpVar);

              // tmp[key] = value
              foreach(var curr in Data)
              {
            var keyType = curr.Item1.GetExpressionType(emitter);
            var valueType = curr.Item2.GetExpressionType(emitter);

            if (keyType != "string")
              Error(Resources.errDictItemTypeMismatch, curr.Item1.Lexem);

            if (valueType != "string")
              Error(Resources.errDictItemTypeMismatch, curr.Item2.Lexem);

            emitter.EmitLoadVariable(tmpVar);
            curr.Item1.Compile(emitter);
            curr.Item2.Compile(emitter);
            emitter.EmitCall(set);
              }

              emitter.EmitLoadVariable(tmpVar);
        }
コード例 #5
0
ファイル: RangeNode.cs プロジェクト: menozz/mirelle
        public override void Compile(Emitter.Emitter emitter)
        {
            var fromType = From.GetExpressionType(emitter);
              var toType = To.GetExpressionType(emitter);

              if (fromType != "int" || toType != "int")
            Error(Resources.errRangeLimitsIntExpected);

              From.Compile(emitter);
              To.Compile(emitter);

              emitter.EmitNewObj(emitter.FindMethod("range", ".ctor", "int", "int"));
        }
コード例 #6
0
ファイル: OperatorInvertNode.cs プロジェクト: menozz/mirelle
        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));
        }
コード例 #7
0
ファイル: IfNode.cs プロジェクト: menozz/mirelle
        public override void Compile(Emitter.Emitter emitter)
        {
            // check if condition is boolean or can be converted
              var condType = Condition.GetExpressionType(emitter);
              MethodNode converter = null;
              if (condType != "bool")
              {
            try
            {
              converter = emitter.FindMethod(condType, "to_b");
            }
            catch
            {
              Error(Resources.errBoolExpected);
            }
              }

              FalseBlockStart = emitter.CreateLabel();

              // condition
              Condition.Compile(emitter);
              if (converter != null)
            emitter.EmitCall(converter);
              emitter.EmitBranchFalse(FalseBlockStart);

              // "true" body
              TrueBlock.Compile(emitter);

              // there is a false block?
              if(FalseBlock != null)
              {
            // create a jump
            FalseBlockEnd = emitter.CreateLabel();
            emitter.EmitBranch(FalseBlockEnd);
            emitter.PlaceLabel(FalseBlockStart);

            // "false" body
            FalseBlock.Compile(emitter);
            emitter.PlaceLabel(FalseBlockEnd);
              }
              else
              {
            // put the 'nop' after the condition body
            emitter.PlaceLabel(FalseBlockStart);
              }
        }
コード例 #8
0
ファイル: NewNode.cs プロジェクト: menozz/mirelle
        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);
        }
コード例 #9
0
ファイル: WhileNode.cs プロジェクト: menozz/mirelle
        public override void Compile(Emitter.Emitter emitter)
        {
            // check if condition is boolean
              var condType = Condition.GetExpressionType(emitter);
              MethodNode converter = null;
              if (condType != "bool")
              {
            try
            {
              converter = emitter.FindMethod(condType, "to_b");
            }
            catch
            {
              Error(Resources.errBoolExpected);
            }
              }

              // create markers
              BodyStart = emitter.CreateLabel();
              BodyEnd = emitter.CreateLabel();

              emitter.PlaceLabel(BodyStart);

              // condition
              Condition.Compile(emitter);
              if (converter != null)
            emitter.EmitCall(converter);
              emitter.EmitBranchFalse(BodyEnd);

              // body
              var preCurrLoop = emitter.CurrentLoop;
              emitter.CurrentLoop = this;
              Body.Compile(emitter);
              emitter.CurrentLoop = preCurrLoop;

              // re-test condition
              emitter.EmitBranch(BodyStart);

              emitter.PlaceLabel(BodyEnd);
        }
コード例 #10
0
ファイル: IdentifierGetNode.cs プロジェクト: menozz/mirelle
        public override void Compile(Emitter.Emitter emitter)
        {
            try
              {
            Resolve(emitter);
              }
              catch (CompilerException ex)
              {
            ex.AffixToLexem(Lexem);
            throw;
              }

              switch (Kind)
              {

            case IdentifierKind.StaticField:    emitter.EmitLoadField(emitter.FindField(OwnerType, Name)); break;

            case IdentifierKind.Field:          if(ExpressionPrefix != null)
                                              ExpressionPrefix.Compile(emitter);
                                            else
                                              emitter.EmitLoadThis();
                                            emitter.EmitLoadField(emitter.FindField(OwnerType, Name)); break;

            case IdentifierKind.StaticMethod:   emitter.EmitCall(emitter.FindMethod(OwnerType, Name)); break;

            case IdentifierKind.Method:         if (ExpressionPrefix != null)
                                              ExpressionPrefix.Compile(emitter);
                                            else
                                              emitter.EmitLoadThis();
                                            emitter.EmitCall(emitter.FindMethod(OwnerType, Name)); break;

            case IdentifierKind.Variable:       emitter.EmitLoadVariable(emitter.CurrentMethod.Scope.Find(Name)); break;

            case IdentifierKind.Parameter:      emitter.EmitLoadParameter(emitter.CurrentMethod.Parameters[Name].Id); break;

            case IdentifierKind.SizeProperty:   ExpressionPrefix.Compile(emitter);
                                            emitter.EmitLoadArraySize(); break;
              }
        }
コード例 #11
0
ファイル: SimulatePlannerNode.cs プロジェクト: menozz/mirelle
        /// <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);
        }
コード例 #12
0
ファイル: SimulatePlannerNode.cs プロジェクト: menozz/mirelle
        /// <summary>
        /// Generate action and condition (possibly) for the currently defined emitter
        /// </summary>
        /// <param name="emitter"></param>
        private void CompileBody(Emitter.Emitter emitter)
        {
            // compile action
              emitter.CurrentMethod = emitter.FindMethod(PlannerID, "Action", "flow[]", "symbol");
              emitter.MakeMethodVirtual(emitter.CurrentMethod);

              LoadClosuredVariables(Closures, emitter);

              emitter.EmitLoadParameter(0);
              emitter.EmitLoadParameter(1);
              Action.Compile(emitter);

              // append the arguments to the user
              emitter.EmitReturn();
        }
コード例 #13
0
ファイル: ForNode.cs プロジェクト: menozz/mirelle
        /// <summary>
        /// Create iteration code for a range
        /// </summary>
        /// <param name="emitter"></param>
        public void CompileRange(Emitter.Emitter emitter)
        {
            // make local variables only visible inside the scope
              emitter.CurrentMethod.Scope.EnterSubScope();
              var idxVar = emitter.CurrentMethod.Scope.Introduce("int", emitter.ResolveType("int"), Key == null ? null : Key.Data);
              var rangeVar = emitter.CurrentMethod.Scope.Introduce("range", emitter.ResolveType("range"));
              var currVar = emitter.CurrentMethod.Scope.Introduce("int", emitter.ResolveType("int"), Item.Data);

              // preface: range = ..., range.reset
              Iterable.Compile(emitter);
              emitter.EmitSaveVariable(rangeVar);
              emitter.EmitLoadVariable(rangeVar);
              emitter.EmitCall(emitter.FindMethod("range", "reset"));

              // set key if exists
              if(Key != null)
              {
            emitter.EmitLoadInt(-1);
            emitter.EmitSaveVariable(idxVar);
              }

              // range.next == false ? exit
              emitter.PlaceLabel(BodyStart);
              emitter.EmitLoadVariable(rangeVar);
              emitter.EmitCall(emitter.FindMethod("range", "next"));
              emitter.EmitLoadBool(false);
              emitter.EmitCompareEqual();
              emitter.EmitBranchTrue(BodyEnd);

              // curr = range.current
              emitter.EmitLoadVariable(rangeVar);
              emitter.EmitCall(emitter.FindMethod("range", "current"));
              emitter.EmitSaveVariable(currVar);

              // increment key if exists
              if(Key != null)
              {
            emitter.EmitLoadVariable(idxVar);
            emitter.EmitLoadInt(1);
            emitter.EmitAdd();
            emitter.EmitSaveVariable(idxVar);
              }

              // body
              var preCurrLoop = emitter.CurrentLoop;
              emitter.CurrentLoop = this;
              Body.Compile(emitter);
              emitter.CurrentLoop = preCurrLoop;

              emitter.EmitBranch(BodyStart);

              emitter.PlaceLabel(BodyEnd);
              emitter.CurrentMethod.Scope.LeaveSubScope();
        }
コード例 #14
0
ファイル: ForNode.cs プロジェクト: menozz/mirelle
        /// <summary>
        /// Create iteration code for a dictionary
        /// </summary>
        /// <param name="emitter"></param>
        public void CompileDict(Emitter.Emitter emitter)
        {
            // check if key defined
              if (Key == null)
            Error(Resources.errDictIterKeyRequired);

              // make local variables only visible inside the scope
              emitter.CurrentMethod.Scope.EnterSubScope();
              var dictVar = emitter.CurrentMethod.Scope.Introduce("dict", emitter.ResolveType("dict"));
              var currVar = emitter.CurrentMethod.Scope.Introduce("string[]", emitter.ResolveType("string[]"));
              var keyVar = emitter.CurrentMethod.Scope.Introduce("string", emitter.ResolveType("string"), Key.Data);
              var itemVar = emitter.CurrentMethod.Scope.Introduce("string", emitter.ResolveType("string"), Item.Data);

              // preface: dictVar = ...;
              Iterable.Compile(emitter);
              emitter.EmitSaveVariable(dictVar);
              emitter.EmitLoadVariable(dictVar);
              emitter.EmitCall(emitter.FindMethod("dict", "reset"));

              // dict.next == false ? exit
              emitter.PlaceLabel(BodyStart);
              emitter.EmitLoadVariable(dictVar);
              emitter.EmitCall(emitter.FindMethod("dict", "next"));
              emitter.EmitLoadBool(false);
              emitter.EmitCompareEqual();
              emitter.EmitBranchTrue(BodyEnd);

              // curr = dict.current
              emitter.EmitLoadVariable(dictVar);
              emitter.EmitCall(emitter.FindMethod("dict", "current"));
              emitter.EmitSaveVariable(currVar);

              // (key, value) = curr
              emitter.EmitLoadVariable(currVar);
              emitter.EmitLoadInt(0);
              emitter.EmitLoadIndex("string");
              emitter.EmitSaveVariable(keyVar);
              emitter.EmitLoadVariable(currVar);
              emitter.EmitLoadInt(1);
              emitter.EmitLoadIndex("string");
              emitter.EmitSaveVariable(itemVar);

              // body
              var preCurrLoop = emitter.CurrentLoop;
              emitter.CurrentLoop = this;
              Body.Compile(emitter);
              emitter.CurrentLoop = preCurrLoop;

              emitter.EmitBranch(BodyStart);

              emitter.PlaceLabel(BodyEnd);
              emitter.CurrentMethod.Scope.LeaveSubScope();
        }
コード例 #15
0
ファイル: ComplexNode.cs プロジェクト: menozz/mirelle
 public override void Compile(Emitter.Emitter emitter)
 {
     emitter.EmitLoadFloat(Real);
       emitter.EmitLoadFloat(Imaginary);
       emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float"));
 }
コード例 #16
0
        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;
              }
        }
コード例 #17
0
ファイル: OperatorAddNode.cs プロジェクト: menozz/mirelle
        public override void Compile(Emitter.Emitter emitter)
        {
            var leftType = Left.GetExpressionType(emitter);
              var rightType = Right.GetExpressionType(emitter);

              var type = GetExpressionType(emitter);

              // concat strings
              if (type == "string")
              {
            Left.Compile(emitter);
            Right.Compile(emitter);
            emitter.EmitCall(emitter.FindMethod("string", "concat", "string", "string"));
              }

              // add matrices
              else if(type == "matrix")
              {
            Left.Compile(emitter);
            Right.Compile(emitter);

            var matrixType = typeof(MN.Matrix<double>);
            var method = emitter.AssemblyImport(matrixType.GetMethod("Add", new [] { matrixType } ));
            emitter.EmitCall(method);
              }

              // add dicts
              else if (type == "dict")
              {
            Left.Compile(emitter);
            Right.Compile(emitter);

            var dictType = typeof(Dict);
            var method = emitter.AssemblyImport(dictType.GetMethod("Add", new[] { dictType }));
            emitter.EmitCall(method);
              }

              // add complex numbers
              else if(type == "complex")
              {
            Left.Compile(emitter);
            if(leftType != "complex")
            {
              emitter.EmitUpcastBasicType(leftType, "float");
              emitter.EmitLoadFloat(0);
              emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float"));
            }

            Right.Compile(emitter);
            if (rightType != "complex")
            {
              emitter.EmitUpcastBasicType(rightType, "float");
              emitter.EmitLoadFloat(0);
              emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float"));
            }

            emitter.EmitCall(emitter.AssemblyImport(typeof(SN.Complex).GetMethod("op_Addition", new[] { typeof(SN.Complex), typeof(SN.Complex) })));
              }

              // add floating point numbers or integers
              else if(type.IsAnyOf("int", "float"))
              {
            Left.Compile(emitter);
            emitter.EmitUpcastBasicType(leftType, type);
            Right.Compile(emitter);
            emitter.EmitUpcastBasicType(rightType, type);
            emitter.EmitAdd();
              }

              // array addition
              else if(type.Contains("[]"))
              {
            Left.Compile(emitter);
            Right.Compile(emitter);

            var method = emitter.AssemblyImport(typeof(ArrayHelper).GetMethod("AddArrays", new[] { typeof(object), typeof(object) } ));
            emitter.EmitCall(method);
              }
        }
コード例 #18
0
        public override string GetExpressionType(Emitter.Emitter emitter)
        {
            if (ExpressionType != "")
            return ExpressionType;

              try
              {
            Resolve(emitter);
              }
              catch(CompilerException ex)
              {
            ex.AffixToLexem(Lexem);
            throw;
              }

              var method = emitter.FindMethod(OwnerType, Name, GetSignature(emitter));
              ExpressionType = method != null ? method.Type.Signature : "";
              return ExpressionType;
        }
コード例 #19
0
ファイル: EmitNode.cs プロジェクト: menozz/mirelle
        /// <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);
        }
コード例 #20
0
ファイル: EmitNode.cs プロジェクト: menozz/mirelle
        /// <summary>
        /// Generate action and condition (possibly) for the currently defined emitter
        /// </summary>
        /// <param name="emitter"></param>
        private void CompileBody(Emitter.Emitter emitter)
        {
            // compile action
              emitter.CurrentMethod = emitter.FindMethod(EmitterID, "Action");
              emitter.MakeMethodVirtual(emitter.CurrentMethod);

              LoadClosuredVariables(ActionClosures, emitter);
              Action.Compile(emitter);
              if (!Action.GetExpressionType(emitter).IsAnyOf("void", ""))
            emitter.EmitPop();
              emitter.EmitReturn();

              // compile condition, if any
              if (Condition != null)
              {
            if (Condition.GetExpressionType(emitter) != "bool")
              Error(Resources.errEmitConditionExpected);

            emitter.CurrentMethod = emitter.FindMethod(EmitterID, "Condition");
            emitter.MakeMethodVirtual(emitter.CurrentMethod);
            LoadClosuredVariables(ConditionClosures, emitter);
            Condition.Compile(emitter);
            emitter.EmitReturn();
              }
        }
コード例 #21
0
        public override void Compile(Emitter.Emitter emitter)
        {
            var leftType = Left.GetExpressionType(emitter);
              var rightType = Right.GetExpressionType(emitter);

              var type = GetExpressionType(emitter);

              // repeat a string
              if (type == "string")
              {
            Left.Compile(emitter);
            Right.Compile(emitter);
            emitter.EmitCall(emitter.FindMethod("string", "repeat", "int"));
              }

              // multiply matrices
              else if(type == "matrix")
              {
            var matrixType = typeof(MN.Matrix<double>);

            // matrix by matrix
            if(leftType == rightType)
            {
              Left.Compile(emitter);
              Right.Compile(emitter);

              var method = emitter.AssemblyImport(matrixType.GetMethod("Multiply", new[] { matrixType }));
              emitter.EmitCall(method);
            }
            else
            {
              // matrix should be the first in stack
              if(leftType == "matrix")
              {
            Left.Compile(emitter);
            Right.Compile(emitter);
            if (rightType != "float")
              emitter.EmitConvertToFloat();
              }
              else
              {
            Right.Compile(emitter);
            Left.Compile(emitter);
            if (leftType != "float")
              emitter.EmitConvertToFloat();
              }

              var method = emitter.AssemblyImport(matrixType.GetMethod("Multiply", new[] { typeof(double) }));
              emitter.EmitCall(method);
            }
              }

              // multiply complex numbers
              else if (type == "complex")
              {
            Left.Compile(emitter);
            if (leftType != "complex")
            {
              emitter.EmitUpcastBasicType(leftType, "float");
              emitter.EmitLoadFloat(0);
              emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float"));
            }

            Right.Compile(emitter);
            if (rightType != "complex")
            {
              emitter.EmitUpcastBasicType(rightType, "float");
              emitter.EmitLoadFloat(0);
              emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float"));
            }

            emitter.EmitCall(emitter.AssemblyImport(typeof(SN.Complex).GetMethod("op_Multiply", new[] { typeof(SN.Complex), typeof(SN.Complex) })));
              }

              // repeat array
              else if (leftType.EndsWith("[]"))
              {
            Left.Compile(emitter);
            Right.Compile(emitter);
            var method = emitter.AssemblyImport(typeof(MirelleStdlib.ArrayHelper).GetMethod("RepeatArray", new[] { typeof(object), typeof(int) }));
            emitter.EmitCall(method);
              }

              // multiply floating point numbers or integers
              else if (type.IsAnyOf("int", "float"))
              {
            Left.Compile(emitter);
            emitter.EmitUpcastBasicType(leftType, type);
            Right.Compile(emitter);
            emitter.EmitUpcastBasicType(rightType, type);
            emitter.EmitMul();
              }
        }
コード例 #22
0
ファイル: OperatorCompareNode.cs プロジェクト: menozz/mirelle
        /// <summary>
        /// Ensure two values can be compared, or issue an exception
        /// </summary>
        /// <param name="leftType">Left-hand operand type</param>
        /// <param name="rightType">Right-hand operand type</param>
        private void CheckComparable(string leftType, string rightType, Emitter.Emitter emitter)
        {
            var ok = false;
              var byValueTypes = new[] { "bool", "int", "float", "complex", "long" };
              var numericTypes = new[] { "int", "float", "complex", "long" };

              // equality comparison
              if(ComparisonType == LexemType.Equal || ComparisonType == LexemType.NotEqual)
              {
            // null and ref-type
            if(leftType == "null" && !rightType.IsAnyOf(byValueTypes))
              ok = true;

            // ref-type and null
            else if(rightType == "null" && !leftType.IsAnyOf(byValueTypes))
              ok = true;

            // types are equal and meaningful
            else if(leftType == rightType && leftType != "void" && leftType != "")
              ok = true;

            // numeric types
            else if(leftType.IsAnyOf(numericTypes) && rightType.IsAnyOf(numericTypes))
            {
              // @TODO: rewrite! a clumsy kludge!
              var zomg = leftType + rightType;
              if(!zomg.Contains("complex") || !zomg.Contains("long"))
            ok = true;
            }

            // a valid comparator exits
            else
            {
              try
              {
            emitter.FindMethod(leftType, "equal", rightType);
            ok = true;
              }
              catch
              {
            try
            {
              emitter.FindMethod(rightType, "equal", leftType);
              ok = true;
            }
            catch { }
              }
            }

            if(!ok)
              Error(String.Format(Resources.errIncomparableTypes, leftType, rightType));
              }

              // relation comparison
              else
              {
            // strings
            if(leftType == "string" && rightType == "string")
              ok = true;

            // numeric types
            else if (leftType.IsAnyOf(numericTypes) && rightType.IsAnyOf(numericTypes))
            {
              // complex cannot be related (math law!)
              if(leftType != "complex" && rightType != "complex")
            ok = true;
            }

            if(!ok)
            {
              if(leftType == rightType)
            Error(String.Format(Resources.errRelationIncomparableTypes, leftType));
              else
            Error(String.Format(Resources.errIncomparableTypes, leftType, rightType));
            }
              }
        }
コード例 #23
0
ファイル: AsNode.cs プロジェクト: menozz/mirelle
        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);
            }
              }
        }
コード例 #24
0
ファイル: MethodNode.cs プロジェクト: menozz/mirelle
        public override void Compile(Emitter.Emitter emitter)
        {
            if (!BuiltIn)
              {
            emitter.CurrentMethod = this;

            // special cases for constructors
            if(Name == ".ctor")
            {
              // invoke base constructor
              emitter.EmitLoadThis();
              if(emitter.CurrentType.Parent != "")
            emitter.EmitCall(emitter.FindMethod(emitter.CurrentType.Parent, ".ctor"));
              else
            emitter.EmitCall(emitter.AssemblyImport(typeof(object).GetConstructor(new Type[] { } )));

              // invoke initializer
              if (emitter.MethodNameExists(emitter.CurrentType.Name, ".init"))
              {
            emitter.EmitLoadThis();
            emitter.EmitCall(emitter.FindMethod(emitter.CurrentType.Name, ".init"));
              }
            }

            Body.Compile(emitter);
            if (!Body.AllPathsReturn)
            {
              if (Type.Signature == "void")
            emitter.EmitReturn();
              else
            Error(String.Format(Resources.errNotAllPathsReturn, Name));
            }

            emitter.CurrentMethod = null;
              }
        }
コード例 #25
0
ファイル: IdentifierGetNode.cs プロジェクト: menozz/mirelle
        public override string GetExpressionType(Emitter.Emitter emitter)
        {
            if (ExpressionType != "")
            return ExpressionType;

              try
              {
            Resolve(emitter);
              }
              catch (CompilerException ex)
              {
            ex.AffixToLexem(Lexem);
            throw;
              }

              switch(Kind)
              {
            case IdentifierKind.StaticField:
            case IdentifierKind.Field: ExpressionType = emitter.FindField(OwnerType, Name).Type.Signature; break;
            case IdentifierKind.StaticMethod:
            case IdentifierKind.Method: ExpressionType = emitter.FindMethod(OwnerType, Name).Type.Signature; break;
            case IdentifierKind.Variable: ExpressionType = emitter.CurrentMethod.Scope.Find(Name).Type; break;
            case IdentifierKind.Parameter: ExpressionType = emitter.CurrentMethod.Parameters[Name].Type.Signature; break;
            case IdentifierKind.SizeProperty: ExpressionType = "int"; break;
              }

              return ExpressionType;
        }
コード例 #26
0
ファイル: ArrayGetNode.cs プロジェクト: menozz/mirelle
        /// <summary>
        /// Compile a dictionary indexer
        /// </summary>
        /// <param name="emitter"></param>
        private void CompileDict(Emitter.Emitter emitter)
        {
            // ensure index is a string
              if (Index.GetExpressionType(emitter) != "string")
            Error(Resources.errStringExpected, Index.Lexem);

              ExpressionPrefix.Compile(emitter);
              Index.Compile(emitter);

              var method = emitter.FindMethod("dict", "get", "string");
              emitter.EmitCall(method);
        }
コード例 #27
0
ファイル: IdentifierGetNode.cs プロジェクト: menozz/mirelle
        /// <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;
            }
              }
        }