Example #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();
              }
        }
Example #2
0
        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();
              }
        }
Example #3
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));
        }
Example #4
0
        public override void Compile(Emitter.Emitter emitter)
        {
            // ensure this is a matrix
              if (ExpressionPrefix.GetExpressionType(emitter) != "matrix")
            Error(Resources.errIndexingNotAMatrix);

              // ensure indexes are integers
              if (Index1.GetExpressionType(emitter) != "int")
            Error(Resources.errIntIndexExpected, Index1.Lexem);

              if (Index2.GetExpressionType(emitter) != "int")
            Error(Resources.errIntIndexExpected, Index2.Lexem);

              // ensure assigned value is either int or float
              var exprType = Expression.GetExpressionType(emitter);
              if (!exprType.IsAnyOf("int", "float"))
            Error(Resources.errMatrixItemTypeMismatch);

              ExpressionPrefix.Compile(emitter);

              Index1.Compile(emitter);
              Index2.Compile(emitter);

              Expression.Compile(emitter);
              if(exprType != "float")
            emitter.EmitConvertToFloat();

              var method = emitter.AssemblyImport(typeof(MN.DenseMatrix).GetMethod("At", new[] { typeof(int), typeof(int), typeof(double) }));
              emitter.EmitCall(method);
        }
Example #5
0
        public override void Compile(Emitter.Emitter emitter)
        {
            var numTypes = new[] { "int", "float" };

              var fromType = From.GetExpressionType(emitter);
              var toType = To.GetExpressionType(emitter);
              var stepType = Step.GetExpressionType(emitter);

              // validate parameters
              if (!fromType.IsAnyOf(numTypes) || !toType.IsAnyOf(numTypes) || !stepType.IsAnyOf(numTypes))
            Error(Resources.errIntFloatExpected);

              // from, to, step
              From.Compile(emitter);
              if (fromType != "float")
            emitter.EmitConvertToFloat();

              To.Compile(emitter);
              if (toType != "float")
            emitter.EmitConvertToFloat();

              Step.Compile(emitter);
              if (stepType != "float")
            emitter.EmitConvertToFloat();

              // invoke method
              var method = typeof(MirelleStdlib.ArrayHelper).GetMethod("CreateRangedArray", new[] { typeof(double), typeof(double), typeof(double) });
              emitter.EmitCall(emitter.AssemblyImport(method));
        }
Example #6
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);
        }
Example #7
0
        public override void Compile(Emitter.Emitter emitter)
        {
            var leftType = Left.GetExpressionType(emitter);
              var rightType = Right.GetExpressionType(emitter);

              // an array of values
              if (rightType == leftType + "[]")
              {
            Right.Compile(emitter);
            Left.Compile(emitter);
            if (leftType.IsAnyOf("int", "bool", "float", "complex"))
              emitter.EmitBox(emitter.ResolveType(leftType));

            var method = typeof(MirelleStdlib.ArrayHelper).GetMethod("Has", new[] { typeof(object), typeof(object) });
            emitter.EmitCall(emitter.AssemblyImport(method));
              }

              // an object has a "has" method that accepts the lefthand expression
              else
              {
            try
            {
              Expr.IdentifierInvoke("has", Right, Left).Compile(emitter);
              return;
            }
            catch { }

            Error(String.Format(Resources.errOperatorTypesMismatch, "in", leftType, rightType));
              }
        }
Example #8
0
        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);
        }
Example #9
0
        public override void Compile(Emitter.Emitter emitter)
        {
            var method = emitter.AssemblyImport(typeof(MirelleStdlib.Events.Simulation).GetMethod("Process", new[] { typeof(int), typeof(int) }));

              // processor count
              if(Processors == null)
            emitter.EmitLoadInt(1);
              else
              {
            if(Processors.GetExpressionType(emitter) != "int")
              Error(Resources.errSimulateProcessorsInt);

            Processors.Compile(emitter);
              }

              // queue length
              if(MaxQueue == null)
            emitter.EmitLoadInt(0);
              else
              {
            if(MaxQueue.GetExpressionType(emitter) != "int")
              Error(Resources.errSimulateQueueInt);

            MaxQueue.Compile(emitter);
              }

              emitter.EmitCall(method);
        }
