Friday, May 25, 2007

Qualified naming, take two

In my previous take at qualified naming in Factor I used an awkward syntax where you might see << math + >> to access the word + in math. I was discussing this on #concatenative, the Factor IRC channel, and Slava proposed something like math-+ for qualified naming. This would need to be prefixed by something like QUALIFIED: math to bring all of those math words in, under a qualified name.

I thought about it, and realized that it's possible to have pretty syntax without all of that renaming. Instead, the syntax math: + could be used, where math: is a parsing word defined by QUALIFIED:. Think of it like a macro generating a macro, something possible in both Scheme and Lisp. Eager to stop what I was working on and do something useless, I wrote the following code, which implements this syntax. Again, it is amazing how easy this is to do in Factor. I don't know of another language that is so flexible yet structured with respect to naming.

Note that this qualified naming solution has the same problem as the previous one I discussed--it has the potential to break homoiconic syntax.


USING: kernel parser words hashtables assocs quotations sequences namespaces ;
IN: qualified

: qual-quot ( vocab-name -- quot )
[ vocab scan swap at parsed ] curry ;

: qual-name ( vocab-name -- word-name )
CHAR: : add ;

: qual-word ( vocab-name -- quot )
[ qual-quot ] keep qual-name ""
[ swap define-compound ] keep
dup t "parsing" set-word-prop ;

: qual-vocab ( vocab-name -- vocab )
[ qual-word ] keep qual-name associate ;

: define-qual ( vocab-name -- )
qual-vocab use get push ;

: QUALIFIED:
scan define-qual ; parsing


For a while, I was getting off the mission of this blog--to do useless exercises in Factor which show how cool it is. Now, I think I'm getting back on track.

No comments: