Syntax.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package kawa.lang;
  2. import gnu.mapping.*;
  3. import gnu.expr.*;
  4. import gnu.lists.*;
  5. import gnu.kawa.format.Printable;
  6. /**
  7. * Abstract class for "syntax" objects.
  8. * Builtins and macros are instances of this class.
  9. * @author Per Bothner
  10. */
  11. abstract public class Syntax implements Printable, Named
  12. {
  13. Object name;
  14. public final String getName()
  15. {
  16. return name == null ? null
  17. : name instanceof Symbol ? ((Symbol) name).getName()
  18. : name.toString();
  19. }
  20. public Object getSymbol() { return name; }
  21. public void setName (Object name) { this.name = name; }
  22. public void setName (String name) { this.name = name; }
  23. public Syntax ()
  24. {
  25. }
  26. public Syntax (Object name)
  27. {
  28. setName(name);
  29. }
  30. /**
  31. * Re-write an expression that is an "application" of this Syntax object.
  32. * @param obj the arguments to this "application" (i.e. the cdr of
  33. * the macro/builtin invocation)
  34. * @param tr the Translator that provides context
  35. * @return the re-written expression
  36. */
  37. public Expression rewrite (Object obj, Translator tr)
  38. {
  39. throw new InternalError("rewrite method not defined");
  40. }
  41. public Expression rewriteForm (Pair form, Translator tr)
  42. {
  43. return rewrite(form.getCdr(), tr);
  44. }
  45. public void scanForm (Pair st, ScopeExp defs, Translator tr)
  46. {
  47. boolean ok = scanForDefinitions(st, defs, tr);
  48. if (! ok)
  49. tr.pushForm(new ErrorExp("syntax error expanding "+this));
  50. }
  51. /** Check if a statement is a definition, for initial pass.
  52. * Semi-deprecated - should convert calls to use scanForm.
  53. * @param st the statement to check
  54. * @param defs where to add Declarations for found definitions
  55. * @param tr the compilation state
  56. * @return true on success
  57. */
  58. public boolean scanForDefinitions(Pair st, ScopeExp defs, Translator tr)
  59. {
  60. tr.pushForm(st);
  61. return true;
  62. }
  63. public void print (Consumer out)
  64. {
  65. out.write("#<syntax ");
  66. String name = this.getName();
  67. out.write(name == null ? "<unnamed>" : name);
  68. out.write('>');
  69. }
  70. }