public static void Parse(TheConstructor pConstructor, CodeBuilder pBuilder, FactoryExpressionCreator pCreator) { if (pConstructor.IsStaticConstructor) { pBuilder.Append("{"); } else { pBuilder.AppendFormat("{4}{0}function {1}({2}){3} {{", ClassParser.IsMainClass ? "private " : As3Helpers.ConvertModifiers(pConstructor.Modifiers, _notValidConstructorMod), ClassParser.IsMainClass ? @"$ctor" : pConstructor.Name, As3Helpers.GetParameters(pConstructor.Arguments), ClassParser.IsMainClass ? ":void" : string.Empty, // pConstructor.MyClass.Name, pConstructor.OverridesBaseConstructor ? "override " : string.Empty ); } pBuilder.AppendLine(); if (pConstructor.HasBaseCall) { pBuilder.AppendFormat("\tsuper({0});", As3Helpers.GetCallingArguments(pConstructor.BaseArguments)); pBuilder.AppendLine(); } BlockParser.Parse(pConstructor.CodeBlock, pBuilder, pCreator); pBuilder.AppendLine("}"); pBuilder.AppendLine(); }
private static void parseForeachStatement(CsStatement pStatement, CodeBuilder pSb, FactoryExpressionCreator pCreator) { CsForeachStatement fes = (CsForeachStatement)pStatement; Expression ex = pCreator.Parse(fes.expression); string type = JsHelpers.Convert(Helpers.GetType(fes.type)); pSb.AppendLine(); _enumCount++; string enumName = String.Format(@"__ie{0}", _enumCount); pSb.AppendFormat("var {0}:IEnumerator = {1}.GetEnumerator();", enumName, ex.Value); pSb.AppendLine(); pSb.AppendFormat("while ({0}.MoveNext()){{", enumName); pSb.AppendLine(); pSb.AppendFormat("\tvar {1}:{2} = {0}.get_Current() as {2};", enumName, fes.identifier.identifier, type ); pSb.AppendLine(); ParseNode(fes.statement, pSb, pCreator); pSb.AppendLine("}"); pSb.AppendLine(); }
public static void Parse(TheConstructor pConstructor, CodeBuilder pBuilder, FactoryExpressionCreator pCreator) { if (pConstructor.IsStaticConstructor) { pBuilder.Append("{"); } else { pBuilder.AppendFormat("{4}{0}function {1}({2}){3} {{", ClassParser.IsMainClass ? "private " : As3Helpers.ConvertModifiers(pConstructor.Modifiers, _notValidConstructorMod), ClassParser.IsMainClass ? @"$ctor" : pConstructor.Name, As3Helpers.GetParameters(pConstructor.Arguments), ClassParser.IsMainClass ? ":void" : string.Empty,// pConstructor.MyClass.Name, pConstructor.OverridesBaseConstructor ? "override " : string.Empty ); } pBuilder.AppendLine(); if (pConstructor.HasBaseCall) { pBuilder.AppendFormat("\tsuper({0});", As3Helpers.GetCallingArguments(pConstructor.BaseArguments)); pBuilder.AppendLine(); } BlockParser.Parse(pConstructor.CodeBlock, pBuilder, pCreator); pBuilder.AppendLine("}"); pBuilder.AppendLine(); }
private static void parseUsingStatement(CsStatement pArg1, CodeBuilder pSb, FactoryExpressionCreator pCreator) { CsUsingStatement statement = (CsUsingStatement)pArg1; CsLocalVariableDeclaration declaration = statement.resource as CsLocalVariableDeclaration; string varname; if (declaration == null) { varname = "$$using$$"; Expression e = pCreator.Parse(statement.resource); pSb.AppendFormat("var {0}:{1} = {2};", varname, As3Helpers.Convert(Helpers.GetType(e.Type)), e.Value ); pSb.AppendLine(); } else { CsLocalVariableDeclarator declarator = declaration.declarators.First.Value; StringBuilder sb = new StringBuilder(); sb.AppendFormat("var {0}:{1}", declarator.identifier.identifier, As3Helpers.Convert(Helpers.GetType(declaration.type)) ); varname = declarator.identifier.identifier; if (declarator.initializer == null) { sb.Append(";"); } else { sb.AppendFormat(" = {0};", parseNode(declarator.initializer, pCreator)); } pSb.Append(sb.ToString()); pSb.AppendLine(); } pSb.Append("try {"); pSb.AppendLine(); ParseNode(statement.statement, pSb, pCreator); pSb.Append("} finally {"); pSb.AppendLine(); pSb.AppendFormat(" if ({0} != null) {0}.Dispose();", varname); pSb.AppendLine(); pSb.Append("}"); pSb.AppendLine(); pSb.AppendLine(); }
private static void parseUsingStatement(CsStatement pArg1, CodeBuilder pSb, FactoryExpressionCreator pCreator) { CsUsingStatement statement = (CsUsingStatement)pArg1; CsLocalVariableDeclaration declaration = statement.resource as CsLocalVariableDeclaration; string varname; if (declaration == null) { varname = "$$using$$"; Expression e = pCreator.Parse(statement.resource); pSb.AppendFormat("var {0}:{1} = {2};", varname, As3Helpers.Convert(Helpers.GetType(e.Type)), e.Value ); pSb.AppendLine(); } else { CsLocalVariableDeclarator declarator = declaration.declarators.First.Value; StringBuilder sb = new StringBuilder(); sb.AppendFormat("var {0}:{1}", declarator.identifier.identifier, As3Helpers.Convert(Helpers.GetType(declaration.type)) ); varname = declarator.identifier.identifier; if (declarator.initializer == null) { sb.Append(";"); } else { sb.AppendFormat(" = {0};", parseNode(declarator.initializer, pCreator)); } pSb.Append(sb.ToString()); pSb.AppendLine(); } pSb.Append("try {"); pSb.AppendLine(); ParseNode(statement.statement, pSb, pCreator); pSb.Append("} finally {"); pSb.AppendLine(); pSb.AppendFormat(" if ({0} != null) {0}.Dispose();", varname); pSb.AppendLine(); pSb.Append("}"); pSb.AppendLine(); pSb.AppendLine(); }
public static void Parse(TheIndexer pGetIndexer, CodeBuilder pBuilder, FactoryExpressionCreator pCreator) { bool isInterface = pGetIndexer.MyClass.IsInterface; if (pGetIndexer.Getter != null) { pBuilder.AppendFormat("{0}function {1}({2}):{3}{4}", As3Helpers.ConvertModifiers(pGetIndexer.Getter.Modifiers, _notValidMod), pGetIndexer.Getter.Name, As3Helpers.GetParameters(pGetIndexer.Getter.Arguments), As3Helpers.Convert(pGetIndexer.ReturnType), isInterface ? ";":" {" ); pBuilder.AppendLine(); if (!isInterface) { BlockParser.Parse(pGetIndexer.Getter.CodeBlock, pBuilder, pCreator); pBuilder.AppendLine(); pBuilder.AppendLine("}"); pBuilder.AppendLine(); } } if (pGetIndexer.Setter == null) { return; } pBuilder.AppendFormat( "{0}function {1}({2}):void{3}", As3Helpers.ConvertModifiers(pGetIndexer.Setter.Modifiers, _notValidMod), pGetIndexer.Setter.Name, As3Helpers.GetParameters(pGetIndexer.Setter.Arguments), isInterface ? ";" : " {" ); pBuilder.AppendLine(); if (isInterface) { return; } //BlockParser.InsideSetter = true; BlockParser.Parse(pGetIndexer.Setter.CodeBlock, pBuilder, pCreator); //BlockParser.InsideSetter = false; pBuilder.AppendLine(); pBuilder.AppendLine("}"); pBuilder.AppendLine(); }
public Expression Parse(CsExpression pStatement, FactoryExpressionCreator pCreator) { CsLambdaExpression ex = (CsLambdaExpression)pStatement; LambdaMethodExpression lambda = new LambdaMethodExpression((CsLambdaExpression)pStatement, pCreator); CodeBuilder b = new CodeBuilder(); b.AppendFormat("function ({0}):{1} {{", As3Helpers.GetParameters(lambda.Arguments), (lambda.ReturnType == null) ? "void" : As3Helpers.Convert(lambda.ReturnType) ); b.Indent(); b.Indent(); b.Indent(); b.AppendLine(); if (!(lambda.CodeBlock is CsBlock)) { b.Append("return "); } BlockParser.ParseNode(lambda.CodeBlock, b, pCreator); b.AppendLine("}"); b.AppendLine(); b.Unindent(); b.Unindent(); b.Unindent(); return new Expression(b.ToString(), ex.entity_typeref); }
private static void parseThrowStatement(CsStatement pStatement, CodeBuilder pSb, FactoryExpressionCreator pCreator) { CsThrowStatement throwStatement = (CsThrowStatement)pStatement; pSb.AppendFormat("throw {0};", parseNode(throwStatement.expression, pCreator)); pSb.AppendLine(); }
public override void VisitElseClause(ElseClauseSyntax node) { IfStatementSyntax ifNode = node.Statement as IfStatementSyntax; if (null != ifNode) { CodeBuilder.AppendFormat("{0}elseif ", GetIndentString()); VisitExpressionSyntax(ifNode.Condition); CodeBuilder.AppendLine(" then"); ++m_Indent; ifNode.Statement.Accept(this); --m_Indent; if (null != ifNode.Else) { VisitElseClause(ifNode.Else); } else { CodeBuilder.AppendFormat("{0}end;", GetIndentString()); CodeBuilder.AppendLine(); } } else { CodeBuilder.AppendFormat("{0}else", GetIndentString()); CodeBuilder.AppendLine(); ++m_Indent; node.Statement.Accept(this); --m_Indent; CodeBuilder.AppendFormat("{0}end;", GetIndentString()); CodeBuilder.AppendLine(); } }
public Expression Parse(CsExpression pStatement, FactoryExpressionCreator pCreator) { CsLambdaExpression ex = (CsLambdaExpression)pStatement; LambdaMethodExpression lambda = new LambdaMethodExpression((CsLambdaExpression)pStatement, pCreator); CodeBuilder b = new CodeBuilder(); b.AppendFormat("function ({0}):{1} {{", As3Helpers.GetParameters(lambda.Arguments), (lambda.ReturnType == null) ? "void" : As3Helpers.Convert(lambda.ReturnType) ); b.Indent(); b.Indent(); b.Indent(); b.AppendLine(); if (!(lambda.CodeBlock is CsBlock)) { b.Append("return "); } BlockParser.ParseNode(lambda.CodeBlock, b, pCreator); b.AppendLine("}"); b.AppendLine(); b.Unindent(); b.Unindent(); b.Unindent(); return(new Expression(b.ToString(), ex.entity_typeref)); }
public override void VisitIfStatement(IfStatementSyntax node) { string postfix = GetSourcePosForVar(node); CodeBuilder.AppendFormat("{0}if( ", GetIndentString()); var oper = m_Model.GetOperationEx(node) as IConditionalOperation; IConversionOperation opd = null; if (null != oper) { opd = oper.Condition as IConversionOperation; } OutputExpressionSyntax(node.Condition, opd); CodeBuilder.Append(", "); CodeBuilder.Append(postfix); CodeBuilder.AppendLine(" ){"); ++m_Indent; node.Statement.Accept(this); --m_Indent; if (null != node.Else) { VisitElseClause(node.Else); } else { CodeBuilder.AppendFormat("{0}}};", GetIndentString()); CodeBuilder.AppendLine(); } }
public override void VisitJoinClause(JoinClauseSyntax node) { var paramInfo = m_LinqParamInfoStack.Peek(); var paramNames = paramInfo.ParamNames; CodeBuilder.AppendFormat("join((function({0}){{ return(", string.Join(", ", paramNames.ToArray())); var opd = m_Model.GetOperation(node.InExpression) as IConversionExpression; OutputExpressionSyntax(node.InExpression, opd); CodeBuilder.Append("); }), "); paramInfo.JoinParamName = node.Identifier.Text; paramNames.Add(paramInfo.JoinParamName); CodeBuilder.AppendFormat("(function({0}){{ return(", string.Join(", ", paramNames.ToArray())); var opdl = m_Model.GetOperation(node.LeftExpression) as IConversionExpression; OutputExpressionSyntax(node.LeftExpression, opdl); CodeBuilder.Append("); }), "); CodeBuilder.AppendFormat("(function({0}){{ return(", string.Join(", ", paramNames.ToArray())); var opdr = m_Model.GetOperation(node.RightExpression) as IConversionExpression; OutputExpressionSyntax(node.RightExpression, opdr); CodeBuilder.Append("); }))"); if (null != node.Into) { VisitJoinIntoClause(node.Into); } }
public override void VisitSimpleLambdaExpression(SimpleLambdaExpressionSyntax node) { SymbolInfo symInfo = m_Model.GetSymbolInfo(node); var sym = symInfo.Symbol as IMethodSymbol; if (null != sym && sym.Parameters.Length == 1) { var param = sym.Parameters[0]; CodeBuilder.AppendFormat("(function({0}) return ", param.Name); IConversionExpression opd = null; var oper = m_Model.GetOperation(node) as ILambdaExpression; if (null != oper && oper.Body.Statements.Length == 1) { var iret = oper.Body.Statements[0] as IReturnStatement; if (null != iret) { opd = iret.ReturnedValue as IConversionExpression; } } var exp = node.Body as ExpressionSyntax; OutputExpressionSyntax(exp, opd); CodeBuilder.Append("; end)"); } else { ReportIllegalSymbol(node, symInfo); } }
public override void VisitContinueStatement(ContinueStatementSyntax node) { if (m_ContinueInfoStack.Count > 0) { var ci = m_ContinueInfoStack.Peek(); if (ci.IsIgnoreBreak) { return; } if (ci.HaveBreak) { CodeBuilder.AppendFormat("{0}{1} = false;", GetIndentString(), ci.BreakFlagVarName); CodeBuilder.AppendLine(); } bool isLastNode = IsLastNodeOfFor(node); if (isLastNode) { CodeBuilder.AppendFormat("{0}do", GetIndentString()); CodeBuilder.AppendLine(); } CodeBuilder.AppendFormat("{0}break;", GetIndentString()); CodeBuilder.AppendLine(); if (isLastNode) { CodeBuilder.AppendFormat("{0}end;", GetIndentString()); CodeBuilder.AppendLine(); } } }
public override void VisitVariableDeclarator(VariableDeclaratorSyntax node) { var ci = m_ClassInfoStack.Peek(); var localSym = m_Model.GetDeclaredSymbol(node) as ILocalSymbol; if (null != node.Initializer) { CodeBuilder.AppendFormat("{0}local {1}; {2}", GetIndentString(), node.Identifier.Text, node.Identifier.Text); } else { CodeBuilder.AppendFormat("{0}local {1}", GetIndentString(), node.Identifier.Text); } if (null != localSym && localSym.HasConstantValue) { CodeBuilder.Append(" = "); OutputConstValue(localSym.ConstantValue, localSym); CodeBuilder.AppendLine(";"); return; } VisitLocalVariableDeclarator(ci, node); if (null != node.Initializer) { var oper = m_Model.GetOperation(node.Initializer.Value); if (null != oper && null != oper.Type && oper.Type.TypeKind == TypeKind.Struct && oper.Type.ContainingAssembly == m_SymbolTable.AssemblySymbol) { CodeBuilder.AppendFormat("{0}{1} = wrapvaluetype({2});", GetIndentString(), node.Identifier.Text, node.Identifier.Text); CodeBuilder.AppendLine(); } } }
private void OutputDelegationCompareWithNull(ISymbol leftSym, ExpressionSyntax left, bool isCs2LuaAssembly, bool isEvent, bool isEqual, IConversionExpression opd) { var ci = m_ClassInfoStack.Peek(); CodeBuilder.AppendFormat("{0}delegationcomparewithnil({1}, ", isCs2LuaAssembly ? string.Empty : "extern", isEvent ? "true" : "false"); if (null != leftSym && (leftSym.Kind == SymbolKind.Field || leftSym.Kind == SymbolKind.Property || leftSym.Kind == SymbolKind.Event)) { var memberAccess = left as MemberAccessExpressionSyntax; if (null != memberAccess) { OutputExpressionSyntax(memberAccess.Expression, opd); CodeBuilder.Append(", "); string intf = "nil"; string mname = string.Format("\"{0}\"", memberAccess.Name.Identifier.Text); CheckExplicitInterfaceAccess(leftSym, ref intf, ref mname); CodeBuilder.AppendFormat("{0}, {1}", intf, mname); } else if (leftSym.ContainingType == ci.SemanticInfo || ci.IsInherit(leftSym.ContainingType)) { CodeBuilder.Append("this, nil, "); CodeBuilder.AppendFormat("\"{0}\"", leftSym.Name); } else { OutputExpressionSyntax(left, opd); CodeBuilder.Append(", nil, nil"); } } else { OutputExpressionSyntax(left, opd); CodeBuilder.Append(", nil, nil"); } CodeBuilder.AppendFormat(", {0})", isEqual ? "true" : "false"); }
public static void Parse(TheMethod pMethod, CodeBuilder pBuilder, FactoryExpressionCreator pCreator) { if (pMethod == null) return; bool isInterface = pMethod.MyClass.IsInterface; Dictionary<string,string> nonValidMethod = new Dictionary<string, string>(_notValidMethodMod); if (ClassParser.IsExtension) { nonValidMethod.Add("static",string.Empty); } pBuilder.AppendFormat("{0}function {1}({2}):{3}{4}", As3Helpers.ConvertModifiers(pMethod.Modifiers, nonValidMethod), pMethod.Name, As3Helpers.GetParameters(pMethod.Arguments), As3Helpers.Convert(pMethod.ReturnType), isInterface ? ";":" {" ); pBuilder.AppendLine(); if (isInterface) return; pBuilder.AppendLine(); BlockParser.Parse(pMethod.CodeBlock, pBuilder, pCreator); pBuilder.AppendLine(); pBuilder.AppendLine("}"); pBuilder.AppendLine(); }
public override void VisitForStatement(ForStatementSyntax node) { ContinueInfo ci = new ContinueInfo(); ci.Init(node.Statement); m_ContinueInfoStack.Push(ci); if (null != node.Declaration) { VisitVariableDeclaration(node.Declaration); } CodeBuilder.AppendFormat("{0}while ", GetIndentString()); if (null != node.Condition) { var oper = m_Model.GetOperation(node) as IForLoopStatement; IConversionExpression opd = null; if (null != oper) { opd = oper.Condition as IConversionExpression; } OutputExpressionSyntax(node.Condition, opd); } else { CodeBuilder.AppendLine("true"); } CodeBuilder.AppendLine(" do"); if (ci.HaveContinue) { if (ci.HaveBreak) { CodeBuilder.AppendFormat("{0}local {1} = false", GetIndentString(), ci.BreakFlagVarName); CodeBuilder.AppendLine(); } CodeBuilder.AppendFormat("{0}repeat", GetIndentString()); CodeBuilder.AppendLine(); } ++m_Indent; node.Statement.Accept(this); --m_Indent; if (ci.HaveContinue) { CodeBuilder.AppendFormat("{0}until true;", GetIndentString()); CodeBuilder.AppendLine(); if (ci.HaveBreak) { CodeBuilder.AppendFormat("{0}if {1} then break; end;", GetIndentString(), ci.BreakFlagVarName); CodeBuilder.AppendLine(); } } foreach (var exp in node.Incrementors) { CodeBuilder.AppendFormat("{0}", GetIndentString()); VisitToplevelExpression(exp, ";"); } CodeBuilder.AppendFormat("{0}end;", GetIndentString()); CodeBuilder.AppendLine(); m_ContinueInfoStack.Pop(); }
public override void VisitIfStatement(IfStatementSyntax node) { CodeBuilder.AppendFormat("{0}if ", GetIndentString()); var oper = m_Model.GetOperation(node) as IIfStatement; IConversionExpression opd = null; if (null != oper) { opd = oper.Condition as IConversionExpression; } OutputExpressionSyntax(node.Condition, opd); CodeBuilder.AppendLine(" then"); ++m_Indent; node.Statement.Accept(this); --m_Indent; if (null != node.Else) { VisitElseClause(node.Else); } else { CodeBuilder.AppendFormat("{0}end;", GetIndentString()); CodeBuilder.AppendLine(); } }
public override void VisitCatchClause(CatchClauseSyntax node) { MethodInfo mi = m_MethodInfoStack.Peek(); mi.TryCatchUsingOrLoopSwitchStack.Push(true); ReturnContinueBreakAnalysis returnAnalysis = mi.TempReturnAnalysisStack.Peek(); string handledVar = string.Format("__catch_handled_{0}", GetSourcePosForVar(node)); string bodyVar = string.Format("__catch_body_{0}", GetSourcePosForVar(node)); CodeBuilder.AppendFormat("{0}(function({1}", GetIndentString(), handledVar); if (null != node.Declaration) { CodeBuilder.Append(", "); CodeBuilder.Append(node.Declaration.Identifier.Text); } CodeBuilder.Append("){"); CodeBuilder.AppendLine(); ++m_Indent; if (null != node.Filter) { CodeBuilder.Append("if("); IConversionExpression opd = m_Model.GetOperation(node.Filter.FilterExpression) as IConversionExpression; OutputExpressionSyntax(node.Filter.FilterExpression, opd); CodeBuilder.Append("){"); CodeBuilder.AppendLine(); ++m_Indent; } CodeBuilder.AppendFormat("{0}{1} = true;", GetIndentString(), handledVar); CodeBuilder.AppendLine(); if (null != node.Filter) { --m_Indent; CodeBuilder.AppendFormat("{0}}};", GetIndentString()); CodeBuilder.AppendLine(); } if (returnAnalysis.Exist) { CodeBuilder.AppendFormat("{0}local({1}); {1} = function(){{", GetIndentString(), bodyVar); CodeBuilder.AppendLine(); ++m_Indent; VisitBlock(node.Block); --m_Indent; CodeBuilder.AppendFormat("{0}}};", GetIndentString()); CodeBuilder.AppendLine(); CodeBuilder.AppendFormat("{0}return({1}, {2}());", GetIndentString(), handledVar, bodyVar); CodeBuilder.AppendLine(); } else { VisitBlock(node.Block); CodeBuilder.AppendFormat("{0}return({1}, null);", GetIndentString(), handledVar); CodeBuilder.AppendLine(); } --m_Indent; CodeBuilder.AppendFormat("{0}}})", GetIndentString()); CodeBuilder.AppendLine(); mi.TryCatchUsingOrLoopSwitchStack.Pop(); }
public override void VisitVariableDeclarator(VariableDeclaratorSyntax node) { var ci = m_ClassInfoStack.Peek(); var localSym = m_Model.GetDeclaredSymbol(node) as ILocalSymbol; if (null != localSym && localSym.HasConstantValue) { if (null != node.Initializer) { CodeBuilder.AppendFormat("{0}local({1}); {2}", GetIndentString(), node.Identifier.Text, node.Identifier.Text); } else { CodeBuilder.AppendFormat("{0}local({1})", GetIndentString(), node.Identifier.Text); } CodeBuilder.Append(" = "); OutputConstValue(localSym.ConstantValue, localSym); CodeBuilder.AppendLine(";"); return; } bool dslToObject = false; IOperation oper = null; if (null != node.Initializer) { oper = m_Model.GetOperationEx(node.Initializer.Value); if (null != localSym && null != localSym.Type && null != oper && null != oper.Type) { dslToObject = InvocationInfo.IsDslToObject(localSym.Type, oper.Type); } } VisitLocalVariableDeclarator(ci, node, dslToObject); if (null != node.Initializer) { var rightSymbolInfo = m_Model.GetSymbolInfoEx(node.Initializer.Value); var rightSym = rightSymbolInfo.Symbol; if (null != oper && null != oper.Type && oper.Type.IsValueType && !dslToObject && !SymbolTable.IsBasicType(oper.Type) && !CsDslTranslater.IsImplementationOfSys(oper.Type, "IEnumerator")) { if (null != rightSym && (rightSym.Kind == SymbolKind.Method || rightSym.Kind == SymbolKind.Property || rightSym.Kind == SymbolKind.Field || rightSym.Kind == SymbolKind.Local) && SymbolTable.Instance.IsCs2DslSymbol(rightSym)) { MarkNeedFuncInfo(); if (SymbolTable.Instance.IsCs2DslSymbol(oper.Type)) { CodeBuilder.AppendFormat("{0}{1} = wrapstruct({2}, {3});", GetIndentString(), node.Identifier.Text, node.Identifier.Text, ClassInfo.GetFullName(oper.Type)); CodeBuilder.AppendLine(); } else { string ns = ClassInfo.GetNamespaces(oper.Type); if (ns != "System") { CodeBuilder.AppendFormat("{0}{1} = wrapexternstruct({2}, {3});", GetIndentString(), node.Identifier.Text, node.Identifier.Text, ClassInfo.GetFullName(oper.Type)); CodeBuilder.AppendLine(); } } } } } }
public override void VisitThrowStatement(ThrowStatementSyntax node) { CodeBuilder.AppendFormat("{0}luathrow(", GetIndentString()); IConversionExpression opd = m_Model.GetOperation(node.Expression) as IConversionExpression; OutputExpressionSyntax(node.Expression, opd); CodeBuilder.AppendLine(");"); }
public override void VisitAssignmentExpression(AssignmentExpressionSyntax node) { var ci = m_ClassInfoStack.Peek(); string op = node.OperatorToken.Text; string baseOp = op.Substring(0, op.Length - 1); bool needWrapFunction = true; var leftOper = m_Model.GetOperation(node.Left); var leftSymbolInfo = m_Model.GetSymbolInfo(node.Left); var leftSym = leftSymbolInfo.Symbol; var leftPsym = leftSym as IPropertySymbol; var leftMemberAccess = node.Left as MemberAccessExpressionSyntax; var leftElementAccess = node.Left as ElementAccessExpressionSyntax; var leftCondAccess = node.Left as ConditionalAccessExpressionSyntax; SpecialAssignmentType specialType = SpecialAssignmentType.None; if (null != leftMemberAccess && null != leftPsym) { if (!leftPsym.IsStatic) { if (CheckExplicitInterfaceAccess(leftPsym)) { specialType = SpecialAssignmentType.PropExplicitImplementInterface; } else if (SymbolTable.IsBasicValueProperty(leftPsym)) { specialType = SpecialAssignmentType.PropForBasicValueType; } } } if (specialType == SpecialAssignmentType.PropExplicitImplementInterface || specialType == SpecialAssignmentType.PropForBasicValueType || null != leftElementAccess || null != leftCondAccess || leftOper.Type.TypeKind == TypeKind.Delegate && (leftSym.Kind != SymbolKind.Local || op != "=")) { needWrapFunction = false; } if (needWrapFunction) { //顶层的赋值语句已经处理,这里的赋值都需要包装成lambda函数的样式 CodeBuilder.Append("(function() "); } VisitAssignment(ci, op, baseOp, node, string.Empty, false, leftOper, leftSym, leftPsym, leftMemberAccess, leftElementAccess, leftCondAccess, specialType); var oper = m_Model.GetOperation(node.Right); if (null != leftSym && leftSym.Kind == SymbolKind.Local && null != oper && null != oper.Type && oper.Type.TypeKind == TypeKind.Struct && oper.Type.ContainingAssembly == m_SymbolTable.AssemblySymbol) { CodeBuilder.AppendFormat("; {0} = wrapvaluetype({1})", leftSym.Name, leftSym.Name); } if (needWrapFunction) { CodeBuilder.Append("; return "); VisitExpressionSyntax(node.Left); CodeBuilder.Append("; end)()"); } }
public override void VisitReturnStatement(ReturnStatementSyntax node) { MethodInfo mi = m_MethodInfoStack.Peek(); mi.ExistTopLevelReturn = IsLastNodeOfMethod(node); bool isLastNode = IsLastNodeOfParent(node); if (!isLastNode) { CodeBuilder.AppendFormat("{0}do", GetIndentString()); CodeBuilder.AppendLine(); } string prestr; if (mi.SemanticInfo.MethodKind == MethodKind.Constructor) { CodeBuilder.AppendFormat("{0}return this", GetIndentString()); prestr = ", "; } else { CodeBuilder.AppendFormat("{0}return ", GetIndentString()); prestr = string.Empty; } if (null != node.Expression) { CodeBuilder.Append(prestr); IConversionExpression opd = null; var iret = m_Model.GetOperation(node) as IReturnStatement; if (null != iret) { opd = iret.ReturnedValue as IConversionExpression; } OutputExpressionSyntax(node.Expression, opd); prestr = ", "; } var names = mi.ReturnParamNames; if (names.Count > 0) { for (int i = 0; i < names.Count; ++i) { CodeBuilder.Append(prestr); CodeBuilder.Append(names[i]); prestr = ", "; } } CodeBuilder.AppendLine(";"); if (!isLastNode) { CodeBuilder.AppendFormat("{0}end;", GetIndentString()); CodeBuilder.AppendLine(); } }
public override void VisitYieldStatement(YieldStatementSyntax node) { MethodInfo mi = m_MethodInfoStack.Peek(); mi.ExistYield = true; if (node.ReturnOrBreakKeyword.Text == "return") { CodeBuilder.AppendFormat("{0}wrapyield(", GetIndentString()); if (null != node.Expression) { var oper = m_Model.GetOperation(node.Expression); var type = oper.Type; OutputExpressionSyntax(node.Expression); if (null != type && (IsImplementationOfSys(type, "IEnumerable") || IsImplementationOfSys(type, "IEnumerator"))) { CodeBuilder.Append(", true"); } else { CodeBuilder.Append(", false"); } if (null != type && IsSubclassOf(type, "UnityEngine.YieldInstruction")) { CodeBuilder.Append(", true"); } else { CodeBuilder.Append(", false"); } } else { CodeBuilder.Append("nil, false, false"); } CodeBuilder.Append(");"); CodeBuilder.AppendLine(); } else { bool isLastNode = IsLastNodeOfParent(node); if (!isLastNode) { CodeBuilder.AppendFormat("{0}do", GetIndentString()); CodeBuilder.AppendLine(); } CodeBuilder.AppendFormat("{0}return nil;", GetIndentString()); CodeBuilder.AppendLine(); if (!isLastNode) { CodeBuilder.AppendFormat("{0}end;", GetIndentString()); CodeBuilder.AppendLine(); } } }
public override void VisitFinallyClause(FinallyClauseSyntax node) { CodeBuilder.AppendFormat("{0}block{{", GetIndentString()); CodeBuilder.AppendLine(); ++m_Indent; VisitBlock(node.Block); --m_Indent; CodeBuilder.AppendFormat("{0}}};", GetIndentString()); CodeBuilder.AppendLine(); }
public static void Parse(TheIndexer pGetIndexer, CodeBuilder pBuilder, FactoryExpressionCreator pCreator) { bool isInterface = pGetIndexer.MyClass.IsInterface; if (pGetIndexer.Getter != null) { pBuilder.AppendFormat("{0}function {1}({2}):{3}{4}", JsHelpers.ConvertModifiers(pGetIndexer.Getter.Modifiers, _notValidMod), pGetIndexer.Getter.Name, JsHelpers.GetParameters(pGetIndexer.Getter.Arguments), JsHelpers.Convert(pGetIndexer.ReturnType), isInterface ? ";":" {" ); pBuilder.AppendLine(); if (!isInterface) { BlockParser.Parse(pGetIndexer.Getter.CodeBlock, pBuilder, pCreator); pBuilder.AppendLine(); pBuilder.AppendLine("}"); pBuilder.AppendLine(); } } if (pGetIndexer.Setter == null) { return; } pBuilder.AppendFormat( "{0}function {1}({2}):void{3}", JsHelpers.ConvertModifiers(pGetIndexer.Setter.Modifiers, _notValidMod), pGetIndexer.Setter.Name, JsHelpers.GetParameters(pGetIndexer.Setter.Arguments), isInterface ? ";" : " {" ); pBuilder.AppendLine(); if (isInterface) return; //BlockParser.InsideSetter = true; BlockParser.Parse(pGetIndexer.Setter.CodeBlock, pBuilder, pCreator); //BlockParser.InsideSetter = false; pBuilder.AppendLine(); pBuilder.AppendLine("}"); pBuilder.AppendLine(); }
public override void VisitContinueStatement(ContinueStatementSyntax node) { MethodInfo mi = m_MethodInfoStack.Peek(); if (m_ContinueInfoStack.Count > 0) { var ci = m_ContinueInfoStack.Peek(); if (ci.IsIgnoreBreak) { return; } if (ci.HaveBreak) { CodeBuilder.AppendFormat("{0}{1} = false;", GetIndentString(), ci.BreakFlagVarName); CodeBuilder.AppendLine(); } bool isLastNode = IsLastNodeOfFor(node); if (!isLastNode) { CodeBuilder.AppendFormat("{0}block{{", GetIndentString()); CodeBuilder.AppendLine(); } if (mi.TryUsingLayer > 0 && mi.TryCatchUsingOrLoopSwitchStack.Peek()) { var returnAnalysis = mi.TempReturnAnalysisStack.Peek(); if (returnAnalysis.ExistReturnInLoopOrSwitch || null == returnAnalysis.RetValVar) { //return(2)代表是tryusing块里的continue语句 CodeBuilder.AppendFormat("{0}return(2);", GetIndentString()); CodeBuilder.AppendLine(); } else { //可以不使用函数对象实现的try块,不能使用return语句,换成变量赋值与break CodeBuilder.AppendFormat("{0}{1} = 2;", GetIndentString(), returnAnalysis.RetValVar); CodeBuilder.AppendLine(); CodeBuilder.AppendFormat("{0}break;", GetIndentString()); CodeBuilder.AppendLine(); } } else { CodeBuilder.AppendFormat("{0}break;", GetIndentString()); CodeBuilder.AppendLine(); } if (!isLastNode) { CodeBuilder.AppendFormat("{0}}};", GetIndentString()); CodeBuilder.AppendLine(); } } }
private static void parseWhileStatement(CsStatement pStatement, CodeBuilder pSb, FactoryExpressionCreator pCreator) { CsWhileStatement whileStatement = (CsWhileStatement)pStatement; pSb.AppendFormat("while ({0}){{", pCreator.Parse(whileStatement.condition)); pSb.AppendLine(); ParseNode(whileStatement.statement, pSb, pCreator); pSb.Append("}"); pSb.AppendLine(); pSb.AppendLine(); }
public override void VisitUsingStatement(UsingStatementSyntax node) { VisitVariableDeclaration(node.Declaration); node.Statement.Accept(this); foreach (var decl in node.Declaration.Variables) { CodeBuilder.AppendFormat("{0}{1}:Dispose();", GetIndentString(), decl.Identifier.Text); CodeBuilder.AppendLine(); } }
public override void VisitOrdering(OrderingSyntax node) { var paramInfo = m_LinqParamInfoStack.Peek(); var paramNames = paramInfo.ParamNames; CodeBuilder.AppendFormat("orderby((function({0}){{ return(", string.Join(", ", paramNames.ToArray())); var opd = m_Model.GetOperation(node.Expression) as IConversionExpression; OutputExpressionSyntax(node.Expression, opd); CodeBuilder.AppendFormat("); }}), {0})", node.AscendingOrDescendingKeyword.Text != "descending" ? "true" : "false"); }
public override void VisitSelectClause(SelectClauseSyntax node) { var paramInfo = m_LinqParamInfoStack.Peek(); var paramNames = paramInfo.ParamNames; CodeBuilder.AppendFormat("select(function({0}){{ return(", string.Join(", ", paramNames.ToArray())); var opd = m_Model.GetOperation(node.Expression) as IConversionExpression; OutputExpressionSyntax(node.Expression, opd); CodeBuilder.Append("); })"); }
public override void VisitSimpleLambdaExpression(SimpleLambdaExpressionSyntax node) { SymbolInfo symInfo = m_Model.GetSymbolInfo(node); var sym = symInfo.Symbol as IMethodSymbol; if (null != sym && sym.Parameters.Length == 1) { if (node.Body is BlockSyntax) { var param = sym.Parameters[0]; CodeBuilder.AppendFormat("(function({0})", param.Name); CodeBuilder.AppendLine(); ++m_Indent; node.Body.Accept(this); if (!sym.ReturnsVoid) { CodeBuilder.AppendFormat("{0}return {1};", GetIndentString(), param.Name); CodeBuilder.AppendLine(); } --m_Indent; CodeBuilder.AppendFormat("{0}end)", GetIndentString()); } else { var param = sym.Parameters[0]; CodeBuilder.AppendFormat("(function({0}) return ", param.Name); IConversionExpression opd = null; var oper = m_Model.GetOperation(node) as ILambdaExpression; if (null != oper && oper.Body.Statements.Length == 1) { var iret = oper.Body.Statements[0] as IReturnStatement; if (null != iret) { opd = iret.ReturnedValue as IConversionExpression; } } var exp = node.Body as ExpressionSyntax; if (null != exp) { OutputExpressionSyntax(exp, opd); } else { ReportIllegalSymbol(node, symInfo); } CodeBuilder.Append("; end)"); } } else { ReportIllegalSymbol(node, symInfo); } }
public override void VisitQueryContinuation(QueryContinuationSyntax node) { var paramInfo = m_LinqParamInfoStack.Peek(); var paramNames = paramInfo.ParamNames; string prestr = paramInfo.Prestr; paramInfo.Prestr = ", "; paramNames.Clear(); paramNames.Add(node.Identifier.Text); CodeBuilder.AppendFormat("{0}{{\"continuation\"}}", prestr); node.Body.Accept(this); }
public override void VisitLetClause(LetClauseSyntax node) { var paramInfo = m_LinqParamInfoStack.Peek(); var paramNames = paramInfo.ParamNames; CodeBuilder.AppendFormat("let(function({0}){{ funcobjret(", string.Join(", ", paramNames.ToArray())); var opd = m_Model.GetOperationEx(node.Expression) as IConversionExpression; OutputExpressionSyntax(node.Expression, opd); CodeBuilder.Append("); })"); paramNames.Add(node.Identifier.Text); }
public static void Parse(TheEvent pEvent, CodeBuilder pBuilder) { if (pEvent.IsFlashEvent) return; ImportStatementList.AddImport(@"System.EventHandler"); bool isStatic = pEvent.Modifiers.Contains("static"); pBuilder.AppendFormat(@"private {1} var _e{0}:EventHandler; {2}{1} function get {0}():EventHandler {{ if (_e{0} == null) _e{0} = new EventHandler(); return _e{0}; }}", pEvent.Name, isStatic ? "static" : string.Empty, As3Helpers.ConvertModifiers(pEvent.Modifiers, _notValidClassMod) //,isStatic ? pEvent.MyClass.Name : "this" ); pBuilder.AppendLine(); }
public static void Parse(CsClass pCsClass, CodeBuilder pBuilder, FactoryExpressionCreator pCreator) { ExtensionName = null; StringBuilder sb = new StringBuilder(); CodeBuilder privateClasses = new CodeBuilder(); TheClass myClass = TheClassFactory.Get(pCsClass, pCreator); IsMainClass = Helpers.HasAttribute(pCsClass.attributes, "JsMainClassAttribute"); bool isResource = Helpers.HasAttribute(pCsClass.attributes, "JsEmbedAttribute"); IsExtension = Helpers.HasAttribute(pCsClass.attributes, "JsExtensionAttribute"); if (IsMainClass) { JsNamespaceParser.MainClassName = myClass.FullName; AttributeItem vals = Helpers.GetAttributeValue(pCsClass.attributes, "JsMainClassAttribute", pCreator)[0]; sb.AppendFormat(@"[SWF(width=""{0}"", height=""{1}"", frameRate=""{2}"", backgroundColor=""{3}"")]", vals.Parameters[0], vals.Parameters[1], vals.Parameters[2], vals.Parameters[3] ); sb.AppendLine(); sb.Append("\t"); } if (isResource) { AttributeItem vals = Helpers.GetAttributeValue(pCsClass.attributes, "JsEmbedAttribute", pCreator)[0]; string path = vals.Parameters[0] as String; if (!string.IsNullOrEmpty(path)) { path = Path.Combine(Project.Root, path); string ex = Path.GetExtension(path).Substring(1); string mimeType; if (vals.NamedArguments.ContainsKey("mimeType")) { mimeType = vals.NamedArguments["mimeType"].Value; } else { switch (ex) { case @"gif": case @"png": case @"jpg": case @"jpeg": case @"svg": mimeType = "image/" + ex; break; case @"mp3": mimeType = @"audio/mpeg"; break; case @"swf": mimeType = @"application/x-shockwave-flash"; break; case @"ttf": case @"otf": mimeType = @"application/x-font"; break; default: mimeType = @"application/octet-stream"; break; } } StringBuilder addParams = new StringBuilder(); foreach (var item in vals.NamedArguments.Where(pItem => !pItem.Key.Equals("mimeType"))) { addParams.AppendFormat(@", {0}=""{1}""", item.Key, item.Value.Value); } sb.AppendFormat(@"[Embed(source=""{0}"", mimeType=""{1}""{2})]", path.Replace("\\", "\\\\"), mimeType, addParams ); sb.AppendLine(); sb.Append("\t"); } } if (!IsExtension) { sb.AppendFormat("{1}class {0}", myClass.Name, JsHelpers.ConvertModifiers(myClass.Modifiers, _notValidClassMod)); if (myClass.Extends.Count != 0) { sb.AppendFormat(" extends {0}", JsHelpers.Convert(myClass.Extends[0])); } if (myClass.Implements.Count != 0) { sb.Append(" implements "); foreach (string s in myClass.Implements) { sb.Append(JsHelpers.Convert(s)); sb.Append(", "); } sb.Remove(sb.Length - 2, 2); } sb.Append(" {"); sb.AppendLine(); pBuilder.Append(sb.ToString()); pBuilder.AppendLineAndIndent(); } if (IsMainClass) { ImportStatementList.List.Add("flash.events.Event"); pBuilder.AppendFormat( @"public function {0}() {{ if (stage) $ctor(); else addEventListener(Event.ADDED_TO_STAGE, __loaded); }} private function __loaded(e:Event = null):void {{ removeEventListener(Event.ADDED_TO_STAGE, __loaded); $ctor(); }} ", myClass.Name); pBuilder.AppendLine(); } if (pCsClass.member_declarations != null) { if (!myClass.IsPrivate) ImportStatementList.List.Add(myClass.NameSpace+".*"); foreach (CsNode memberDeclaration in pCsClass.member_declarations) { if (memberDeclaration is CsConstructor) { MethodParser.Parse(myClass.GetConstructor((CsConstructor)memberDeclaration), pBuilder, pCreator); } else if (memberDeclaration is CsMethod) { MethodParser.Parse(myClass.GetMethod((CsMethod)memberDeclaration), pBuilder, pCreator); if (IsExtension && string.IsNullOrEmpty(ExtensionName)) { ExtensionName = ((CsMethod)memberDeclaration).identifier.identifier; } } else if (memberDeclaration is CsIndexer) { IndexerParser.Parse(myClass.GetIndexer((CsIndexer)memberDeclaration), pBuilder, pCreator); } else if (memberDeclaration is CsVariableDeclaration) { VariableParser.Parse(myClass.GetVariable((CsVariableDeclaration)memberDeclaration), pBuilder); } else if (memberDeclaration is CsConstantDeclaration) { ConstantParser.Parse(myClass.GetConstant((CsConstantDeclaration)memberDeclaration), pBuilder); } else if (memberDeclaration is CsDelegate) { DelegateParser.Parse(myClass.GetDelegate((CsDelegate)memberDeclaration), pBuilder); } else if (memberDeclaration is CsEvent) { EventParser.Parse(myClass.GetEvent(((CsEvent)memberDeclaration).declarators.First.Value.identifier.identifier), pBuilder); } else if (memberDeclaration is CsProperty) { PropertyParser.Parse(myClass.GetProperty((CsProperty)memberDeclaration), pBuilder, pCreator); } else if (memberDeclaration is CsClass) { Parse((CsClass)memberDeclaration, privateClasses, pCreator); } else { throw new NotSupportedException(); } } } string imports = getImports(); pBuilder.Replace(IMPORT_MARKER, imports); pBuilder.AppendLineAndUnindent("}"); if (IsExtension) { return; } if (!myClass.IsPrivate) { pBuilder.AppendLineAndUnindent("}"); } if (privateClasses.Length == 0) { return; } pBuilder.AppendLine(); pBuilder.Append(JsNamespaceParser.Using); pBuilder.AppendLine(imports); pBuilder.Append(privateClasses); }
private static void parseForeachStatement(CsStatement pStatement, CodeBuilder pSb, FactoryExpressionCreator pCreator) { CsForeachStatement fes = (CsForeachStatement)pStatement; Expression ex = pCreator.Parse(fes.expression); string type = JsHelpers.Convert(Helpers.GetType(fes.type)); pSb.AppendLine(); _enumCount++; string enumName = String.Format(@"__ie{0}", _enumCount); pSb.AppendFormat("var {0}:IEnumerator = {1}.GetEnumerator();", enumName, ex.Value); pSb.AppendLine(); pSb.AppendFormat("while ({0}.MoveNext()){{", enumName); pSb.AppendLine(); pSb.AppendFormat("\tvar {1}:{2} = {0}.get_Current() as {2};", enumName, fes.identifier.identifier, type ); pSb.AppendLine(); ParseNode(fes.statement, pSb, pCreator); pSb.AppendLine("}"); pSb.AppendLine(); }
private void WriteDefinition(CodeBuilder cb) { cb.AppendFormat("private {0} {1} {{", typeName, name).AppendLine().Indent() .AppendFormat("get {{ return ({0})controls[\"{1}\"]; }}", typeName, name).AppendLine(); if (customInstantiate) { cb.AppendLine("set {").Indent() .AppendFormat("controls[\"{0}\"] = value;", name).AppendLine() .AppendLine("if (!string.IsNullOrEmpty(id))").Indent() .AppendFormat("((IControl)controls[\"{0}\"]).Id = id + \"_{0}\";", name).Outdent().AppendLine() .Outdent().AppendLine("}"); } cb.Outdent().AppendLine("}").AppendLine(); }
public static void Parse(TheProperty pProperty, CodeBuilder pBuilder, FactoryExpressionCreator pCreator) { if (pProperty == null) return; bool isInterface = pProperty.MyClass.IsInterface; string type = JsHelpers.Convert(pProperty.ReturnType); if (pProperty.IsEmpty && !isInterface) { pBuilder.AppendFormat("private var _{0}:{1};", pProperty.Name, type); pBuilder.AppendLine(); } TheClass parent = pProperty.MyClass; bool isStandardGetSet = false; while (parent.Base != null) { isStandardGetSet |= parent.FullName.StartsWith("flash."); parent = parent.Base; } if (pProperty.Getter != null) {//Getter if (isStandardGetSet) {//base is flash, use standard setter/getter pBuilder.AppendFormat("{0}function get {1}():{2}{3}", JsHelpers.ConvertModifiers(pProperty.Getter.Modifiers, _notValidPropertyMod), pProperty.Name, type, isInterface ? ";" : " {" ); } else { bool isEnum = false; foreach (string s in pProperty.MyClass.Implements) { if (!s.StartsWith("IEnumerator", StringComparison.Ordinal)) { continue; } isEnum = true; break; } isEnum &= pProperty.Getter.Name.Equals("get_Current"); //bool isEnum = // pProperty.Getter.Name.Equals("get_Current") && // pProperty.MyClass.Implements.Contains() pBuilder.AppendFormat("{0}function {1}():{2}{3}", JsHelpers.ConvertModifiers(pProperty.Getter.Modifiers, _notValidPropertyMod), pProperty.Getter.Name, isEnum ? "*" : type, isInterface ? ";" : " {" ); } pBuilder.AppendLine(); if (!isInterface) { if (pProperty.IsEmpty) { pBuilder.Indent(); pBuilder.AppendFormat("return _{0};", pProperty.Name); pBuilder.Unindent(); } else { BlockParser.Parse(pProperty.Getter.CodeBlock, pBuilder, pCreator); } pBuilder.AppendLine(); pBuilder.AppendLine("}"); pBuilder.AppendLine(); } } if (pProperty.Setter == null) { return; } if (isStandardGetSet) {//Setter //base is flash, use standard setter/getter pBuilder.AppendFormat("{0}function set {1}(value:{2}):void{3}", JsHelpers.ConvertModifiers(pProperty.Setter.Modifiers, _notValidPropertyMod), pProperty.Name, type, isInterface ? ";" : " {" ); } else { pBuilder.AppendFormat("{0}function {1}(value:{2}):{2}{3}", JsHelpers.ConvertModifiers(pProperty.Setter.Modifiers, _notValidPropertyMod), pProperty.Setter.Name, type, isInterface ? ";" : " {" ); } pBuilder.AppendLine(); if (!isInterface) { if (pProperty.IsEmpty) { pBuilder.Indent(); pBuilder.AppendFormat("_{0} = value;", pProperty.Name); pBuilder.AppendLine(); pBuilder.Append("return value;"); pBuilder.Unindent(); } else { BlockParser.InsideSetter = !isStandardGetSet; BlockParser.Parse(pProperty.Setter.CodeBlock, pBuilder, pCreator); BlockParser.InsideSetter = false; if (!isStandardGetSet) pBuilder.AppendLine(" return value;"); } pBuilder.AppendLine(); pBuilder.AppendLine("}"); } pBuilder.AppendLine(); }
private static void parseThrowStatement(CsStatement pStatement, CodeBuilder pSb, FactoryExpressionCreator pCreator) { CsThrowStatement throwStatement = (CsThrowStatement)pStatement; pSb.AppendFormat("throw {0};", parseNode(throwStatement.expression, pCreator)); pSb.AppendLine(); }
private static void parseIfStatement(CsStatement pStatement, CodeBuilder pSb, FactoryExpressionCreator pCreator) { CsIfStatement ifStatement = (CsIfStatement)pStatement; pSb.AppendFormat("if ({0}){{", pCreator.Parse(ifStatement.condition)); pSb.AppendLine(); ParseNode(ifStatement.if_statement, pSb, pCreator); if (ifStatement.else_statement != null) { pSb.AppendLine(); pSb.Append("} else {"); pSb.AppendLine(); pSb.AppendLine(); ParseNode(ifStatement.else_statement, pSb, pCreator); } pSb.Append("}"); pSb.AppendLine(); pSb.AppendLine(); }
private static void parseForeachStatement(CsStatement pStatement, CodeBuilder pSb, FactoryExpressionCreator pCreator) { CsForeachStatement fes = (CsForeachStatement)pStatement; Expression ex = pCreator.Parse(fes.expression); string type = As3Helpers.Convert(Helpers.GetType(fes.type)); pSb.AppendLine(); TheClass theClass = TheClassFactory.Get(ex.Type, pCreator); if (ex.Type.type == cs_entity_type.et_array || ex.IsAs3Generic || (theClass != null && @"System.Array".Equals(theClass.FullName))) {//foreach pSb.AppendFormat("for each(var {0}:{1} in {2}){{", fes.identifier.identifier, type, ex.Value); pSb.AppendLine(); } else if (ex.Type.type == cs_entity_type.et_object || ex.Type.type == cs_entity_type.et_generic_param || (theClass != null && @"flash.utils.Dictionary".Equals(theClass.FullName))) { pSb.AppendFormat("for (var {0}:{1} in {2}){{", fes.identifier.identifier, type, ex.Value); pSb.AppendLine(); if (ex.Type.type == cs_entity_type.et_object) { pSb.AppendFormat(" if (!{1}.hasOwnProperty({0})) continue;", fes.identifier.identifier, ex.Value ); } pSb.AppendLine(); } else { _enumCount++; //TheClass theClass = TheClassFactory.Get(fes.expression.entity_typeref); string enumName = String.Format(@"__ie{0}", _enumCount); pSb.AppendFormat("var {0}:IEnumerator = {1}.GetEnumerator();", enumName, ex.Value); pSb.AppendLine(); pSb.AppendFormat("while ({0}.MoveNext()){{", enumName); pSb.AppendLine(); pSb.AppendFormat("\tvar {1}:{2} = {0}.get_Current() as {2};", enumName, fes.identifier.identifier, type ); pSb.AppendLine(); } ParseNode(fes.statement, pSb, pCreator); pSb.AppendLine("}"); pSb.AppendLine(); }
private static void parseWhileStatement(CsStatement pStatement, CodeBuilder pSb, FactoryExpressionCreator pCreator) { CsWhileStatement whileStatement = (CsWhileStatement)pStatement; pSb.AppendFormat("while ({0}){{", pCreator.Parse(whileStatement.condition)); pSb.AppendLine(); ParseNode(whileStatement.statement, pSb, pCreator); pSb.Append("}"); pSb.AppendLine(); pSb.AppendLine(); }
private static void parseReturnStatement(CsStatement pStatement, CodeBuilder pSb, FactoryExpressionCreator pCreator) { CsReturnStatement returnStatement = (CsReturnStatement) pStatement; if (returnStatement.expression == null) { //pSb.AppendLine(InsideConstructor ? "return this" : InsideSetter ? "return value;" : "return;"); pSb.AppendLine(InsideSetter ? "return value;" : "return;"); } else { pSb.AppendFormat("return {0};", pCreator.Parse(returnStatement.expression).Value); pSb.AppendLine(); } }
private static void parseSwitchStatement(CsStatement pStatement, CodeBuilder pSb, FactoryExpressionCreator pCreator) { CsSwitchStatement switchStatement = (CsSwitchStatement)pStatement; pSb.AppendFormat("switch ({0}){{", pCreator.Parse(switchStatement.expression).Value); pSb.AppendLine(); pSb.Indent(); foreach (CsSwitchSection caseNode in switchStatement.sections) { LinkedList<CsSwitchLabel> labels = caseNode.labels; foreach (CsSwitchLabel label in labels){ if (label.default_label) { pSb.Append("default:"); pSb.AppendLine(); } else { Expression txt = pCreator.Parse(label.expression); pSb.AppendFormat("case {0}:", txt.Value); pSb.AppendLine(); } } foreach (CsStatement statementNode in caseNode.statements) { pSb.Indent(); parseStatement(statementNode, pSb, pCreator); pSb.Unindent(); } } pSb.Unindent(); pSb.Append("}"); pSb.AppendLine(); }