private static IIntermediateClassMethodMember AddPushStringMethod(IIntermediateClassType result, IIntermediateClassFieldMember charBufferSize, IIntermediateClassFieldMember charBuffer, IIntermediateClassMethodMember growBufferMethod, IIntermediateCliManager identityManager) { /* * * Full Method: * if (buffer == null) * GrowBuffer(s.Length); * else if (buffer.Length < actualSize + s.Length) * GrowBuffer(actualSize + s.Length); * for (int i = 0; i < s.Length; i++) * { * buffer[actualSize] = s[i]; * actualSize++; * } * */ IIntermediateClassMethodMember pushStringMethod = result.Methods.Add(new TypedName("Push", identityManager.ObtainTypeReference(RuntimeCoreType.VoidType))); pushStringMethod.AccessLevel = AccessLevelModifiers.Public; var sParameter = pushStringMethod.Parameters.Add(new TypedName("s", identityManager.ObtainTypeReference(RuntimeCoreType.String))); // if (buffer == null) var nullCheck = pushStringMethod.If(charBuffer.GetReference().EqualTo(IntermediateGateway.NullValue)); // GrowBuffer(s.Length); nullCheck.Call(growBufferMethod.GetReference().Invoke(sParameter.GetReference().GetProperty("Length"))); // else if (buffer.Length < actualSize + s.Length) nullCheck.CreateNext(charBuffer.GetReference().GetProperty("Length").LessThan(charBufferSize.GetReference().Add(sParameter.GetReference().GetProperty("Length")))); var rangeCheck = (IConditionBlockStatement)nullCheck.Next; // GrowBuffer(actualSize + s.Length); rangeCheck.Call(growBufferMethod.GetReference().Invoke(charBufferSize.GetReference().Add(sParameter.GetReference().GetProperty("Length")))); //int i = 0; var iLocal = pushStringMethod.Locals.Add(new TypedName("i", identityManager.ObtainTypeReference(RuntimeCoreType.Int32))); //So it isn't declared in the main body. iLocal.InitializationExpression = IntermediateGateway.NumberZero; iLocal.AutoDeclare = false; //i++ // for (int i = 0; i < s.Length; i++) // { var sToBufferIterate = pushStringMethod.Iterate(iLocal.GetDeclarationStatement(), iLocal.LessThan(sParameter.GetReference().GetProperty("Length")), new IStatementExpression[] { iLocal.Increment() }); //var sToBufferIterate = pushStringMethod.Iterate(iLocal.GetDeclarationStatement(), IntermediateGateway.NumberZero, sParameter.GetReference().GetProperty("Length")); // buffer[actualSize++] = s[i]; sToBufferIterate.Assign(charBuffer.GetReference().GetIndexer(charBufferSize.Increment()), sParameter.GetReference().GetIndexer(iLocal.GetReference())); // } return(pushStringMethod); }
private static IIntermediateClassMethodMember AddToStringMethod(IIntermediateClassType result, IIntermediateClassFieldMember charBufferSize, IIntermediateClassFieldMember charBuffer, IIntermediateCliManager identityManager) { /* * * Full method: * char[] result = new char[this.actualSize]; * for (int i = 0; i < this.actualSize; i++) * result[i] = buffer[i]; * return new string(result); * */ IIntermediateClassMethodMember toStringOverride = result.Methods.Add(new TypedName("ToString", identityManager.ObtainTypeReference(RuntimeCoreType.String))); toStringOverride.AccessLevel = AccessLevelModifiers.Public; toStringOverride.IsOverride = true; //char[] result = new char[this.actualSize]; var resultCharsInitExp = new MalleableCreateArrayDetailExpression(identityManager.ObtainTypeReference(RuntimeCoreType.Char)); resultCharsInitExp.Sizes.Add(charBufferSize.GetReference()); var resultChars = toStringOverride.Locals.Add(new TypedName("result", identityManager.ObtainTypeReference(RuntimeCoreType.Char).MakeArray()), resultCharsInitExp); var iLocal = toStringOverride.Locals.Add(new TypedName("i", identityManager.ObtainTypeReference(RuntimeCoreType.Int32))); //int i = 0; iLocal.InitializationExpression = IntermediateGateway.NumberZero; //So it isn't declared in the main body. iLocal.AutoDeclare = false; //i++ var increment = iLocal.Increment(); //for (int i = 0; i < this.actualSize; i++) var loop = toStringOverride.Iterate(iLocal.GetDeclarationStatement(), iLocal.LessThan(charBufferSize), new IStatementExpression[] { increment }); // result[i] = this.buffer[i]; loop.Assign(resultChars.GetReference().GetIndexer(iLocal.GetReference()), charBuffer.GetReference().GetIndexer(iLocal.GetReference())); //return new string(result); toStringOverride.Return(identityManager.ObtainTypeReference(RuntimeCoreType.String).GetNewExpression(resultChars.GetReference())); return(toStringOverride); }