/// <summary> /// Visit a parse tree produced by <see cref="algoParser.stat_import"/>. /// <para> /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/> /// on <paramref name="context"/>. /// </para> /// </summary> /// <param name="context">The parse tree.</param> /// <return>The visitor result.</return> public virtual Result VisitStat_import([NotNull] algoParser.Stat_importContext context) { return(VisitChildren(context)); }
//When a file is imported. public override object VisitStat_import([NotNull] algoParser.Stat_importContext context) { //The following are checked for the parent library, in order: //1. Executing directory of the script + whatever referenced folder path. //2. Packages directory for Algo. //3. Standard libraries. //Make sure they're not trying to import multiple as a single identifier. if (context.literal_params().expr().Length > 1 && context.AS_SYM() != null) { Error.Warning(context, "Failed to import scripts, cannot import multiple scripts as a single identifier."); return(null); } //Import each. foreach (var item in context.literal_params().expr()) { //Getting directory tree text. string importLoc = ""; //Evaluating the statement to get dir text. AlgoValue locVal = (AlgoValue)VisitExpr(item); if (locVal.Type != AlgoValueType.String) { Error.Fatal(context, "Given file path to import was not a string."); return(null); } importLoc = (string)locVal.Value; List <string> fileParts = importLoc.Split('/').ToList(); //Append the extension to the end (imports don't require an extension). if (!fileParts[fileParts.Count - 1].EndsWith(".ag")) { fileParts[fileParts.Count - 1] = fileParts.Last() + ".ag"; } //Is the import being placed into a different scope? string importScope = ""; if (context.AS_SYM() != null) { //Yes, get the name of the scope. importScope = context.IDENTIFIER().GetText(); } //Test 1: Executing directory of the script. string[] dirParts = new string[] { Environment.CurrentDirectory }.Concat(fileParts).ToArray(); string dirToCheck = CPFilePath.GetPlatformFilePath(dirParts); //Is it there? if (File.Exists(dirToCheck)) { //Yes! Run the load function. RunAlgoScript(dirToCheck, importScope); continue; } //Nope. //Test 2: Packages directory for Algo. dirParts = DefaultDirectories.PackagesDirectory.Concat(fileParts).ToArray(); dirToCheck = CPFilePath.GetPlatformFilePath(dirParts); //Is it there? if (File.Exists(dirToCheck)) { //Yep, load it. RunAlgoScript(dirToCheck, importScope); continue; } //Nope. //Test 3: Standard libraries. dirParts = DefaultDirectories.StandardLibDirectory.Concat(fileParts).ToArray(); dirToCheck = CPFilePath.GetPlatformFilePath(dirParts); //Is it there? if (File.Exists(dirToCheck)) { //Yep, load it. RunAlgoScript(dirToCheck, importScope); continue; } //No, nowhere else to check from, so throw a linking warning. Error.Warning(context, "Failed to link the Algo script '" + importLoc + "'. It has not been loaded."); } return(null); }
/// <summary> /// Exit a parse tree produced by <see cref="algoParser.stat_import"/>. /// <para>The default implementation does nothing.</para> /// </summary> /// <param name="context">The parse tree.</param> public virtual void ExitStat_import([NotNull] algoParser.Stat_importContext context) { }