From 834442148bae7dd6816868d8377bf4445c6b7102 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Fri, 25 Dec 2009 23:17:34 -0800 Subject: [PATCH] docs for assignment-as-expression --- documentation/coffee/embedded.coffee | 1 + .../coffee/expressions_assignment.coffee | 1 + documentation/coffee/strings.coffee | 1 + documentation/css/docs.css | 2 + documentation/index.html.erb | 9 + documentation/js/aliases.js | 2 +- documentation/js/array_comprehensions.js | 21 +- documentation/js/assignment.js | 5 +- documentation/js/conditionals.js | 4 +- documentation/js/embedded.js | 3 +- documentation/js/expressions.js | 5 +- documentation/js/expressions_assignment.js | 4 + documentation/js/functions.js | 5 +- documentation/js/objects_and_arrays.js | 5 +- documentation/js/overview.js | 21 +- documentation/js/scope.js | 10 +- documentation/js/slices.js | 7 +- documentation/js/strings.js | 3 +- documentation/js/super.js | 11 +- index.html | 195 ++++++++++-------- .../execution/test_lexical_scope.coffee | 3 + 21 files changed, 190 insertions(+), 128 deletions(-) create mode 100644 documentation/coffee/expressions_assignment.coffee create mode 100644 documentation/js/expressions_assignment.js create mode 100644 test/fixtures/execution/test_lexical_scope.coffee diff --git a/documentation/coffee/embedded.coffee b/documentation/coffee/embedded.coffee index 400c15b4..17910189 100644 --- a/documentation/coffee/embedded.coffee +++ b/documentation/coffee/embedded.coffee @@ -2,3 +2,4 @@ hi: `function() { return [document.title, "Hello JavaScript"].join(": "); }` + diff --git a/documentation/coffee/expressions_assignment.coffee b/documentation/coffee/expressions_assignment.coffee new file mode 100644 index 00000000..b8ecf4e3 --- /dev/null +++ b/documentation/coffee/expressions_assignment.coffee @@ -0,0 +1 @@ +six: (one: 1) + (two: 2) + (three: 3) \ No newline at end of file diff --git a/documentation/coffee/strings.coffee b/documentation/coffee/strings.coffee index 0f41e4fa..1ec91081 100644 --- a/documentation/coffee/strings.coffee +++ b/documentation/coffee/strings.coffee @@ -5,3 +5,4 @@ to interest me on shore, I thought I would sail about a little and see the watery part of the world..." + diff --git a/documentation/css/docs.css b/documentation/css/docs.css index d1b5e4bf..1a4361f3 100644 --- a/documentation/css/docs.css +++ b/documentation/css/docs.css @@ -48,6 +48,8 @@ code, pre, tt { font-size: 12px; line-height: 18px; color: #191955; + white-space: pre-wrap; + word-wrap: break-word; } tt { background: #f8f8ff; diff --git a/documentation/index.html.erb b/documentation/index.html.erb index c68e78b4..6bf5ece6 100644 --- a/documentation/index.html.erb +++ b/documentation/index.html.erb @@ -230,6 +230,10 @@ coffee-script --print app/scripts/*.coffee > concatenation.js mathy things.

<%= code_for('assignment', 'greeting') %> +

+ Declarations of new variables are pushed up to the top of the current scope, + so that assignments may always be used within expressions. +

