The Chirp-Scribbler Lite Language Specification Li Xu |
Chirp-Scribbler Lite is a simplified version of the Chirp language. The Chirp language is designed as a simple C-like language to program embedded robot controllers. As the hardware features of each robot device are highly specific to the individual robot platform, to support multiple types of robots, Chirp provides a set of common language constructs along with specialized data types and functions to program each robot platform. Chirp-Scribbler Lite is the Chirp language variant for the Scribbler robots. This document describes the language constructs and usage of Chirp-Scribbler Lite.
Chirp-Scribbler Lite has a syntax similar to C. A Chirp-Scribbler Lite program consists of the data section and code section. The data section has global variable definitions which define the type and name of global variables. Chirp-Scribbler Lite has only global variables, the type of the variables can be either int or byte type. The code section includes Chirp-Scribbler Lite statements, such as assignments, if-else conditionals, system function calls and loop statements.
Chirp-Scribbler Lite is case sensitive, eg., Foo and foo are distinct names. Chirp-Scribbler Lite keywords are reserved – the programmer cannot use a Chirp-Scribbler Lite keyword as the name of a variable. The valid keywords are: int, byte, if, else, loop, while, until, for, break.
Chirp-Scribbler Lite also uses the following special characters:
The Chirp-Scribbler Lite grammar in Figure 1 specifies the usage of the special characters.
Chirp-Scribbler Lite accepts C/C++ style comments: comments are delimited by matching /* and */ for multi-line comments, or follow // for single line comments.
Chirp-Scribbler Lite identifiers start with a letter or the underscore character which is then followed by letter, underscore or digit characters. Chirp-Scribbler Lite identifiers are used for variable and system function names. Chirp-Scribbler Lite constants are integers and string constants. String constants are quoted by double quotes and consist of character sequence of non-quote characters.
| ChirpLetter | → | a | b | c | ... | z | A | B | ... | Z | _ |
| Digit | → | 0 | 1 | 2 | ... | 9 |
| ID | → | ChirpLetter ( ChirpLetter | Digit )* |
| STRING | → | " ( Char )* " |
Chirp-Scribbler Lite supports the following boolean, relational and arithmetic operators:
The full syntax of Chirp-Scribbler Lite is shown in Figure 1. The terminal tokens are in the typewriter font. The meta symbol * indicates the unit can repeat zero or multiple times, and ? indicates the unit can repeat zero or one time.
program → data_section code_section data_section → ( var_def )* var_def → data_type ID ; data_type → int | byte code_section → ( statement )* statement → assignment_stmt ; | if_else_stmt | loop_stmt | break_stmt ; | function_call ; | block assignment_stmt → ID = expression if_else_stmt → if ( expression ) statement ( else statement )? loop_stmt → loop ( while ( expression ) )? block ( until ( expression ) )? | for ID ( expression : expression (: expression )? ) block break_stmt → break function_call → ID ( . ID )* ( ( expression ( , expression )* )? ) block → { ( statement )* } expression → ! relational_expr | relational_expr ( ( && | || ) relational_expr )* relational_expression → arithmetic_expression ( ( == | != | <= | < | >= | > ) arithmetic_expression )? arithmetic_expression → term ( ( + | - ) term )* term → factor ( ( * | / | % ) factor )* factor → ID | INT | STRING | ( expression )
Figure 1: Chirp-Scribbler Lite Grammar
Chirp-Scribbler Lite has int and byte data types, which correspond to the 16-bit WORD and 8-bit BYTE in Scribbler PBASIC. Similar to C, user can use integer types to represent boolean values: 0 represents boolean false and any non-zero value represents boolean true.
Chirp-Scribbler Lite allows only global variables.
Chirp-Scribbler Lite has the following statements:
Chirp-Scribbler Lite expressions compute values of the basic types. As boolean values are represented by integer values, Chirp-Scribbler Lite defines boolean and, or and not operator to evaluate boolean values. The relational operators are ==, !=, <=, <, >=, >. Chirp-Scribbler Lite also supports arithmetic operations.
To control Scribbler I/O hardware, Chirp-Scribbler Lite provides the following System.Scribbler built-in functions:
This section shows several code examples of using Chirp-Scribbler Lite to program the Scribbler robot. A Chirp-Scribbler Lite reference compiler is implemented in Java. To run an example program, type java csc.cslc prog.csl to use the cslc reference compiler to compile prog.csl into .bs2 code and use the Scribbler StampEditor to compile and upload the target binary code onto Scribbler.
int i;
int led1;
int led2;
int led3;
for i (1:100) {
led1 = i%2;
led2 = (i/2)%2;
led3 = (i/4)%3;
System.Scribbler.setLED(led3, led2, led1);
System.Scribbler.print("i is: ", i);
System.Scribbler.wait(1000);
}
System.Scribbler.print("now i am done");
int a;
int l1;
int l2;
int l3;
loop {
System.Scribbler.senseLight(l1, l2, l3);
System.Scribbler.print("light: ", l1, ", ", l2, ", ", l3);
System.Scribbler.wait(500);
System.Scribbler.senseStall(a);
if (a) {
System.Scribbler.print("stalled: a=", a);
} else {
System.Scribbler.print("not stalled: a=", a);
}
}
loop {
System.Scribbler.moveForward(3, 3);
System.Scribbler.wait(1000);
System.Scribbler.stop();
System.Scribbler.moveBackward(3, 3);
System.Scribbler.wait(1000);
System.Scribbler.stop();
System.Scribbler.print("stopped");
System.Scribbler.wait(3000);
}
This document was translated from LATEX by HEVEA.