# CocoaScript Syntax reference

### Formal Syntax

Production |  |  | Description
--- | --- | --- | ---
Script | ::= | Sequence |
| | | Script ; Sequence |
Object | ::= | nil | true | false | Builtin Constant
| | | Constname | Name of a Cocoa Constant or Class
| | | self | super | app | script | argv | Builtin Runtime Variable
| | | "string" | constant NSString
| | | number | constant NSNumber (int/double)
| | | Pobject |
Pobject | ::= | ( Sequence ) | directly evaluated script: { script } eval
| | | { Script } | HNSCocoaScript object
| | | [ Sequence [, ...] ] | NSMutableArray
| | | [ ] | empty NSMutableArray
| | | [ Sequence:Sequence [, ...] ] | NSMutableDictionary
| | | [ : ] | empty NSMutableDictionary
| | | Monadic-Operator Object |
Sequence | ::= | Object |
| | | Sequence [ Object ] | objectAtIndex:
| | | Sequence [: Object ] | objectForKey:
| | | Sequence Dyadic-Operator Object | dyadic operation (no precedence rules!)
| | | Sequence name | method call (without arguments)
| | | Sequence Method-Call [, Pobject ...] | method call (with optional variable argument list)
Method-Call | ::= | name:Pobject |
| | | Method-Call name:Pobject |
Dyadic-Operator | ::= | + | - | * | / | == | != | > | < | >= | <= | := | & | | | ^ |
Monadic-Operator | ::= | + | - | ! |

### Statements

Expression | Description
--- | ---
Ignored Characters |
white space | ignored (unless part of string constant, number or symbol)
Constants
nil | NSNull object
true / false | NSNumber with boolean value YES / NO
classname | evaluates to class constant
NSxxx | evaluates to Cocoa constant
5 | NSNumber with integer value 5
5.3 | NSNumber with double value 5.3
"abcdef" | NSString with value "abcdef"
Variables
self | the self reference (=argv[0])
super | the superclass of the self object
script | the current script
argv | the script (method) arguments
var | value of a variable
var:=expr | assignment of expression result to (local) variable
Method Calls
a method | [a method]
a method:arg | [a method:arg]
a method:(arg) x:(b) | [a method:arg x:b] converts argument to proper data type by calling [arg integerValue] etc.
a left right | [[a left] right]
a left:(5) right:(3) | [a left:5 right:3]
(a left:(5)) right:(3) | [[a left:5] right:3]
Operators
(expr) | change order/priority of execution
a+b | call [a CSadd:b]
a-b | call [a CSsub:b]
x//y | form NSPoint(x, y)
a[x] | [a objectAtIndex:x]
a[:x] | [a objectForKey:x]
argv[i] | access script argument (0=self, 1=_cmd)
script eval | no argument
script evaluate:[a] | with single argument
script evaluate:[a, ...] | with several arguments
Constructed Objects
{ statement; statement; ... } | CocoaScript-Object with statement list initialized
[val, ...] [] | [NSMutableArray initWithValues:val, ...] empty array
[key:val, ...] [:] | [NSMutableDictionary initWithValues:key, val, ...] empty dictionary
Flow Control
stop | stop in debugger if in single stepping mode
bkpt | breakpoint in debugger
{block} loop | infinitely loop this block
script breakLoop | exit from current loop
script continue | restart current loop from beginning
value return | exit from current method

### Idioms (i.e. common sequences)

C language style expression | CocoaScript
--- | ---
if(cond) block; | cond CSIF:block
if(cond) block1; else block2; | cond CSIF:block1 ELSE:block2
while(cond) block; | { cond CSIF:{block} ELSE:{breakLoop} } loop
for(expr1;expr2; expr3) block; | expr1; { expr2 CSIF:{block} ELSE:{breakLoop}; expr3 } loop
select(x) { case 1: block1; case 7: block2; default: block3; } | x CSSELECT:[1:{block1}, 7:{block2}, ...] DEFAULT:{block3}

Last Change: 09 Mar 2003 - ©hns@dsitri.de, 2003