= = = OCL 2 GUIDE = = =

Return to the Compiler

Ostracod Command Language was originally created to facilitate programming on the iPad, but it may be used on any JavaScript enabled machine. Its primary feature is that code contains very little punctuation.

= = = SYNTAX = = =

If you test the code below, you will see a console with the number 3 in it:

Print 3

The first letter of any term may be lowercase or capitalized without a functional difference:

print 3

The kind of whitespace separating terms is also irrelevant:

      Print
3

OCL is unique because it uses whitespace to separate both commands and arguments. Despite the random breaks in the code below, the compiler still knows what you want to do:

Print 1
Print 2 print 3
Print
4 print 5

Variables are declared using the local command. This command accepts two arguments: a variable name and a value. Variable names should be in camel case.

Local asdf 4
Print asdf
Local mySpecialNumber 9
Print mySpecialNumber

Commands may be embedded as arguments of other commands. The negative command accepts one argument and returns the negative value. The example below shows the negative command embedded one and two levels deep:

Print negative 1
Print negative negative 2

Operators accept two arguments: one before the operator and one after. The minus operator returns the difference of its arguments. Parentheses can coerce the order of operations:

Print 5 minus 9
Print 12 minus 6 minus 4
Print 12 minus (6 minus 4)

Each operator has a certain priority number which determines its precedence over other operators. The order of operations is in the table below.

Short name Long name Priority
+ plus 3
- minus 3
* times 2
/ over 2
% modulus 3
^ toThe 1
. call 0
: at 4
= equals 5
!= doesNotEqual 5
> isAbove 5
< isBelow 5
& and 6
| or 6

