MatchDef.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package kawa.standard;
  2. import kawa.lang.*;
  3. import gnu.mapping.*;
  4. import gnu.expr.*;
  5. import gnu.lists.*;
  6. /** Handle the {@code (! pattern init)} syntax. */
  7. public class MatchDef extends Syntax {
  8. public static final MatchDef matchDef = new MatchDef();
  9. public void scanForm (Pair st, ScopeExp defs, Translator tr) {
  10. Object arg = st.getCdr();
  11. if (! (arg instanceof Pair)) {
  12. tr.error('e', "missing pattern following '!'");
  13. return;
  14. }
  15. Pair p1 = (Pair) arg;
  16. SetExp sexp = new SetExp(null, null);
  17. st = Translator.makePair(st, this, sexp);
  18. tr.pushForm(st);
  19. Object[] r = BindDecls.instance.parsePatternCar(p1, 0, defs, tr);
  20. Object rest = r[0];
  21. Declaration decl = (Declaration) r[1];
  22. sexp.setBinding(decl);
  23. Expression init;
  24. if (rest instanceof Pair) {
  25. Pair prest = (Pair) rest;
  26. if (prest.getCdr() != LList.Empty) {
  27. tr.error('e', "junk after initializer");
  28. }
  29. init = new LangExp(rest);
  30. }
  31. else {
  32. tr.error('e', "missing initializer");
  33. init = QuoteExp.nullExp;
  34. }
  35. sexp.setNewValue(init);
  36. }
  37. public Expression rewriteForm (Pair form, Translator tr) {
  38. Object arg = form.getCdr();
  39. if (! (arg instanceof SetExp))
  40. return tr.syntaxError("! definition is only allowed in a <body>");
  41. SetExp sexp = (SetExp) arg;
  42. Declaration decl = sexp.getBinding();
  43. Expression init = sexp.getNewValue();
  44. if (init instanceof LangExp) { // FIXME check for bad syntax
  45. init = tr.rewrite_car((Pair) ((LangExp) init).getLangValue(), false);
  46. sexp.setNewValue(init);
  47. }
  48. decl.noteValueFromSet(sexp);
  49. sexp.setDefining (true);
  50. return sexp;
  51. }
  52. }