Urbiscript
Encyclopedia
urbiscript is a programming language for robotics. It features syntactic support for concurrency and event-based programming. It is a prototype-based
Prototype-based programming
Prototype-based programming is a style of object-oriented programming in which classes are not present, and behavior reuse is performed via a process of cloning existing objects that serve as prototypes. This model can also be known as classless, prototype-oriented or instance-based programming...

 object-oriented
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

 scripting language
Scripting language
A scripting language, script language, or extension language is a programming language that allows control of one or more applications. "Scripts" are distinct from the core code of the application, as they are usually written in a different language and are often created or at least modified by the...

. It is dynamic: name resolution
Name resolution
-In computer languages:Expressions in computer languages can contain identifiers. The semantics of such expressions depend on the entities that the identifiers refer to. The algorithm that determines what an identifier in a given context refers to is part of the language definition.The complexity...

 is performed during the program execution (late binding
Late binding
Late binding is a computer programming mechanism in which the method being called upon an object is looked up by name at runtime. This is informally known as duck typing or name binding....

); slots (member variables
Member variable
In object-oriented programming, a member variable is a variable that is associated with a specific class, and accessible for all its methods. If there is only one copy of the variable shared with all instances of the class, it is called a class variable or static member variable...

) can be added/removed at runtime, and even prototypes (superclasses) of an object can be changed at runtime.

Memory management
Memory management
Memory management is the act of managing computer memory. The essential requirement of memory management is to provide ways to dynamically allocate portions of memory to programs at their request, and freeing it for reuse when no longer needed. This is critical to the computer system.Several...

 is performed by reference counting
Reference counting
In computer science, reference counting is a technique of storing the number of references, pointers, or handles to a resource such as an object, block of memory, disk space or other resource...

.

Tightly bound to the Urbi platform
URBI
Urbi is an open source cross-platform software platform in C++ used to develop applications for robotics and complex systems. Urbi is based on the UObject distributed C++ component architecture. It also includes the urbiscript orchestration language which is a parallel and event-driven script...

 it supports seamless integration of C++/Java components.

Inspiration

From the syntactical point of view, urbiscript belongs to the C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

-family of programming languages.

Its prototype-based object-oriented design was influenced by the Self and the Io
Io (programming language)
Io is a pure object-oriented programming language inspired by Smalltalk, Self, Lua, Lisp, Act1, and NewtonScript. Io has a prototype-based object model similar to the ones in Self and NewtonScript, eliminating the distinction between instance and class. Like Smalltalk, everything is an object and...

 programming languages.

It is designed to program, but also interact with robots; as such, it is influenced by Unix shells and other languages that provide a read-eval-print loop style interactive toplevel. However, contrary to others, there is no prompt for user input but answers from the system are prefixed by a timestamp (in milliseconds) between square brackets:

1 + 1; sleep(1s); 1 + 2 * 3;
[00005420] 2
[00006420] 7

Sequential statements and control flow

urbiscript statements include (among others):
  • The if statement, which conditionally executes a block of code, along with else.
  • The traditional for statement
    For loop
    In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement....

    , as in C which iterates over an iterable object, capturing each element to a local variable for use by the attached block.
  • Another for statement, which iterates over an iterable object, capturing each element to a local variable for use by the attached block.
  • The while statement
    While loop
    In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement....

    , which executes a block of code as long as its condition is true.
  • The try statement
    Exception handling
    Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions, special conditions that change the normal flow of program execution....

    , which allows exceptions thrown in its attached code block to be caught and handled by catch clauses. An optional else clause is run if no exception was thrown. Clean-up code can be guaranteed to be run in every case when given in a finally-clause.
  • The assert statement, used during debugging to check for conditions that ought to apply. urbiscript also feature assert blocks, which can be used to factor several assert statements.


Actually, contrary to most C-like languages and despite what the syntax suggests, statements "have a value", and therefore are expressions, provided they are embedded in braces:
var status = { if (closed) "closed" else "open" };
var pass = { try { foo } catch { false } else { true } };

Concurrent statements and control flow

In urbiscript, some control-flow constructs come in several "flavors": two types of sequential composition, and two types of concurrent composition. Under the hood, concurrency is implemented using coroutines
Coroutine
Coroutines are computer program components that generalize subroutines to allow multiple entry points for suspending and resuming execution at certain locations...

.

Statement composition

Like in C, the semicolon denotes sequential composition: a;b stands for "run statement a then run statement b. Other tasks may be run between a and b. Another statement separator, pipe, denotes "tight sequential composition": no other task can be run between a and b in a|b.

Similarly urbiscript features two means to compose statements concurrently. With a,b, first a is run, and at some point b will be --- possibly while a is still running. This is very similar to the & operator in Unix shells. Alternatively, with a&b, both a and b are started together; in interactive sessions, this means that a won't be run until b is fully entered and properly followed by either a ; or a ,.

Scopes are boundaries for backgrounded jobs, as demonstrated in the following example:

{
{ sleep(2s); echo(2) },
{ sleep(1s); echo(1) },
};
echo(3);
[00012451] *** 1
[00013447] *** 2
[00013447] *** 3

Concurrent flavors of sequential constructs

Most looping constructs in urbiscript come in several "flavors", which are based on the four statement separators: ;, |, ,, and &.

For instance

// This is actually "for;".
for (var i : [0, 1, 2])
{
echo(i);
echo(i ** 2);
};

displays

[00002919] *** 0
[00002921] *** 0
[00002921] *** 1
[00002922] *** 1
[00002922] *** 2
[00002922] *** 4

i.e., the loop bodies are not executed sequentially, while the for& keyword runs the loop bodies concurrently:

for& (var i : [0, 1, 2])
{
echo(i);
echo(i ** 2);
};
[00021680] *** 0
[00021680] *** 1
[00021680] *** 2
[00021682] *** 0
[00021682] *** 1
[00021682] *** 4

Event-based programming

Aiming at the development of portable robotic applications , urbiscript relies on specific syntactic constructs to specify reactive behaviors such as "go to the charging dock when the battery is low", "play a friendly sound when a known face is recognized", or "stop when an obstacle is detected".

Explicit event handling

Event handling goes into three steps. First, define an event

var e = Event.new;

Second, specify event handlers

at (e?)
echo("received event e");

Third, "emit" this event

e!;
[00014333] *** received event e

Events can have payloads, and event handlers enjoy pattern matching on the payload:

at (e?(1, var x) if x % 2 0)
echo("received event e(1, %s)" % x);
e!(1, 1);
[00014336] *** received event e
e!(1, 2);
[00014336] *** received event e
[00014336] *** received event e(1, 2)

Implicit events

The urbiscript language also allows to monitor expressions:

at (batteryLevel <= 0.2)
robot.goToChargingDock;

The following example demonstrates the feature:

var x = 0;
[00002165] 0
var y = 0;
[00002166] 0
var z = 0;
[00002167] 0
at (x + y z)
echo("%s + %s

%s" % [x, y, z]);
[00002168] *** 0 + 0

0
x = 1;
[00002169] 1
z = 1;
[00002170] 1
[00002170] *** 1 + 0

1

External links
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK