public List <TokenizedLine> TokenizeFile(CompilerLog log, string filetext) { filetext = filetext.Replace('\r', '\n'); string[] lines = filetext.Split('\n'); var sw = new Stopwatch(); sw.Start(); var result = new List <TokenizedLine>(); for (int lineNumber = 0; lineNumber < lines.Length; ++lineNumber) { string line = lines[lineNumber]; if (string.IsNullOrWhiteSpace(line) || line.TrimStart().StartsWith("#")) { continue; } try { SyntaxTree tree = Tokenize(line); result.Add(new TokenizedLine { Line = line, LineNumber = lineNumber, SyntaxTree = tree }); log.Debug(line, lineNumber, 0, $"Success => {tree}"); } catch (SyntaxParseException ex) { log.Error(line, lineNumber, ex.Position, ex.Message); } } sw.Stop(); log.ParseTime = sw.ElapsedMilliseconds; return(result); }
private void CompileExit( CompilerLog log, Compilation.StackFrame currentFrame, TokenizedLine tokenizedLine) { SyntaxTree tree = tokenizedLine.SyntaxTree; switch (tree.DeclaredType) { case DeclaredType.Act: if (currentFrame.Act == null) { log.Warning(tokenizedLine, $"Attempted to exit act but currently not in an act."); } else { log.Debug(tokenizedLine, $"Exiting act \"{currentFrame.Act.Name}\""); currentFrame.Act = null; } break; case DeclaredType.Scene: if (currentFrame.Scene == null) { log.Warning(tokenizedLine, $"Attempted to exit scene but currently not in a scene."); } else if (currentFrame.Scene.Characters.Count != 0) { log.Warning(tokenizedLine, "Exiting scene with characters present will exeunt all characters. Did you mean to do this?"); currentFrame.Scene = null; } else { log.Debug(tokenizedLine, $"Exiting scene \"{currentFrame.Scene.Name}\""); currentFrame.Scene = null; } break; case DeclaredType.Character: var characterName = tree.AssignedValue.ToString(); if (currentFrame.Scene == null) { log.Error(tokenizedLine, $"Cannot exit character outside of scene. Please enter a scene first."); } else { Character characterToExit = currentFrame.Scene.Characters .SingleOrDefault(t => t.Name == characterName); if (characterToExit == null) { log.Warning(tokenizedLine, $"Attempted to exit character \"{characterName}\" who was not present in scene."); } else { log.Debug(tokenizedLine, $"Exiting character \"{characterName}\""); currentFrame.Scene.Characters.Remove(characterToExit); } } break; case DeclaredType.Setting: var settingName = tree.AssignedValue.ToString(); if (currentFrame.Setting == null) { log.Error(tokenizedLine, $"Attempted to exit setting \"{settingName}\" which was never entered."); } else if (currentFrame.Setting.Name != settingName) { log.Error(tokenizedLine, $"Attempted to exit setting \"{settingName}\", but expected to exit current setting with name \"{currentFrame.Setting.Name}\"."); } else { log.Debug(tokenizedLine, $"Exiting setting \"{settingName}\""); currentFrame.Setting = null; } break; default: throw new InternalCompilerException($"Cannot exit declared type {tree.DeclaredType}"); } }
private void CompileEntrance( StackObject objectDefinitions, CompilerLog log, Compilation.StackFrame currentFrame, TokenizedLine tokenizedLine) { SyntaxTree tree = tokenizedLine.SyntaxTree; switch (tree.DeclaredType) { case DeclaredType.Act: var actName = tree.AssignedValue.ToString(); if (currentFrame.Act != null) { log.Error(tokenizedLine, $"Cannot enter act with name \"{actName}\" when already in act \"{currentFrame.Act.Name}\"."); } else { var actToEnter = new Act(actName); if (objectDefinitions.Acts.Contains(actToEnter)) { log.Error(tokenizedLine, $"Cannot re-enter act with name \"{actName}\"."); } else { objectDefinitions.Acts.Add(actToEnter); currentFrame.Act = actToEnter; } } break; case DeclaredType.Scene: var sceneName = tree.AssignedValue.ToString(); if (currentFrame.Scene != null) { log.Error(tokenizedLine, $"Cannot enter scene with name \"{sceneName}\" when already in scene \"{currentFrame.Scene.Name}\"."); } else { var sceneToEnter = new Scene(sceneName); if (objectDefinitions.Scenes.Contains(sceneToEnter)) { log.Error(tokenizedLine, $"Cannot re-enter scene with name \"{sceneName}\"."); } else { log.Debug(tokenizedLine, $"Entered scene \"{sceneName}\""); objectDefinitions.Scenes.Add(sceneToEnter); currentFrame.Scene = sceneToEnter; } } break; case DeclaredType.Character: var characterName = tree.AssignedValue.ToString(); var character = objectDefinitions.Characters.SingleOrDefault(t => t.Name == characterName); if (character == null) { log.Error(tokenizedLine, $"Cannot enter undefined character \"{characterName}\"."); } else { var currentScene = currentFrame.Scene; if (currentScene == null) { log.Error(tokenizedLine, $"Cannot enter character \"{characterName}\" outside of a scene. Please enter a scene first."); } else { log.Debug(tokenizedLine, $"Entered Character \"{characterName}\""); currentScene.Characters.Add(character); } } break; case DeclaredType.Setting: var settingName = tree.AssignedValue.ToString(); var setting = objectDefinitions.Settings.SingleOrDefault(t => t.Name == settingName); if (setting == null) { log.Error(tokenizedLine, $"Cannot enter undefined setting \"{settingName}\"."); } else { if (currentFrame.Setting != null) { log.Warning(tokenizedLine, $"Switching setting to \"{settingName}\" from current setting \"{currentFrame.Setting.Name}\"."); } log.Debug(tokenizedLine, $"Entered setting \"{settingName}\""); currentFrame.Setting = setting; } break; default: throw new InternalCompilerException($"Cannot enter declared type {tree.DeclaredType}"); } }