2

I'm currently implementing a different language (Shen) in Clojure.

Shen has a symbol "./" but in Clojure this is interpreted before evaluation and thus results in an error. I do not need "./" inside the macro which is compiling this function to Clojure code.

Is there a way to ignore this? I think it is interpreted as an qualified symbol but without a name, since replacing it by a/ or xyz/ results in the same error messages.

My current macro is as simple as

(defmacro kl/trap-error [x [y z r]] `(try ~x (catch Exception '~z ~r)))

But when I call it with Shen code the following happens:

kl=> (trap-error (/ 1 0) (./ E (error-to-string E)
RuntimeException Invalid token: ./  clojure.lang.Util.runtimeException (Util.java:156)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: E in this context, compiling:(NO_SOURCE_PATH:0)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: E in this context, compiling:(NO_SOURCE_PATH:89)

I hope someone can help me with this.

Thanks in advance.

Dominik G
  • 1,459
  • 3
  • 17
  • 37
  • 1
    In CL you can use |./|, I don't know if it works for Clojure. – Daimrod Jan 08 '12 at 17:34
  • 1
    As far as I know, you would need a custom reader to do this in Clojure, but I'm not sure. – danlei Jan 08 '12 at 18:00
  • @danlei: But as far as I know Clojure doesn't have a standard way to define a reader macro, you have to [hack a bit](http://briancarper.net/blog/449/clojure-reader-macros) and that's not really appreciated. – Daimrod Jan 08 '12 at 21:00
  • Daimrod, yes, that's what I meant when I wrote *custom reader*: a dedicated Shen reader, written from scratch. – danlei Jan 09 '12 at 08:07
  • Apart from being appreciated or not, maybe one could get something like CL's standard syntax multiple escape character to work in Clojure with http://github.com/klutometis/reader-macros, but I didn't really take a closer look at it. (A custom reader would surely be the preferred solution, but still this might be useful at least for testing purposes.) – danlei Jan 09 '12 at 08:20

1 Answers1

2

This is not possible without modifying the Clojure reader (a privilege which is not given to Clojure programmers). Your statement "Shen has a symbol ./ but in Clojure this is interpreted before evaluation and thus results in an error" is incorrect, though - no interpretation or evaluation goes on at all, because the reader sees these characters and can't even figure out what data type they should be.

  • Are they a list? Nope, no parens.
  • Are they a string? Nope, no quotes.
  • Are they a symbol? Nope, there would be a namespace but no name.
  • ...many more cases like this...
  • I give up, this isn't a data structure that represents valid Clojure code.
amalloy
  • 89,153
  • 8
  • 140
  • 205
  • So writing Shen code in the Clojure REPL is not possible, right? Only reading files and replacing "./" before using e.g. read-string... – Dominik G Jan 09 '12 at 09:48
  • Well. clojure.main/repl allows you to specify a function that will be used for reading instead of the built-in. So for the specific case of writing it on the repl, you can probably do just fine with a little work. But requiring files has no such hook, so I don't think there's anything you can do in the general case. – amalloy Jan 09 '12 at 20:24
  • Thank you. I might try to write a "Shen REPL" that can read this. For now I go with only reading Shen files and replace the /. by lambda before the evaluation. – Dominik G Jan 11 '12 at 21:13