String literals are enclosed in either commas (,) or quotation marks (").

Print ,Hello world,
Print "This is another way to write a string."

Some commands initiate code blocks which are terminated with the end command. These blocks may be embedded. The if command accepts a single boolean argument and initiates a code block:

If true
Print ,The first condition is true.,
End
If false
Print ,The second condition is true.,
If true
Print ,The second and third conditions are true.,
End
End

= = = VALUE COMMANDS = = =

Print (value): Displays the value in the console output.

Example:
Print 8
Print ,This is some text,

ClearConsole: Deletes the console output.

Example:
Print ,This message will be deleted!,
ClearConsole
Print ,You can only see this.,

Local (var name) (value): Declares a new variable.

Example:
Local first 13
Print first
Local theSecondVariable ,I am cooler than first.,
Print theSecondVariable

Set (var name) (value): Changes the value of an existing variable.

Example:
Local color ,blue,
Print color
Set color ,red,
Print color

Increment, Decrement (var name): Increases or decreases the value of a variable by 1.

Example:
Local x 2
Increment x
Print x
Decrement x
Decrement x
Print x

Shift (var name) (value): Adds a value to the given variable.

Example:
Local index 2
Shift index 5
Print index
Shift index 3
Print index

Zero, One, Two, Three... Nine, Ten: Returns the corresponding positive number.

Example:
Print six
Local myNumber eight
Print myNumber

True, False: Returns a Boolean constant.

Example:
Print true
Print false

Pi: Returns a very accurate approximation of pi.

Example:
Print pi
Local radius 3
Local area pi * radius ^ 2
Print ,A circle with radius , + radius + , has area , + area

Comment: Used for adding comments to code.

Example:
Comment ,The command below will output text to the console.,
Print ,I won the spelling bee!,
Comment ,The comment command does not affect code functionality.,


= = = MATH COMMANDS = = =

(number) +, -, *, /, %, ^ (number): Performs the corresponding operation on the given arguments and returns the result.

Example:
Print 10 + 5
Print 10 - 5
Print 10 * 5
Print 10 / 5
Print 11 % 5
Print 10 ^ 5
Comment ,There is a standard order of operations:,
Print 7 + 2 * 9 / 3
Print "Addition works with " + "text, too"

Alternatively, use the operators plus, minus, times, over, toThe, and modulus.

Example:
Print 2 plus 6
Print 2 times 6

ArcTangentTwo (number) (number): Finds the angle to the given coordinate

Example:
Print arcTangentTwo 3 3
Print arcTangentTwo 0 4

_ (number): Returns the negative value of a number.

Example:
Print _ 5
Print _ _ 2

Alternatively, use the command negative.

Random: Generates and returns a random decimal number between 0 and 1.

Example:
Print random
Print random * 100

AbsoluteValue, Sine, Cosine, SquareRoot, Round (number): Performs the corresponding operation on a single argument and returns the result.

Example:
Print absoluteValue -5
Print sine 0
Print cosine pi
Print squareRoot 4
Print round 8.9

ParseInt (value): Converts a float or string into an integer and returns the result. This command rounds down.

Example:
Print parseInt 5.6
Print 2 + parseInt ,8.3,

(value) =, !=, >, < (value): Compares the given values and returns a boolean result.

Example:
Print 3 = 4
Print ,cheese, = ,cheese,
Print 3 != 4
Print 3 > 4
Print 3 < 4

Alternatively, use the operators equals, doesNotEqual, isAbove, and isBelow.

(boolean) |, & (boolean); ! (boolean): Performs an operation on the boolean value(s) and returns a boolean result.

Example:
Print true | false
Print false & false
Print ! false

Alternatively, use the operators or and and, and the command not.


= = = BRANCHING COMMANDS = = =

If (boolean): Initiates a new code block which is ignored if the given boolean value is false.

End: Terminates any kind of code block.

Example:
If 6 > 5
Print "6 is, in fact, greater than 5!"
End
Local food ,burger,
If food = ,pasta,
Print ,You should have sauce.,
End
If food = ,burger,
Print ,You should have ketchup.,
End

Else: Terminates an if block, begins a new block which is executed only if the last block was ignored.

Example:
Local myAge 19
If myAge isAbove 20
Print "I can legally drink!"
Else
Print "I should have creme soda instead."
End

While (expression): Initiates a new block which is repeatedly executed in a loop. Each time execution reaches the start of the loop, the program will check the value of the expression. If the expression is false, the program will exit the loop.

Example:
Local count 1
While count < 6
Print count
Increment count
End

Break: Terminates a while block in the middle of a loop.

Example:
Local myNumber 10
While myNumber > 0
Decrement myNumber
Print myNumber
If myNumber < 5
Break
End
Decrement myNumber
Print myNumber
End

Continue: Causes one iteration of a while loop to be skipped.

Example:
Local blah 0
While blah isBelow 7
Set blah (blah + 1)
If blah equals 3
Continue
End
Print blah
End

Function (command name) (var name) (var name) (var name)...: Declares a function which accepts the given arguments and executes the following code block.

Example:
Function greetUser name location
Print "Hello " + name + ", welcome to " + location + "!"
End
GreetUser ,Bob, ,the supermarket,

Return (value): Terminates a function block and returns a value to the parent command.

Example:
Function doubleTheNumber input
Return input times two
End
Print doubleTheNumber 15

Operator (operator name) (priority) (var name) (var name): Declares an operator which has the given priority, accepts the given arguments, and executes the following code block.

Example:
Operator roundAndSubtract 3 firstNumber secondNumber
Return (round firstNumber) - secondNumber
End
Print 8 roundAndSubtract 3
Print 9.3 roundAndSubtract 7


= = = ARRAY COMMANDS = = =

NewArray: Returns a new array containing no elements.

CreateArray (value) (value) (value)... End: Returns an array containing the given values.

Example:
Local places createArray ,Spain, ,USA, ,Japan, end
Print places
Comment "Arrays can be embedded."
Set places createArray ,Spain, createArray ,Chicago, ,Florida, end ,Japan, end
Print places
Local quantities createArray 5 9 (4 + 8) 2 end
Print quantities

(array) : (index): Refers to the element of an array in the given index. Note that the index may be a number or a string.

Example:
Local myPlants newArray
Set myPlants : 0 ,flower,
Set myPlants : 1 ,tree,
Set myPlants : 2 ,grass,
Print myPlants
Print myPlants : 1

Alternatively, use the operator at.

(array) . find (element): Returns the index at which the element first appears in the array.

Example:
Local colorNames createArray ,red, ,blue, ,white, ,gray, end
Print colorNames . find ,white,
Print colorNames . find ,red,

(array) . push (value): Adds a value to the end of the array.

Example:
Local partyMembers newArray
partyMembers . push ,Bartholemew,
partyMembers . push ,Frederick,
partyMembers . push ,Willowsworth,
Print partyMembers
partyMembers . push ,Billy,
Print partyMembers

(array) . remove (index): Deletes the element at the given array in the index, causing all elements afterward to shift to the left.

Example:
Local buildings createArray ,library, ,school, ,mall, end
Print buildings
Buildings . remove 1
Print buildings

(array) . getKeys: Returns the (index) keys of the input array. The keys themselves are stored in an array.

Example:
Local itemQuantities newArray
Set itemQuantities : ,shirt, 50
Set itemQuantities : ,jacket, 23
Set itemQuantities : ,hat, 47
Print itemQuantities . getKeys

(array) . copy: Returns a duplicate of the given array. Use this for untangling pointers.

Example:
Local letters newArray
Letters . push ,m,
Letters . push ,z,
Print letters
Comment "tangledCopy is bound to the value of
letters even after it is set"
Local tangledCopy letters
Local separateCopy letters . copy
Set letters : 0 ,a,
Print tangledCopy
Print separateCopy

ForEach (var name) (array): Begins a new block where consecutive elements of the array are stored in the given variable for each iteration.

Example:
Local materials createArray ,wood, ,stone, ,metal, end
ForEach substance materials
Print ,My house is made of , + substance
End

(string or array) . length: Returns the length of a string or array.

Example:
Local funnySounds createArray ,boop, ,kasplunk, ,wawawa, ,poof poof, end
Print funnySounds . length
Print ,this is a string, . length
Local animalType ,sheep,
Print animalType . length


= = = STRING COMMANDS = = =

(string) . substring (start index) (end index): Returns part of a string. The end index is not inclusive.

Example:
Print ,my name is bob, . substring 3 7
Local myString ,The moon is made of cheese,
Print myString . substring 20 myString . length

(string) . replaceSubstring (start index) (end index) (substring): Overwrites parts of a string and returns the result.

Example:
Print ,I think that chocolate tastes good, . replaceSubstring 13 22 ,pasta,

(string) . find (substring): Returns the index of the first occurrence of a substring within a string.

Example:
Print ,cheese panda marshmallow, . find ,panda,
Print ,This page is sometimes red and sometimes blue, . find ,sometimes,

(string) . findAfterIndex (substring) (index): Returns the index of the first substring to appear after a given index.

Example:
Print ,abcabcdefcdef, . findAfterIndex ,c, 4
Print ,sea shells on the sea shore, . findAfterIndex ,sea, 12

(string) . toUpperCase, toLowerCase: Converts all characters of the string to uppercase or lowercase.

Example:
Print ,Billy Mays is very loud!, . toUpperCase
Local arbitraryMessage ,The pony named Fluttershy is very quiet.,
Print arbitraryMessage . toLowerCase

(string) . split (separator), (array) . join (separator): Splits a string into an array or joins an array into a string using a delimiting string.

Example:
Local text "He made no sense after that"
Print text
Local tempList text . split " "
Print tempList : 3
Local tempText tempList . join "~~"
Print tempText


= = = EVENT COMMANDS = = =

AddTimerEvent (delay): Causes the commands in the following block to be executed repeatedly with a given millisecond delay.

Example:
Local count 0
AddTimerEvent 1000
Increment count
Print count
End

GetTime: Returns the current POSIX time in milliseconds.

Example:
AddTimerEvent 500
Local numberOfSeconds round (getTime / 1000)
Print "The POSIX time in seconds is: " + numberOfSeconds
End

SetInputEvent: Causes the commands in the following block to be executed whenever the user enters input into the console.

Example:
Print ,Click the input box and press enter.,
SetInputEvent
Print ,Hello!,
End

GetInput: Returns the string entered by the user into the console.

Example:
Local runningTotal 0
Print ,I am starting with , + runningTotal + , apples. Enter a number to get more apples.,
SetInputEvent
Shift runningTotal parseInt getInput
Print ,Now I have , + runningTotal + , apples.,
End


= = = OOP COMMANDS = = =

Class (class name): Defines a new class using the contents of the following code block. A new instance of the class may be obtained with the command New(class name).

InstanceVar (var name) (default value): Adds an instance variable to the class.

(object) . (var name or method): Accesses instance variables an methods of an object.

Example:
Class door
InstanceVar color ,blue,
InstanceVar state ,closed,
End
Local myDoor newDoor
Print myDoor . color
Print myDoor . state
Set myDoor . state ,open,
Print myDoor . state

(object) . getClass: Returns the class name of the object as a string.

Example:
Class building
InstanceVar floors 40
InstanceVar company ,None,
End
Local duoHeadquarters newBuilding
Set duoHeadquarters . company ,DUO Computing Inc,
Print duoHeadquarters . getClass

This: Refers to the instance being created or called.

Method (method name) (var name) (var name) (var name)...: Declares a class method which accepts various arguments and performs the following code block.

Example:
Class duck
InstanceVar age 0
Method speak count
  While count > 0
  If this . age < 2
  Print "Peep!"
  Else
  Print "Quack!"
  End
  Decrement count
  End
End
End
Local flapper newDuck
Flapper . speak 3
Set flapper . age 5
Flapper . speak 2

Constructor (command name) (var name) (var name) (var name)...: Declares a command which returns a new instance of the class.

Example:
Class atom
InstanceVar element ,none,
InstanceVar pos 0
Constructor createAtom element pos
Set (this . element) element
Set (this . pos) pos
End
End
Local a newAtom
Local b createAtom ,hydrogen, 8
Print a . element + " " + a . pos
Print b . element + " " + b . pos

AddSuperclass (class name): Adds a class parent to a class. This causes the class to inherit all private variables, methods, and constructors of the superclass. The inheritance command must be placed first within a class definition.

Example:
Class cup
InstanceVar liquidAmount 3
End
Local x newCup
Print x . liquidAmount
Class mug
AddSuperclass cup
InstanceVar caption ,I hate mondays,
End
Local y newMug
Print y . liquidAmount + " " + y . caption

(object) . applyConstructor (command): Modifies the object's instance variables using a given constructor command.

Example:
Class window
InstanceVar isOpened false
InstanceVar paneAmount 4
Constructor createWindow paneAmount
Set (this . paneAmount) paneAmount
End
End
Class stainedWindow
AddSuperclass window
InstanceVar color "blue"
End
Local myWindow newStainedWindow
MyWindow . applyConstructor createWindow 20
Print myWindow . isOpened
Print myWindow . paneAmount
Print myWindow . color

(object) . isInClass (class name text): Returns a boolean value which shows whether the object has the same class or is in a subclass of the given class. The input class name is a string.

Example:
Class person
InstanceVar name ,billy,
InstanceVar age 23
End
Class worker
AddSuperclass person
InstanceVar job ,secretary,
End
Local someObject newWorker
Print someObject . isInClass ,person,
Print someObject . isInClass ,worker,
Print someObject . isInClass ,panda,


= = = ADDITIONAL SYNTAX = = =

It is possible to store and call a function reference using an exclamation point and a question mark (! and ?). To insert a reference to a function, place the function name immediately followed by an exclamation point ((function name)!). When calling a reference, place the variable name, then a question mark, then the number of arguments for the function to accept ((var name)?(argument amount)). Several examples are below:

Function makeGreeting name
Return "Hello, " + name + "!"
End

Function greet
Print "Welcome to Perkins!"
End

Local myFunctionReference makeGreeting!
Local anotherReference greet!
Print myFunctionReference?1 "Alex"
AnotherReference?0

Function filter list predicate
Local output newArray
ForEach element list
If predicate?1 element
Output . push element
End
End
Return output
End

Function isMoreThanTen number
Return number > 10
End

Function isEven number
Return number % 2 = 0
End

Local tempList createArray 3 9 12 6 38 5 9001 end
Print filter tempList isMoreThanTen!
Print filter tempList isEven!

= = = JAVASCRIPT SPECIFIC COMMANDS = = =

ShowConsole, hideConsole: Either shows or hides the text console. The console is visible by default.

Example:
Local isShown true
AddTimerEvent 500
If isShown
HideConsole
Else
ShowConsole
End
Set isShown ! isShown
End

ShowCanvas, hideCanvas: Either shows or hides the graphical canvas. The canvas is not visible by default.

Example:
HideConsole
AddTimerEvent 100
If random > 0.5
HideCanvas
Else
ShowCanvas
End
End

SetCanvasDimensions (width) (height): Changes the dimensions of the canvas. The default width and height are 200.

Example:
ShowCanvas
SetCanvasDimensions 30 400

DrawRectangle (pos x) (pos y) (width) (height): Draws a rectangle onto the canvas.

Example:
ShowCanvas
DrawRectangle 10 50 40 20

SetFillColor (red) (green) (blue): Changes the fill color. Each RGB value is between 0 and 255. The default initial color is black.

Example:
ShowCanvas
SetFillColor 192 0 192
DrawRectangle 10 10 30 30


SetFont (font text), drawText (pos x) (pos y) (text): Sets the canvas font and draws text on the canvas. Use setFillColor to change the text color.

Example:
ShowCanvas
SetFont "12pt Courier"
DrawText 20 50 "Hello canvas!"


SetCanvasEvent (type text): Causes the following code block to be executed whenever a particular event happens. Some possible type texts include:
  • "onmousedown"
  • "onmousemove"
  • "onmouseup"
  • "onkeydown"
  • "onkeyup"
  • "onclick"
  • "ontouchstart"
  • "ontouchmove"
  • "ontouchend"

Example:
ShowCanvas
SetCanvasEvent "onclick"
Print "The canvas was clicked!"
End


GetEventAttribute (type text): Retrieves a property from within an event block. Some possible type texts include:
  • "offsetX"
  • "offsetY"
  • "which" (key or mouse button)
  • "touches"

Example:
ShowCanvas
SetCanvasEvent "onmousedown"
Local posX getEventAttribute "offsetX"
Local posY getEventAttribute "offsetY"
DrawRectangle posX - 10 posY - 10 20 20
End


Return to the Compiler
Return to the Ostracod Pond