diff --git a/documentation/cs/aliases.cs b/documentation/cs/aliases.cs
index 3bbbb490..efb592eb 100644
--- a/documentation/cs/aliases.cs
+++ b/documentation/cs/aliases.cs
@@ -3,3 +3,5 @@ launch() if ignition is on
volume: 10 if band aint spinal_tap
let_the_wild_rumpus_begin() unless answer is no
+
+if car.speed < speed_limit then accelerate().
diff --git a/documentation/cs/switch.cs b/documentation/cs/switch.cs
index 41109413..955adef6 100644
--- a/documentation/cs/switch.cs
+++ b/documentation/cs/switch.cs
@@ -1,9 +1,9 @@
switch day
-case "Tuesday" then eat_breakfast()
-case "Wednesday" then go_to_the_park()
-case "Saturday"
+when "Tuesday" then eat_breakfast()
+when "Wednesday" then go_to_the_park()
+when "Saturday"
if day is bingo_day
go_to_bingo()
go_dancing().
-case "Sunday" then go_to_church()
+when "Sunday" then go_to_church()
else go_to_work().
\ No newline at end of file
diff --git a/documentation/index.html.erb b/documentation/index.html.erb
index fabb13a0..b9884447 100644
--- a/documentation/index.html.erb
+++ b/documentation/index.html.erb
@@ -69,7 +69,7 @@
Array Slice Literals
Calling Super from a Subclass
Embedded JavaScript
- Switch/Case/Else
+ Switch/When/Else
Try/Catch/Finally
Multiline Strings
Change Log
@@ -278,6 +278,11 @@ coffee-script --print app/scripts/*.cs > concatenation.js
You can use not as an alias for !.
++ Instead of a newline or semicolon, then can be used to separate + conditions from expressions, in while, + if/else, and switch/when statements. +
As in YAML, on and yes are the same as boolean true, while off and no are boolean false. @@ -343,15 +348,14 @@ coffee-script --print app/scripts/*.cs > concatenation.js <%= code_for('embedded', 'hi()') %>
- Switch/Case/Else + Switch/When/Else Switch statements in JavaScript are rather broken. You can only do comparisons based on string equality, and need to remember to break at the end of every case statement to avoid accidentally falling through to - the default case. CoffeeScript - compiles switch statements into JavaScript if-else chains, allowing you to + the default case. CoffeeScript compiles switch statements into JavaScript if-else chains, allowing you to compare any object (via ===), preventing fall-through, and resulting - in a returnable, assignable expression. To specify the default case, just - use else. + in a returnable, assignable expression. The format is: switch condition, + when clauses, else the default case.
<%= code_for('switch') %> diff --git a/documentation/js/aliases.js b/documentation/js/aliases.js index f30e00ea..804d56ea 100644 --- a/documentation/js/aliases.js +++ b/documentation/js/aliases.js @@ -9,4 +9,5 @@ if (!(answer === false)) { let_the_wild_rumpus_begin(); } + car.speed < speed_limit ? accelerate() : null; })(); \ No newline at end of file diff --git a/documentation/js/super.js b/documentation/js/super.js index d4bd6261..d9199aff 100644 --- a/documentation/js/super.js +++ b/documentation/js/super.js @@ -6,6 +6,7 @@ }; var Snake = function(name) { this.name = name; + return this.name; }; Snake.prototype = new Animal(); Snake.prototype.move = function() { @@ -14,6 +15,7 @@ }; var Horse = function(name) { this.name = name; + return this.name; }; Horse.prototype = new Animal(); Horse.prototype.move = function() { diff --git a/examples/code.cs b/examples/code.cs index f6d58537..919a991b 100644 --- a/examples/code.cs +++ b/examples/code.cs @@ -115,10 +115,10 @@ drink(bottle) for bottle, i in ['soda', 'wine', 'lemonade'] if even(i). # Switch statements ("else" serves as a default). activity: switch day -case "Tuesday" then eat_breakfast() -case "Sunday" then go_to_church() -case "Saturday" then go_to_the_park() -case "Wednesday" +when "Tuesday" then eat_breakfast() +when "Sunday" then go_to_church() +when "Saturday" then go_to_the_park() +when "Wednesday" if day is bingo_day go_to_bingo() else diff --git a/index.html b/index.html index ff9087fe..8777fdb7 100644 --- a/index.html +++ b/index.html @@ -55,7 +55,7 @@ Array Slice LiteralsYou can use not as an alias for !.
++ Instead of a newline or semicolon, then can be used to separate + conditions from expressions, in while, + if/else, and switch/when statements. +
As in YAML, on and yes are the same as boolean true, while off and no are boolean false. @@ -465,6 +470,8 @@ var eldest = 24 > 21 ? "Liz" : "Ike"; volume: 10 if band aint spinal_tap let_the_wild_rumpus_begin() unless answer is no + +if car.speed < speed_limit then accelerate().
if (ignition === true) { launch(); } @@ -475,6 +482,7 @@ let_the_wild_rumpus_begin() unless answer if (!(answer === false)) { let_the_wild_rumpus_begin(); } +car.speed < speed_limit ? accelerate() : null;
@@ -595,6 +603,7 @@ tom.move()
};
var Snake = function(name) {
this.name = name;
+ return this.name;
};
Snake.prototype = new Animal();
Snake.prototype.move = function() {
@@ -603,6 +612,7 @@ tom.move()
};
var Horse = function(name) {
this.name = name;
+ return this.name;
};
Horse.prototype = new Animal();
Horse.prototype.move = function() {
@@ -620,6 +630,7 @@ Animal.prototype.move = function(meters) {
};
var Snake = function(name) {
this.name = name;
+ return this.name;
};
Snake.prototype = new Animal();
Snake.prototype.move = function() {
@@ -628,6 +639,7 @@ Snake.prototype.move = function() {
};
var Horse = function(name) {
this.name = name;
+ return this.name;
};
Horse.prototype = new Animal();
Horse.prototype.move = function() {
@@ -658,24 +670,23 @@ return [document.title, "Hello JavaScript"].join(": ");
;alert(hi());'>run: hi()
- Switch/Case/Else + Switch/When/Else Switch statements in JavaScript are rather broken. You can only do comparisons based on string equality, and need to remember to break at the end of every case statement to avoid accidentally falling through to - the default case. CoffeeScript - compiles switch statements into JavaScript if-else chains, allowing you to + the default case. CoffeeScript compiles switch statements into JavaScript if-else chains, allowing you to compare any object (via ===), preventing fall-through, and resulting - in a returnable, assignable expression. To specify the default case, just - use else. + in a returnable, assignable expression. The format is: switch condition, + when clauses, else the default case.
switch day -case "Tuesday" then eat_breakfast() -case "Wednesday" then go_to_the_park() -case "Saturday" +when "Tuesday" then eat_breakfast() +when "Wednesday" then go_to_the_park() +when "Saturday" if day is bingo_day go_to_bingo() go_dancing(). -case "Sunday" then go_to_church() +when "Sunday" then go_to_church() else go_to_work().
if (day === "Tuesday") { eat_breakfast(); diff --git a/lib/coffee_script/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage b/lib/coffee_script/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage index cb348dcc..af4540fd 100644 --- a/lib/coffee_script/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage +++ b/lib/coffee_script/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage @@ -205,7 +205,7 @@diff --git a/lib/coffee_script/grammar.y b/lib/coffee_script/grammar.y index b90de5c9..31509e49 100644 --- a/lib/coffee_script/grammar.y +++ b/lib/coffee_script/grammar.y @@ -9,7 +9,7 @@ token CODE PARAM NEW RETURN token TRY CATCH FINALLY THROW token BREAK CONTINUE token FOR IN WHILE -token SWITCH CASE +token SWITCH WHEN token SUPER token DELETE token NEWLINE @@ -320,23 +320,23 @@ rule IF Expression "." { result = ForNode.new(val[0], val[6], val[2], val[8], val[4]) } ; - # Switch/Case blocks. + # Switch/When blocks. Switch: SWITCH Expression Then - Cases "." { result = val[3].rewrite_condition(val[1]) } + Whens "." { result = val[3].rewrite_condition(val[1]) } | SWITCH Expression Then - Cases ELSE Expressions "." { result = val[3].rewrite_condition(val[1]).add_else(val[5]) } + Whens ELSE Expressions "." { result = val[3].rewrite_condition(val[1]).add_else(val[5]) } ; - # The inner list of cases. - Cases: - Case { result = val[0] } - | Cases Case { result = val[0] << val[1] } + # The inner list of whens. + Whens: + When { result = val[0] } + | Whens When { result = val[0] << val[1] } ; - # An individual case. - Case: - CASE Expression Then Expressions { result = IfNode.new(val[1], val[3]) } + # An individual when. + When: + WHEN Expression Then Expressions { result = IfNode.new(val[1], val[3]) } ; # All of the following nutso if-else destructuring is to make the diff --git a/lib/coffee_script/lexer.rb b/lib/coffee_script/lexer.rb index d44c486d..f704ebe5 100644 --- a/lib/coffee_script/lexer.rb +++ b/lib/coffee_script/lexer.rb @@ -13,7 +13,7 @@ module CoffeeScript "try", "catch", "finally", "throw", "break", "continue", "for", "in", "while", - "switch", "case", + "switch", "when", "super", "delete"] diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index 38d3e57e..e1b228d0 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -594,7 +594,7 @@ module CoffeeScript end end - # If/else statements. Switch/cases get compiled into these. Acts as an + # If/else statements. Switch/whens get compiled into these. Acts as an # expression by pushing down requested returns to the expression bodies. # Single-expression IfNodes are compiled into ternary operators if possible, # because ternaries are first-class returnable assignable expressions. diff --git a/test/fixtures/execution/test_switch.cs b/test/fixtures/execution/test_switch.cs new file mode 100644 index 00000000..bce30883 --- /dev/null +++ b/test/fixtures/execution/test_switch.cs @@ -0,0 +1,11 @@ +num: 10 + +result: switch num +when 5 then false +when 'a' + false +when 10 then true +when 11 then false +else false. + +print(result) diff --git a/test/fixtures/execution/test_switch.js b/test/fixtures/execution/test_switch.js new file mode 100644 index 00000000..05874d4e --- /dev/null +++ b/test/fixtures/execution/test_switch.js @@ -0,0 +1,16 @@ +(function(){ + var num = 10; + var result; + if (num === 5) { + result = false; + } else if (num === 'a') { + result = false; + } else if (num === 10) { + result = true; + } else if (num === 11) { + result = false; + } else { + result = false; + } + print(result); +})(); \ No newline at end of file match -\b(break|case|catch|continue|else|finally|for|if|return|switch|then|throw|try|unless|while)\b +\b(break|when|catch|continue|else|finally|for|if|return|switch|then|throw|try|unless|while)\b name keyword.control.cs