public void Add(IEmitable statement) { if (statement is IPyStatement && !PyCodeBlock.HasAny(statement as IPyStatement)) { return; } if (statement is PyNamespaceStatement) { var tmp = statement as PyNamespaceStatement; var item = GetItemForNamespace(Container, tmp.Name); item.Items.AddRange(tmp.Code.Statements); } else if (statement is PyClassDefinition) { var tmp = statement as PyClassDefinition; if (tmp.IsEmpty) { return; } var item = GetItemForNamespace(Container, tmp.Name.Namespace); item.Items.Add(statement); } else { var item = GetItemForNamespace(Container, PyNamespace.Root); item.Items.Add(statement); } }
protected override IPyStatement[] VisitCodeBlock(CodeBlock src) { var res = new PyCodeBlock(); res.Statements.AddRange(this.TranslateStatements(src.Items)); return(MkArray(res)); }
protected override IPyStatement[] VisitSwitchStatement(CsharpSwitchStatement src) { var switchStatement = new PySwitchStatement { Expression = TransValue(src.Expression) }; foreach (var sec in src.Sections) { var section = new PySwitchSection { Labels = sec.Labels.Select(q => new PySwitchLabel { Value = q.Expression == null ? null : TransValue(q.Expression), IsDefault = q.IsDefault }).ToArray() }; var statements = this.TranslateStatements(sec.Statements); var block = new PyCodeBlock(); block.Statements.AddRange(statements); section.Statement = PyCodeBlock.Reduce(block); switchStatement.Sections.Add(section); } return(MkArray(switchStatement)); }
protected virtual T VisitPyCodeBlock(PyCodeBlock node) { if (ThrowNotImplementedException) { throw new NotImplementedException(string.Format("Method {0} is not supported in class {1}", "VisitPyCodeBlock", this.GetType().FullName)); } return(default(T)); }
public IPyStatement[] Translate(ForStatement src) { var condition = _translator.TransValue(src.Condition); var statement = _translator.TranslateStatementOne(src.Statement); var incrementors = _translator.TranslateStatements(src.Incrementors); var declarations = _translator.TranslateStatement(src.Declaration).ToArray(); var pyDeclarations = new List <PyAssignExpression>(); foreach (object declaration in declarations) { var d = declaration; if (declaration is PyExpressionStatement) { d = (declaration as PyExpressionStatement).Expression; } if (d is PyAssignExpression) { pyDeclarations.Add(d as PyAssignExpression); } else { throw new NotSupportedException(); } } var pyDeclarationsA = pyDeclarations.ToArray(); var candidate = TrySimpleForLoop(pyDeclarationsA, incrementors, condition, statement); if (candidate != null) { return(candidate); } // convert to while loop { var s = new List <IPyStatement>(); foreach (var i in pyDeclarationsA) { s.Add(new PyExpressionStatement(i)); } var block = new PyCodeBlock(); block.Statements.Add(statement); foreach (var i in incrementors) { block.Statements.Add(i); } var wl = new PyWhileStatement(condition, block); s.Add(wl); return(s.ToArray()); } }
public void Emit(PySourceCodeEmiter emiter, PySourceCodeWriter writer, PyEmitStyle style) { var nsManager = new PyModuleNamespaceManager(); style.CurrentNamespace = null; if (!string.IsNullOrEmpty(_topComments)) { writer.WriteLn("'''\r\n" + _topComments.Trim() + "\r\n'''"); } var module = this; { // var noBracketStyle = PyEmitStyle.xClone(style, ShowBracketsEnum.Never); { // top code var collectedTopCodeBlock = new PyCodeBlock(); collectedTopCodeBlock.Statements.AddRange(ConvertRequestedToCode()); collectedTopCodeBlock.Statements.AddRange(ConvertDefinedConstToCode()); if (TopCode != null) { collectedTopCodeBlock.Statements.AddRange(TopCode.Statements); } nsManager.Add(collectedTopCodeBlock.Statements); } foreach (var m in Methods) { m.Emit(emiter, writer, style); } { var classesGbNamespace = module.Classes.GroupBy(u => u.Name.Namespace); foreach (var classesInNamespace in classesGbNamespace.OrderBy(i => !i.Key.IsRoot)) { foreach (var c in classesInNamespace) { nsManager.Add(c); } } } if (BottomCode != null) { nsManager.Add(BottomCode.Statements); } if (nsManager.Container.Any()) { if (nsManager.OnlyOneRootStatement) { foreach (var cl in nsManager.Container[0].Items) { cl.Emit(emiter, writer, style); } } else { foreach (var ns in nsManager.Container) { EmitWithNamespace(ns.Name, emiter, writer, style, ns.Items); } } } } }
protected override IPyStatement VisitPyCodeBlock(PyCodeBlock node) { var newNode = new PyCodeBlock(); foreach (var i in node.GetPlain()) { newNode.Statements.Add(Simplify(i)); } if (op.JoinEchoStatements) { for (var i = 1; i < newNode.Statements.Count; i++) { var e1 = GetPyNativeMethodCall(newNode.Statements[i - 1], "echo"); if (e1 == null) { continue; } var e2 = GetPyNativeMethodCall(newNode.Statements[i], "echo"); if (e2 == null) { continue; } Func <IPyValue, IPyValue> AddBracketsIfNecessary = ee => { if (ee is PyParenthesizedExpression || ee is PyConstValue || ee is PyPropertyAccessExpression) { return(ee); } if (ee is PyBinaryOperatorExpression && ((PyBinaryOperatorExpression)ee).Operator == ".") { return(ee); } return(new PyParenthesizedExpression(ee)); }; var a1 = AddBracketsIfNecessary(e1.Arguments[0].Expression); var a2 = AddBracketsIfNecessary(e2.Arguments[0].Expression); IPyValue e = new PyBinaryOperatorExpression(".", a1, a2); e = Simplify(e); IPyValue echo = new PyMethodCallExpression("echo", e); newNode.Statements[i - 1] = new PyExpressionStatement(echo); newNode.Statements.RemoveAt(i); i--; } for (var i = 0; i < newNode.Statements.Count; i++) { var a = newNode.Statements[i]; if (a is PySourceBase) { newNode.Statements[i] = Visit(a as PySourceBase); } } } return(PySourceBase.EqualCode_List(node.Statements, newNode.Statements) ? node : newNode); }