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 static void parseLocalVariable(CsStatement pStatement, CodeBuilder pSb, FactoryExpressionCreator pCreator) { CsDeclarationStatement declarationStatement = (CsDeclarationStatement)pStatement; CsLocalVariableDeclaration localVariableDeclaration = declarationStatement.declaration as CsLocalVariableDeclaration; if (localVariableDeclaration == null) { parseLocalConstantDeclaration(pStatement, pSb, pCreator); return; } foreach (var declarator in localVariableDeclaration.declarators) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("var {0}:{1}", declarator.identifier.identifier, JsHelpers.Convert(Helpers.GetType(localVariableDeclaration.type)) ); if (declarator.initializer == null) { sb.Append(";"); } else { sb.AppendFormat(" = {0};", parseNode(declarator.initializer, pCreator)); } pSb.Append(sb.ToString()); 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, JsHelpers.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, JsHelpers.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(TheConstant pConstant, CodeBuilder pBuilder) { string modifiers = JsHelpers.ConvertModifiers(pConstant.Modifiers); foreach (Constant declarator in pConstant.Constants) { StringBuilder sb = new StringBuilder(); sb.AppendFormat(@"{0}const {1}:{2} = {3};", modifiers, declarator.Name, JsHelpers.Convert(declarator.ReturnType), declarator.Initializer.Value ); pBuilder.Append(sb.ToString()); pBuilder.AppendLine(); } }
private static void parseLocalConstantDeclaration(CsStatement pStatement, CodeBuilder pSb, FactoryExpressionCreator pCreator) { CsDeclarationStatement declarationStatement = (CsDeclarationStatement)pStatement; CsLocalConstantDeclaration lcd = (CsLocalConstantDeclaration)declarationStatement.declaration; foreach (CsLocalConstantDeclarator declarator in lcd.declarators) { StringBuilder sb = new StringBuilder(); sb.AppendFormat(@"const {0}:{1} = {2};", declarator.identifier.identifier, JsHelpers.Convert(Helpers.GetType(lcd.type)), pCreator.Parse(declarator.expression).Value ); pSb.Append(sb.ToString()); pSb.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); }
public Expression Parse(CsExpression pStatement, FactoryExpressionCreator pCreator) { CsAsIsExpression ex = (CsAsIsExpression)pStatement; return(new Expression( pCreator.Parse(ex.expression).Value + " " + JsHelpers.ConvertTokens(Helpers.GetTokenType(ex.oper)) + " " + JsHelpers.Convert(Helpers.GetType(ex.type)), ex.entity_typeref )); }
private static void parseForStatement(CsStatement pStatement, CodeBuilder pSb, FactoryExpressionCreator pCreator) { CsForStatement forStatement = (CsForStatement)pStatement; StringBuilder sb = new StringBuilder("for ("); CsLocalVariableDeclaration localVariableDeclaration = forStatement.initializer as CsLocalVariableDeclaration; CsStatementExpressionList expressionList; if (localVariableDeclaration == null) { expressionList = forStatement.initializer as CsStatementExpressionList; foreach (CsExpression expression in expressionList.expressions) { Expression ex = pCreator.Parse(expression); sb.Append(ex.Value); sb.Append(", "); } sb.Remove(sb.Length - 2, 2); sb.Append("; "); } else if (localVariableDeclaration.declarators.Count > 0) { sb.Append("var "); int count = localVariableDeclaration.declarators.Count; int now = 0; foreach (CsLocalVariableDeclarator declarator in localVariableDeclaration.declarators) { sb.AppendFormat("{0}:{1}", declarator.identifier.identifier, JsHelpers.Convert(Helpers.GetType(localVariableDeclaration.type)) ); now++; if (declarator.initializer != null) { sb.AppendFormat(" = {0}", parseNode(declarator.initializer, pCreator)); } if (now < count) { sb.Append(", "); } } sb.Append("; "); } sb.Append(pCreator.Parse(forStatement.condition).Value); sb.Append("; "); expressionList = (CsStatementExpressionList)forStatement.iterator; if (expressionList != null) { foreach (CsExpression expression in expressionList.expressions) { Expression ex = pCreator.Parse(expression); sb.Append(ex.Value); sb.Append(", "); } sb.Remove(sb.Length - 2, 2); } sb.Append("){"); pSb.AppendLine(sb.ToString()); ParseNode(forStatement.statement, pSb, pCreator); pSb.AppendLine("}"); pSb.AppendLine(); }