diff --git a/ssc/ast/nodes.cs b/ssc/ast/nodes.cs index 9fcfe93..1205916 100644 --- a/ssc/ast/nodes.cs +++ b/ssc/ast/nodes.cs @@ -37,6 +37,35 @@ namespace arookas public bool IsBranch { get { return Count > 0; } } public bool IsLeaf { get { return Count < 1; } } + public int MaxLocalCount + { + get + { + int locals = 0; + int maxChildLocals = 0; + foreach (var child in this) + { + if (child is sunVariableDeclaration || child is sunVariableDefinition) + { + ++locals; + } + else if (child is sunCompoundStatement) + { + locals += child.MaxLocalCount; // HACK: compound statements aren't their own scope + } + else + { + int childLocals = child.MaxLocalCount; + if (childLocals > maxChildLocals) + { + maxChildLocals = childLocals; + } + } + } + return locals + maxChildLocals; + } + } + public sunNode(sunSourceLocation location) { children = new List(5); diff --git a/ssc/symbol table.cs b/ssc/symbol table.cs index a76dc5b..eb8518d 100644 --- a/ssc/symbol table.cs +++ b/ssc/symbol table.cs @@ -135,7 +135,7 @@ namespace arookas context.DeclareParameter(parameter); } context.Text.StoreDisplay(1); - context.Text.DeclareLocal(CalculateMaxLocalCount(Body)); + context.Text.DeclareLocal(Body.MaxLocalCount); Body.Compile(context); context.Text.ReturnVoid(); context.Scopes.Pop(); @@ -152,32 +152,6 @@ namespace arookas context.Text.ClosePoint(callSite, Offset); } } - - static int CalculateMaxLocalCount(sunNode node) - { - int locals = 0; - int maxChildLocals = 0; - foreach (var child in node) - { - if (child is sunVariableDeclaration || child is sunVariableDefinition) - { - ++locals; - } - else if (child is sunCompoundStatement) - { - locals += CalculateMaxLocalCount(child); // HACK: compound statements aren't their own scope - } - else - { - int childLocals = CalculateMaxLocalCount(child); - if (childLocals > maxChildLocals) - { - maxChildLocals = childLocals; - } - } - } - return locals + maxChildLocals; - } } class sunParameterInfo : IEnumerable