private static void GenerateIfElseStatement(ScriptGenerator generator, MemberSymbol symbol, IfElseStatement statement) { if ((statement.IfStatement == null) && (statement.ElseStatement == null)) { return; } ScriptTextWriter writer = generator.Writer; writer.Write("if ("); ExpressionGenerator.GenerateExpression(generator, symbol, statement.Condition); writer.WriteLine(") {"); if (statement.IfStatement != null) { writer.Indent++; GenerateStatement(generator, symbol, statement.IfStatement); writer.Indent--; } writer.Write("}"); writer.WriteLine(); if (statement.ElseStatement != null) { bool writeEndBlock = false; if (statement.ElseStatement is IfElseStatement) { writer.Write("else "); } else { writer.WriteLine("else {"); writeEndBlock = true; writer.Indent++; } GenerateStatement(generator, symbol, statement.ElseStatement); if (writeEndBlock) { writer.Indent--; writer.WriteLine("}"); } } }
private static void GenerateEventExpression(ScriptGenerator generator, MemberSymbol symbol, EventExpression expression) { ScriptTextWriter writer = generator.Writer; EventSymbol eventSymbol = expression.Event; ExpressionGenerator.GenerateExpression(generator, symbol, expression.ObjectReference); if (eventSymbol.HasCustomAccessors) { writer.Write("."); if (expression.Type == ExpressionType.EventAdd) { writer.Write(eventSymbol.AddAccessor); } else { writer.Write(eventSymbol.RemoveAccessor); } writer.Write("('"); writer.Write(expression.Event.GeneratedName); writer.Write("', "); ExpressionGenerator.GenerateExpression(generator, symbol, expression.Handler); writer.Write(")"); } else { if (expression.Type == ExpressionType.EventAdd) { writer.Write(".add_"); } else { writer.Write(".remove_"); } writer.Write(expression.Event.GeneratedName); writer.Write("("); ExpressionGenerator.GenerateExpression(generator, symbol, expression.Handler); writer.Write(")"); } }
private static void GenerateSwitchStatement(ScriptGenerator generator, MemberSymbol symbol, SwitchStatement statement) { ScriptTextWriter writer = generator.Writer; writer.WriteTrimmed("switch "); writer.Write("("); ExpressionGenerator.GenerateExpression(generator, symbol, statement.Condition); writer.WriteTrimmed(") "); writer.Write("{"); writer.WriteNewLine(); writer.Indent++; foreach (SwitchGroup group in statement.Groups) { if (group.Cases != null) { foreach (Expression caseExpression in group.Cases) { writer.Write("case "); ExpressionGenerator.GenerateExpression(generator, symbol, caseExpression); writer.Write(":"); writer.WriteNewLine(); } } if (group.IncludeDefault) { writer.Write("default:"); writer.WriteNewLine(); } writer.Indent++; foreach (Statement childStatement in group.Statements) { GenerateStatement(generator, symbol, childStatement); } writer.Indent--; } writer.Indent--; writer.Write("}"); writer.WriteNewLine(); }
private static void GeneratePropertyExpression(ScriptGenerator generator, MemberSymbol symbol, PropertyExpression expression) { Debug.Assert(expression.Type == ExpressionType.PropertyGet); ScriptTextWriter writer = generator.Writer; if (expression.ObjectReference is BaseExpression) { Debug.Assert(symbol.Parent is ClassSymbol); writer.Write(((ClassSymbol)symbol.Parent).FullGeneratedName); writer.Write(".callBaseMethod(this, 'get_"); writer.Write(expression.Property.GeneratedName); writer.Write("')"); } else { ExpressionGenerator.GenerateExpression(generator, symbol, expression.ObjectReference); writer.Write(".get_"); writer.Write(expression.Property.GeneratedName); writer.Write("()"); } }
private static void GenerateForStatement(ScriptGenerator generator, MemberSymbol symbol, ForStatement statement) { if (statement.Body == null) { return; } ScriptTextWriter writer = generator.Writer; writer.WriteTrimmed("for "); writer.Write("("); if (statement.Initializers != null) { ExpressionGenerator.GenerateExpressionList(generator, symbol, statement.Initializers); } else if (statement.Variables != null) { GenerateVariableDeclarations(generator, symbol, statement.Variables); } writer.WriteTrimmed("; "); if (statement.Condition != null) { ExpressionGenerator.GenerateExpression(generator, symbol, statement.Condition); } writer.WriteTrimmed("; "); if (statement.Increments != null) { ExpressionGenerator.GenerateExpressionList(generator, symbol, statement.Increments); } writer.WriteTrimmed(") "); writer.Write("{"); writer.WriteNewLine(); writer.Indent++; GenerateStatement(generator, symbol, statement.Body); writer.Indent--; writer.Write("}"); writer.WriteNewLine(); }
private static void GenerateForInStatement(ScriptGenerator generator, MemberSymbol symbol, ForInStatement statement) { ScriptTextWriter writer = generator.Writer; if (statement.IsDictionaryEnumeration) { writer.Write("var "); writer.Write(statement.DictionaryVariable.GeneratedName); writer.WriteTrimmed(" = "); ExpressionGenerator.GenerateExpression(generator, symbol, statement.CollectionExpression); writer.Write(";"); writer.WriteNewLine(); writer.WriteTrimmed("for "); writer.Write("(var "); writer.Write(statement.LoopVariable.GeneratedName); writer.Write(" in "); writer.Write(statement.DictionaryVariable.GeneratedName); writer.WriteTrimmed(") "); writer.Write("{"); writer.WriteNewLine(); writer.Indent++; writer.Write("var "); writer.Write(statement.ItemVariable.GeneratedName); writer.WriteTrimmed(" = "); writer.WriteTrimmed("{ "); writer.WriteTrimmed("key: "); writer.Write(statement.LoopVariable.GeneratedName); writer.WriteTrimmed(", "); writer.WriteTrimmed("value: "); writer.Write(statement.DictionaryVariable.GeneratedName); writer.Write("["); writer.Write(statement.LoopVariable.GeneratedName); writer.Write("]"); writer.WriteTrimmed(" };"); writer.WriteNewLine(); GenerateStatement(generator, symbol, statement.Body); writer.Indent--; writer.Write("}"); writer.WriteNewLine(); } else { writer.Write("var "); writer.Write(statement.LoopVariable.GeneratedName); writer.WriteTrimmed(" = "); writer.Write("ss.IEnumerator.getEnumerator("); ExpressionGenerator.GenerateExpression(generator, symbol, statement.CollectionExpression); writer.Write(");"); writer.WriteNewLine(); writer.WriteTrimmed("while "); writer.Write("("); writer.Write(statement.LoopVariable.GeneratedName); writer.WriteTrimmed(".moveNext()) "); writer.Write("{"); writer.WriteNewLine(); writer.Indent++; writer.Write("var "); writer.Write(statement.ItemVariable.GeneratedName); writer.WriteTrimmed(" = "); writer.Write(statement.LoopVariable.GeneratedName); writer.Write(".current;"); writer.WriteNewLine(); GenerateStatement(generator, symbol, statement.Body); writer.Indent--; writer.Write("}"); writer.WriteNewLine(); } }
private static void GenerateDelegateExpression(ScriptGenerator generator, MemberSymbol symbol, DelegateExpression expression) { ScriptTextWriter writer = generator.Writer; bool createDelegate = false; if ((expression.Method.Visibility & MemberVisibility.Static) == 0) { createDelegate = true; writer.Write("ss.Delegate.create("); ExpressionGenerator.GenerateExpression(generator, symbol, expression.ObjectReference); writer.WriteTrimmed(", "); } AnonymousMethodSymbol anonymousMethod = expression.Method as AnonymousMethodSymbol; if (anonymousMethod == null) { // TODO: This probably needs to handle global method roots... if (expression.Method.IsGlobalMethod == false) { ExpressionGenerator.GenerateExpression(generator, symbol, expression.ObjectReference); writer.Write("."); } writer.Write(expression.Method.GeneratedName); } else { writer.Write("function("); if ((anonymousMethod.Parameters != null) && (anonymousMethod.Parameters.Count != 0)) { bool obfuscateParams = generator.Options.Minimize; string obfuscationPrefix = null; int paramIndex = 0; foreach (ParameterSymbol parameterSymbol in anonymousMethod.Parameters) { if (paramIndex > 0) { writer.WriteTrimmed(", "); } if (obfuscateParams) { if (paramIndex == 0) { obfuscationPrefix = "$p" + anonymousMethod.Depth.ToString(CultureInfo.InvariantCulture) + "_"; } parameterSymbol.SetTransformedName(obfuscationPrefix + paramIndex); } writer.Write(parameterSymbol.GeneratedName); paramIndex++; } } writer.Write(")"); writer.WriteTrimmed(" {"); writer.WriteLine(); writer.Indent++; CodeGenerator.GenerateScript(generator, anonymousMethod); writer.Indent--; writer.Write("}"); } if (createDelegate) { writer.Write(")"); } }