updated pdf manual for new with syntax
[clinton/parenscript.git] / docs / reference.lisp
index f0f9e40..1ae0996 100644 (file)
 ;;;t \index{symbol conversion}
 
 ;;; Lisp symbols are converted to JavaScript symbols by following a
-;;; few simple rules. Special characters `!', `?', `#', `$', `@', `%',
+;;; few simple rules. Special characters `!', `?', `#', `@', `%',
 ;;; '/', `*' and `+' get replaced by their written-out equivalents
-;;; "bang", "what", "hash", "dollar", "at", "percent", "slash",
-;;; "start" and "plus" respectively.
+;;; "bang", "what", "hash", "at", "percent", "slash",
+;;; "start" and "plus" respectively. The `$' character is untouched.
 
-!?#$@% => bangwhathashdollaratpercent
+!?#@% => bangwhathashatpercent
 
 ;;; The `-' is an indication that the following character should be
 ;;; converted to uppercase. Thus, `-' separated symbols are converted
@@ -233,9 +233,14 @@ an-object.foo => anObject.foo
 ;
 ; regex ::= a Lisp string
 
-;;; Regular expressions can be created by using the `REGEX' form. The
-;;; regex form actually does nothing at all to its argument, and
-;;; prints it as is.
+;;; Regular expressions can be created by using the `REGEX' form. If
+;;; the argument does not start with a slash, it is surrounded by
+;;; slashes to make it a proper JavaScript regex. If the argument
+;;; starts with a slash it is left as it is. This makes it possible
+;;; to use modifiers such as slash-i (case-insensitive) or
+;;; slash-g (match-globally (all)).
+
+(regex "foobar") => /foobar/
 
 (regex "/foobar/i") => /foobar/i
 
@@ -733,7 +738,7 @@ a-variable  => aVariable
           tmpI2 = tmpI2 + 1) {
           var l = tmpArr1[tmpI2];
           document.write('L is ' + l);
-        }
+        };
       }
 
 
@@ -758,30 +763,54 @@ a-variable  => aVariable
 
 ;;;# The `CASE' statement
 ;;;t \index{CASE}
+;;;t \index{SWITCH}
 ;;;t \index{switch}
 
 ; (CASE case-value clause*)
 ;
-; clause     ::= (value body)
+; clause     ::= (value body) | ((value*) body) | t-clause
 ; case-value ::= a ParenScript expression
 ; value      ::= a ParenScript expression
+; t-clause   ::= {t | otherwise | default} body
 ; body       ::= a list of ParenScript statements
 
 ;;; The Lisp `CASE' form is transformed to a `switch' statement in
 ;;; JavaScript. Note that `CASE' is not an expression in
-;;; ParenScript. The default case is not named `T' in ParenScript, but
-;;; `DEFAULT' instead.
+;;; ParenScript. 
 
 (case (aref blorg i)
-  (1 (alert "one"))
+  ((1 "one") (alert "one"))
   (2 (alert "two"))
-  (default (alert "default clause")))
+  (t (alert "default clause")))
     => switch (blorg[i]) {
-         case 1:   alert('one');
-         case 2:   alert('two');
+         case 1:   ;
+         case 'one':
+                   alert('one');
+                   break;
+         case 2:
+                   alert('two');
+                   break;
          default:   alert('default clause');
        }
 
+; (SWITCH case-value clause*)
+; clause     ::= (value body) | (default body)
+
+;;; The `SWITCH' form is the equivalent to a javascript switch statement.
+;;; No break statements are inserted, and the default case is named `DEFAULT'.
+;;; The `CASE' form should be prefered in most cases.
+
+(switch (aref blorg i)
+  (1 (alert "If I get here"))
+  (2 (alert "I also get here"))
+  (default (alert "I always get here")))
+    => switch (blorg[i]) {
+         case 1:   alert('If I get here');
+         case 2:   alert('I also get here');
+         default:   alert('I always get here');
+       }
+
+
 ;;;# The `WITH' statement
 ;;;t \index{WITH}
 ;;;t \index{dynamic scope}
@@ -789,7 +818,7 @@ a-variable  => aVariable
 ;;;t \index{scoping}
 ;;;t \index{closure}
 
-; (WITH (object) body)
+; (WITH object body)
 ;
 ; object ::= a ParenScript expression evaluating to an object
 ; body   ::= a list of ParenScript statements
@@ -798,7 +827,7 @@ a-variable  => aVariable
 ;;; adds the object `object' as an intermediary scope objects when
 ;;; executing the body.
 
-(with ((create :foo "foo" :i "i"))
+(with (create :foo "foo" :i "i")
   (alert (+ "i is now intermediary scoped: " i)))
    => with ({ foo : 'foo',
               i : 'i' }) {