Syntax syntax syntax

20 views
Skip to first unread message

timmacfarlane

unread,
Jul 17, 2009, 5:39:31 AM7/17/09
to tycho-language
Ok, so there hasn't been much of an update to the main Tycho release
for a little while. I just want to let people know that *I'm still
working on it*! :) Specifically the new parser, which I've outlined on
my blog here: http://refractalize.blogspot.com/2009/06/tychos-new-parser_24.html

Some new things have come to light in the meantime. While working on
the new parser I've been thinking about supporting a keyword syntax
for method/function calls, I'm going to briefly outline that here to
give people a heads up and hopefully get some feedback.

Essentially I've been thinking about adding keyword arguments as a
means to improve the readability of Tycho code. I've heard
Smalltalkers mention that Smalltalk keyword syntax can improve
readability by quite a margin, so naturally I'm interested.
(Specifically, Gilad Bracha mentions it in his Newspeak doc:
http://bracha.org/newspeak-spec.pdf (top of page 4), and Smalltalk
Dave Thomas also mentions it in an interview somewhere.) I've also
built a couple of (very basic) iPhone apps using Objective-C so I've
become a little bit aquainted with the keyword syntax, I think I like
it. BTW, I've never really used Smalltalk by any measure!

The new Tycho syntax is a little bit different, and it *will* break
with the current syntax. Here's what it will look like:

Function invocation is the same as ever:

> add (1, 2)

Method invocation is essentially the same as function invocation,
except it will use keyword syntax:

> file (write-line: 'hi')

You'll notice the colon in there, the identifier before the colon is
the keyword, the value after it is the argument. Notice also that
there is no method name as you might see in Java or C#, or the current
Tycho syntax. The keyword is the method name.

Multiple keywords are delimited by commas

> file-system (move-file-from: 'c:\temp\some-file.txt', to: 'c:\some-file.txt')

And we can mix non-keyword arguments with keyword arguments:

> create-point (with-values: 1, 2)

Splat arguments will continue to be supported (where a list object is
inlined into the argument list):

> another-list = list [1, 2, 3]
> my-list = list [4, 5, 6]
> my-list (add-items: another-list, ...)

Methods that don't have arguments can omit the argument after the
keyword:

> file (close:)

Method definitions are also slightly different because they don't have
a normal method name:

file = object
method (write-line: line) =>
// ... write line to file ...
method (close:) =>
// ... close the file ...

Function definitions will also change to support keywords, eg:

> add = (a, to: b) => a + b
> add (1, to: 2)

This brings functions closer inline with methods and objects.
Essentially functions are objects with exactly one method. Objects are
functions that support invocations with different keywords.

Properties are accessed normally:

> structure.property

But what looks like a method call in current Tycho is actually a
property get, plus a function invocation:

> file.close ()

Could be rewritten as:

> (file.close) ()

Where as this isn't the case in current Tycho syntax, the whole thing
is taken as a method-call.

In order to use the colon, I'm planning to remove namespaces.
Namespaces used the colon syntax to define the namespace of a symbol,
a bit like in XML. eg:

> structure.ns:field

The field name 'field' is of the namespace 'ns', which allows us to be
very specific about which field it is. But most of the time having to
qualify a field with its namespace was a bit of a pain, and namespaces
probably weren't that useful anyway. Except for in one case, loading
external module dependencies. In the new (proposed) syntax, we'll use
the @ symbol to retrieve dependencies. The current syntax:

> system:console.write-line ('hi')

Would load the Console class from the .Net System assembly, and would
now become:

> @system.console.write-line ('hi')

So 'system' is a dependency of the current module, it must be
satisfied for it to function correctly. Dependencies can be
constructed from defaults or explicitly if you want to override them,
either for testing purposes or for security purposes.

In fact, the above code could be rewritten more idiomatically in the
new syntax as:

> @system.console (write-line: 'hi')


That's it. Quite a departure from the previous syntax, but one that I
hope will make Tycho programs that much more readable. Nothing has
been committed to code yet so I'm still open to suggestions and/or
criticism. I understand that it's quite a bit different from the usual
Java/C++/C#, and Ruby/Python/JavaScript, and well, even Smalltalk, so
I feel like I'm a bit out on a limb, but... hopefully for the best!
And really, what's the point of a new programming language that's the
same as all the rest, eh? :)

Thoughts?

Tim.
Reply all
Reply to author
Forward
0 new messages