public static Branch Parse(GameObject go, string code)
{
var tokens = new Queue<string> (Tokenizer.Tokenize (code));
List<object> arguments = new List<object> ();
Branch branch = null;
Node instance = null;
while (tokens.Count > 0) {
var token = tokens.Dequeue ();
var atom = Atomize (token);
if (atom == OPEN) {
branch = instance as Branch;
instance = null;
DebugLog ("Open Branch: " + branch);
continue;
}
if (atom == CLOSE) {
DebugLog ("Close Branch: " + branch);
if (branch.parent != null)
branch = branch.parent;
continue;
}
if (atom == EOL) {
if (arguments.Count > 0 && instance != null) {
DebugLog ("Applying arguments: " + string.Join (", ", (from i in arguments select i == null ? "NULL" : i.ToString ()).ToArray ()));
instance.Apply (arguments.ToArray ());
arguments.Clear ();
}
continue;
}
if (atom is Node) {
DebugLog ("Node: " + atom + (atom is Branch ? "(Branch)" : "(Leaf)"));
instance = atom as Node;
instance.reactor = go.GetComponent<BADReactor>();
if (branch != null) {
DebugLog ("Adding to branch: " + branch);
branch.Add (instance);
}
continue;
}
if (atom is ComponentMethodLookup) {
var lookup = atom as ComponentMethodLookup;
lookup.Resolve (go);
arguments.Add (lookup);
continue;
}
arguments.Add (atom);
}
return branch;
}