PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Backend/Runtime/Importer.cs

https://bitbucket.org/AdamMil/boaold
C# | 107 lines | 66 code | 18 blank | 23 comment | 14 complexity | 529f8edc5ff2607e18ef2f414fdad0c8 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. Boa is the reference implementation for a language similar to Python,
  3. also called Boa. This implementation is both interpreted and compiled,
  4. targeting the Microsoft .NET Framework.
  5. http://www.adammil.net/
  6. Copyright (C) 2004-2005 Adam Milazzo
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. using System;
  20. using System.IO;
  21. using Boa.AST;
  22. using Boa.Modules;
  23. // TODO: clean up broken module out of sys.modules if import fails
  24. // TODO: add __builtins__ to all namespaces that don't otherwise define it
  25. // FIXME: make sys.modules synchronized, and remove manual synchronization from this file
  26. namespace Boa.Runtime
  27. {
  28. public sealed class Importer
  29. { Importer() { }
  30. public static object Import(string name) { return Import(name, true, false); }
  31. public static object Import(string name, bool throwOnError) { return Import(name, throwOnError, false); }
  32. public static object Import(string name, bool throwOnError, bool returnTop)
  33. { object ret;
  34. lock(sys.modules) ret = sys.modules[name]; // TODO: look at this... for a dotted name, will this ever be true?
  35. if(ret!=null) return ret;
  36. string[] names = name.Split('.');
  37. object top = Load(names[0]), module = top;
  38. if(top!=null) lock(sys.modules) sys.modules[names[0]] = top;
  39. for(int i=1; i<names.Length && module!=null; i++) module = Ops.GetAttr(module, names[i]);
  40. if(returnTop) module=top;
  41. if(throwOnError && module==null) throw Ops.ImportError("module {0} could not be loaded", name);
  42. return module;
  43. }
  44. public static object ImportTop(string name) { return Import(name, true, true); }
  45. public static object ImportTop(string name, bool throwOnError) { return Import(name, throwOnError, true); }
  46. public static void ImportStandardModules()
  47. { Import("string");
  48. }
  49. static object Load(string name)
  50. { object ret = LoadBuiltin(name);
  51. if(ret!=null) return ret;
  52. ret = LoadFromPath(name);
  53. if(ret!=null) return ret;
  54. ret = LoadReflected(name);
  55. return ret;
  56. }
  57. static object LoadBuiltin(string name)
  58. { Type type = Type.GetType("Boa.Modules."+name);
  59. return type==null ? null : ReflectedType.FromType(type);
  60. }
  61. static object LoadFromPath(string name)
  62. { foreach(string dirname in Boa.Modules.sys.path)
  63. { string path = Path.Combine(dirname=="" ? Environment.CurrentDirectory : dirname , name);
  64. if(Directory.Exists(path) && File.Exists(Path.Combine(path, "__init__.boa"))) return LoadPackage(name, path);
  65. path += ".boa";
  66. if(File.Exists(path)) return LoadFromSource(name, path, null);
  67. }
  68. return null;
  69. }
  70. static object LoadFromSource(string name, string filename, List __path__)
  71. { string outfile = Path.GetDirectoryName(filename);
  72. if(outfile!="") outfile += Path.DirectorySeparatorChar;
  73. outfile += Path.GetFileNameWithoutExtension(filename)+".dll";
  74. Module mod = ModuleGenerator.Generate(name, outfile, Parser.FromFile(filename).Parse());
  75. if(__path__!=null) mod.__setattr__("__path__", __path__);
  76. lock(sys.modules) sys.modules[name] = mod;
  77. mod.Run(new Frame(mod));
  78. return mod;
  79. }
  80. static object LoadPackage(string name, string path)
  81. { List __path__ = new List();
  82. __path__.append(path);
  83. return LoadFromSource(name, Path.Combine(path, "__init__.boa"), __path__);
  84. }
  85. static object LoadReflected(string name) { return ReflectedPackage.GetPackage(name); }
  86. }
  87. } // namespace Boa.Runtime