PocketC Language updated for B1.29

The language is very similar to standard C in syntax, but has a different feature set. Most notably, there are no pointers, no structs, and no preprocessor. A string data type has been added, and values are somewhat loosely typed. The next section is for those who are already somewhat familiar with C.

Brief guide to the PocketC language


Data Types

There are four types in PocketC: integers (int), floating point numbers (float), characters (char), strings (string), and single-dimensional arrays.

Expressions

An expression consists of any number of variables, function calls, and constants of any type, joined by operators and parentheses. Thus, the following expressions are valid:

3 + 5

6.7 + 23

"Hello " + "World"

5*3 + foobar()

When an expression contains values of different types, the two values are first cast to the same type, and then the operator applied. For example in the following expression:

 5.7 + 8

 The 8 is first cast to a float (8.0), and the two numbers are added (5.7 + 8.0). This becomes slightly more interesting when we use strings. For example:

 "The result is " + (5 + 8)

 5 is first added to 8, resulting in 13. The 13 is then converted to a string, and appended to "The result is " resulting in "The result is 13". However,

 "The result is " + 5 + 8

 would result in "The result is 58", since the the 5 is first converted to a string, then appended to "The result is "...

 Of course, explicit casting is possible:

 56 + (int)"7"

 evaluates to 63.

 The following operators are provided (in order of precedence, with associativity):

Operator Assoc Description
= right assigns the value of the expression on the right to the variable on the left.
|| left logical ‘or’, returns 0 if false, 1 if true
&& left logical ‘and’
== != < <= > >= left relational operators
+ - left addition, subtraction
* / % left multiplication, division, modulus
- ! ++ -- left unary operators

Note: No shortcut logic is performed on the operands of || and &&
Note2: The +=, -=, *=, etc. operators are not supported.

Variables

Statements

The following statements are fully supported: for, while, do, switch, case, break, continue, if, then, return.
Note: for requires a condition expression (i.e. for (;;) is not legal, instead, use for (;1;) or while (1))

Functions

Preprocessor

Example:

// My large applet

#include "ctrlheader.h"

#include "appheader.pc"

#define BUTTON	"button"

#define PI	3.1415926

#define VERSION	100

main() {

// use predefined value in other header files

// call functions defined in other files

}

Special characters

There are two ways to add special characters to a string. The first is by appending them by number, such as:

str = "Here is a neat little square: " + (char)149;

The other method is through using escape sequences. The following escape sequences are supported:

Escape sequence \\ \' \" \n \r \t \x
Interpretation \ ' " newline return char tab character specified by the following two hex digits. Example: '\x95' is the block character (decimal 149)