# CocoaBasic Syntax description

### Statements

for var=expr [down]to expr [step expr] ... next | loop through statements with integer variable incrementing (or decrementing in case of downto) by step until to or downto limit is exceeded |
for each var in array ... next | loop through elements of array (which may be an expression) and setting the variable to each value |
if cond then ... elseif cond then ... else ... end if | conditional execution the elseif part can be repeated and is optional the else part is optional |
select case value case val ... case val ... else ... end select | conditional execution the else part is optional |
while cond ... wend | loop through statements while condition is true |
do until cond ... loop | loop until condition is true |
do ... loop until cond | loop at least once through statements until condition is true |
dim var [ (index, ...) ] [,var...] as type, ... | define (local) variables and array variables |
redim array(index, ...) | change dimension of (existing) array | array setRho:[index, ...]
[class] sub name [([name:] [byref | byval] arg as type, [name:] [byref | byval] arg as type, ...)] ... end sub | define method with void result |
[class] function name [([name:] [byref | byval] arg as type, [name:] [byref | byval] arg as type, ...)] as type ... end function | define method with result of given type |
var=expr | assign new value (expression) to variable | _var:=expr
expr | evaluate expression (which should have void result) | expr
call expr | evaluate expression and ignore result | expr
exit | exit from loop |
try ... exception [var [as type]] ... end try | exception handler block access exception record as var |
raise expr[, expr] | raise exception; first (string) argument is exception name; second one is the exception description | NSException raise:(expr) format:"%@", expr

### Expressions

CocoaBasic | Description | CocoaScript
--- | --- | ---
a+b | numerical addition or string concatenation | a CSadd:(b)
a-b | numerical subtraction | a CSsub:(b)
a*b | numerical multiplication | a CSmul:(b)
a/b | numerical division (floating point) | a CSdiv:(b)
a^b | numerical exponentiation (floating point) | a CSpow:(b)
-a | numerical negation (0-a) | a CSchs
a mod b | numerical remainder (floating point) | a CSmod:(b)
a div b | numerical division (integer) | a CSidiv:(b)
a\b | numerical integer division (integer) | a CSidiv:(b)
new class | create new initialized instance of class during runtime | class alloc init
new class([name:]arg, [name:]arg, ...) | create new initialized (with parameters) instance of class during runtime | class alloc initWith:arg)
a isa class | return true if a is a (sub) class of the given class, i.e. recognizes the methods and components of the given class | a isKindOfClass:(class class)
a and b | logical and | a CSand:(b)
a or b | logical or | a CSor:(b)
a xor b | logical exclusive or | a CSxor:(b)
not a | logical negation | a CSnot
a=b | numerical or string equality | a CSeq:(b)
a<b | numerical or string less than | a CSlt:(b)
a>b | numerical or string greater than | a CSgt:(b)
a<=b | numerical or string less than or equal | a CSle:(b)
a>=b | numerical or string greater than or equal | a CSge:(b)
a<>b | numerical or string inequality | a CSne:(b)
(a) | parenthesised expression | (a)
[a, b, c, ...] | define initialized NSArray | [a,b,c,...]
[ka:a,kb:b, ...] | define initialized NSDictionary | [ka:a,kb:b, ...]
a.component | getter for object components or object modifier method | a component
a.method | call method | a method
a.method() | call method without arguments | a method
a.method([name:]arg, [name:]arg, ...) | call method with arguments | a method:(arg) name:(arg), ...
[a method:arg name:arg ...] | alternate (Objective-C) syntax | a method:(arg) name:(arg), ...
function(arg, ...) | call function (from a list of builtin functions) | arg function:(arg)
function arg, ... | call function (from a list of builtin functions) | arg function:(arg)
nil-function | call builtin function without arguments | NSApp nil-function (?)
nil-function() | call builtin function without arguments | NSApp nil-function (?)
classname | class object | classname class
classname(arg) | cast argument to type | arg castTo:(classname class)
array(index, ...) | access indexed value in array | array index:[index,...]

### Variables

A symbolic name may stand for

- a global instance
- a local variable
- a function parameter
- a component or method of the current object (i.e. self.a)
- a class name

Variable names must begin with a letter and may be followed by letters, digits and Underline. A $ sign is also allowed. Upper and lower case characters are distinguished (i.e. AbC and abc are NOT the same variable)

### Constants

CocoaBasic | Description | CocoaScript
--- | --- | ---
"a string" | a string constant (encoded in UNICODE) | "a string"
123 | an integer constant | 123
3.1415 | a floating point constant (may have exponent like e-12) | 3.1415
self | the class a method resides in | self
super | the super class | super
nil | the nil object | nil
true | logical true constant | YES
false | logical false constant | NO
app | the application instance | app

Constants are treated as Instances of its class, i.e. you can say "string".length

### Built in Classes/Data types

CocoaBasic | Description | CocoaScript
--- | --- | ---
boolean | true or false, result of comparisons, argument of conditionals | NSNumber
double | floating point number | NSNumber
integer | integer number | NSNumber
string | character string | NSString
object | any object | NSObject
var(index) as type | Multi-Dimensional array | HNSTypedTensor

### Example code

class function test(a as integer, b as string) as double
dim c(5) as string
dim i as integer
dim o as Object
o=new NSWindow
call o.makeKeyAndOrderFront(nil)
for i=1 to 10
c(2)=c(2)+b+"x"
if i>5 then
i=a+3*i+5
elseif i>2 then
i=a+3*(-i+5)
else
raise "exception", "this is an error"
end if
next
i=o.title
c(3)=left(b,4)
msgbox c(3)
return a/1.5
end function

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