Algorithmatic Reference Manual
The following is an attempt to cover all areas on Algorithmatic as a programming language. Feel free to edit this document to provide a clarification, or add related information.
Note that editor mode refers to Algorithmatic.com's IDE (when clicking SUBMIT from the top). And display mode refers to displaying and executing the algorithm outside Algorithmatic.com's IDE (by viewing an algorithm).
The general structure of your code should separate the read and write statements from the algorithm itself. Moreover, your algorithm should be consolidated into one self-contained function, called Execute. If you need to have helper functions, these functions should reside inside the Execute function as well. (See Function Statement).
Object provide the following functions:
You can create an array either using `Array.New()` or using its notation, e.g. `[1,2,3,4]`.
You can create a hashtable either using `Hashtable.New()` or using its notation, e.g. `{"x" => 1.2, "y" => 5}`.
The RegExMatch object supports the functions:
Note that currently the power operation has the same precedence as the mult (*) and div (/) operations.
Applicable to Numbers. The += will append to Strings.
Note that editor mode refers to Algorithmatic.com's IDE (when clicking SUBMIT from the top). And display mode refers to displaying and executing the algorithm outside Algorithmatic.com's IDE (by viewing an algorithm).
Table of Content
Lexical Conventions
Algorithmatic is case-sensitive. The lexical conventions are almost identical to other C-style languages.Code Structure
Algorithmtic follows a strict code structure. Although code that does not conform to this structure will run seemingly smoothly; this will affect the usage and external consumption of the submission. More clearly: code that does not follow Algorithmatic.com's code structure will not run in _display mode and users will not be able to call the algorithm externally. (See Algorithmatic Object).The general structure of your code should separate the read and write statements from the algorithm itself. Moreover, your algorithm should be consolidated into one self-contained function, called Execute. If you need to have helper functions, these functions should reside inside the Execute function as well. (See Function Statement).
// read input
var n1 = Screen.ReadNumber("Enter the first number", 3);
var n2 = Screen.ReadNumber("Enter the second number", 5);
// execute
var result = Execute(n1, n2);
// print result
Screen.WriteLine("The sum of the two numbers is " + result);
// the algorithm implementation - consolidated inside one function
function Execute(a, b)
{
return a + b;
// helper functions go here
}
Identifiers
Variable names and function names are considered identifiers. An identifier starts with a letter followed by zero or more charachters, digits, or underscore.Keywords
Keywords are identifiers that are reserved by Algorithmatic and cannot be used as user-defined identifiers. The keywords are: `true`, `false`, `null`, `this`, `var`, `functions`, `if`, `else`, `for`, `while`, `break`, `continue`, `return`Naming Convention
Algorithmatic follows PascalCase (a.k.a. upper camel case) convention for function names, and camelCase (a.k.a. lower camel case) for variable names. For identifiers that are two characters or less (such as UI, IO) upper case should be used.String Literals
Literal strings start and end with double quotes. Unlike some of the other C-style languages, single quotes are not recognized. In addition, Algorithmatic handles a single character as a string (no Character object). Currently, only two escape characters are implemented: _\n for new line, and \t for tab.Comments
Like other C-style languages, Algorithmatic implements two styles of commenting:- in-line comments, example: // this is a comment
- block comments, example: /* this is a comment */
Objects & Types
Algorithmatic is a dynamically typed programming language. This means that the type of an object is determined in run-time. Currently Algorithmatic supports several types of objects:Object
All objects inherit from the type Object. You can create custum objects via Object.New()Object provide the following functions:
- New(): returns a new instance of Object
function CreatePerson(name, age)
{
var foo = Object.New();
foo.Name = name;
foo.Age = age;
foo.Speak = function() {
var output = "My name is " + this.Name + " and i'm " + this.Age + " years old";
Screen.Write(output);
};
return foo;
}
var p1 = CreatePerson("Adolf Hitler", 20);
var p2 = CreatePerson("Steve Jobs", 50);
p1.Speak();
p2.Speak();
/*
Output:
My name is Adolf Hitler and i'm 20 years old
My name is Steve Jobs and i'm 50 years old
*/
String
Functions:- Length(): returns the count of characters inside that string
- IndexOf(subString): returns the index of the substring `subString`
- LastIndexOf(subString): returns the last index of the substring `subString`
- Substring(startIndex, length): returns a substring starting from index `startIndex` and of length `length`
- StartsWith(subString): returns a Boolean object indication wither the string starts with the substring `subString `
- EndsWith(subString): returns a Boolean object indication wither the string ends with the substring `subString `
- Contains(subString): returns a Boolean object indication wither the string contains the substring `subString `
- Replace(oldString, newString): returns a String object after replacing all occurrences of the substring `oldString` with `newString`
- ToUpper(): returns a capitalized-form of the string object
- ToLower(): returns a lower-character representation of the string object
- Trim(): returns a new String object after trimming all leading and trailing whitespace
- Split(separator): splits a String into an Array of Strings, by taking a `separator` String object
- ToArray(): returns an Array of Strings, where each String is a character
- ToNumber(): parses the String object and returns the Number equivalent
- ToAscii(): only to be used on a single-character Strings, returns the ASCII character representation of the character
- FromAscii(asciiCode): takes a Number object `asciiCode` and returns ASCII character representation of the Number
- GetCharAt(index): returns a single-character String from that index
- SetCharAt(index, char): updates the current string by replacing the character at the specified index
- IsBlank(): returns true if string is empty or whitespace, false otherwise
Number
Functions:- ToBigInteger(): returns the BigInteger representation of the current number
- ToNumber(): returns its self - this is used for compatibility where a function can accept both types
- ToBitString(): returns a binary representation of the current instance as a String
Boolean
No specific functions.Array
Functions:- Length(): returns a Number representing the count of the items in the Array
- IndexOf(object): returns a Number representing the index of the item `object` in the Array (-1 if not found)
- Push(object): handles the array as a stack by adding the object on top of the stack
- Pop(): handles the array as a stack by removing and returning the last
- Peek(): handles the array as a stack and returns the top element without removing it
- Enqueue(object): handles the array as a que and adds the object to the end of the que
- Dequeue(): handles the array as a que and removes and returns the first object in the que
- Insert(index, object): insert the object `object` to the index `index`
- Add(object): adds the object `object` to the end of the array
- RemoveAt(index): removes the object at index `index`
- AppendArray(array): adds the elements in the specified array to the end of the current array
- PrependArray(array): adds the elements in the specified array to the start of the current array
- InsertRange(index, array): adds the elements in the specified array to the specified index of the current array
- RemoveRange(index, count): removes `count` number of elements starting from `index`
- Contains(object): returns boolean value (true) if object was found in array, false otherwise
- Reverse(): reverse the order of the objects
- Sort(): sorts the array - depending on type: alphabetically for string and numerically for numbers
- Join(separator): returns a string representing all elements of the array joined with a separator
You can create an array either using `Array.New()` or using its notation, e.g. `[1,2,3,4]`.
Hashtable
Functions:- New(): returns a new Hashtable instance
- Count(): returns a Number representing the count of non-Null keys in the specified hashtable
- Set(key, value): adds the specified value and key -- alternatively array-index notation can be used
- Get(key): returns the value paired with the specified key, Null if not found -- alternatively array-index notation can be used
- ContainsKey(key): returns True if the key was found, False otherwise
- ContainsValue(value): returns True if the value was found, False otherwise
- GetKeys(): returns an Array of all keys
- GetValues(): returns an Array of all values
You can create a hashtable either using `Hashtable.New()` or using its notation, e.g. `{"x" => 1.2, "y" => 5}`.
BigInteger
Functions:- New(number): returns a new BigInteger instance having the decimal value `number`
- New(string): returns a new BigInteger instance having the decimal value represented in `string`
- And(biginteger): returns the result of AND operation on the current BigInteger and the specified BigInteger
- Or(biginteger): returns the result of OR operation on the current BigInteger and the specified BigInteger
- Xor(biginteger): returns the result of XOR operation on the current BigInteger and the specified BigInteger
- Not(): returns ones complement of the current instance by complementing all bits
- ShiftLeft(number): returns a new instance having the current binary value shifted left `number` of times
- ShiftRight(number): returns a new instance having the current binary value shifted right `number` of times
- ToBitString(): returns a binary representation of the current instance as a String
- ToNumber(): tries to return the current instance as a Number
Screen
Functions:- Write(object): prints the result of the `ToString()` function of `object`
- WriteLine(object): prints the result of the `ToString()` function of `object` followed by a new-line
- ReadString(label, default): prompts the user with `label` as label and `default` as default input, accepts and returns strings
- ReadString(label, default, tooltip): same as above and displays the String `tooltip` as a tooltip on focus
- ReadNumber(label, default): prompts the user with `label` as label and `default` as default input, accepts and returns numbers
- ReadNumber(label, default, tooltip): same as above and displays the String `tooltip` as a tooltip on focus
- ReadArray(label, default): prompts the user with `label` as label and `default` as default input, accepts and returns arrays
- ReadArray(label, default, tooltip): same as above and displays the String `tooltip` as a tooltip on focus
- ReadBoolean(label, default): prompts the user with `label` as label and `default` as default input, accepts and returns boolean
- ReadBoolean(label, default, tooltip): same as above and displays the String `tooltip` as a tooltip on focus
- ReadBigInteger(label, default): prompts the user with `label` as label and `default` as default input, accepts and returns numbers
- ReadBigInteger(label, default, tooltip): same as above and displays the String `tooltip` as a tooltip on focus
Math
Functions:- Sin(number): returns the sine of the angle `number`
- Asin(number): returns the angle whose sine is `number`
- Sinh(number): returns the hyperbolic sine of the angle `number`
- Cos(number): returns the cosine of the angle `number`
- Acos(number): returns the angle whose cosine is `number`
- Cosh(number): returns the hyperbolic cosine of the angle `number`
- Tan(number): returns the tangent of the angle `number`
- Atan(number): returns the tangent whose angle is `number`
- Tanh(number): returns the hyperbolic tangent of the angle `number`
- Sqrt(number): returns the square root of `number`
- Abs(number): returns the absolute value of `number`
- Ceiling(number): returns the smallest integer greater than or equal to `number`
- Floor(number): returns the largest integer less than or equal to `number`
- Log(number, base): returns the logarithm of `number` to the base `base`
- Pow(numberX, numberY): returns `numberX` to the power `numberY`
- Random(min, max): returns a random integer from the specified inclusive range
- PI(): returns the constant value of the ratio of the circumference of a circle to its diameter
- E(): returns the natural logarithmic base
- EvaluateExpression(expression, constants): dynamically evaluates a mathematical expression - see Math.EvaluateExpression
RegEx
Functions:- IsMatch(input, pattern): returns a boolean value by matching `pattern` to `input`
- ReplaceByString(input, pattern, string): returns a new string, by matching `pattern` to `input` and replacing matches with `string`
- ReplaceByFunction(input, pattern, function): returns a new string, by matching `pattern` to `input` and calling `function` which takes a RegExMatch object and returns a new string
The RegExMatch object supports the functions:
- GetCapture(): returns the captured RegEx string value
- GetResult(replacement): returns the expansion of the replaced values, replacing variables such as: $1, $2, etc
DateTime
No specific functions - yet.Algorithmatic
Used to call external functions. See Algorithmatic ObjectStatements
Algorithmatic supports a convenient subset of other major programming languages.Variable Declaration & Assignment
Variables are declared using the `var` keyword. One declaration per `var`. Currently, assignments don't return a value (which is a behavior in most programming languages).
var foo;
var bar = 42;
var baz = [2, 4, 8];
var qux = Screen.ReadString("What's your name?", "Hitler");
Flow Control
Algorithmatic supports basic flow control using the keywords `if` and `else`.
if (true.ToString() == "True")
Screen.Write("It is True!");
else
Screen.Write("This will not execute.");
Iteration
Algorithmatic supports iteration using the keywords `for`, `while`, `continue` and `break`.
var foo = "";
for(var i = 0; i < 10; i++)
{
foo.FromAscci(i + 65);
Screen.Write(foo);
}
// prints: ABCDEFGHIJ
Return
The `return` statement will stop execution of the current scope and return the result (if available) to the parent scope. See Function for example.Function
Algorithmatic supports function creation and function nesting.
Foo();
function Foo()
{
return "Foo" + Bar();
function Bar()
{
return "Bar";
}
}
// prints: FooBar
Operators
Logical
Algorithmatic currently supports only short-circuit logical operators.Arithmatic
Arithmatic operators: *, \, -, +, ^Note that currently the power operation has the same precedence as the mult (*) and div (/) operations.
Shorthand
Short-hand operators: ++, --, +=, -=Applicable to Numbers. The += will append to Strings.
updated 3 months and one week ago by guest
