OK Language Manual

Welcome

Welcome to the Omniglot Kidogo (OK) Language Manual page.
OK is a Programming Language designed to be used with any Natural Language (English, French, German, Hindi, etc) with a simple and meaningful syntax.

It compiles to Javascript, so it can run on any Web Browser.
Find below the programming language guide. If you have any question, comment or suggestion, please feel free to contact at this email address.

Choosing a Language

OK, by default, is written using English but it also supports other languages.
To select, on the Editor top row choose your language on the Language selection element.
By selecting a language, you can use a different set of keywords in your code.

You can check the keyword translations by selecting Browse Dictionary in Code Samples, after selecting your preferred language.

In a near future, you will also be able to create your own set of keyword aliases (or dictionary) to use in OK.

Keywords

Keywords are words with a special meaning in a programming language, in this case OK.
OK keywords are:

if var for else break check times until repeat return default function


Each keyword can be used in its default language, English, or in any chosen language.
You can check the keyword translations by selecting Browse Dictionary in Code Samples, after selecting your preferred language.

Hello World!

The Hello World! program can be written in OK as follows:

IO.show('Hello World!');

IO.show is a function that takes a string and displays it to the user.

Compiling and running OK programs

After writing your OK program, click on COMPILE on the Editor top row.
If you made any mistakes on your code, an error message will be displayed with information regarding your error.
Otherwise a success message will be displayed.

To see the output of your program click RUN!

Comments

Comments are information on your code that describe the steps you are following.
They are very helpful to you and to anyone who might read it because it makes your code easier to understand.

# This is a comment in OK

# To have multiple-line
# comments in OK
# you have to start each line
# with #

@xx This is a special kind of comment called Glotation

A normal comment in OK can be created by using # and following with the comment message.
Unlike other programming languages, OK only supports Single Line comments. If you have a comment spanning multiple lines you will have to start each line with #

A Glotation (Glot Annotation) is a special type of comment that lets you specify in which language it is being created.
This allows the comment to be translated into different languages.

The format for a glotation is @xx Message where:

  • @ specifies that what follows it is a Glotation
  • xx specifies a two letter language code for the language on which you are writing the comment.
    Example: en for English, pt for Portuguese, fr for French, hi for Hindi, and so on.
  • Message is your comment message

Variables and Datatypes

A variable is a name given to a memory location that will store a value during your program's execution.
You can declare a variable by using the keyword var followed by a space and then the variable name.
Example: var x;

Datatypes

A Data type specifies which type of values a variable can hold.
OK compiles to Javascript. By doing so, it inherits Javascript's Data types.
They are:

  • Number
    A number can hold an Integer or a Floating-point value.
    Example:
    var x = 1;
    var y = 1.5;
    x and y are varibles of the type Number, where x is a Decimal number and y is a Floating-point number.
    To explicitly specify the data-types you can use:
    var x = new Number();
    x = 1;
  • String
    A String is any sequence of words and numbers enclosed by " (double quotes) or ' (single quotes).
    Example:
    var x = "This is a String";
    var y = 'This is also a String';
    
    # Alternatively
    var z = new String();
    z = 'This is also a String';
  • Boolean
    A Boolean value only supports two options: true or false. They can be used to specify states or conditions.
    Example:
    var x = true;
    var y = false;
    
    # Alternatively
    var z = new Boolean();
    z = true;
  • Object An Object is a composite data type, formed by grouping a set of variables which are related but might be of different datatypes.
    Objects are used to represent characteristics and actions of real world entities.
    Example:
    # Empty Object
    var x = {};
    
    # Declaring and assigning the properties and methods to an Object
    var y = {
      property1: value,
      property2: value,
      propertyN: value,
      method1: function(){
        # code
      },
      method2: function(){
        # code
      },
      methodN: function(){
        # code
      }
    }
    
    # Alternatively
    var person = new Object();
    person.name = "John";
    person.age = 30;
    person.showAge = function(){
      IO.show(this.age);
    }
  • Array
    An Array is a group or collection of variables of the same datatype. In OK, as in Javascript, Arrays may contain variables of different Data types and even methods.
    An Array has a length property which returns the count of elements in it.
    Example:
    # Empty Array
    var array = [];
    
    # Declaring and initializing an Array
    var list = [1, 2, 3, 4, 5];
    
    # Alternatively
    var list = new Array(5);
    list[0] = 1;
    list[1] = 2;
    list[2] = 3;
    list[3] = 4;
    list[4] = 5;
    
    # Displaying the number of elements
    IO.show( list.length ); # 5

Assignment Operators

Assignment operators are used to assign values of constants, variables or expressions to variables.

