= OCL GUIDE =

Return to the Compiler

Ostracod Command Language (previously named the JavaScript Cypher) was originally created to allow iPad programming, but it may be used on any JavaScript enabled machine. Two significant features of OCL:

If you test the code below, your browser will display an alert box containing the number 3:

Alert 3

Since capitalization does not matter, this code will do the same thing:

aLeRt 3

The kind of whitespace separating terms is also irrelevant:

      Alert
3

= = = SYNTAX = = =

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:

Alert 1
Alert 2 alert 3
Alert
4 alert 5

Variables are declared using the local command. It accepts two arguments: the name of the variable, and its value. Variable names may contain periods (.) to separate words:

Local asdf 4
Alert asdf
Local my.special.number 9
Alert my.special.number

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

Alert add 1 1
Alert add 7 add 1 2

String literals are enclosed in commas (,). Concatentation occurs when a variable name is placed adjacent to a string literal:

Alert ,hello world,
Local blah ,muffin,
Alert ,this is a ,blah

To express a case sensitive string literal, enclose text in tripple commas (,,,). Single comma string literals will not preserve case:

Alert ,Some people say that I look like Billy Mays.,
Alert ,,,Some people say that I look like Billy Mays.,,,

Sometimes you may want to include commentary in your code. This is done by enclosing text in exclamation marks (!):

!This code generates a number between 1 and 10.!
Alert add 1 parse.int multiply random 10

What happens if you want to use a comma or exclamation point in a string literal? You can cancel these characters with a question mark ?. Question marks can even cancel other question marks:

Alert ,,,I love Ostracod Command Language?! If I jump over a house?, will I be arrested??,,,

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
Alert ,The first condition is true.,
End
If false
Alert ,The second condition is true.,
If true
Alert ,The second and third conditions are true.,
End
End

= = = VALUE COMMANDS = = =

Alert (value): Displays the value in an alert box.

Example:
Alert 8
Alert ,this is some text,


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

Example:
Local first 13
Alert first
Local the.second.variable ,,,I am cooler than first.,,,
Alert the.second.variable


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

Example:
Local color ,blue,
Alert ,the color is ,color
Set color ,red,
Alert ,the color is now ,color


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

Example:
Local x 2
Increment x
Alert x
Decrement x
Decrement x
Alert x


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

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


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

Example:
Alert six
Local my.number eight
Alert my.number


True, False: Returns a Boolean constant.

Example:
Alert true
Alert false


Pi: Returns a very accurate approximation of pi.

Example:
Alert pi
Local radius 3
Local area multiply pi power radius 2
Alert ,a circle with radius ,radius, has area ,area



= = = MATH COMMANDS = = =

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

Example:
Alert random
Alert multiply random 100


Add, Subtract, Multiply, Divide, Modulus, Power, Arctangent.two (number) (number): Performs the corresponding operation on the given arguments and returns the result.

Example:
Alert add 10 5
Alert subtract 10 5
Alert multiply 10 5
Alert divide 10 5
Alert modulus 11 5
Alert power 10 5
Alert arctangent.two 3 3
!Combine commands like so:!
Alert add 7 multiply 2 divide 9 3
!This is equivalent to the last command:!
Alert add multiply divide 9 3 2 7


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

Example:
Alert negative 5
Alert negative negative 2


Absolute.value, Sine, Cosine, Square.root, Round (number): Performs the corresponding operation on a single argument and returns the result.

Example:
Alert absolute.value -5
Alert sine 0
Alert cosine pi
Alert square.root 4
Alert round 8.9


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

Example:
Alert parse.int 5.6
Alert add parse.int ,8.3, 2


Equal, Unequal, Greater, Less (value) (value): Compares the given values and returns a boolean result.

Example:
Alert equal 3 4
Alert equal ,cheese, ,cheese,
Alert unequal 3 4
Alert greater 3 4
Alert less 3 4


Or, And, Xor (boolean) (boolean); Not (boolean): Performs an operation on the boolean values and returns a boolean result.

Example:
Alert or true false
Alert and false false
Alert xor true true
Alert not false



= = = 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 greater 6 5
Alert ,,,6 is?, in fact?, greater than 5?!,,,
End
Local food ,burger,
If equal food ,pasta,
Alert ,,,You should have sauce.,,,
End
If equal food ,burger,
Alert ,,,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 my.age 18
If greater my.age 20
Alert ,,,I can legally drink?!,,,
Else
Alert ,,,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 less count 6
Alert count
Increment count
End


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

