gilch / hebigo

蛇語(HEH-bee-go): An indentation-based skin for Hissp.
https://github.com/gilch/hissp
Mozilla Public License 2.0
24 stars 3 forks source link

Consider allowing control words to take arguments #16

Closed gilch closed 5 years ago

gilch commented 5 years ago

I'm renaming :keywords to "control words". "Keyword" means something else in Python. In the Hissp documentation I was calling them "key symbols", I think I'll change that as well for consistency.

:controlwords are used by some of the little DSLs created by macros, sometimes to label what the next argument is for (e.g. :as in from:/import: and try:.)

But there are cases when I want blocks to have these labels attached. I think it's more natural to just allow the control word to take arguments like symbols do than to use :foo pass: or pass: :foo.

Obviously this makes no sense at the top level. Python identifiers can't have :. (That doesn't stop you from putting a non-identifier string in a __dict__ though.) So I don't think Hissp would compile it into anything sensible. But inside a macro, the code would get rewritten anyway, and this can make sense, for example

try:
    !begin:
        print: "It's dangerous!"
        something_risky: thing
    :except: LikelyProblemError
        print: "Oops!"
        fix_it:
    :except: Exception :as ex
        do_something: ex
    :else:
        print: "Hooray!"
        thing
    :finally:
        .close: thing

or

if: (a<b)
    :then:
        print: "less"
    :elif: (a>b)
        print: "more"
    :elif: (a==b)
        print: "equal"
    :else:
        print: "nan"

Because of automatic qualification of reserved words and of normal unqualified symbols in masks, using symbols here is kind of more difficult than it should be. Control word wouldn't have this issue. And because control words can have special characters, the !mask: macro can look a little more lispy and use :,:/:,@: instead of _:/__: like before.

On the downside, we're using even more colons than before. Unfortunately, Hissp had to special case :keywords at the compiler level to make them work, and Python used colon to start blocks for completely unreleated reasons. Maybe I could change the :keyword symbol to $keyword or something. The code examples in docstrings also look funny if the IDE thinks it's supposed to be reStructuredText, because having a word beginning and ending with a colon (like :foo:) means something in that language.