with_compile_options.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package kawa.standard;
  2. import kawa.lang.*;
  3. import gnu.expr.*;
  4. import gnu.lists.*;
  5. import java.util.Stack;
  6. public class with_compile_options extends Syntax
  7. {
  8. public static final with_compile_options with_compile_options
  9. = new with_compile_options();
  10. static { with_compile_options.setName("with-compile-options"); }
  11. public void scanForm (Pair form, ScopeExp defs, Translator tr)
  12. {
  13. Stack stack = new Stack();
  14. Object rest = getOptions(form.getCdr(), stack, this, tr);
  15. if (rest == LList.Empty)
  16. return;
  17. if (rest == form.getCdr())
  18. {
  19. tr.scanBody(rest, defs, false);
  20. return;
  21. }
  22. rest = tr.scanBody(rest, defs, true);
  23. rest = new Pair(stack, rest);
  24. tr.currentOptions.popOptionValues(stack);
  25. tr.pushForm(Translator.makePair(form, form.getCar(), rest));
  26. }
  27. public static Object getOptions (Object form, Stack stack,
  28. Syntax command, Translator tr)
  29. {
  30. boolean seenKey = false;
  31. gnu.text.Options options = tr.currentOptions;
  32. SyntaxForm syntax = null;
  33. for (;;)
  34. {
  35. while (form instanceof SyntaxForm)
  36. {
  37. syntax = (SyntaxForm) form;
  38. form = syntax.getDatum();
  39. }
  40. if (! (form instanceof Pair))
  41. break;
  42. Pair pair = (Pair) form;
  43. Object pair_car = Translator.stripSyntax(pair.getCar());
  44. if (! (pair_car instanceof Keyword))
  45. break;
  46. String key = ((Keyword) pair_car).getName();
  47. seenKey = true;
  48. Object savePos = tr.pushPositionOf(pair);
  49. try
  50. {
  51. form = pair.getCdr();
  52. while (form instanceof SyntaxForm)
  53. {
  54. syntax = (SyntaxForm) form;
  55. form = syntax.getDatum();
  56. }
  57. if (! (form instanceof Pair))
  58. {
  59. tr.error('e', "keyword " + key + " not followed by value");
  60. return LList.Empty;
  61. }
  62. pair = (Pair) form;
  63. Object value = Translator.stripSyntax(pair.getCar());
  64. form = pair.getCdr();
  65. Object oldValue = options.getLocal(key);
  66. if (options.getInfo(key) == null)
  67. {
  68. tr.error('w', "unknown compile option: "+key);
  69. continue;
  70. }
  71. if (value instanceof FString)
  72. value = value.toString();
  73. else if (value instanceof Boolean
  74. || value instanceof Number)
  75. ;
  76. else
  77. {
  78. value = null;
  79. tr.error('e', "invalid literal value for key "+key);
  80. }
  81. options.set(key, value, tr.getMessages());
  82. if (stack != null)
  83. {
  84. stack.push(key);
  85. stack.push(oldValue);
  86. stack.push(value);
  87. }
  88. }
  89. finally
  90. {
  91. tr.popPositionOf(savePos);
  92. }
  93. }
  94. if (! seenKey)
  95. tr.error('e', "no option keyword in "+command.getName());
  96. return Translator.wrapSyntax(form, syntax);
  97. }
  98. public Expression rewriteForm (Pair form, Translator tr)
  99. {
  100. Object rest;
  101. Stack stack;
  102. Object obj = form.getCdr();
  103. Pair p;
  104. if (obj instanceof Pair
  105. && (p = (Pair) obj).getCar() instanceof Stack)
  106. {
  107. stack = (Stack) p.getCar();
  108. rest = p.getCdr();
  109. tr.currentOptions.pushOptionValues(stack);
  110. }
  111. else
  112. {
  113. stack = new Stack();
  114. rest = getOptions(obj, stack, this, tr);
  115. }
  116. try
  117. {
  118. Expression result = tr.rewrite_body(rest);
  119. BeginExp bresult;
  120. if (result instanceof BeginExp)
  121. bresult = (BeginExp) result;
  122. else
  123. bresult = new BeginExp (new Expression[] { result });
  124. bresult.setCompileOptions(stack);
  125. return bresult;
  126. }
  127. finally
  128. {
  129. tr.currentOptions.popOptionValues(stack);
  130. }
  131. }
  132. }