Cool language Notes

1
i : IO <- new IO;

new 用于创建 object , 后一个IO是 object的类型。<- 似乎是赋值的意思。

let ... in创建local variable.

if then else statement.

The last statement of the block is the value of the block. 这个和perl中一样.

= 在COOL中是比较operator.

The special value void is a member of all types and is used as the default initialzation for variable. There is no name for void.

isvoid expr, which tests whether a value is void.

A dispatch to or case on void generates a runtime error.

A statement o an expression block has to be included in its own set of curly braces. 如:

1
main(): Int { { i.out_string("Hello World!"); 1; } };

第一个 curly braces is the body of the main program, and the second braces means a block of expression.

6 Method

The type of the method body must conform to th declared return type.

7 Expressions

7.2 Identifiers

It is an error to assign to self or bind self in a let, a case, or as a formal parameter.

It is also illegal to have attributes named self.

7.4 Dispatch

就是 method call.

The value of the expression is the value returned by the method invocation.

Inferring accurate static types for dispatch expression is what justifies including SELF_TYPE in the Cool type system.

Three ways to dispatch:

- <expr>.<id>(<expr>,...,<expr>)
- <id>(<expr>,...,<expr>)
- <expr>@<type>.id(<expr>,...,<expr>)

7.5 Conditionals

1
if <expr> then <expr> else <expr> fi

The predicate is evaluated first, if the predicate is true, then the then branch is evaluated. If the predicate is false, then the else branch is evaluated.

7.6 Loops

1
while <expr> loop <expr> pool

If the predicate is false, the loop terminates and void is returned.

7.7 Blocks

1
{ <expr>; ... <expr>; }

The static type of a block is the static type of the last expression.

Semi-colons are used as terminators in lists of expressions and not as expression separators. Semi-colons also terminate other Cool constructs. 这句话的意思是,每一个 semicolon 表示一个 expression 的结束,然后会按顺序执行下一个 expression.

Block 里面的每一个 expression 都要有 semicolon, body 里面只有一个 expression 且不用 semicolon.

7.8 Let

1
let <id1>: <type1> [ <- <expr> ], ..., <idn>: <typen> [ <- <exprn> ] in <expr>

The value of the let is the value of the body.

If an identifier is defined multiple times in a let, later bindings hide earlier ones.

7.9 Case

1
2
3
4
5
6
7
case <expr0> of 
<id1>: <type1> => <expr1>;

...

<idn>: <typen> => <exprn>;
esac

The result of the case is the value of .

case 是根据<expr0><idn>type 来决定是否执行。

7.10 New

1
isvoid expr

7.12 Arithmetic and Comparison Operations

Four binary arithmetic operations: +, -, *, /.

1
expr1 <op> expr2

The static types of the two sub-expression must be Int. The static type of the expression is Int. Cool has only integer division.

Cool has only three comparison operations: <, <=, =.

The expression ~<expr> is the integer complement of .

The expression not <expr> is the boolean complement of .

8 Basic Classes

8.1 Object

The Object class is the root of the inheritance graph.

8.2 IO

A class can make use of the methods in the IO class by inheriting from IO. It is an error to redifine the IO class.

8.3 Int

The default initilization for variables of type Int is 0(not void).

8.4 String

The default initilization for variables of type String is “”(not void).

8.5 Bool

The Bool class provides true and dault. The default initilization for variables of type Bool is false(not void).

9 Main Class

Every program must have a class Main. The Main class must have a method main that takes no formal parameters. The main method must be defined in class Main.

10 Lexical Structure

The lexical units of Cool are integers, type identifiers, object identifiers, special notation, strings, key-words, and white space.

10.2 Strings

1
2
"This \
is OK"

A string may not contain EOF, A string may not contain the null(character \0).

10.3 Comments

There are two forms of comments:

 --
(*...*)

11 Cool Syntax

12 Type Rules

12.1 Type Environments

Three parts: a method environment M, an object environment O, and the name of the current class in which the expression appears. The method environment and object environment are both functions(also called mappings).

12.2 Type Checking Rules

13 Operational Semantics

The context has three components:
- an environment
- a store
- a self object

13.1 Environment and the Store

An environment is a mapping of variable identifiers to locations.

To describe the environment:

1
E = [a:l1,b:l2]

This environment maps a to location l1, and b to location l2.

The store(memory) maps location to values. Intuitively, a store tells us what value is stored in a given memory location.

A store is similar to an environment:

1
S = [l1 -> 55, l2 -> 77]

13.2 Syntax for Cool Objects

Every Cool value is an object.

13.3 Class definitions

Two mappings, called class and implementation, are associated with class definitions.

The default initializing for a variable or attribute is the default of its type.


Cool language Notes
http://example.com/2022/07/29/Cool-language-Notes/
作者
Jie
发布于
2022年7月29日
许可协议