load.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package kawa.standard;
  2. import java.io.*;
  3. import gnu.mapping.*;
  4. import gnu.expr.*;
  5. import gnu.kawa.io.Path;
  6. import gnu.text.SourceMessages;
  7. import gnu.text.SyntaxException;
  8. import gnu.lists.*;
  9. import gnu.text.*;
  10. import kawa.Shell;
  11. public class load extends Procedure1or2 {
  12. boolean relative;
  13. public load (String name, boolean relative)
  14. {
  15. super(name);
  16. this.relative = relative;
  17. }
  18. public static final load load = new load("load", false);
  19. public static final load loadRelative = new load("load-relative", true);
  20. public final Object apply1 (Object arg1)
  21. throws Throwable
  22. {
  23. return apply2 (arg1, Environment.getCurrent ());
  24. }
  25. public final Object apply2 (Object name, Object arg2)
  26. throws Throwable
  27. {
  28. Environment env = (Environment) arg2;
  29. Path path = Path.valueOf(name);
  30. if (relative)
  31. {
  32. Path curPath = (Path) Shell.currentLoadPath.get();
  33. if (curPath != null)
  34. path = curPath.resolve(path);
  35. }
  36. InputStream fs;
  37. try
  38. {
  39. fs = path.openInputStream();
  40. }
  41. catch (java.io.FileNotFoundException e)
  42. {
  43. Class clas = null;
  44. if (! relative && name instanceof CharSequence)
  45. {
  46. try
  47. {
  48. clas = Class.forName(name.toString());
  49. }
  50. catch (Exception e2)
  51. {
  52. // Handled below since clas==null
  53. }
  54. }
  55. if (clas == null)
  56. throw new RuntimeException ("cannot load "+e.getMessage());
  57. Shell.runClass(clas, env);
  58. return Values.empty;
  59. }
  60. try
  61. {
  62. Shell.runFile(fs, path, env, true, 0);
  63. return Values.empty;
  64. }
  65. catch (SyntaxException ex)
  66. {
  67. throw new RuntimeException("load: errors while compiling '"+
  68. name+"':\n"+ex.getMessages().toString(20));
  69. }
  70. }
  71. }