public void CodeEntryStep(ICodeEntry inCodeEntry) { if (inCodeEntry is StatementCode) { var tmpStatement = inCodeEntry as StatementCode; if (tmpStatement.StatementType == StatementTypeEnum.Elvis) { if (IsCodeBlockNull(tmpStatement.StatementCodeBlocks[1])) { var tmpTypeConverter = new TypeConversion(); tmpTypeConverter.Type = CreateNullableType(GetTypeFromCodeBlock(tmpStatement.StatementCodeBlocks[2])); var tmpType = _currentClass.Parent.ClassFromBaseType(tmpTypeConverter.Type); if (tmpType.ModifierList.Contains(Modifiers.Struct)) { tmpTypeConverter.PreconversionValue = tmpStatement.StatementCodeBlocks[2]; tmpStatement.StatementCodeBlocks[2] = new CodeBlock() { CodeEntries = new List <ICodeEntry> { tmpTypeConverter } }; } } else if (IsCodeBlockNull(tmpStatement.StatementCodeBlocks[2])) { var tmpTypeConverter = new TypeConversion(); tmpTypeConverter.Type = CreateNullableType(GetTypeFromCodeBlock(tmpStatement.StatementCodeBlocks[1])); var tmpType = _currentClass.Parent.ClassFromBaseType(tmpTypeConverter.Type); if (tmpType.ModifierList.Contains(Modifiers.Struct)) { tmpTypeConverter.PreconversionValue = tmpStatement.StatementCodeBlocks[1]; tmpStatement.StatementCodeBlocks[1] = new CodeBlock() { CodeEntries = new List <ICodeEntry> { tmpTypeConverter } }; } } } } }
/// <summary> /// Set Value to Variable /// </summary> public static CodeBlock SetFieldValue(this CodeBlock inParentBlock, ICodeEntry inVariableToAccess, params ICodeEntry[] inDefaultValueCalculation) { var tmpData = new SetFieldWithValue() { VariableToAccess = new CodeBlock { CodeEntries = new List <ICodeEntry> { inVariableToAccess } }, ValueToSet = new CodeBlock { CodeEntries = inDefaultValueCalculation.ToList() } }; inParentBlock.CodeEntries.Add(tmpData); return(inParentBlock); }
/// <summary> /// Handle Code Entry and subEntries /// </summary> public static void CheckCodeEntry(ICodeStepperEvents inCodeEntryEvents, ICodeEntry inCodeEntry) { inCodeEntryEvents.CodeEntryStep(inCodeEntry); if (inCodeEntry is StatementCode) { var tmpStatement = inCodeEntry as StatementCode; CheckCodeBlock(inCodeEntryEvents, tmpStatement.InnerContent); CheckCodeBlockList(inCodeEntryEvents, tmpStatement.StatementCodeBlocks); } else if (inCodeEntry is ReturnCodeEntry) { foreach (var tmpEntry in (inCodeEntry as ReturnCodeEntry).CodeEntries) { CheckCodeEntry(inCodeEntryEvents, tmpEntry); } } else if (inCodeEntry is NewObjectDeclaration) { var tmpObjectDecl = (inCodeEntry as NewObjectDeclaration); CheckCodeBlockList(inCodeEntryEvents, tmpObjectDecl.ArgumentList); CheckCodeEntry(inCodeEntryEvents, tmpObjectDecl.InnerCode); } else if (inCodeEntry is MethodeCall) { var tmpMethodeCall = inCodeEntry as MethodeCall; CheckCodeBlockList(inCodeEntryEvents, tmpMethodeCall.Parameter); } else if (inCodeEntry is CodeExpression) { var tmpExpr = inCodeEntry as CodeExpression; CheckCodeBlockList(inCodeEntryEvents, tmpExpr.SubClauseEntries); } else if (inCodeEntry is CodeBlockContainer) { var tmpExpr = inCodeEntry as CodeBlockContainer; CheckCodeBlock(inCodeEntryEvents, tmpExpr.InnerBlock); } else if (inCodeEntry is TypeConversion) { var tmpExpr = inCodeEntry as TypeConversion; CheckCodeBlock(inCodeEntryEvents, tmpExpr.PreconversionValue); } else { } }
public void CodeEntryStep(ICodeEntry inCodeEntry) { if (inCodeEntry is StatementCode) { var tmpStatement = inCodeEntry as StatementCode; if (tmpStatement.StatementType == StatementTypeEnum.Assert) { AddToUsingIfRequired(_currentClass, Modifiers.SystemDiagnosis); } } else if (inCodeEntry is VariableDeclaration) { var tmpVarDecl = inCodeEntry as VariableDeclaration; AddToUsingIfRequired(_currentClass, tmpVarDecl.Type); } else if (inCodeEntry is MethodeCall) { var tmpMethodeCall = inCodeEntry as MethodeCall; //throw new System.Exception("Resolve Methode Call to Static Methodes with Namespace of other Class"); } else { } }
/// <summary> /// Write Code-Entry into C# Code /// </summary> /// <param name="inOutput"></param> /// <param name="inCodeEntry"></param> private void AddCodeEntryToString(StringBuilder inOutput, ICodeEntry inCodeEntry, int inIndentDepth = 0) { if (inCodeEntry == null) { return; } if (inCodeEntry is VariableDeclaration) { var tmpVar = inCodeEntry as VariableDeclaration; inOutput.Append($"{tmpVar.Type.Type?.Name ?? tmpVar.Type.Name} {tmpVar.Name}"); } else if (inCodeEntry is ConstantValue) { var tmpConstant = inCodeEntry as ConstantValue; if (tmpConstant.Value is BaseType) { inOutput.Append($"{(tmpConstant.Value as BaseType).Name}"); } else if (tmpConstant.Value is IName) { inOutput.Append($"{(tmpConstant.Value as IName).Name}"); } else if (tmpConstant.Value is ClassContainer) { inOutput.Append($"{tmpConstant.Type.Type.Name}"); if (tmpConstant.Type.IsArray) { inOutput.Append("["); if (tmpConstant.Type.ArrayInizialiser != null) { var tmpSb = new StringBuilder(); AddCodeEntryToString(tmpSb, tmpConstant.Type.ArrayInizialiser); inOutput.Append(tmpSb.ToString()); } inOutput.Append("]"); } } else if (tmpConstant.Value is TypeContainer) { inOutput.Append($"{tmpConstant.Value}"); if (tmpConstant.Type?.ArrayInizialiser != null) { inOutput.Append("["); AddCodeEntryToString(inOutput, tmpConstant.Type.ArrayInizialiser); inOutput.Append("]"); } } else { if (tmpConstant.Value == null) { inOutput.Append($"{tmpConstant.Type.Type.Name}"); } else { inOutput.Append($"{tmpConstant.Value}"); } if (tmpConstant.Type?.ArrayInizialiser != null) { inOutput.Append("["); AddCodeEntryToString(inOutput, tmpConstant.Type.ArrayInizialiser); inOutput.Append("]"); } } } else if (inCodeEntry is StatementCode) { AddStatementToString(inOutput, inCodeEntry as StatementCode, inIndentDepth); } else if (inCodeEntry is SetFieldWithValue) { var tmpFieldVal = inCodeEntry as SetFieldWithValue; foreach (var tmpEntry in tmpFieldVal.VariableToAccess.CodeEntries) { AddCodeEntryToString(inOutput, tmpEntry); } inOutput.Append(" = "); foreach (var tmpEntry in tmpFieldVal.ValueToSet.CodeEntries) { AddCodeEntryToString(inOutput, tmpEntry); } } else if (inCodeEntry is VariableAccess) { var tmpVarAccess = inCodeEntry as VariableAccess; AddCodeEntryToString(inOutput, tmpVarAccess.Access); if (tmpVarAccess.Child != null) { if (tmpVarAccess.IsArrayAccess) { inOutput.Append("["); AddCodeEntryToString(inOutput, tmpVarAccess.Child); inOutput.Append("]"); } else { inOutput.Append("."); AddCodeEntryToString(inOutput, tmpVarAccess.Child); } } else if (tmpVarAccess.BaseDataSource != null) { inOutput.Append(" = "); AddCodeEntryToString(inOutput, tmpVarAccess.BaseDataSource); } } else if (inCodeEntry is ReturnCodeEntry) { var tmpReturn = inCodeEntry as ReturnCodeEntry; inOutput.Append($"{(tmpReturn.IsYield ? "yield " : "")}return "); foreach (var tmpEntry in tmpReturn.CodeEntries) { AddCodeEntryToString(inOutput, tmpEntry); } } else if (inCodeEntry is NewObjectDeclaration) { var tmpReturn = inCodeEntry as NewObjectDeclaration; inOutput.Append(" new "); AddCodeEntryToString(inOutput, tmpReturn.InnerCode); if (tmpReturn.ArgumentList != null) { inOutput.Append($"({string.Join(", ", tmpReturn.ArgumentList.Select(inItem => CreateStatementCodeBlock(inItem)))})"); } else { if (inOutput[inOutput.Length - 1] != ']') { inOutput.Append("()"); } } } else if (inCodeEntry is MethodeCall) { var tmpMethodeCall = inCodeEntry as MethodeCall; if (tmpMethodeCall.MethodeLink?.IsProperty == true) { if (tmpMethodeCall.Parameter.Count == 0) { inOutput.Append($"{tmpMethodeCall.Name}"); } else if (tmpMethodeCall.Parameter.Count == 1) { throw new Exception("Set C# Property with Code not Implemented"); } else { throw new ArgumentException("To Many Parameters for a C# Property"); } } else { inOutput.Append($"{tmpMethodeCall.Name}({string.Join(",", tmpMethodeCall.Parameter.Select(inItem => AddCodeBlockToString(inItem, false)))})"); } } else if (inCodeEntry is CodeExpression) { var tmpExpr = inCodeEntry as CodeExpression; var tmpIsSingleEntryExpression = tmpExpr.Manipulator == VariableOperatorType.MinusMinus || tmpExpr.Manipulator == VariableOperatorType.PlusPlus; if (!tmpIsSingleEntryExpression) { inOutput.Append("("); } if (tmpExpr.Manipulator == VariableOperatorType.Not) { inOutput.Append(CSharpStaticInfo.GetOperatorString(tmpExpr.Manipulator)); } inOutput.Append(string.Join($" {CSharpStaticInfo.GetOperatorString(tmpExpr.Manipulator)} ", tmpExpr.SubClauseEntries.Select(inItem => CreateStatementCodeBlock(inItem)) )); // Operators -- and ++ do not habe a second Entry if (tmpIsSingleEntryExpression) { inOutput.Append(CSharpStaticInfo.GetOperatorString(tmpExpr.Manipulator)); } else { inOutput.Append(")"); } } else if (inCodeEntry is CodeBlockContainer) { inOutput.Append(CreateStatementCodeBlock((inCodeEntry as CodeBlockContainer).InnerBlock)); } else if (inCodeEntry is TypeConversion) { var tmpTypeConversion = inCodeEntry as TypeConversion; if (tmpTypeConversion.IsAsConversion) { inOutput.Append($"({CreateStatementCodeBlock(tmpTypeConversion.PreconversionValue)} as {CreateStringFromType(tmpTypeConversion.Type, false)}"); throw new Exception("To be tested"); } else { inOutput.Append($"({CreateStringFromType(tmpTypeConversion.Type, false)})"); inOutput.Append($"{CreateStatementCodeBlock(tmpTypeConversion.PreconversionValue)}"); } } else { throw new Exception("Code Entry Type not Implement"); } }
/// <summary> /// Write Code-Entry into C# Code /// </summary> /// <param name="inOutput"></param> /// <param name="inCodeEntry"></param> private TypeContainer CodeEntryHandling(ICodeEntry inCodeEntry, FieldNameFinder inNameFinder, TypeContainer inReturnType = null) { if (inCodeEntry == null) { return(null); } TypeContainer tmpReturnType = null; if (inCodeEntry is VariableDeclaration) { var tmpVarDecl = inCodeEntry as VariableDeclaration; inNameFinder.VariableList.Add(tmpVarDecl); var tmpConstant = new ConstantValue() { Type = tmpVarDecl.Type }; GetGlobalTypeForType(new FieldNameFinder(inNameFinder), tmpConstant, tmpVarDecl.Type.Name); tmpVarDecl.Type = tmpConstant.Type; } else if (inCodeEntry is ConstantValue) { var tmpConstant = (inCodeEntry as ConstantValue); var tmpVal = tmpConstant.Value?.ToString() ?? tmpConstant.Type.Name; if (tmpConstant.Value is FieldContainer) { tmpVal = (tmpConstant.Value as FieldContainer).Name; } else if (tmpConstant.Value is VariableDeclaration) { tmpVal = (tmpConstant.Value as VariableDeclaration).Name; } else if (tmpVal.EndsWith("\"")) { //It's a number, nothing to do tmpReturnType = ProjectInformation.GetAliasType("string")?.Type ?? tmpReturnType; } else if (RegexHelper.NumberCheck.IsMatch(tmpVal)) { //It's a number, nothing to do tmpReturnType = ProjectInformation.GetAliasType("int")?.Type ?? tmpReturnType; } else { //Access to Variable, Field, Param or static Class, so we need to do a lot here for Code-link if (tmpVal == "this") { inNameFinder.VariableList = new List <VariableDeclaration>(); tmpConstant.Type = inNameFinder.MethodeParentClass.Type; } else if (tmpVal == "base") { inNameFinder.Class = inNameFinder.Class.InterfaceList .Select(inItem => ProjectInformation.GetClassForType(inItem.Type.Name, new List <string> { inItem.Type.Namespace })) .FirstOrDefault(inItem => !inItem.IsInterface()); inNameFinder.VariableList = new List <VariableDeclaration>(); tmpConstant.Type = inNameFinder.Class.GetParentClass().Type; } else if (tmpVal.StartsWith("\"") && tmpVal.EndsWith("\"")) { //GetGlobalTypeForType(inNameFinder, tmpConstant, "String"); } else if (new Regex("^\\-?[0-9]*(\\.[0-9]*)?(S|D|F|L)?$").IsMatch(tmpVal)) { var tmpConstValue = tmpConstant.Value; if (tmpVal.EndsWith("L")) { GetGlobalTypeForType(inNameFinder, tmpConstant, "long"); } else if (tmpVal.EndsWith("D")) { GetGlobalTypeForType(inNameFinder, tmpConstant, "double"); } else if (tmpVal.EndsWith("S")) { GetGlobalTypeForType(inNameFinder, tmpConstant, "short"); } else if (tmpVal.EndsWith("F")) { GetGlobalTypeForType(inNameFinder, tmpConstant, "float"); } else { GetGlobalTypeForType(inNameFinder, tmpConstant, "int"); } tmpConstant.Value = tmpConstValue; } else { GetGlobalTypeForType(inNameFinder, tmpConstant, tmpVal); } tmpReturnType = tmpConstant.Type; } } else if (inCodeEntry is StatementCode) { var tmpStatement = inCodeEntry as StatementCode; if (tmpStatement.StatementType == Enum.StatementTypeEnum.If || tmpStatement.StatementType == Enum.StatementTypeEnum.Else || tmpStatement.StatementType == Enum.StatementTypeEnum.For || tmpStatement.StatementType == Enum.StatementTypeEnum.While) { if (tmpStatement.InnerContent != null) { foreach (var tmpEntry in tmpStatement.InnerContent.CodeEntries) { CodeEntryHandling(tmpEntry, new FieldNameFinder(inNameFinder)); } } if (tmpStatement.StatementCodeBlocks != null) { foreach (var tmpEntry in tmpStatement.StatementCodeBlocks.SelectMany(inItem => inItem.CodeEntries)) { CodeEntryHandling(tmpEntry, new FieldNameFinder(inNameFinder)); } } } else if (tmpStatement.StatementType == Enum.StatementTypeEnum.Assert || tmpStatement.StatementType == Enum.StatementTypeEnum.Elvis) { foreach (var tmpCodeBlock in tmpStatement.StatementCodeBlocks) { foreach (var tmpEntry in tmpCodeBlock.CodeEntries) { CodeEntryHandling(tmpEntry, new FieldNameFinder(inNameFinder)); } } } else { throw new NotImplementedException("CodeEntryHandling: StatementCode Handling not Implemented"); } } else if (inCodeEntry is SetFieldWithValue) { var tmpFieldVal = inCodeEntry as SetFieldWithValue; foreach (var tmpEntry in tmpFieldVal.VariableToAccess.CodeEntries) { CodeEntryHandling(tmpEntry, new FieldNameFinder(inNameFinder)); } foreach (var tmpEntry in tmpFieldVal.ValueToSet.CodeEntries) { CodeEntryHandling(tmpEntry, new FieldNameFinder(inNameFinder)); } } else if (inCodeEntry is VariableAccess) { var tmpVarAccess = inCodeEntry as VariableAccess; inNameFinder.StackVariables(true, true); ClassContainer tmpPrevClass = null; if (!(tmpVarAccess.Access is VariableDeclaration)) { tmpReturnType = CodeEntryHandling(tmpVarAccess.Access, inNameFinder); tmpPrevClass = inNameFinder.Class; if (tmpReturnType != null) { inNameFinder.Class = ProjectInformation.ClassFromBaseType(tmpReturnType); } else { inNameFinder.Class = null; } } if (tmpVarAccess.Child != null) { tmpReturnType = CodeEntryHandling(tmpVarAccess.Child, inNameFinder, inReturnType); } inNameFinder.Class = tmpPrevClass; inNameFinder.UnstackVariableList(); if (tmpVarAccess.BaseDataSource != null) { CodeEntryHandling(tmpVarAccess.BaseDataSource, inNameFinder, inReturnType); } } else if (inCodeEntry is ReturnCodeEntry) { foreach (var tmpEntry in (inCodeEntry as ReturnCodeEntry).CodeEntries) { CodeEntryHandling(tmpEntry, inNameFinder); } } else if (inCodeEntry is NewObjectDeclaration) { var tmpObjectDecl = (inCodeEntry as NewObjectDeclaration); CodeEntryHandling(tmpObjectDecl.InnerCode, inNameFinder); if (tmpObjectDecl.ArgumentList != null) { foreach (var tmpArgument in tmpObjectDecl.ArgumentList) { foreach (var tmpEntry in tmpArgument.CodeEntries) { CodeEntryHandling(tmpEntry, inNameFinder); } } } } else if (inCodeEntry is MethodeCall) { var tmpMethodeCall = inCodeEntry as MethodeCall; var tmpParentClass = inNameFinder.Class; while (tmpParentClass != null) { tmpMethodeCall.MethodeLink = tmpParentClass.MethodeList.FirstOrDefault(inItem => inItem.Name == tmpMethodeCall.Name); if (tmpMethodeCall.MethodeLink != null) { break; } tmpParentClass = tmpParentClass.GetParentClass(); foreach (var tmpParam in tmpMethodeCall.Parameter) { foreach (var tmpEntry in tmpParam.CodeEntries) { CodeEntryHandling(tmpEntry, new FieldNameFinder() { VariableList = inNameFinder.GetMethodeVariableList(), Class = inNameFinder.MethodeParentClass ?? inNameFinder.Class, MethodeParentClass = inNameFinder.MethodeParentClass }); } } } if (tmpMethodeCall.MethodeLink == null) { //TODO Implement Extension Methode finding if (inNameFinder.Class is UnknownTypeClass || inNameFinder.Class is ClassContainer) { if (tmpMethodeCall.Parameter.Count > 0) { foreach (var tmpParam in tmpMethodeCall.Parameter) { for (var tmpI = 0; tmpI < tmpParam.CodeEntries.Count; tmpI++) { var tmpCodeBlock = tmpParam.CodeEntries[tmpI]; CodeEntryHandling(tmpCodeBlock, new FieldNameFinder() { VariableList = inNameFinder.GetMethodeVariableList(), Class = inNameFinder.MethodeParentClass ?? inNameFinder.Class, MethodeParentClass = inNameFinder.MethodeParentClass }); } } } var tmpMethode = Create.AddMethode(inNameFinder.Class, tmpMethodeCall.Name, inReturnType ?? TypeContainer.Void); tmpMethode.ReturnType = /*inReturnType ??*/ TypeContainer.Void; tmpReturnType = tmpMethode.ReturnType; } else if (inNameFinder.Class == null) { } else { throw new NotImplementedException("Unknown Methode on Class"); } } else { if (tmpMethodeCall.Parameter.Count > 0) { foreach (var tmpParam in tmpMethodeCall.Parameter) { for (var tmpI = 0; tmpI < tmpParam.CodeEntries.Count; tmpI++) { var tmpCodeBlock = tmpParam.CodeEntries[tmpI]; CodeEntryHandling(tmpCodeBlock, new FieldNameFinder() { VariableList = inNameFinder.GetMethodeVariableList(), Class = inNameFinder.MethodeParentClass ?? inNameFinder.Class, MethodeParentClass = inNameFinder.MethodeParentClass }); } } } tmpReturnType = tmpMethodeCall.MethodeLink.ReturnType; } } else if (inCodeEntry is CodeExpression) { var tmpExpr = inCodeEntry as CodeExpression; tmpReturnType = inReturnType; foreach (var tmpSubClause in tmpExpr.SubClauseEntries) { foreach (var tmpCodeEntry in tmpSubClause.CodeEntries) { tmpReturnType = CodeEntryHandling(tmpCodeEntry, inNameFinder, tmpReturnType); } } } else if (inCodeEntry is CodeBlockContainer) { var tmpExpr = inCodeEntry as CodeBlockContainer; tmpReturnType = inReturnType; foreach (var tmpEntry in tmpExpr.InnerBlock.CodeEntries) { tmpReturnType = CodeEntryHandling(tmpEntry, inNameFinder); } } else if (inCodeEntry is TypeConversion) { var tmpExpr = inCodeEntry as TypeConversion; tmpReturnType = tmpExpr.Type; foreach (var tmpEntry in tmpExpr.PreconversionValue.CodeEntries) { CodeEntryHandling(tmpEntry, inNameFinder); } } else { throw new NotImplementedException("Code Entry Type not Implement"); } return(tmpReturnType); }