Example:
Local my.number 10
While greater my.number 0
Decrement my.number
Alert my.number
If less my.number 5
Break End
Decrement my.number
Alert my.number
End


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

Example:
Local blah 0
While less blah 7
Set blah add blah 1
If equal blah 3
Continue
End
Alert 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 greet.user name location
Alert ,,,Hello ,,,name,,,?, welcome to ,,,location,,,?!,,,
End
Greet.user ,,,Bob,,, ,the supermarket,


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

Example:
Function double.number input
Return multiply input 2
End
Alert double.number 15



= = = ARRAY COMMANDS = = =

Empty.array: Returns a new array containing no elements.

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

Example:
Local places new.array ,spain, ,usa, ,japan, end
Alert places
Set places new.array ,spain, new.array ,chicago, ,florida, end ,japan, end
Alert places
Set quantities new.array 5 9 add 4 8 2 end
Alert quantities


Set.array.element (var name) (index) (value): Sets the value of an array element at the given index.

Example:
Local my.plants empty.array
Set.array.element my.plants 0 ,flower,
Set.array.element my.plants 1 ,tree,
Set.array.element my.plants 2 ,grass,
Alert my.plants


Get.array.element (var name) (index) (value): Gets the value of an array element at the given index. As shown below, arrays can be indexed with strings.

Example:
Local color.definitions empty.array
Set.array.element color.definitions ,apple, ,red,
Set.array.element color.definitions ,sky, ,blue,
Set.array.element color.definitions ,snow, ,white,
Alert get.array.element color.definitions ,snow,
Alert get.array.element color.definitions ,apple,


Add.array.element (var name) (value): Adds a value to the end of the array.

Example:
Local party.members empty.array
Add.array.element party.members ,,,Bartholemew,,,
Add.array.element party.members ,,,Frederick,,,
Add.array.element party.members ,,,Willowsworth,,,
Alert party.members
Add.array.element party.members ,,,Billy,,,
Alert party.members


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

Example:
Local buildings empty.array
Set.array.element buildings 0 ,library,
Set.array.element buildings 1 ,school,
Set.array.element buildings 2 ,mall,
Alert buildings Remove.array.element buildings 1 Alert buildings


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

Example:
Local item.quantities empty.array
Set.array.element item.quantities ,shirt, 50
Set.array.element item.quantities ,jacket, 23
Set.array.element item.quantities ,hat, 47
Alert get.array.keys item.quantities


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

Example:
Local letters empty.array
Add.array.element letters ,m,
Add.array.element letters ,z,
Alert letters
!tangled.copy is bound to the value of
letters even after it is set!
Local tangled.copy letters
Local separate.copy copy.array letters
Set.array.element letters 0 ,a,
Alert tangled.copy
Alert separate.copy


