📄 language-full.html
字号:
<html>
<head>
<title>PocketC Language</title>
</head>
<body bgcolor="ffffff">
<h1>PocketC Language</h1>
<p>First, PocketC is a case sensitive language, meaning that typing <em>word</em> is not
the same as type <em>Word</em>.</p>
<p>There are three elements to a PocketC applet: the title line, the global variables, and
the functions. </p>
<h2>The Title</h2>
<p>The title is by far the easiest part of the PocketC language. The first line of your
applet is two slashes followed by the name of your applet. Example:</p>
<p><code>// My Applet</code></p>
<p>This is also known as a <b>comment</b>. Anytime the compiler finds '<code>//</code>' in
your applet, it will ignore the rest of the line that it is on. This allows you to place
explanatory text in your applet. There is one other way to put a comment in your applet,
by surrounding the text with '<code>/* */</code>'. This method allows you to spread a
comment out over several lines. Example:</p>
<pre>/* This is a multi-line comment.
All the text between the
asterisks is ignored */</pre>
<p>Multi-line comments are not nestable. In other words: </p>
<pre>/* comment1 /* comment2 */ a=b+c; */</pre>
<p>is NOT valid. </p>
<h2>The Global Variables</h2>
<p>Variables are the things that are used to store values in a program. There are four
types of variables in PocketC: </p>
<table border="1">
<tr>
<th>Type</th>
<th>Name</th>
<th>Example</th>
</tr>
<tr>
<td>integer (32-bit, signed)</td>
<td><code>int</code></td>
<td><code>1, 2, 5, -789, 452349</code></td>
</tr>
<tr>
<td>floating point (32-bit)</td>
<td><code>float</code></td>
<td><code>-1.2, 3.141592, 5.7e-4</code></td>
</tr>
<tr>
<td>characters (8-bit, signed)</td>
<td><code>char</code></td>
<td><code>'a', 'b', '#'</code></td>
</tr>
<tr>
<td>strings</td>
<td><code>string</code></td>
<td><code>"Bob" "Katie" "Hello"</code></td>
</tr>
<tr>
<td>pointers</td>
<td><code>pointer</code></td>
<td>discussed later</td>
</tr>
</table>
<p><strong>Note:</strong> String constants may only be 1023 characters. To strore a longer
string in a variable, use addition: <code>str = "long1..." +
"long2...";</code> </p>
<p>Variables are declared like this:<br>
<i>variable-type name</i>[,<i>name</i>...];</p>
<p>Here are a few examples: </p>
<pre><b>int</b> myInteger, row, column;
<b>string</b> name;
<b>float</b> pi;
<b>char</b> c, last, first;
<b>pointer</b> ptr;</pre>
<p>It is also possible to have an <i>array</i> of values. An array is a list of values
that are stored in one variable. Arrays are declared like normal variables except that the
variable name is followed by '<code>[<i>size</i>]</code>' where <i>size</i> is the number
of item that the variable can hold. A declaration might look like this: </p>
<pre><b>int</b> values[10];
<b>string</b> names[7];</pre>
<p>Of course, arrays and normal variables can be declared together: </p>
<pre><b>int</b> row, values[10], column;
<b>string</b> name, colors[8];</pre>
<p>You can also give default values to the variables: </p>
<pre><b>int</b> nine = 9, eight = 8, zero;
<b>string</b> days[7] = { "Sun", "Mon", "Tues" };</pre>
<p>In the case of arrays, the initials values must be in braces and separated by
commas. If the number of values in the initializer list is less than the length
of the array, then the uninitialized elements have default values. For the <code>days</code>
array above, the last 4 members of <code>days</code> are the empty string (""). </p>
<p>We'll discuss variables a little more later. </p>
<h2>The Functions</h2>
<p>Functions are the most important part of a program because they contain the actual
instructions that make a program useful. All functions have a name and a parameter list
(which may be empty) and are declared like the:<br>
<code><i>func-name</i>(</code>[<code><i>param-type param-name</i>,...</code>]<code>) { <i>statements</i>
}</code></p>
<p>Statements are discussed later, but for now, here are a few examples: </p>
<pre>area(int width, int height) {
return width * height;
}
square(float x) {
return x * x;
}
five() {
return 5;
}</pre>
<p>There is one special function name which all programs must have: <code>main</code>. The
<code>main</code> function is the function which is called first in your program. When the
<code>main</code> function exits, the program terminates. The <code>main</code> function
must be declared with no paramters: </p>
<pre>// My Applet
main() {
puts("Hello World");
}</pre>
<p>Functions can also have local variables, which are variables that can only be accessed
within the function that declares them. Global variables, however, can be accessed from
anywhere. Local variables are declared in the same way that global variables are except
that they immediately follow the opening brace of a function: </p>
<pre>// My Applet
main() {
string localString;
localString = "Hello World";
puts(localString);
}</pre>
<p><b>Note:</b> If you are creating large arrays, it is best to make them global
variables instead of local variable is possible. </p>
<p>Before we go any further, we need to talk a little bit about expressions. </p>
<h3>Expressions</h3>
<p>An expression is any number of constants, variables, and function calls connected by
operators and parentheses.</p>
<p>A <b>constant</b> is any value that is directly entered into the program, such as: <code>5
5.4 'a' "String"</code></p>
<p>A value stored in a <b>variable</b> can be accessed by just typing its name: <code>myInteger
name</code><br>
However, if that variable is an array, each value must be accessed individually by index.
The valid indices for a given array are 0 to <i>n</i>-1 where <i>n</i> is the number of
values in the array. So an array declared: </p>
<pre>string names[4]</pre>
<p>can be accessed like so: </p>
<pre>names[0] = "first name";
names[1] = "second name";
names[2] = "third name";
names[3] = "fourth name";</pre>
<p>A <b>function call</b> consists of the name of a function, followed by an open paren,
the parameter list, and a closing paren: </p>
<pre>area(5, 7);
square(8.9);
clear();
text(30, 55, "Game Over");</pre>
<p>A function can only be called after it is defined. If you want to call a function
before defining it, you can use a function prototype. A prototype of a function is a
global line (not within another function) which states the name and parameters of a
function followed by a semicolon:</p>
<pre>area(int x, int y);
square(float); // the use of variable names is optional in a declaration</pre>
<p>These three basic elements can be combined with operators: </p>
<pre>5 + 7 - area(12, 34);
square(5) * pi;
"Hello, " + "World";</pre>
<p>Of course, function calls can have expressions in them as well: </p>
<pre>area(6+3, 8*9);
area(8 * square(4), 7);</pre>
<h3>Assignment</h3>
<p>Variable assignment is actually just another form of expression. Assignment is done in
one of two ways--for a normal variable:<br>
<i>name</i> = <i>expression</i></p>
<p>and for an array:<br>
<i>name</i>[<i>index-expression</i>] = <i>expression</i></p>
<p>Here are a few examples: </p>
<pre>int myInt, numbers[3];
string myString;
...
myInt = 8;
myString = "Animaniacs";
numbers[0] = myInt + 5;
numbers[2] = numbers[0] * 8;</pre>
<p>However, since PocketC is loosely typed, any type of value can be assigned to any type
of variable and the value will be automatically converted: </p>
<pre>myString = 95; // The value of myString is now "95"
numbers[1] = "78"; // The value of numbers[1] is now 78;
numbers["2"] = "2"; // Another neat trick. numbers[2] is now 2</pre>
<p>Now, what are all the operators that can be used in an expression, and what is their
associativity? Good question. </p>
<h3>Operators</h3>
<p>The following table is in order of precedence, lowest first.</p>
<table BORDER="1" CELLSPACING="1" WIDTH="623">
<tr>
<th VALIGN="middle"><p ALIGN="CENTER">Operator</th>
<th VALIGN="middle"><p ALIGN="CENTER">Assoc</th>
<th VALIGN="middle"><p ALIGN="CENTER">Description</th>
</tr>
<tr>
<td VALIGN="MIDDLE"><p ALIGN="CENTER">=</td>
<td VALIGN="MIDDLE">right</td>
<td VALIGN="MIDDLE">assigns the value of the expression on the right to the variable on
the left. Evaluates to the expression on the right.</td>
</tr>
<tr>
<td VALIGN="MIDDLE"><p ALIGN="CENTER">||</td>
<td VALIGN="MIDDLE">left</td>
<td VALIGN="MIDDLE">logical 'or', evaluates to 0 if false, 1 if true</td>
</tr>
<tr>
<td VALIGN="MIDDLE"><p ALIGN="CENTER">&&</td>
<td VALIGN="MIDDLE">left</td>
<td VALIGN="MIDDLE">logical 'and'</td>
</tr>
<tr>
<td VALIGN="MIDDLE"><p ALIGN="CENTER">|</td>
<td VALIGN="MIDDLE">left</td>
<td VALIGN="MIDDLE">bitwise 'or'</td>
</tr>
<tr>
<td VALIGN="MIDDLE"><p ALIGN="CENTER">^</td>
<td VALIGN="MIDDLE">left</td>
<td VALIGN="MIDDLE">bitwise 'xor'</td>
</tr>
<tr>
<td VALIGN="MIDDLE"><p ALIGN="CENTER">&</td>
<td VALIGN="MIDDLE">left</td>
<td VALIGN="MIDDLE">bitwise 'and'</td>
</tr>
<tr>
<td VALIGN="MIDDLE"><p ALIGN="CENTER">== != < <= > >=</td>
<td VALIGN="MIDDLE">left</td>
<td VALIGN="MIDDLE">relational operators. == (equal), != (not equal), <= (less than or
equal), >= (greater than or equal). These evaluate to 1 if the expression is true, 0
otherwise</td>
</tr>
<tr>
<td><< >></td>
<td>left</td>
<td>bitwise shift operators. The operands must be int or char.</td>
</tr>
<tr>
<td VALIGN="MIDDLE"><p ALIGN="CENTER">+ -</td>
<td VALIGN="MIDDLE">left</td>
<td VALIGN="MIDDLE">addition, subtraction (subtraction cannot be used with a string
argument)</td>
</tr>
<tr>
<td VALIGN="MIDDLE"><p ALIGN="CENTER">* / %</td>
<td VALIGN="MIDDLE">left</td>
<td VALIGN="MIDDLE">multiplication, division, modulus (cannot be used with strings, nor
can modulus be used with floats)</td>
</tr>
<tr>
<td VALIGN="MIDDLE"><p ALIGN="CENTER">- ! ++ - - ~ * [] () & @[]</td>
<td VALIGN="MIDDLE">left</td>
<td VALIGN="MIDDLE">- (negation), ! (logical 'not'), ++ (increment), -- (decrement), ~
(bitwise neg), [] (array subscript), () (function pointer dereference), & (address of
), and @[] (string character accessor) Of these, only the logical 'not' and
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -