ozra / onyx-lang

The Onyx Programming Language
Other
97 stars 5 forks source link

Syntax Structure #6

Open ozra opened 8 years ago

ozra commented 8 years ago

[RFC] Syntax Structure

"offside syntax": significant indent and optional end tokens

Yes ,you do know how to write proper sentences ,right ? It' s
  quiteannoying  to read Improperly Formatted sentences
   .Even when
 the content is good . Here 's a run_down with about forty_seven
  Reasons
   :
     1 . You convey a message , getting the  words right is hard enough
 1 .1 : Our reasoning minds ( especially programmers) are spatially orientated
   1-2-  Not structuring information so that it conveys intent ,spatially, is a bad mistake.
1.3.
   2.  Using non_spatial constructs to convey it : {2.1 doesn't help that much does it ?;2.2 At least I don 't think so;}3. My eyes are already bleeding from this exercise— maybe someone more clear_headed likes this ?;}

Structure, indentation and white space is important. If above didn't convince you... Yes, yes, no-one worth their salt writes with such bad style -- granted -- though it did get the point across. I hope!

Onyx is structured via indentation and has optional end tokens to make block ends explicit when wanted. There's experimental pattern matching support for the end-tokens also, making it possible to match up intended block starts even more explicitly. This will likely be dropped from the syntax though.

Details:

Here's an example showing off one of the many ways below code can be written and formatted (specially cond/case syntax currently has a shitload of variations to choose from - waiting to be reduced!), including the experimental pattern matching end token extension.

type Foo
    mood = #happy
    bar = "Hey"

    speak(word, mood) ->
        @mood = mood
        speak word

    speak(@word) ->
        speak

    speak() ->
        cond @mood
            #happy
                say "{@bar} - Yeay!"
            #sad
                say "{@bar}. Sob."
            *
                say "{@bar}."
        end
    end~speak
end-type  -- could have done `end-type~Foo`, `end` or nothing.

-- don't do the below at home kids!
my-foo = Foo()
my_foo.speak "Hello", mood: #workaholistic
myFoo.speak "Hi again"

Prior Art

For offside / indentation significance: ISWIM, Python, Haskell, Nim, Cytoc-Cython ;-), LiveScript, CoffeeScript, SugarCpp, F#, etc.

The voluntary end tokens look more like Ruby, Lua, Pascal, etc., and - dare I say it - Visual Basic, for the extended semantic matching.

Preference and Motivation

I've coded in C, then C++, for a loooong time (well, aside from hoards of other languages and a lot of assembler of course). But already in '99 I had enough with the braces-semicolon syntax. I coded a simple heuristic transpiler called Cython (no relation what so ever to the project with the same name that came 9 years later), obviously inspired by Python lexicography.

I find the indent based structuring superior, after having used both, side by side, for about 15 years. "If it looks right - it is right". Not to mention that studies on the programmer's mind seem to hint at it being a better choice. Lacking only one thing in most languages that leverage it; one very important thing; the possibility of ending blocks explicitly (and doing it for long/complex blocks).

When scopes grow over more than a few lines, or nesting depth grows, those dangling ends become very... loose.

The perfect solution is of course: both!

The recommendation, as I see it, is to absolutely not use end-tokens in constructs that are just a few lines, with no complex nesting - it's just visual clutter. Beyond that it's much clearer to use explicit end-tokens, and you can catch and contain scoping and localize syntax errors much better.

Some References

stugol commented 8 years ago

What does a leading # signify? For example in #happy? Is it a symbol?

Suggest case instead of cond.

I approve of hyphen-case and indent-based structuring.

ozra commented 8 years ago

@stugol - Yes, that's it! It is referred to as Tag in Onyx-lingua, however it's the exact same thing. (see #9)

Regarding case|cond|etc. see #13.