Scan.java 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. package kawa.standard;
  2. import gnu.expr.*;
  3. import gnu.lists.Pair;
  4. import kawa.lang.*;
  5. /** Create a scan expression from a sequence expression. */
  6. public class Scan extends Syntax {
  7. public static final Scan scan = new Scan();
  8. static { scan.setName("scan"); }
  9. @Override
  10. public Expression rewrite (Object obj, final Translator tr) {
  11. if (Translator.listLength(obj) != 1)
  12. return tr.syntaxError("'scan' requires a single argument");
  13. if (tr.getScanContextStack().empty())
  14. return tr.syntaxError("'scan' not in a '...'- context");
  15. Translator.ScanContext savedScanCtx = tr.getScanContextStack().pop();
  16. try {
  17. Expression scanExp = tr.rewrite_car((Pair) obj, false);
  18. Declaration paramDecl =
  19. savedScanCtx.getLambda().addParameter(null);
  20. savedScanCtx.addSeqExpression(scanExp);
  21. return new ReferenceExp(paramDecl);
  22. } finally {
  23. tr.getScanContextStack().push(savedScanCtx);
  24. }
  25. }
  26. }