Operator Description Example Explanation
= Assignment variable = value Assigns the value to the given variable
+= Addition Assignment variable += value Equivalent to variable = variable + value
-= Subtraction Assignment variable = value Equivalent to variable = variable - value
*= Multiplication Assignment variable = value Equivalent to variable = variable * value
/= Division Assignment x || y Equivalent to variable = variable / value
%= Modulo Assignment !x Equivalent to variable = variable % value

Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations between on numerical operands (values, constants and variables).
Operator Description Example Explanation
+ Addition x + y Adds the values of x and y
- Subtraction x - y Subtracts the value of y from the value of x
* Multiplication x * y Multiplies x by y
/ Division x / y Divides x by y
% Modulo Division x % y Returns the remainder of the division of x by y
++ Increment ++x or x++ Increments x by 1
-- Decrement --x or x-- Decrements x by 1

Relational Operators

Relational operators are used to establish relations between values and variables.
The result of a relational operation is a Boolean value, i.e., true or false

Operator Description Example Explanation
< Less than x < y Returns true if x is less than y. Otherwise returns false
<= Less than or equal x <= y Returns true if x is less than or equal to y. Otherwise returns false
== Equal x == y Returns true if x and y are equal. Otherwise returns false
!= Not Equal x != y Returns true if x is not equal to y. Otherwise returns false
> Greater than x > y Returns true if x is greater than y. Otherwise returns false
>= Greater than or equal x >= y Returns true if x is greater than or equal to y. Otherwise returns false

Logical Operators

Logical operators are used to perform logical operations on variables.

Operator Description Example Explanation
&& Logical AND x && y Returns true only if both x and y are true. Otherwise returns false
|| Logical OR x || y Returns false only if both x and y are false. Otherwise returns true
! Logical NOT !x Inverts the logical value of x. Returns true if x is false and returns false if x is true

Conditional Statements

Conditional statements provide a flow of control or decision-making in the program.
The conditional statements in OK are: if and check.

if statement

# General syntax
if ( condition ) {
  statements # executed when condition is true
}

# if-else
if ( condition ) {
  statements # executed when condition is true
} else {
  statements # executed when condition is false
}

if ( condition1 ) {
  statements # executed when condition1 is true
} else if ( condition2 ) {
  statements # executed when condition1 is false and condition2 is true
} else {
  statements # executed when none of condition1 nor condition2 is true
}

# nested if-else
if ( condition1 ) {
  if ( condition2 ) {
    statements # executed when condition1 and condition2 are both true
  } else {
    statements # executed when condition1 is true but condition2 is false
  }
}

check statement

# General syntax
check variable for {
  value1:
        statements; # executed when variable is equal to value1
        break;
  value2:
        statements; # executed when variable is equal to value2
        break;
  valueN:
        statements; # executed when variable is equal to valueN
        break;
  default:
        statements # executed when variable is not equal to any of the values
}

# Example: Verifying whether a number is even or odd
var x = 5 % 2; # x = 1
check x for {
  0:
    IO.show('x is even');
    break;
  1:
    IO.show('x is odd');
    break;
  default:
    IO.show("Ooops. I don't know whether x is even or odd")
}

Repetition Statements

Repeats the execution of a set of statements a given number of times

Repetition statements allow a certain instruction or group of instructions to be executed many times, repeatedly.
The repetition statements in OK are: repeat n times and repeat until

repeat n times

# General syntax
repeat n times {
  statements
}
# where n is a valid integer or integer variable

# Examples
# Shows Hello! 5 times
repeat 5 times {
  IO.show('Hello!')
}

var x = 5;
repeat x times {
  IO.show('Hello!')
}

# To get the current iteration index/number use the variable _index
repeat 5 times {
  IO.show(_index)
}
# Shows 0 1 2 3 4 since _index starts from 0

repeat until

Repeats the execution of a set of statements until a condition is true

# General Syntax
repeat until condition {
  statements
}

# Examples
var i = 1;
repeat until i == 5 {
  IO.show(i);
  i++;
}
# Shows 1 2 3 4

Functions

Functions are set of statements that perform a task and may return a value.
A function has a name and can be called when its execution is necessary.

Function definition

A function can be declared as follows:

# General Syntax
function name( parameters ) {
  statements

  return value
}

# Example
function greet( name ){
  IO.show('Hello ' + name)
}

# Without parameters
function name() {
  statements
}

# Example
function farewell(){
  IO.show('Bye!')
}

# Example with a return value
function greet( name ){
  greetingString = 'Hello ' + name;

  return greetingString;
}

Function call

After being declared, to use or execute the function it is necessary to make a function call. This can be done as follows:

# General Syntax
functionName()

# If it has a return value
variable = functionName()

# Example
function greet( name ) {
  greetingString = 'Hello ' + name;

  return greetingString;
}

function farewell() {
  IO.show('Bye!')
}

# Calling the function greet
var message = greet('John') # Returns 'Hello John'
IO.show(message) # Shows 'Hello John'

# Calling the function farewell
farewell() # Shows Bye!

Input/Output

To receive input or show messages to the user, use the following functions

Function Description Example Explanation
IO.show(message) Shows a message to the user IO.show(' This is a string with a message ') Displays a message to the user given as a string parameter to the function
IO.receive(message) Receives input from the user var x = IO.receive('Enter the value of x') Receives the input from the user, after displaying an appropriate input message, and saves the value in the variable x