public virtual void Visit(FunctionClosure e) { LuaAST f = Transform(e.Function); function.ChildFunction(f); result = new FunctionClosure(e.SourceSpan, f); }
public void UpVal(Variable upval) { for (LuaAST f = this; !f.ContainsUpVal(upval); f = f.Parent) { f.upvals.Add(upval); } }
public virtual LuaAST Transform(LuaAST f) { function = new LuaAST(f.Name, function); // Upvals. foreach (Variable upval in f.UpVals) { function.UpVal(upval); } // Parameters. foreach (Variable parameter in f.Parameters) { function.Parameter(parameter); } if (f.IsVararg) { function.SetVararg(); } // Locals. foreach (Variable local in f.Locals) { function.Local(local); } // Labels. foreach (Label label in f.Labels) { function.Label(label); } // Block. function.SetBlock((Block)Transform(f.Block)); // Multiple value return. if (f.ReturnsMultipleValues) { function.SetReturnsMultipleValues(); } // Return. f = function; function = function.Parent; return(f); }
// Construtor. public LuaAST(string name, LuaAST parent) { functions = new List <LuaAST>(); upvals = new List <Variable>(); parameters = new List <Variable>(); locals = new List <Variable>(); labels = new List <Label>(); Name = name; Parent = parent; Functions = functions.AsReadOnly(); UpVals = upvals.AsReadOnly(); Parameters = parameters.AsReadOnly(); IsVararg = false; Locals = locals.AsReadOnly(); Labels = labels.AsReadOnly(); Block = null; ReturnsMultipleValues = false; }
public void Write(LuaAST function) { // Function signature. o.Write("function "); if (function.Name != null) { o.Write(function.Name); } else { o.Write("x"); o.Write(function.GetHashCode().ToString("X")); } o.Write("("); if (function.Parameters.Count > 0 || function.IsVararg) { o.Write(" "); bool bFirst = true; foreach (Variable parameter in function.Parameters) { if (!bFirst) { o.Write(", "); } bFirst = false; o.Write(parameter.Name); } if (function.IsVararg) { if (!bFirst) { o.Write(", "); } bFirst = false; o.Write("..."); } o.Write(" "); } o.WriteLine(")"); // Returns multiple values. if (function.ReturnsMultipleValues) { o.WriteLine(" -- multiple return"); } // Labels. if (function.Labels.Count > 0) { o.Write(" -- label "); bool bFirst = true; foreach (Label label in function.Labels) { if (!bFirst) { o.Write(", "); } bFirst = false; o.Write(label.Name); } o.WriteLine(); } // Upvals. if (function.UpVals.Count > 0) { o.Write(" -- upval "); bool bFirst = true; foreach (Variable upval in function.UpVals) { if (!bFirst) { o.Write(", "); } bFirst = false; o.Write(upval.Name); } o.WriteLine(); } // Locals. if (function.Locals.Count > 0) { o.Write(" -- local "); bool bFirst = true; foreach (Variable local in function.Locals) { if (!bFirst) { o.Write(", "); } bFirst = false; o.Write(local.Name); } o.WriteLine(); } // Statements. WriteBlock(function.Block); // Final. o.WriteLine("end"); o.WriteLine(); // Other functions. foreach (LuaAST child in function.Functions) { Write(child); } }
public static void Write(TextWriter writer, LuaAST function) { LuaASTWriter w = new LuaASTWriter(writer); w.Write(function); }
public FunctionTransform() { function = null; block = null; }
// Building interface. public void ChildFunction(LuaAST function) { functions.Add(function); }