private static int FindBlockLength(TokenArray tokens) { var iterator = new TokenIterator(tokens); List <TokenMode> blocks = new List <TokenMode>(); { var token = iterator.GetNext(); var end = GetBlockEndMode(token.Mode); Debug.Assert(end != null, "Block must begin with a block start"); blocks.Add(end); } while (iterator.HasNext && blocks.Count > 0) { var token = iterator.GetNext(); if (token.Mode == blocks[blocks.Count - 1]) { blocks.RemoveAt(blocks.Count - 1); } else { var end = GetBlockEndMode(token.Mode); if (end != null) { blocks.Add(end); } } } return(iterator.Iterator - tokens.Start + 1); // +1 to include the last bracket }
public static Instruction Parse(TokenArray tokens) { var iterator = new TokenIterator(tokens); /* VDeclr: type, named, (equals, Statement){0..1}, semicolon * Declr: [FDeclr, VDeclr] * PDeclr: (VarDeclr, comma){1..n} * Params: (Statement, comma){1..n} * Block: [Function, Declr, Call]{1..n} * Statement: [Call, Equation], semicolon * Call: named, paren, Params, paren * FDeclr: type, named, paren, PDeclr, paren, brace, Block, brace */ var iter = Parse(ref iterator, blockRule, 0); if (iter == null) { iterator.Iterator = farthestIterator; string str = ""; for (int t = 0; t < 10 && iterator.HasNext; ++t) { str += (t > 0 ? " " : "") + iterator.GetNext().String; } Debug.WriteLine("Unable to complete parse, error at " + str); return(null); } var context = new ParseContext(); iter(context); var store = context.GetStoreAt(0); for (int o = 0; o < store.Count; ++o) { Console.WriteLine(store[o]); } return(store[0] as Instruction); while (iterator.HasNext) { var token = iterator.GetNext(); if (token.Mode == commentToken) { continue; } else if (token.Mode == typeToken) // Variable / function declaration { string type = token.String; token = iterator.GetNext(); Debug.Assert(token.Mode == nameToken, "A name must come after the type in a variable declaration, " + type); string name = token.String; Debug.Assert(iterator.HasNext, "Missing semicolon, assignment operator or open parenthesis after type declaration, " + type + " " + name); token = iterator.GetNext(); if (token.Mode == endStatementToken) { Console.WriteLine("Variable found, " + type + " " + name); } else if (token.Mode == operatorToken) { Debug.Assert(token.String == "=", "A type declaration can only have an assignment operator, " + type + " " + name); string equalTo = ""; token = iterator.GetNext(); int equalI = iterator.Iterator; while (token.Mode != endStatementToken) { equalTo += token.String; token = iterator.GetNext(); } Console.WriteLine("Variable found, " + type + " " + name + " = " + equalTo); Parse(new TokenArray(tokens.Tokens, equalI, iterator.Iterator)); } else if (token.Mode == parmStartToken) { int len = FindBlockLength(new TokenArray(tokens.Tokens, iterator.Iterator, tokens.End)); iterator.Iterator += len - 1; token = iterator.GetNext(); if (token.Mode == blockStartToken) { int bodyLen = FindBlockLength(new TokenArray(tokens.Tokens, iterator.Iterator, tokens.End)); int bodyStart = iterator.Iterator + 1; iterator.Iterator += bodyLen - 1; Console.WriteLine("Function found, " + type + " " + name + "(" + len + ") " + bodyLen); Parse(new TokenArray(tokens.Tokens, bodyStart, iterator.Iterator)); } else if (token.Mode == endStatementToken) { Console.WriteLine("Function call, " + type + " " + name + "(" + len + ") "); } } else { Debug.Assert(false, "Unknown or incomplete variable declaration, " + type + " " + name); } } else if (token.Mode == nameToken) { } else if (token.Mode == flowControlToken) { switch (token.String) { case "if": { //Console.WriteLine("If"); } break; } } } return(null); }
public TokenIterator(TokenArray arr, int iter) { Tokens = arr; Iterator = iter - 1; }
public TokenIterator(TokenArray arr) : this(arr, arr.Start) { }