Cind language documentation

Syntax structures

Blocks

A block of code is a list of instruction executed in given order.
Instructions are placed inside brackets { ... } and separated by the semicolon character ( ; ).
The last instruction of block does not have to be closed by the semicolon,
and also a block itself does not have to be ended by the semicolon.
Every variable declared in the block will be visible only inside that block (see Variables).
The result returned by the block is a result from the last instruction from the list.
Example:

{ int x = 2; { int y = 3; x = x - y; } int y = 1; host.println(x+y); }

The ending bracket of a block ends given instruction, and therefore there is no need to put semicolon after the block statement to end the instruction. To use the value of block anyway, the block can be inserted inside the brackets ( ... ).
Examples:

({ ... ; a }) = b; c = ({ ... ; d }); ({ ... ; x }).get();

Concurrent blocks

Concurrent block {# ... } is a set of concurrently worked threads of code.
Threads are separated by the hash character ( # ), and instructions inside one thread can be separated by the semicolon character ( ; ), just like in the block of code.
The main code execution will wait on concurrent block until all threads ends.
Result of concurrent block is a result from the last ending thread.

Examples:

{# x = 1; y = 2; # x = 2; y = 3; # host.println(x+y); } // start 3 concurrent threads z = {# true # false }; // this starts 2 threads, but don't know which one ends last {#[7] x.hello(); } // this starts 7 threads, each of them makes: x.hello()

For more information about concurrent block see Concurrent expressions.

Separate threads

Separate thread {* ... } is a piece of code, that will be executed as an independent thread.
The thread are being executed concurrently, from the place of declaration, and the main code execution will not wait for its end.
Example:

host.print('A'); {* host.print("B"); } host.print("C");

See Concurrent expressions for more information.

Conditional instruction

Instruction if provides conditional evaluation of code.
The condition should be putted in the brackets ( ... ) and it will be maped to bool type during evaluation.
When condition has true value, then code after condition will be evaluated, and if not, then code after else will be evaluated.
Section else is optional.
Example:

if (a < b) { c = a; } else { d = b+1; }

It is allowed to use conditional structures in assignment. Examples:

a = (if (...) b else c); // a=b or a=c (depending on the condition) (if (...) x else y) = z; // x=z or y=z

Ternary operator ( ? : ) is another way to do conditional evaluation.
An expression a?b:c maps a to bool type, and if it's true, then evaluates to b, otherwise to c (see Operators).

Loop instructions

Instructions while and do-while makes code execution in a loop, as long as given condition is met.
Instruction while checks condition before code execution, and after its done checks it again;
whereas do-while instruction executes code firstly and then checks condition to keep looping.
Condition expresion is evaluated every time when it's checked, and maped to bool type.
Examples:

while (a > b) { x++; } do { x += y; } while (x < z);

For statement

Instruction for makes iteration of given code under given condition.
Syntax of for instruction has brackets ( ... ) and after them code to iterate. The brackets includes three items: some code called initial declaration, condition of iteration and incremental step, all separated by semicolons.
The idea is, that given code will be periodically executed until some condition is met, but with incremented some parameter value in each step.
Example:

for(int i=0; i<10; i++) { host.println(i); }

Actually, instruction for is a kind of instruction while, and can be replaced by its equivalence:

for(a;b;c) d; <---> { a; while(b){ d;c } };

Switch statement

Instruction switch compares a given value with values placed in case pointers, in declared order, and after first match jumps to the code pointed by the case.
Values placed in case pointers will be evaluated in declarated order until first match, rest evaluation will be omitted.
There is also default pointer, which will be choosen if there will be no match with given cases.
Pointer default is optional, and don't need to be declated at the end but can be in any other place inside switch statement.
The break instruction inside switch statemant ends code execution and jumps to the end of switch instruction.
The comparison of values is realized by applying operator == and then maping to bool type
(but in most cases compiler will optimize switch statament to simpler form, which may compares values in different way).

Example:

switch (x) { case 1: host.println("got number 1"); break; case 'a': host.println("got string 'a'"); break; case (y+10): host.println("here x == y+10"); case (y+12): host.println("here x == y+10 or y+12"); break; default: host.println("x has unexpected value"); break; }

Switching type

Instruction switch-typeof works just like switch statement, described above, but it compares types of values instead of values.
The comparision of types is realized by operator >>= (see Operations on types).
There is also a special case unknown: which will be choosen when value has unknown type (e.g. external object has unknown type).

Example:

switch typeof(x) { case a.b.c: host.println("x is an object of class a.b.c"); break; unknown: case float[]: host.println("x has unknown type or it is array of floats"); break; default: host.println("x has other type, than class a.b.c, int, unknown, and float[]"); break; case int: host.println("x is an integer"); break; }

Instruction break

Instruction break breaks current looping statament.
It is allowed inside instructions: while, do-while, for, switch, do-with and try.
Example:

while(x < y) { ... if (x > z) break; // --> it jumps from here ... } ... // <-- to here

Instruction return

Instruction return ends current function, method, thread or accept section.
The value given to instruction return will be the result of called function returned in response.
Example:

fun sum(a,b) { return (a+b); }

Cind programming language 1.0.4

 

Advertisements