I've just pushed two changes to Anarki that allow people to modify Arc syntax in any way they want. The first is a new feature that allows the Arc reader to be changed at run time. (require "lib/custom-reader.arc")
(set-reader f)
This sets the reader to a function called f, so you can replace the standard Scheme read function with an arbitrary Arc (or Scheme) function of your choice.The second change is lib/arc-read.pack, a package (made with stefano's package system) that implements a complete Lisp reader in Arc. It implements the Common Lisp reader algorithm, so you can customise it by adding reader macros and dispatching macros. You can also customise it in other ways too (see the readme file for more info). It already comes with one feature that the standard Arc reader doesn't have: the use of #. to execute arbitrary code at read-time. An example: (set-reader arc-read)
(= x 1)
(let x 2
#.(prn "The read-time value of x is: " x)
(prn "The eval-time value of x is: " x))
=>
The read-time value of x is 1
The eval-time value of x is 2
The file extensions.arc also contains some examples of other things you can do with arc-read. Suppose you happen to use commas to represent decimal places where you live. You can now swap the comma and dot signs around like this: (load "lib/arc-read.pack/extensions.arc")
(swap-commas-and-dots)
Arc will now recognise 2,3 as a number, and can process the following: `(1 2 .(+ 1 2))
=> (1 2 3)
Ok, I admit that's a bit pointless. But how about this: (unprocessed-strings)
@"the\backslashes\in\this\string\have\no\meaning"
=> "the\\backslashes\\in\\this\\string\\have\\no\\meaning"
This is particularly useful for representing Windows pathnames. It could also be used for regular expressions, which would otherwise be a nightmare.One final example: many languages allow you to embed arbitrary text (e.g. HTML) in source code. Well, you can now do that too: (heredocs)
#<EOF
This is an arbitrary lump of text terminated by EOF.
You can put characters like " or \ or \n and they
will just be treated like normal text.
EOF
As you can see, arc-read is extremely customisable, more so that Scheme or Common Lisp. There are a few features missing, such as numbers in different bases, but these are left as exercises for the reader. Another exercise is getting table literals to work properly. #hash((1 . 2) (3 . 4))
Error: "Function call on inappropriate object nil (#<input-port:stdin> #\\h)"
:-(There's a full set of instructions for using and customising arc-read in the readme.txt file. There is also a suite of unit tests included. |