The intention of this page is to supply a 'quick look' at SmallScript syntax for those beginning in the language. I'd appreciate any additions or corrections that you may have. Please email Chris Double if you have any comments.
The content of this page was originally modelled after the examples at 'Goo By Example'. The intention is not to show SmallScript as a 'better' language but rather I thought the Goo examples were great and wanted to have a language to compare against.
Function[ double(x) ^2 * x. ] stdout << 'double: ' << double(10).
Function[
doubleNumber: x
^2 * x.
]
stdout << 'doubleNumber: ' << (doubleNumber: 30).
Function [
helloWorld
stdout << 'Hello World'; cr.
]
Function [
helloNumber(n)
stdout << 'Hello Number ' << n << '!'; cr.
]
Function [
helloInteger(<Integer> n)
stdout << 'Hello Integer ' << n << '!'; cr.
]
Function [ <Integer>
doubleInteger(<Integer> n)
^2 * n.
]
Function [
hello(<Integer> x)
stdout << 'Hello integer ' << x; cr.
]
Function [
hello(<Float> x)
stdout << 'Hello floating point ' << x; cr.
]
Function [
didYou(<String> thing)
^'No! I didn''t ' + thing.
]
Function [
formalHello(<String> name)
|formalName| := 'mister ' + name.
stdout << 'Hello ' << formalName; cr
]
Function [
quadruple(>Integer> x)
|double| := [ :y | 2 * y ].
^double(double(x)).
]
Function [
makeNamesStupid(<Collection> names)
^names do: [ :theName | 'Smelly ' + theName ]
]
Function [
insultManyPeople
|people| := #('Santa', 'Easter Bunny', 'Ringo').
makeNamesStupid(people)
do: [ :name | stdout << name << ' is Smelly!'; cr ]
]
class name: Animal
fields: auto bathroom
{
Function [
new
^super new initialize; yourself.
]
}
class name: Cat
extends: Animal
{
Method [
initialize
bathroom := 'dirt'.
].
}
class name: Dog
extends: Animal
{
Method [
initialize
bathroom := 'tree'.
].
}
Function [
testCatDog
|myCat| := Cat new.
|myDog| := Dog new.
stdout << 'Cat: ' << myCat bathroom; cr.
stdout << 'Dog: ' << myDog bathroom; cr.
]
Function [
testWhile
|a| := 0.
while(a < 10)
[
a :+= 1.
stdout << a; cr.
]
].
Function [
usingToAndDo
1 to: 10 do: [ :a | stdout << a; cr ]
].
Function [
conditional1(a, b)
^if(a < b) then [a] else [b]
]
Function [
conditional2(a, b)
^a < b ifTrue: [a] ifFalse: [b].
]
Function [
conditional3(a, b)
^(a < b) ?: a : b.
]
Function [
conditional4(failed)
^unless(failed) [ 'succeeded' ].
]
Function [
conditional5(a, b, c)
if(b >= a and: [b <= c])
[stdout << 'b is between c and a']
else
[stdout << 'b is not between c and a']
]
Function [
conditional6(a)
^if(a = 10)
['ten']
else if (a = 'ten')
[10]
else if (a = 'eleven')
[11]
else if (a = 11)
['eleven']
else
[a]
]
Function [
conditional7(a)
^switch(a)
[
case 10:
'ten'.
break.
case 'ten':
10.
break.
case 'eleven':
11.
break.
case 11:
'eleven'.
break.
caseIf (a respondsTo(#isOdd) and: [a isOdd]):
'a is odd'.
break.
default:
a.
].
]