From 9d8668f37fab992977d65a28eb0b692db04a58aa Mon Sep 17 00:00:00 2001
From: Jeremy Ashkenas
Date: Sun, 10 Jan 2010 23:27:57 -0500
Subject: [PATCH] added a whole slew of nice Potion examples from the pamphlet
-- CoffeeScript stacks up pretty well.
---
.../coffee/object_comprehensions.coffee | 3 +-
examples/potion.coffee | 205 ++++++++++++++++++
index.html | 3 +-
.../Syntaxes/CoffeeScript.tmLanguage | 2 +-
4 files changed, 210 insertions(+), 3 deletions(-)
create mode 100644 examples/potion.coffee
diff --git a/documentation/coffee/object_comprehensions.coffee b/documentation/coffee/object_comprehensions.coffee
index 6e5b8b6e..ba88f448 100644
--- a/documentation/coffee/object_comprehensions.coffee
+++ b/documentation/coffee/object_comprehensions.coffee
@@ -1,3 +1,4 @@
years_old: {max: 10, ida: 9, tim: 11}
-ages: child + " is " + age for child, age ino years_old
\ No newline at end of file
+ages: for child, age ino years_old
+ child + " is " + age
\ No newline at end of file
diff --git a/examples/potion.coffee b/examples/potion.coffee
new file mode 100644
index 00000000..7e45ca40
--- /dev/null
+++ b/examples/potion.coffee
@@ -0,0 +1,205 @@
+# Examples from _why's Potion, the Readme and "Potion: A Short Pamphlet".
+
+# 5 times: "Odelay!" print.
+
+print("Odelay!") for i in [1..5]
+
+
+# add = (x, y): x + y.
+# add(2, 4) string print
+
+add: x, y => x + y
+print(add(2, 4))
+
+
+# loop: 'quaff' print.
+
+while true
+ print('quaff')
+
+
+# ('cheese', 'bread', 'mayo') at (1) print
+
+print(['cheese', 'bread', 'mayo'][1])
+
+
+# (language='Potion', pointless=true) at (key='language') print
+
+print({language: 'Potion', pointless: true}['language'])
+
+
+# minus = (x, y): x - y.
+# minus (y=10, x=6)
+
+minus: x, y => x - y
+minus(6, 10)
+
+
+# foods = ('cheese', 'bread', 'mayo')
+# foods (2)
+
+foods: ['cheese', 'bread', 'mayo']
+foods[2]
+
+
+# (dog='canine', cat='feline', fox='vulpine') each (key, val):
+# (key, ' is a ', val) join print.
+
+for key, val ino {dog: 'canine', cat: 'feline', fox: 'vulpine'}
+ print(key + ' is a ' + val)
+
+
+# Person = class: /name, /age, /sex.
+# Person print = ():
+# ('My name is ', /name, '.') join print.
+
+Person: =>
+Person::print: =>
+ print('My name is ' + this.name + '.')
+
+
+# p = Person ()
+# p /name string print
+
+p: new Person()
+print(p.name)
+
+
+# Policeman = Person class (rank): /rank = rank.
+# Policeman print = ():
+# ('My name is ', /name, ' and I'm a ', /rank, '.') join print.
+#
+# Policeman ('Constable') print
+
+Policeman: rank => this.rank: rank
+Policeman extends Person
+Policeman::print: =>
+ print('My name is ' + this.name + " and I'm a " + this.rank + '.')
+
+print(new Policeman('Constable'))
+
+
+# app = [window (width=200, height=400)
+# [para 'Welcome.', button 'OK']]
+# app first name
+
+app = {
+ window: {width: 200, height: 200}
+ para: 'Welcome.'
+ button: 'OK'
+}
+app.window
+
+
+# x = 1
+# y = 2
+#
+# x = 1, y = 2
+
+x: 1
+y: 2
+
+x: 1; y: 2
+
+
+# table = (language='Potion'
+# pointless=true)
+
+table: {
+ language: 'Potion'
+ pointless: yes
+}
+
+
+# # this foul business...
+# String length = (): 10.
+
+# this foul business...
+String::length: => 10
+
+
+# block = :
+# 'potion' print.
+
+block: =>
+ print('potion')
+
+
+# if (age > 100): 'ancient'.
+
+if age > 100 then 'ancient'
+
+
+# author =
+# if (title == 'Jonathan Strange & Mr. Norrell'):
+# 'Susanna Clarke'.
+# elsif (title == 'The Star Diaries'):
+# 'Stanislaw Lem'.
+# elsif (title == 'The Slynx'):
+# 'Tatyana Tolstaya'.
+# else:
+# '... probably Philip K. Dick'.
+
+switch author
+ when 'Jonathan Strange & Mr. Norrell'
+ 'Susanna Clarke'
+ when 'The Star Diaries'
+ 'Stanislaw Lem'
+ when 'The Slynx'
+ 'Tatyana Tolstaya'
+ else
+ '... probably Philip K. Dick'
+
+
+# count = 8
+# while (count > 0):
+# 'quaff' print
+# count--.
+
+count: 8
+while count > 0
+ print('quaff')
+ count--
+
+
+# 1 to 5 (a):
+# a string print.
+
+print(a) for a in [1..5]
+
+
+# if (3 ?gender):
+# "Huh? Numbers are sexed? That's amazing." print.
+
+if (3).gender?
+ print("Huh? Numbers are sexed? That's amazing.")
+
+
+# HomePage get = (url):
+# session = url query ? at ('session').
+
+HomePage::get: url =>
+ session: url.query.session if url.query?
+
+
+# BTree = class: /left, /right.
+# b = BTree ()
+# b /left = BTree ()
+# b /right = BTree ()
+
+BTree: =>
+b: new BTree()
+b.left: new BTree()
+b.right: new BTree()
+
+
+# BTree = class: /left, /right.
+# b = BTree ()
+#
+# if (b ? /left):
+# 'left path found!' print.
+
+BTree: =>
+b: new BTree()
+
+print('left path found!') if b.left?
diff --git a/index.html b/index.html
index 21d461e7..7c992eda 100644
--- a/index.html
+++ b/index.html
@@ -787,7 +787,8 @@ egg_delivery = function egg_delivery() {
years_old: {max: 10, ida: 9, tim: 11}
-ages: child + " is " + age for child, age ino years_old
+ages: for child, age ino years_old
+ child + " is " + age
var __a, __b, age, ages, child, years_old;
years_old = {
max: 10,
diff --git a/lib/coffee_script/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage b/lib/coffee_script/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage
index a50aafae..6cc3c32a 100644
--- a/lib/coffee_script/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage
+++ b/lib/coffee_script/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage
@@ -214,7 +214,7 @@
match
- \b([a-zA-Z$_](\w|\$|:)*)(\:)\s
+ \b([a-zA-Z$_](\w|\$|:|\.)*)(\:)\s
name
variable.assignment.coffee
captures