module_name.java 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package kawa.standard;
  2. import kawa.lang.*;
  3. import gnu.expr.*;
  4. import gnu.lists.*;
  5. import gnu.mapping.Symbol;
  6. public class module_name extends Syntax {
  7. public static final module_name module_name = new module_name();
  8. static { module_name.setName("module-name"); }
  9. public void scanForm (Pair form, ScopeExp defs, Translator tr) {
  10. Object form_cdr = form.getCdr();
  11. SyntaxForm nameSyntax = null;
  12. while (form_cdr instanceof SyntaxForm) {
  13. nameSyntax = (SyntaxForm) form_cdr;
  14. form_cdr = nameSyntax.getDatum();
  15. }
  16. Object arg = form_cdr instanceof Pair ? ((Pair) form_cdr).getCar() : null;
  17. while (arg instanceof SyntaxForm) {
  18. nameSyntax = (SyntaxForm) arg;
  19. arg = nameSyntax.getDatum();
  20. }
  21. String name = null;
  22. Pair p;
  23. String err = null;
  24. if (arg instanceof Pair && (p = (Pair) arg).getCar() == "quote") {
  25. arg = p.getCdr();
  26. if (! (arg instanceof Pair)
  27. || (p = (Pair) arg).getCdr() != LList.Empty
  28. || ! (p.getCar() instanceof String))
  29. err = "invalid quoted symbol for 'module-name'";
  30. else
  31. name = (String) p.getCar();
  32. } else if (arg instanceof Pair) {
  33. name = listToModuleName(arg, tr);
  34. } else if (arg instanceof FString || arg instanceof String)
  35. name = arg.toString();
  36. else if (arg instanceof Symbol) {
  37. name = arg.toString();
  38. int len = name.length();
  39. if (len > 2
  40. && name.charAt(0) == '<'
  41. && name.charAt(len-1) == '>') {
  42. name = name.substring(1, len-1);
  43. }
  44. } else
  45. err = "un-implemented expression in module-name";
  46. if (err != null)
  47. tr.pushForm(tr.syntaxError(err));
  48. else {
  49. int index = name.lastIndexOf('.');
  50. String className = name;
  51. if (index >= 0)
  52. tr.classPrefix = name.substring(0, index+1);
  53. else
  54. className = tr.classPrefix + Mangling.mangleClassName(name);
  55. ModuleExp module = tr.getModule();
  56. if (tr.mainClass == null)
  57. tr.mainClass = new gnu.bytecode.ClassType(className);
  58. else {
  59. String oldName = tr.mainClass.getName();
  60. if (oldName == null)
  61. tr.mainClass.setName(className);
  62. else if (! oldName.equals(className))
  63. tr.syntaxError("inconsistent module-name - old name: "+oldName);
  64. }
  65. if (tr.getState() > Compilation.PROLOG_PARSING)
  66. tr.error('e', "too late to set module-name");
  67. module.setType(tr.mainClass);
  68. module.setName(name);
  69. tr.mustCompileHere();
  70. }
  71. }
  72. public static String listToModuleName(Object list, Translator tr) {
  73. StringBuilder sbuf = new StringBuilder(Compilation.classPrefixDefault);
  74. boolean first = true;
  75. for (;;) {
  76. Pair parg = (Pair) list;
  77. if (! first)
  78. sbuf.append('.');
  79. first = false;
  80. Object car = parg.getCar();
  81. if (car != null)
  82. sbuf.append(Mangling.mangleClassName(car.toString()));
  83. list = parg.getCdr();
  84. if (list == LList.Empty)
  85. break;
  86. if (car == null || ! (list instanceof Pair)) {
  87. tr.error('e', "invalid list in module name");
  88. break;
  89. }
  90. }
  91. return sbuf.toString();
  92. }
  93. }