Example #10
0
        public override void Compile(Emitter.Emitter emitter)
        {
            // retrieve matrix dimensions
              var height = MatrixItems.Count;
              var width = MatrixItems[0].Count;

              // retrieve info about matrices
              var matrixType = typeof(MN.DenseMatrix);
              var matrixCtor = emitter.AssemblyImport(matrixType.GetConstructor(new[] { typeof(int), typeof(int) }));
              var matrixSet = emitter.AssemblyImport(matrixType.GetMethod("At", new[] { typeof(int), typeof(int), typeof(double) }));

              var tmpVar = emitter.CurrentMethod.Scope.Introduce("matrix", emitter.AssemblyImport(matrixType));

              // create matrix
              emitter.EmitLoadInt(height);
              emitter.EmitLoadInt(width);
              emitter.EmitNewObj(matrixCtor);
              emitter.EmitSaveVariable(tmpVar);

              // set items
              for(var idx1 = 0; idx1 < MatrixItems.Count; idx1++)
              {
            // ensure all lines have the same number of items
            if (idx1 > 0 && MatrixItems[0].Count != MatrixItems[idx1].Count)
              Error(String.Format(Resources.errMatrixLineLengthMismatch, MatrixItems[0].Count, idx1+1, MatrixItems[idx1].Count));

            for(var idx2 = 0; idx2 < MatrixItems[idx1].Count; idx2++)
            {
              var item = MatrixItems[idx1][idx2];
              var itemType = item.GetExpressionType(emitter);
              if (!itemType.IsAnyOf("int", "float"))
            Error(Resources.errMatrixItemTypeMismatch, item.Lexem);

              emitter.EmitLoadVariable(tmpVar);

              emitter.EmitLoadInt(idx1);
              emitter.EmitLoadInt(idx2);
              item.Compile(emitter);

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

              emitter.EmitCall(matrixSet);
            }
              }

              emitter.EmitLoadVariable(tmpVar);
        }
Example #11
0
        public override void Compile(Emitter.Emitter emitter)
        {
            var args = new[] { Parameters.Count == 1 ? typeof(object) : typeof(IEnumerable<dynamic>), typeof(bool) };
              var printMethod = emitter.AssemblyImport(typeof(MirelleStdlib.Printer).GetMethod("Print", args));

              if (Parameters.Count == 1)
              {
            var currType = Parameters[0].GetExpressionType(emitter);
            Parameters[0].Compile(emitter);
            if (currType.IsAnyOf("int", "bool", "float", "complex"))
              emitter.EmitBox(emitter.ResolveType(currType));
              }
              else
              {
            var objType = emitter.AssemblyImport(typeof(object));
            var arrType = new ArrayType(objType);

            var tmpVariable = emitter.CurrentMethod.Scope.Introduce("object[]", arrType);

            // load count & create
            emitter.EmitLoadInt(Parameters.Count);
            emitter.EmitNewArray(objType);
            emitter.EmitSaveVariable(tmpVariable);

            int idx = 0;
            foreach (var curr in Parameters)
            {
              var currType = curr.GetExpressionType(emitter);
              emitter.EmitLoadVariable(tmpVariable);
              emitter.EmitLoadInt(idx);
              curr.Compile(emitter);

              if (currType.IsAnyOf("int", "bool", "float", "complex"))
            emitter.EmitBox(emitter.ResolveType(currType));

              emitter.EmitSaveIndex("object");

              idx++;
            }

            // return the created array
            emitter.EmitLoadVariable(tmpVariable);
              }

              emitter.EmitLoadBool(PrintLine);
              emitter.EmitCall(printMethod);
        }
Example #12
0
        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);
              }
        }
Example #13
0
        public override void Compile(Emitter.Emitter emitter)
        {
            // ensure this is a matrix
              if (ExpressionPrefix.GetExpressionType(emitter) != "matrix")
            Error(Resources.errIndexingNotAMatrix);

              // ensure indexes are integers
              if (Index1.GetExpressionType(emitter) != "int")
            Error(Resources.errIntIndexExpected, Index1.Lexem);

              if(Index2.GetExpressionType(emitter) != "int")
            Error(Resources.errIntIndexExpected, Index2.Lexem);

              ExpressionPrefix.Compile(emitter);
              Index1.Compile(emitter);
              Index2.Compile(emitter);

              var method = emitter.AssemblyImport(typeof(MN.DenseMatrix).GetMethod("At", new[] { typeof(int), typeof(int) }));
              emitter.EmitCall(method);
        }
