Programming Language user guide

1. Overview

This is a visual programming language with flowchart like looking. Program flow is represented by arrows and commands or operations are contained in the two types of blocks: condition and procedure.

Content (statements inside) of each block defines what the block will do. These statements are entered/edited by typing. The blocks are linked together to form a program.

2. General structure

Program is what to be downloaded and runs on Ansteron Board. A program can have one or more functions inside it. One of the functions is main function, which is start point of the program. Each function is identified by its name.

Function is a routine that will do a certain task or operation. A function can optionally takes inputs and returns a value. A function can be called by its name, from other functions and within itself as well(recursive). Function contains conditions and procedures inside it.

Procedure is a list of operations to be executed.

Condition contains a statement that will result in true or false. Program flow will switch according to that result.

3. Elements if the language

Function Procedure Condition Names Keywords
Data types Variables Operators Definition Comment

Function

Each function must have a name to identify itself. The name must be unique within a program. When a function is created, a statement is entered into its header to specify its name, inputs that it will take (if any) and what data it will return (if any).

Statement for a function must be in the following form:

keyword function_name(keyword name_of_input_1, keyword name_of_input_2,...);

The first keyword define what data type the function will return. If there is no data return, keyword void must be used. Similarly, each input of the function must have type and name. If function doesn't take any input, keyword void should be entered between parentheses (or leaving empty is also acceptable).

Procedures

Procedure is block that contains a list of statements, each one is a command or operation or function call. Every statement must end with a semicolon.

Condition

Inside condition is a statement whose result or value will determine which of the two branches to take control flow. There is only one statement in each condition.

If the statement inside condition results to a numeric value instead of logical, that value will be compared with zero. It is true if result is different than zero, false otherwise.

Names

Names are used to identify functions and variables (giving each of them a name). Name can be any word except keywords (reserved for other use). Requirements for a name include:

  • Unique within a program.
  • No space between words. If name have more than one word, use low dash (_) between words (recommended) or make the first letter of each word upper case.
  • Names are case-sensitive ("Name", "NAME" and "nAme" are all different).

Names should be short and expressive. If a name is enter incorrectly or used before given to anything, an error says "undefined identifier" will show up. Note that names are also called identifiers.

Keywords

They are words that were reserved and cannot be used as name. They are all lower case and highlighted blue. There are 8 keywords, char, int, long, void and ptr are used for data types,return is used as command and signed, unsigned are reserved for future use.

Keyword return is used to return to calling function with or without data.

Data types

They are storage units of data values. Keywords are used to specify data type of function and variable.

KeywordsTypeRangeMemory use
charA letter(alphabet) or small integer0-255 1 byte
intmedium integer0-65535 2 bytes
longlarge integer0-4294967295 4 bytes
voidNo data, used to define functions--
char ptrPointer to a char value0-64K 2 bytes
int ptrPointer to a int value0-64K 2 bytes
long ptrPointer to a long value0-64K 2 bytes

All values are actually whole numbers. Characters don't formally exist here but they are coded by numbers according to ASCII standard.

When doing mathematic operations between different types, make sure that results will not overflow. Otherwise, result may be incorrect.

Pointer is a low level concept (also known as memory address). They are frequently passed to function so that returned data can be written to a variable prepared for it. To get the pointer to a variable, use "@" in front. This is a very powerful and efficient tool but must be used carefully to avoid logical bugs.

Variables

The same as mathematic variables, they hold a value that can be any number within their ranges. In electronic term, they are memory cells. Variables must be defined before they can be used.

There are local and global variables. Local variables are only accessible within a function. Global variable are accessible to all functions of the program. Local variables can be created in any procedure of the function, before they are used. Global variables are defined in Global variables area of Preprocessor tab.

Each variable have a name and data type which is the data it holds. To define one variable or more of the same type, use the following syntax:

keyword name_of_variable_1, name_of_variable_2,...;

Example:

When only one variable is defined, an initial value (must be a constant number) can be assigned to it in the same line.

When doing operations between variables of different types, watch for overflow happens when result is larger than the range of variable. Such cases may give incorrect results.

Type casting is a method to instruct compiler to temporally treats a variable as a different type than it is to avoid data lost during calculation. This is done by placing type keyword in a pair of parentheses in front of variable. Note that type casting does not modify data of variable.

Array is a group of variables that have the same type locate next to each others and any individual can be accessed by array name and index. To define an array:

keyword name_of_array[size_of_array];

Note that size of array must be a constant and should not larger than available memory.

Operators

Most basic mathematic operators are supported, including Add(+), subtract(-), multiply(*),divide(/) and modulus(%).

Logical operators: AND(&&), OR(||), Compare if equal(==), different (!=), larger(>), less(<), larger or equal (>=), less or equal(<=), Not (!).

Bitwise operators: AND(&), OR(|), NOT(!), XOR(^), left shift (<<), right shift (>>).

Other operators: assign(=), data at pointer value (@), address of (&).

Definition

A definition is name of a value or other name. Giving a name to a value to make it easier to remember, especially different options for input of a function. Giving a name to another name to allow two name to mean the same thing.

To define a name, enter statements in the following format into preprocessor script area, in Preprocessor tab of Ansteron IDE.

#define name [equivalent]

Each definition takes one line. Basically, everything after "name" in the line will replace "name" wherever it occurs in the program code.

It is a convention that all definitions of a constant numbers are typed in upper case.

Comment

Comment are text entered between code lines to provide information or explanations about the code. It is used to enhance readability of the program. Each comment begins with two slashes "//" and it takes the rest of a line.

4. Notes for new programmers

Data overflow

Result of a calculation can be incorrect when data was lost along the way. In the example below, when b was multiply by 10, the result was 1500 which is out of range for char variable and therefore, the final result is incorrect (viewed on Serial Terminal).

To correct this, make b an int variable as well or use type casting, telling compiler to treat b as an int for this calculation.

Working strings with characters

Strings and characters are not formally defined as data types. In computer-like environment, characters are coded by numbers and strings are arrays of characters one next to another. Characters are coded according to ASCII standard. Each character will fit into a char data type, which equals 1 byte. The end of a string is usually indicated by a zero value (called zero-terminated string).

When typing, one character in between two single quotes is understand as coded value for a character. A string in between two double quotes is understand as a pointer to a location on memory that hold the array of characters.