static IEnumerable<TaggedPair> HandleSequence(IEnumerable<TaggedPair> taggedTokens) { var pattern = Grammar.sequenceForm; while (true) { int index = taggedTokens.IndexOfPattern(pattern, (x, p) => x.item1 == p); if (index == -1) { break; } var subSequence = taggedTokens.Sub(index, pattern.Count()); var newAction = ActionParser.ParseToSingle(subSequence.Single(x => x.item1 == SyntacticPart.String).item2 as string); var newPair = new TaggedPair(SyntacticPart.Action, newAction); taggedTokens = taggedTokens.RemoveRange(index, pattern.Count()); taggedTokens = taggedTokens.Insert(index, newPair); } //Debug.LogWarning(taggedTokens.Select(x => x.item1 == SyntacticPart.Action ? "action" : x.item2).ToStringList("\n")); //Debug.LogWarning(taggedTokens.Select(x => x.item1).ToStringList("\n")); return taggedTokens; }
static IEnumerable<TaggedPair> HandleIf(IEnumerable<TaggedPair> taggedTokens) { var pattern = Grammar.ifForm; int start = 0; while (true) { int index = taggedTokens.IndexOfPattern(pattern, (x, p) => x.item1 == p, start); if (index == -1) { break; } if (taggedTokens.Count() > index + pattern.Count() && taggedTokens.ElementAt(index + pattern.Count()).item1 == SyntacticPart.Else) { start += pattern.Count() + 1; continue; } var subSequence = taggedTokens.Sub(index, pattern.Count()); var newAction = new ActionIf( subSequence.Single(x => x.item1 == SyntacticPart.String).item2 as string, subSequence.Single(x => x.item1 == SyntacticPart.Action).item2 as Action2); var newPair = new TaggedPair(SyntacticPart.Action, newAction); taggedTokens = taggedTokens.RemoveRange(index, pattern.Count()); taggedTokens = taggedTokens.Insert(index, newPair); } //Debug.LogWarning(taggedTokens.Select(x => x.item1 == SyntacticPart.Action ? "action" : x.item2).ToStringList("\n")); //Debug.LogWarning(taggedTokens.Select(x => x.item1).ToStringList("\n")); return taggedTokens; }