Example #14
0
        public override void Compile(Emitter.Emitter emitter)
        {
            Resolve(emitter);

              // operands
              Left.Compile(emitter);
              emitter.EmitConvertToFloat();
              Right.Compile(emitter);
              emitter.EmitConvertToFloat();

              // magic method
              var method = emitter.AssemblyImport(typeof(Math).GetMethod("Pow", new[] { typeof(double), typeof(double) }));
              emitter.EmitCall(method);

              // convert back
              if (IsInt)
            emitter.EmitConvertToInt();
              else
            emitter.EmitConvertToFloat();
        }
Example #15
0
        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);
        }
Example #16
0
        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;
              }
        }
Example #17
0
 public override void Compile(Emitter.Emitter emitter)
 {
     var method = typeof(Environment).GetMethod("Exit", new[] { typeof(int) });
       emitter.EmitLoadInt(0);
       emitter.EmitCall(emitter.AssemblyImport(method));
 }
Example #18
0
        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;
              }
        }
Example #19
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();
              }
        }
Example #20
0
        /// <summary>
        /// Compare two values for their relative order: less and greater
        /// </summary>
        /// <param name="emitter">Emitter link</param>
        /// <param name="leftType">Left-hand argument type</param>
        /// <param name="rightType">Left-hand argument type</param>
        private void CompileRelation(Emitter.Emitter emitter, string leftType, string rightType)
        {
            if(leftType == "string")
              {
            var method = emitter.AssemblyImport(typeof(string).GetMethod("Compare", new[] { typeof(string), typeof(string) }));
            emitter.EmitCall(method);
            emitter.EmitLoadBool(false);
              }

              switch (ComparisonType)
              {
            case LexemType.Less: emitter.EmitCompareLess(); break;

            case LexemType.LessEqual: emitter.EmitCompareGreater();
              emitter.EmitLoadBool(false);
              emitter.EmitCompareEqual(); break;

            case LexemType.Greater: emitter.EmitCompareGreater(); break;

            case LexemType.GreaterEqual: emitter.EmitCompareLess();
              emitter.EmitLoadBool(false);
              emitter.EmitCompareEqual(); break;
              }
        }
Example #21
0
        /// <summary>
        /// Compare two values for their equality
        /// </summary>
        /// <param name="emitter">Emitter link</param>
        /// <param name="leftType">Left-hand argument type</param>
        /// <param name="rightType">Left-hand argument type</param>
        private void CompileEquality(Emitter.Emitter emitter, string leftType, string rightType)
        {
            var basicTypes = new[] { "bool", "int", "float", "string" };
              if (leftType.IsAnyOf(basicTypes))
              {
            // compare strings
            if (leftType == "string")
            {
              var method = emitter.AssemblyImport(typeof(string).GetMethod("Compare", new[] { typeof(string), typeof(string) }));
              emitter.EmitCall(method);
              emitter.EmitLoadBool(false);
            }

            emitter.EmitCompareEqual();
              }

              // long, complex and others
              else
              {
            System.Reflection.MethodInfo method;
            if(leftType.IsAnyOf("long", "complex"))
            {
              var type = (leftType == "long" ? typeof(System.Numerics.BigInteger) : typeof(System.Numerics.Complex));
              method = type.GetMethod("op_Equality", new[] { type, type });
            }
            else
              method = typeof(MirelleStdlib.Compare).GetMethod("Equal", new[] { typeof(object), typeof(object) });

            emitter.EmitCall(emitter.AssemblyImport(method));
              }

              // invert ?
              if (ComparisonType == LexemType.NotEqual)
              {
            emitter.EmitLoadBool(false);
            emitter.EmitCompareEqual();
              }
        }
Example #22
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);
        }
Example #23
0
        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);
              }
        }
Example #24
0
        /// <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);
        }
Example #25
0
        /// <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();
        }
Example #26
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);
            }
              }
        }
Example #27
0
        /// <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);
        }
Example #28
0
        /// <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();
        }