Sugar is a meta-programming language that provides a syntactic wrapper for other programming languages. It aims to have an easy to learn syntax that focuses on software architecture, readability, and expressiveness. Sugar translates code written in its syntax to commonly used languages like JavaScript, Python, and ActionScript by taking advantage of their common features while abstracting differences. It provides a full program representation through its LambdaFactory API to allow meta-programming capabilities like modifying or analyzing programs.
1. Sugar :
More sweetness for
programming languages
Sébastien Pierre, Datalicious
@FreeHackers's Union Mtl, Mar. 2009
www.datalicious.ca | github.com/sebastien/sugar
3. Much in common
Many languages
… Java, C#, Python, Ruby, JavaScript, ActionScript ....
have a lot in common
Primitive types : numbers, strings, arrays, etc.
Control structures : if, for, while, etc.
Constructs : functions, objects, modules, etc.
3
4. So what's the difference ?
Syntax, of course !
F (1) (Algolstyle)
(f 1) (Lispstyle)
[f withN:1] (Smalltalkstyle)
f←1 (Why not ?)
4
5. Syntax only ?
Well, we have traits*
Never change anything (Purely functional)
Everything is an object (Purely objectoriented)
Evaluate only when needed (Lazy evaluation)
Code as data (Homoiconic)
* we could actually call that “ language features”
5
6. Syntax, Traits, and ... ?
… the base library !
Lisp : lambda, car, cdr
C : libc, POSIX
JavaScript : ECMA standard library
Java : everything but the kitchen sink
6
7. The Idea
Considerations
Languages have a lot in common
But I keep rewriting the same code in different languages
I came to learn how I prefer to express myself
Ideally
I would like to write once, in my syntax of choice
And translate to the target language
7
8. However...
Languages are still “ black boxes”
Cannot change syntax easily
Difficult to access program representation
We think that the program is the source code, but...
The source code is one expression of the program
The program is a compound construct interpreted by a
runtime system
8
9. So...
Let's do “ something*” that
Is a syntactic wrapper for other languages
Offers full program model representation
Translates to commonly used languages
Can be easily customized by mortals
* This would be a program that writes programs (metaprogram)
9
11. It's all about syntax
And we can have the luxury to design it !
readability : visual cues, typographic rhythm
expressiveness : constructs to describe common patterns
consistency : many developers, same code
accessibility : learn it in a few hours
11
12. We can reuse a lot
We take advantage of
Languagespecific primitive types (lists, maps)
Languagespecific libraries (file, os, etc.)
And we provide
Abstraction over languagespecific traits
Emulation of nonsupported primitive types, constructs or
operations
12
13. Design Goals
“ A program is a design expressed in code”
Goals
Be learned in a couple of hours
Put focus on software architecture
Encourage clear, readable code
Favor thinking over typing
13
14. Inspiration
Python : expressiveness and consistency
Eiffel : formalizing structure and dynamics
Smalltalk : objects and messagepassing
JavaScript : functions and objects having fun together !
And indirectly, Lisp, Io, Erlang and Scala.
14
16. The syntax : Indexes and Slices
value [ start index : end index]
var list = [1,2,3,4,5,6,7,8,9]
list [0] 1 (first element)
list [1] 9 (last element)
list [:] [1,2,3,4,5,6,7,8,9] (list copy)
list [1:] [2,3,4,5,6,7,8,9] (copy after index 1)
list [:1] [1,2,3,4,5,6,7,8] (copy until last index)
16
17. The syntax : Iterators
The traditional “ for each in”
for value, index in [1,2,3,4]
print (value, index)
end
With objects too, one one line
for value, index in {a:1,b:2,c:3} > print (value, index)
And the compact form (with a closure)
[1,2,3,4] :: {v,k | print (v,k) }
17
18. The syntax : Closures
Closures as blocks
var hello = {message|
print (“ You said:” + message)
}
Compact syntax for callbacks (here using jQuery)
$ (“ .sayhello” ) click {alert (“ say hello” )}
18
19. The syntax : Idioms (1)
Spaces instead of dots
var user = {name:” bob” , email:” bob gmail.com” }
print (user name)
Why ?
Less dense code / more whitespace
space denotes “ message sending”
dot denotes “ structural resolution” (ask me for details ;)
19
20. The syntax : Idioms (2)
Indentation instead of commas or semicolons
var user = {name:” bob” , email:” bob@gmail.com” }
var user = {
← no trailing comma here
name : “ bob”
email : “ bob@gmail.com”
}
Why ?
Got too many errors for missing or extra trailing comma
20
21. The syntax : Idioms (3)
Optional parens in singleargument invocations
print “ Hello, world !”
user addPoints 10
$ “ .sayhello” click {alert “ Hello” }
article setAuthor 'user ← single quote to denote singleargument
Why ?
A lot of invocations are single argument
21
22. The syntax: Constructs
Constructs start with @, body is indented
@function name arg1, arg2,...rest
| Documentation string
...
@end
The flavors
@module, @function
@class, @property, @shared, @constructor, @method, @operation
22
23. Under The Hood
Full program model API (LambdaFactory)
Constructs Program, Module, Class, Function, ...
Control Iteration, Selection, ...
Operations Computation, Evaluation, Resolution
Metaprogramming
Flexible plugin “ program pass” system:
Modify, manipulate, analyze your program
23
24. Plugin Languages
Multiple backends (provided by LambdaFactory)
JavaScript (production quality)
ActionScript (not so bad)
Python (mature, but missing some stuff)
Pnuts (for fast scripting on the JDK)
Multiple frontends
Sugar is just one of them !
24
26. Why would I use Sugar ?
To replace JavaScript for front (or back) end Web dev :
simplified syntax fewer errors
simplified semantics less surprised
higher level constructs more structured code
easy to learn no excuse !
26
27. You will code like Douglas Crockford !
In Sugar (with mandatory indentation)
In JavaScript
var requestNumber = JSONRequest post (
requestNumber = JSONRequest.post(
quot;https://json.penzance.org/requestquot;
quot;https://json.penzance.org/requestquot;,
{
{
user : quot;doctoravatar@yahoo.comquot;
user: quot;doctoravatar@yahoo.comquot;,
t : quot;vlIjquot;
t: quot;vlIjquot;,
zip : 94089
zip: 94089,
forecast : 7
forecast: 7
}
},
{requestNumber, value, exception|
function (requestNumber, value, exception) {
if value
if (value) {
processResponse(value)
processResponse(value);
else
} else {
processError(exception)
processError(exception);
end
}
}
}
)
);
From Douglas Crockford : http://www.json.org/JSONRequest.html
27
28. Get started !
You'll need
Python 2.4+
And install these
git clone git://github.com/sebastien/lambdafactory.git
git clone git://github.com/sebastien/sugar.git
28
29. The end
Thank you !
www.datalicious.ca
www.github.com/sebastien
sebastien@datalicious.ca