Objects and Arrays @@ -293,7 +297,12 @@ coffee-script --print app/scripts/*.coffee > concatenation.js

The same mechanism is used to push down assignment through switch statements, and if-elses (although the ternary operator is preferred). + Another part of manipulating assignment statements is + CoffeeScript's declaration of new variables at the top of the + current scope. This allows assignment to be used as a piece of an + expression.

+ <%= code_for('expressions_assignment', 'six') %>

Aliases diff --git a/documentation/js/aliases.js b/documentation/js/aliases.js index 804d56ea..c033ee2e 100644 --- a/documentation/js/aliases.js +++ b/documentation/js/aliases.js @@ -1,8 +1,8 @@ (function(){ + var volume; if (ignition === true) { launch(); } - var volume; if (band !== spinal_tap) { volume = 10; } diff --git a/documentation/js/array_comprehensions.js b/documentation/js/array_comprehensions.js index 790281c9..636ccb49 100644 --- a/documentation/js/array_comprehensions.js +++ b/documentation/js/array_comprehensions.js @@ -1,20 +1,19 @@ (function(){ - + var __a, __b, __c, __d, __e, __f, __g, __h, food, i, lunch, row; // Eat lunch. - var lunch; - var __a = ['toast', 'cheese', 'wine']; - var __d = []; - for (var __b=0, __c=__a.length; __b<__c; __b++) { - var food = __a[__b]; + __a = ['toast', 'cheese', 'wine']; + __d = []; + for (__b=0, __c=__a.length; __b<__c; __b++) { + food = __a[__b]; __d[__b] = food.eat(); } lunch = __d; // Zebra-stripe a table. - var __e = table; - var __h = []; - for (var __f=0, __g=__e.length; __f<__g; __f++) { - var row = __e[__f]; - var i = __f; + __e = table; + __h = []; + for (__f=0, __g=__e.length; __f<__g; __f++) { + row = __e[__f]; + i = __f; __h[__f] = i % 2 === 0 ? highlight(row) : null; } __h; diff --git a/documentation/js/assignment.js b/documentation/js/assignment.js index 0a586871..69bd8eb2 100644 --- a/documentation/js/assignment.js +++ b/documentation/js/assignment.js @@ -1,4 +1,5 @@ (function(){ - var greeting = "Hello CoffeeScript"; - var difficulty = 0.5; + var difficulty, greeting; + greeting = "Hello CoffeeScript"; + difficulty = 0.5; })(); \ No newline at end of file diff --git a/documentation/js/conditionals.js b/documentation/js/conditionals.js index b501b7e7..b5d12fe3 100644 --- a/documentation/js/conditionals.js +++ b/documentation/js/conditionals.js @@ -1,5 +1,5 @@ (function(){ - var mood; + var date, mood; if (singing) { mood = greatly_improved; } @@ -7,6 +7,6 @@ claps_hands(); cha_cha_cha(); } - var date = friday ? sue : jill; + date = friday ? sue : jill; expensive = expensive || do_the_math(); })(); \ No newline at end of file diff --git a/documentation/js/embedded.js b/documentation/js/embedded.js index 3c16b185..d097f260 100644 --- a/documentation/js/embedded.js +++ b/documentation/js/embedded.js @@ -1,5 +1,6 @@ (function(){ - var hi = function() { + var hi; + hi = function() { return [document.title, "Hello JavaScript"].join(": "); }; })(); \ No newline at end of file diff --git a/documentation/js/expressions.js b/documentation/js/expressions.js index 9301da9c..78f9244c 100644 --- a/documentation/js/expressions.js +++ b/documentation/js/expressions.js @@ -1,5 +1,6 @@ (function(){ - var grade = function(student) { + var eldest, grade; + grade = function(student) { if (student.excellent_work) { return "A+"; } else if (student.okay_stuff) { @@ -8,5 +9,5 @@ return "C"; } }; - var eldest = 24 > 21 ? "Liz" : "Ike"; + eldest = 24 > 21 ? "Liz" : "Ike"; })(); \ No newline at end of file diff --git a/documentation/js/expressions_assignment.js b/documentation/js/expressions_assignment.js new file mode 100644 index 00000000..ec67227a --- /dev/null +++ b/documentation/js/expressions_assignment.js @@ -0,0 +1,4 @@ +(function(){ + var one, six, three, two; + six = (one = 1) + (two = 2) + (three = 3); +})(); \ No newline at end of file diff --git a/documentation/js/functions.js b/documentation/js/functions.js index 2fc0d2f8..d77c2c26 100644 --- a/documentation/js/functions.js +++ b/documentation/js/functions.js @@ -1,8 +1,9 @@ (function(){ - var square = function(x) { + var cube, square; + square = function(x) { return x * x; }; - var cube = function(x) { + cube = function(x) { return square(x) * x; }; })(); \ No newline at end of file diff --git a/documentation/js/objects_and_arrays.js b/documentation/js/objects_and_arrays.js index bd3276cc..cfd1011e 100644 --- a/documentation/js/objects_and_arrays.js +++ b/documentation/js/objects_and_arrays.js @@ -1,6 +1,7 @@ (function(){ - var song = ["do", "re", "mi", "fa", "so"]; - var ages = { + var ages, song; + song = ["do", "re", "mi", "fa", "so"]; + ages = { max: 10, ida: 9, tim: 11 diff --git a/documentation/js/overview.js b/documentation/js/overview.js index 0ed09490..32cc4b34 100644 --- a/documentation/js/overview.js +++ b/documentation/js/overview.js @@ -1,20 +1,20 @@ (function(){ - + var __a, __b, __c, __d, cubed_list, list, math, num, number, opposite_day, square; // Assignment: - var number = 42; - var opposite_day = true; + number = 42; + opposite_day = true; // Conditions: if (opposite_day) { number = -42; } // Functions: - var square = function(x) { + square = function(x) { return x * x; }; // Arrays: - var list = [1, 2, 3, 4, 5]; + list = [1, 2, 3, 4, 5]; // Objects: - var math = { + math = { root: Math.sqrt, square: square, cube: function(x) { @@ -22,11 +22,10 @@ } }; // Array comprehensions: - var cubed_list; - var __a = list; - var __d = []; - for (var __b=0, __c=__a.length; __b<__c; __b++) { - var num = __a[__b]; + __a = list; + __d = []; + for (__b=0, __c=__a.length; __b<__c; __b++) { + num = __a[__b]; __d[__b] = math.cube(num); } cubed_list = __d; diff --git a/documentation/js/scope.js b/documentation/js/scope.js index 1f110baa..c5735c04 100644 --- a/documentation/js/scope.js +++ b/documentation/js/scope.js @@ -1,9 +1,11 @@ (function(){ - var num = 1; - var change_numbers = function() { + var change_numbers, new_num, num; + num = 1; + change_numbers = function() { + var new_num; num = 2; - var new_num = 3; + new_num = 3; return new_num; }; - var new_num = change_numbers(); + new_num = change_numbers(); })(); \ No newline at end of file diff --git a/documentation/js/slices.js b/documentation/js/slices.js index 5f590283..d819c4ae 100644 --- a/documentation/js/slices.js +++ b/documentation/js/slices.js @@ -1,5 +1,6 @@ (function(){ - var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; - var three_to_six = numbers.slice(3, 6 + 1); - var numbers_copy = numbers.slice(0, numbers.length); + var numbers, numbers_copy, three_to_six; + numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; + three_to_six = numbers.slice(3, 6 + 1); + numbers_copy = numbers.slice(0, numbers.length); })(); \ No newline at end of file diff --git a/documentation/js/strings.js b/documentation/js/strings.js index b3407d7f..0b486548 100644 --- a/documentation/js/strings.js +++ b/documentation/js/strings.js @@ -1,5 +1,6 @@ (function(){ - var moby_dick = "Call me Ishmael. Some years ago -- \ + var moby_dick; + moby_dick = "Call me Ishmael. Some years ago -- \ never mind how long precisely -- having little \ or no money in my purse, and nothing particular \ to interest me on shore, I thought I would sail \ diff --git a/documentation/js/super.js b/documentation/js/super.js index cfb99e5c..5f857d2f 100644 --- a/documentation/js/super.js +++ b/documentation/js/super.js @@ -1,10 +1,11 @@ (function(){ - var Animal = function() { + var Animal, Horse, Snake, sam, tom; + Animal = function() { }; Animal.prototype.move = function(meters) { return alert(this.name + " moved " + meters + "m."); }; - var Snake = function(name) { + Snake = function(name) { this.name = name; return this.name; }; @@ -15,7 +16,7 @@ alert("Slithering..."); return Snake.__superClass__.move.call(this, 5); }; - var Horse = function(name) { + Horse = function(name) { this.name = name; return this.name; }; @@ -26,8 +27,8 @@ alert("Galloping..."); return Horse.__superClass__.move.call(this, 45); }; - var sam = new Snake("Sammy the Python"); - var tom = new Horse("Tommy the Palomino"); + sam = new Snake("Sammy the Python"); + tom = new Horse("Tommy the Palomino"); sam.move(); tom.move(); })(); \ No newline at end of file diff --git a/index.html b/index.html index 437fd44d..615fd9be 100644 --- a/index.html +++ b/index.html @@ -86,22 +86,22 @@ math: { # Array comprehensions: cubed_list: math.cube(num) for num in list. -

+
var __a, __b, __c, __d, cubed_list, list, math, num, number, opposite_day, square;
 // Assignment:
-var number = 42;
-var opposite_day = true;
+number = 42;
+opposite_day = true;
 // Conditions:
 if (opposite_day) {
   number = -42;
 }
 // Functions:
-var square = function(x) {
+square = function(x) {
   return x * x;
 };
 // Arrays:
-var list = [1, 2, 3, 4, 5];
+list = [1, 2, 3, 4, 5];
 // Objects:
-var math = {
+math = {
   root: Math.sqrt,
   square: square,
   cube: function(x) {
@@ -109,30 +109,29 @@ cubed_list: math.cube(num) fo
   }
 };
 // Array comprehensions:
-var cubed_list;
-var __a = list;
-var __d = [];
-for (var __b=0, __c=__a.length; __b<__c; __b++) {
-  var num = __a[__b];
+__a = list;
+__d = [];
+for (__b=0, __c=__a.length; __b<__c; __b++) {
+  num = __a[__b];
   __d[__b] = math.cube(num);
 }
 cubed_list = __d;
-

@@ -315,11 +315,17 @@ var cube = function(x) {

greeting: "Hello CoffeeScript"
 difficulty: 0.5
-
var greeting = "Hello CoffeeScript";
-var difficulty = 0.5;
-

+

+ Declarations of new variables are pushed up to the top of the current scope, + so that assignments may always be used within expressions. +

Objects and Arrays @@ -334,14 +340,16 @@ ages: { ida: 9 tim: 11 } -

var song = ["do", "re", "mi", "fa", "so"];
-var ages = {
+
var ages, song;
+song = ["do", "re", "mi", "fa", "so"];
+ages = {
   max: 10,
   ida: 9,
   tim: 11
 };
-

Notice how the variables are declared with var the first time @@ -408,7 +420,7 @@ var new_num = change_numbers(); date: if friday then sue else jill. expensive ||= do_the_math() -

var mood;
+
var date, mood;
 if (singing) {
   mood = greatly_improved;
 }
@@ -416,7 +428,7 @@ expensive ||= do_the_m
   claps_hands();
   cha_cha_cha();
 }
-var date = friday ? sue : jill;
+date = friday ? sue : jill;
 expensive = expensive || do_the_math();
 

@@ -444,7 +456,8 @@ expensive = expensive ||"C".. eldest: if 24 > 21 then "Liz" else "Ike". -

var grade = function(student) {
+
var eldest, grade;
+grade = function(student) {
   if (student.excellent_work) {
     return "A+";
   } else if (student.okay_stuff) {
@@ -453,8 +466,9 @@ eldest: if return "C";
   }
 };
-var eldest = 24 > 21 ? "Liz" : "Ike";
-

The same mechanism is used to push down assignment through switch statements, and if-elses (although the ternary operator is preferred). + Another part of manipulating assignment statements is + CoffeeScript's declaration of new variables at the top of the + current scope. This allows assignment to be used as a piece of an + expression.

+
six: (one: 1) + (two: 2) + (three: 3)
+
var one, six, three, two;
+six = (one = 1) + (two = 2) + (three = 3);
+

Aliases @@ -505,10 +529,10 @@ volume: 10 unless answer is no if car.speed < speed_limit then accelerate(). -

if (ignition === true) {
+
var volume;
+if (ignition === true) {
   launch();
 }
-var volume;
 if (band !== spinal_tap) {
   volume = 10;
 }
@@ -556,22 +580,21 @@ lunch: food.eat() for
 
 # Zebra-stripe a table.
 highlight(row) for row, i in table if i % 2 is 0.
-
+
var __a, __b, __c, __d, __e, __f, __g, __h, food, i, lunch, row;
 // Eat lunch.
-var lunch;
-var __a = ['toast', 'cheese', 'wine'];
-var __d = [];
-for (var __b=0, __c=__a.length; __b<__c; __b++) {
-  var food = __a[__b];
+__a = ['toast', 'cheese', 'wine'];
+__d = [];
+for (__b=0, __c=__a.length; __b<__c; __b++) {
+  food = __a[__b];
   __d[__b] = food.eat();
 }
 lunch = __d;
 // Zebra-stripe a table.
-var __e = table;
-var __h = [];
-for (var __f=0, __g=__e.length; __f<__g; __f++) {
-  var row = __e[__f];
-  var i = __f;
+__e = table;
+__h = [];
+for (__f=0, __g=__e.length; __f<__g; __f++) {
+  row = __e[__f];
+  i = __f;
   __h[__f] = i % 2 === 0 ? highlight(row) : null;
 }
 __h;
@@ -592,12 +615,14 @@ three_to_six: numbers[3
 
 numbers_copy: numbers[0...numbers.length]
 
-
var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
-var three_to_six = numbers.slice(3, 6 + 1);
-var numbers_copy = numbers.slice(0, numbers.length);
-

@@ -642,12 +667,13 @@ tom.move() -

var Animal = function() {
+
var Animal, Horse, Snake, sam, tom;
+Animal = function() {
 };
 Animal.prototype.move = function(meters) {
   return alert(this.name + " moved " + meters + "m.");
 };
-var Snake = function(name) {
+Snake = function(name) {
   this.name = name;
   return this.name;
 };
@@ -658,7 +684,7 @@ Snake.__superClass__ = Animal.alert("Slithering...");
   return Snake.__superClass__.move.call(this, 5);
 };
-var Horse = function(name) {
+Horse = function(name) {
   this.name = name;
   return this.name;
 };
@@ -669,16 +695,17 @@ Horse.__superClass__ = Animal.alert("Galloping...");
   return Horse.__superClass__.move.call(this, 45);
 };
-var sam = new Snake("Sammy the Python");
-var tom = new Horse("Tommy the Palomino");
+sam = new Snake("Sammy the Python");
+tom = new Horse("Tommy the Palomino");
 sam.move();
 tom.move();
-

@@ -715,10 +742,13 @@ tom.move(); return [document.title, "Hello JavaScript"].join(": "); }` -
var hi = function() {
+
+
var hi;
+hi = function() {
 return [document.title, "Hello JavaScript"].join(": ");
 };
-

@@ -791,13 +821,16 @@ when "Sundayabout a little and see the watery part of the world..." -
var moby_dick = "Call me Ishmael. Some years ago -- \
+
+
var moby_dick;
+moby_dick = "Call me Ishmael. Some years ago -- \
 never mind how long precisely -- having little \
 or no money in my purse, and nothing particular \
 to interest me on shore, I thought I would sail \
 about a little and see the watery part of the \
 world...";
-