Sugar Presentation - YULHackers March 2009

29
Sugar : More sweetness for programming languages Sébastien Pierre, Datalicious @FreeHackers's Union Mtl, Mar. 2009 www.datalicious.ca | github.com/sebastien/sugar

Transcript of Sugar Presentation - YULHackers March 2009

Page 1: Sugar Presentation - YULHackers March 2009

Sugar :More sweetness for

programming languages

Sébastien Pierre, Datalicious@FreeHackers's Union Mtl, Mar. 2009

www.datalicious.ca | github.com/sebastien/sugar

Page 2: Sugar Presentation - YULHackers March 2009

1: The Origin

Page 3: Sugar Presentation - YULHackers March 2009

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.

Page 4: Sugar Presentation - YULHackers March 2009

4

So what's the difference ?

Syntax, of course !

F (1) (Algol­style)

(f 1) (Lisp­style)

[f withN:1] (Smalltalk­style)

f 1← (Why not ?)

Page 5: Sugar Presentation - YULHackers March 2009

5

Syntax only ?

Well, we have traits*

Never change anything (Purely functional)

Everything is an object (Purely object­oriented)

Evaluate only when needed (Lazy evaluation)

Code as data (Homoiconic)

* we could actually call that “ language features”

Page 6: Sugar Presentation - YULHackers March 2009

6

Syntax, Traits, and ... ?

… the base library !

Lisp : lambda, car, cdr

C : libc, POSIX

JavaScript : ECMA standard library

Java : everything but the kitchen sink

Page 7: Sugar Presentation - YULHackers March 2009

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

Page 8: Sugar Presentation - YULHackers March 2009

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

Page 9: Sugar Presentation - YULHackers March 2009

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 (meta­program)

Page 10: Sugar Presentation - YULHackers March 2009

10

2: SugarA Meta­Programming

Language

Page 11: Sugar Presentation - YULHackers March 2009

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

Page 12: Sugar Presentation - YULHackers March 2009

12

We can reuse a lot

We take advantage of

Language­specific primitive types (lists, maps)

Language­specific libraries (file, os, etc.)

And we provide

Abstraction over language­specific traits

Emulation of non­supported primitive types, constructs or operations

Page 13: Sugar Presentation - YULHackers March 2009

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

Page 14: Sugar Presentation - YULHackers March 2009

14

Inspiration

Python : expressiveness and consistency

Eiffel : formalizing structure and dynamics

Smalltalk : objects and message­passing

JavaScript : functions and objects having fun together !

And indirectly, Lisp, Io, Erlang and Scala.

Page 15: Sugar Presentation - YULHackers March 2009

15

The syntax : Primitive Types

Lists

[] [1,2,3] [“ a” , 2, [3, 4]]

Maps/objects­as­maps

{} {a:1, b:2} {name:” bob” , email:” [email protected]” }

And of course... numbers

1 ­1.0 0xFFAAD0

Page 16: Sugar Presentation - YULHackers March 2009

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)

Page 17: Sugar Presentation - YULHackers March 2009

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) }

Page 18: Sugar Presentation - YULHackers March 2009

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” )}

Page 19: Sugar Presentation - YULHackers March 2009

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 ;)

Page 20: Sugar Presentation - YULHackers March 2009

20

The syntax : Idioms (2)

Indentation instead of commas or semicolons

var user = {name:” bob” , email:” [email protected]” }

var user = {name : “ bob” no trailing comma here←

email : “ [email protected]

}

Why ?Got too many errors for missing or extra trailing comma

Page 21: Sugar Presentation - YULHackers March 2009

21

The syntax : Idioms (3)

Optional parens in single­argument invocationsprint “ Hello, world !”

user addPoints 10

$ “ .sayhello” click {alert “ Hello” }

article setAuthor 'user single quote to denote single­argument←

Why ?A lot of invocations are single argument

Page 22: Sugar Presentation - YULHackers March 2009

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

Page 23: Sugar Presentation - YULHackers March 2009

23

Under The Hood

Full program model API (LambdaFactory)

Constructs Program, Module, Class, Function, ...

Control Iteration, Selection, ...

Operations Computation, Evaluation, Resolution

Meta­programming

Flexible plug­in “ program pass” system:

Modify, manipulate, analyze your program

Page 24: Sugar Presentation - YULHackers March 2009

24

Plugin Languages

Multiple back­ends (provided by LambdaFactory)

JavaScript (production quality)

ActionScript (not so bad)

Python (mature, but missing some stuff)

Pnuts (for fast scripting on the JDK)

Multiple front­ends

Sugar is just one of them !

Page 25: Sugar Presentation - YULHackers March 2009

25

3: What's In It for Me ?Sugar in practice

Page 26: Sugar Presentation - YULHackers March 2009

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 !

Page 27: Sugar Presentation - YULHackers March 2009

27

You will code like Douglas Crockford !

In JavaScriptrequestNumber = JSONRequest.post( "https://json.penzance.org/request", { user: "[email protected]", t: "vlIj", zip: 94089, forecast: 7 }, function (requestNumber, value, exception) { if (value) { processResponse(value); } else { processError(exception); } });

In Sugar (with mandatory indentation)var requestNumber = JSONRequest post ( "https://json.penzance.org/request" { user : "[email protected]" t : "vlIj" zip : 94089 forecast : 7 } {requestNumber, value, exception| if value processResponse(value) else processError(exception) end })

From Douglas Crockford : http://www.json.org/JSONRequest.html

Page 28: Sugar Presentation - YULHackers March 2009

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

Page 29: Sugar Presentation - YULHackers March 2009

The end

Thank you !www.datalicious.ca

www.github.com/[email protected]