protected override void Visit(MacroChunk chunk) { _source.Write("def ").Write(chunk.Name).Write("("); string delimiter = ""; foreach (var parameter in chunk.Parameters) { _source.Write(delimiter).Write(parameter.Name); delimiter = ","; } _source.WriteLine("):"); _source.Indent++; foreach (var global in _globals.Keys) _source.Write("global ").WriteLine(global); var generator = new GeneratedCodeVisitor(_source, _globals); _source.WriteLine("__output__scope__=OutputScopeAdapter(None)"); _source.WriteLine("try:"); _source.Indent++; generator.Accept(chunk.Body); _source.WriteLine("return Output.ToString()"); _source.Indent--; _source.WriteLine("finally:"); _source.Indent++; _source.WriteLine("__output__scope__.Dispose()"); _source.Indent--; _source.Indent--; }
protected override void Visit(MacroChunk chunk) { _source.Write("def ").Write(chunk.Name).Write("("); string delimiter = ""; foreach (var parameter in chunk.Parameters) { _source.Write(delimiter).Write(parameter.Name); delimiter = ","; } _source.WriteLine(")"); _source.Indent++; var generator = new GeneratedCodeVisitor(_source, _globals); _source.WriteLine("__output__scope__=output_scope"); _source.WriteLine("begin"); _source.Indent++; generator.Accept(chunk.Body); _source.WriteLine("return output.to_string"); _source.Indent--; _source.WriteLine("ensure"); _source.Indent++; _source.WriteLine("__output__scope__.dispose"); _source.Indent--; _source.WriteLine("end"); _source.Indent--; _source.WriteLine("end"); }
protected override void Visit(MacroChunk chunk) { //no-op }
public void MacroAddsFunction() { var macro = new MacroChunk { Name = "foo", Parameters = new[] { new MacroParameter { Name = "x", Type = "string" } } }; macro.Body.Add(new SendExpressionChunk { Code = "x" }); var chunks = Chunks( new SendExpressionChunk { Code = "foo(\"hello\")" }, macro); _compiler.CompileView(chunks, chunks); var contents = ExecuteView(); Assert.AreEqual("hello", contents); }