For.each (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 empty.array
Add.array.element materials ,wood,
Add.array.element materials ,stone,
Add.array.element materials ,metal,
For.each substance materials
Alert ,my house is made of ,substance
End


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

Example:
Local funny.sounds empty.array
Add.array.element funny.sounds ,boop,
Add.array.element funny.sounds ,kasplunk,
Add.array.element funny.sounds ,wawawa,
Add.array.element funny.sounds ,poof poof,
Alert length funny.sounds
Alert length ,this is a string,
Alert length ,sheep,



= = = STRING COMMANDS = = =

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

Example:
Alert get.substring ,my name is bob, 3 7
Local my.string ,the moon is made of cheese,
Alert get.substring my.string 20 length my.string


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

Example:
Alert replace.substring ,i think that chocolate tastes good, 13 22 ,pasta,


Find.substring (string) (substring): Returns the index of the first occurence of a substring within a string.

Example:
Alert find.substring ,cheese panda marshmallow, ,panda,
Alert find.substring ,this page is sometimes red and sometimes blue, ,sometimes,


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

Example:
Alert find.substring.after.index ,abcabcdefcdef, 4
Alert find.substring.after.index ,sea shells on the sea shore, 12



= = = TAG COMMANDS = = =

Add.tag (ID) (type) (parent ID): Adds a new HTML tag to the page. The ID of the body is "body". If you do not already know HTML, you should learn it.

Set.tag.contents (ID) (contents): Fills a tag with the given contents.

Example:
Add.tag ,my.paragraph, ,p, ,body,
Set.tag.contents ,my.paragraph, ,,,Hello OCL?!,,,
Add.tag ,grocery.list, ,ul, ,body,
Add.tag ,first.item, ,li, ,grocery.list,
Set.tag.contents ,first.item, ,milk,
Add.tag ,second.item, ,li, ,grocery.list,
Set.tag.contents ,second.item, ,eggs,
Add.tag ,third.item, ,li, ,grocery.list,
Set.tag.contents ,third.item, ,cheese,


Get.tag.contents (ID): Returns the contents of a tag.

Example:
Add.tag ,obvious.statement, ,span, ,body,
Set.tag.contents ,obvious.statement, ,,,everybody eats food,,,
Alert get.tag.contents ,obvious.statement,


Set.tag.attribute (ID) (attribute) (value): Sets the attribute of a tag.

Get.tag.attribute (ID) (attribute): Returns the attribute of a tag.

Example:
Add.tag ,x, ,textarea, ,body,
Set.tag.attribute ,x, ,rows, 50
Set.tag.attribute ,x, ,cols, 10 Set.tag.attribute ,x, ,value, ,,,This is a really tall text area,,,
Alert get.tag.attribute ,x, ,rows,


Set.tag.style (ID) (style name) (value): Sets a CSS style for the tag.

Get.tag.style (ID) (style name): Returns a CSS style for the tag.

Example:
Add.tag ,pretty.text, ,span, ,body,
Set.tag.contents ,pretty.text, ,,,I am beautiful (no matter what they say),,,
Set.tag.style ,pretty.text, ,background, ,yellow,
Set.tag.style ,pretty.text, ,color, ,green,
Alert get.tag.style ,pretty.text, ,background,


Make.tag (type) (attribute name) (attribute value) (attribute name) (attribute value) ... Style (style name) (style value) (style name) (style value) ... Contents (content) (content) ... End: Creates a tag with the given properties.

Example:
Make.tag ,p, id ,bobby,
Contents ,,,This is a paragraph?! ,,,
End
Make.tag ,span, parent ,bobby,
Style background ,magenta,
Contents ,,,This is a span inside the paragraph.,,,
End


Note that you can embed a make.tag command as the contents of another make.tag command. The code below is equivalent to the code above.

Example:
Make.tag ,p,
Contents
,,,This is a paragraph?! ,,,
Make.tag ,span,
Style background ,magenta,
Contents ,,,This is a span inside the paragraph.,,,
End
End

Get.tag.children (ID): Returns an array of IDs for all the tags inside the given tag.

Get.tag.parent (ID): Returns the ID of the surrounding tag.

Example:
Add.tag ,outer.container, ,div, ,body,
Make.tag ,p, id ,first.child, parent ,outer.container, contents ,,,I am the older sibling.,,, end
Make.tag ,p, id ,second.child, parent ,outer.container, contents ,,,I am the younger sibling.,,, end
Alert get.tag.children ,outer.container,
Alert get.tag.parent ,second.child,


Remove.tag (ID): Deletes a tag on the page.

Example:
Add.tag ,good, ,p, ,body,
Set.tag.contents ,good, ,,,I did my homework.,,,
Add.tag ,bad, ,p, ,body,
Set.tag.contents ,bad, ,,,I drew all over the walls with crayon.,,,
Add.tag ,super, ,p, ,body,
Set.tag.contents ,super, ,,,I took out the trash.,,,
Remove.tag ,bad,


Set.canvas.line.color (ID) (red) (green) (blue): Sets the line color of a canvas.

Set.canvas.fill.color (ID) (red) (green) (blue): Sets the fill color of a canvas.

Draw.canvas.line (ID) (pos x) (pos y) (pos x) (pos y): Draws a line on a canvas.

Draw.canvas.rectangle (ID) (pos x) (pos y) (width) (height): Draws a rectangle on a canvas.

Draw.canvas.polygon (ID) (coordinate array): Draws a polygon on a canvas. The given array contains a sequence of X and Y values.

Example:
Add.tag ,my.canvas, ,canvas, ,body,
Set.tag.attribute ,my.canvas, ,width, 200
Set.tag.attribute ,my.canvas, ,height, 200
Set.canvas.line.color ,my.canvas, 255 0 0
Set.canvas.fill.color ,my.canvas, 0 128 128
Draw.canvas.line ,my.canvas, 20 20 30 50
Draw.canvas.rectangle ,my.canvas, 110 50 50 90
Draw.canvas.polygon ,my.canvas, new.array 40 60 40 80 60 60 end



= = = EVENT COMMANDS = = =

Evaluate (function name text) (arg) (arg)...: Evaluates a function whose name is given as text and whose arguments are given afterward. The arguments may not be embedded commands.

Example:
Function speak.loudly count
While greater count zero
Alert ,,,I AM TRYING TO WALK HERE?, GET OUT OF MY WAY,,,
Decrement count
End
End
Function speak.softly
Alert ,sorry i will move out of the way,
End
Local volume ,loudly,
Evaluate ,speak.,volume 3


Add.timer.event (delay) (function name text) (arg) (arg)...: Causes a function to be executed repeatedly with a given millisecond delay.

Example:
Local count 0
Add.tag ,x, ,p, ,body,
Function step
Increment count
Set.tag.contents ,x, count
End
Add.timer.event 1000 ,step,


Set.tag.event (ID) (event name) (function name text) (arg) (arg)...: Causes a function to be executed when a certain event is triggered for a tag.

Example:
Function boast
Alert ,,,I am the best button in the world?!,,,
End
Add.tag ,narcissist, ,button, ,body,
Set.tag.contents ,narcissist, ,,,CLICK ME,,,
Set.tag.event ,narcissist, ,onclick, ,boast,


Get.event.attribute (attribute name): Returns an attribute of the last triggered event. This includes mouse clicks and keyboard presses.

Example:
Function respond
Local keycode get.event.attribute ,which,
Set.tag.contents ,message, keycode
End
Add.tag ,message, ,p, ,body,
Set.tag.contents ,message, ,,,Press a key?!,,,
Set.tag.event ,body, ,onkeydown, ,respond,


Prevent.default.event.action: Stops the browser from performing the default action of an event.

Example:
Function clicked
Alert ,,,The link was pressed?, but you are going nowhere?!,,,
Prevent.default.event.action
End
Add.tag ,link, ,a, ,body,
Set.tag.contents ,link, ,,,Go to google.com,,,
Set.tag.attribute ,link, ,href, ,http://www.google.com,
Set.tag.event ,link, ,onclick, ,clicked,



= = = PSEUDO OOP COMMANDS = = =

Constructor (class) (private var) (private var)...: Creates a new constructor for a class, including get and set commands for all private variables. For quick access to the get methods, use the double dot convention (..) shown in the example.

Example:
Constructor door color state
Local my.door new.door ,blue, ,closed,
Alert get.door.color my.door
Alert get.door.state my.door
Set.door.state my.door ,open,
Alert get.door.state my.door
Alert my.door..state


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

Example:
Constructor building floors company
Local duo.headquarters new.building 40 ,duo computing inc,
Alert get.object.class duo.headquarters


Set.class.parent (class) (superclass): Sets the superclass of a class. When a constructor is made for the class, it will inherit all private variables of the parent. The inheritance command must come before the constructor.

Example:
Constructor cup liquid.amount
Local x new.cup 8
Alert get.cup.liquid.amount x
Set.class.parent mug cup
Constructor mug caption
Local y new.mug 5 ,,,I hate mondays,,,
Alert get.mug.liquid.amount y
Alert get.mug.caption y
Set.cup.liquid.amount y 0
Alert get.cup.liquid.amount y


Object.is.in.class (object) (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:
Constructor person name age
Set.class.parent worker person
Constructor worker job
Local some.object new.worker ,billy, 23 ,secretary,
Alert object.is.in.class some.object ,person,
Alert object.is.in.class some.object ,worker,
Alert object.is.in.class some.object ,panda,


Incrementor, Decrementor, Shifter (object) (private var): Creates commands for incrementing, decrementing, and shifting private variables of an object.

Example:
Constructor atom element pos
Incrementor atom pos
Decrementor atom pos
Shifter atom pos
Local x new.atom ,hydrogen, 8
Alert get.atom.pos x
Increment.atom.pos x
Alert get.atom.pos x
Shift.atom.pos x -4
Alert get.atom.pos x
Decrement.atom.pos x
Alert get.atom.pos x



Return to the Compiler
Return to the Ostracod Pond