Expression in D language (2)

xiaoxiao2021-03-06  50

In expression

Relationship expression in shift expression

Relevance in shiftexpression

You can detect if an element is in an associated array:

Int foo [char []];

......

IF ("Hello" in foo)

......

IN expression same relationship expression

<, <=, Etc. has the same priority.

Displacement expression

Displacement expression << and expressions

Displacement expression >> and expressions

Displacement expression >>> and expressions

Shiftexpression << AddExpression

Shiftexpression >> AddExpression

Shiftexpression >>> AddExpression

The operand must be an integer type and a commonly used integer increase will be used. The type of result is the type after the left operating capacity is increased. The result of the result is the value obtained by the left operating number of mobile right operands.

<< is right left, >> is a symbol right shift (translation: also called count). >>> is no symbol right shift. (Translation: also called logically right)

If the number of bits to be moved exceeds the number of bits of left operating, it will be considered illegal:

INT C;

C << 33; error

Expressive

And expressions volume list

And expressions

And expressions ~

AddExpression MULEXPRESSION

AddExpression - MULEXPRESSION

AddExpression ~ MULEXPRESSION

If the operand is an integer type, an integer is applied, and then the common type of arithmetic conversion will be improved.

If the operands are floating point types, the other operand will be implicitly converted to a floating point type, and then the common type of arithmetic conversion is improved to them.

If the operator is or -, the first operand is a pointer, and the second operand is an integer type. The type of the result is the type of the first operand, and the value of the result is a pointer plus (or subtract) The second operand multiplies the value obtained by the size of the type of pointer.

If the type referred to by the pointer is bit, the value of the result is the second operand divided by 8 after 8 and the pointer is added. If the second operand cannot be divided by 8, it will be considered illegal.

Bit * p;

P = 1; // error, 1% 8 is not equal to zero

P = 8; // ok

If the second operand is a pointer, the first operand is an integer type, and the operator is

, Will make a pointer operation in the way above, but only the order of operands is in turn.

The floating point operands are not combined.

Accumulate

Complexed by the format * one yuan express

Complexed format / one yuan express

Complexed by volume% of one yuan express

MULEXPRESSION * UnaryExpression

MULEXPIESSION / UNARYEXPRESSION

MULEXPRESSION% UnaryExpression

The operand must be an arithmetic type. The integer increase will be performed first, and then the common type of arithmetic conversion will be improved.

For integer operands, *, / and% correspond to multiplication, division and mode operation. For the pending, overflows are ignored, and the result will be simply intercepting the integer type. If the number of right operations except or take the operation is 0, a DivideByzero exception will be thrown.

For floating point operations, various computments are the same as the corresponding IEEE 754 floating point operation. Template can only be used for real types and cannot be used for imaginary or complex types.

The formulation of floating point numbers is not combined. One yuan expressions

& One dollar expression

one yuan expressions

- a metal expression

* One yuan expressions

- a metal expression

1 yuan express

! One yuan expressions

~ One yuan expression

DELETE 1 yuan expressions

New expression

CAST (type) one yuan expressions

(Type). Mark

(Expression)

& UnaryExpression

unaryExpression

- UnaryExpression

* UnaryExpression

- UnaryExpression

UnaryExpression

! Unaryexpression

~ UnaryExpression

Delete UnaryExpression

NewExpression

Cast (Type) UnaryExpression

(TYPE). Identifier

(Expression)

The New Expression New Expression is used to assign memory on the garbage collection stack (default) or using the assigner specified by the class.

When allocating memory for multi-dimensional array, the declaration is based on the same order in the same order as the read rear array declaration.

Char [] [] foo; // string dynamic array

...

Foo = new char [] [30]; // Assign 30 strings array

Transformation expressions are in the form of type C , the formal conversion is:

(Type) one yuan expression

(TYPE) UnaryExpression

However, this will cause ambiguity on grammar. consider:

(foo) - P;

Is this a negative P transformation into foo, or foo minus P? It is impossible to make a judgment if it is a type or a variable that does not make it a type or a variable it is. But D's design goal is to make the syntax not related to the context - the syntax should be performed without retrieving the symbol table. Therefore, in order to distinguish between transformation and strip-to-band formula, you need to introduce different syntax.

C is introduced:

Dynamic_cast (Expression)

Dynamic_cast (expression)

Solve this problem, but this is ugly and clumsy. D introduced

Cast keyword:

Cast (foo) -p; // c will transform (-P) into FOO

(foo) - p; // foo minus P

Cast has a good nature that can easily find it through textual findings, which will also reduce the burden on the '()' operator that is ruthlessly overloaded.

In other aspects of transformation, D is also different from C / C . Any conversion from class reference to derived class will perform runtime checks to ensure that the transformation is appropriate. This behavior is equivalent to the Dynamic_CAST operator in C .

Class a {...}

Class B: a {...}

Void Test (a a, b b)

{

B bx = a; // error, need type conversion

B bx = cast (b) a; // If a is not B type, BX is null

A AX = b; // No type conversion

A AX = CAST (a) b; // Do not need to be converted upward type conversion

}

If you want to detect an object

o Whether it is a class

B An instance of B, you can use the transformation:

IF (CAST (B) O)

{

// o is an example of B

}

Else

{

// o is not an instance of B

}

Suffix expression

Suffix expression.

Suffix expression -> logo

Suffix expression

Suffix expression -

Suffix expression (parameter list) suffix expression [parameter list]

Suffix expression [assignment expression .. assignment expression]

PostfixExpression. Identifier

PostfixExpression -> Identifier

PostfixExpression

PostfixExpression -

PostfixExpression (argumentlist)

PostfixExpression [argumentlist]

PostfixExpression [assignexpression .. assignexpression]

Index expression

Suffix expression [parameter list]

PostfixExpression [argumentlist]

The suffix expression will be calculated. in case

The type of suffix expression is static or dynamic array, which implicitly declared variables.

Length, and assigns the length of the array to it.

The list of parameters has its own independent declaration scope.

Length only appears in this scope.

Sliced ​​expression

Suffix expression [assignment expression .. assignment expression]

PostfixExpression [assignexpression .. assignexpression]

Or embellished expression will be calculated. in case

The type of suffix expression is static or dynamic array, which implicitly declared variables.

Length, and assigns the length of the array to it.

Assignment expressions have their own independent statements,

Length and

Assignment expressions appear in this scope.

The first assignment expression is the closed lower bound of the slice, and the second assignment expression is the upper bound of the slice. (Translation: That is, [...), you have learned about mathematics, huh, huh). The result of the expression is a slice of the suffix expression array.

Basic expression

Marker

Flag

THIS

Super

NULL

True

False

Numerical text

Character text

Character string

Functional volume

Assertion expression

Basic type. Signature

TYPEID (Type)

Identifier

.IDENTIFIER

THIS

Super

NULL

True

False

Numericliteral

Characterliteral

Stringliteral

FunctionLiteral

AskSERTEXPRESSION

BasicType. Identifier

TypeID (Type)

The flag will look for in the module scope

The flag is instead of nested in the current lexic.

This is within a non-statist member function,

This is a pointer to the object called this function. If the member function is explicitly referenced

TypeOf (this) called, generates a non-virtual function call:

Class A

{

Char get () {return 'a';}

Char foo () {Return TypeOf (this) .get ();

Char bar () {return this.get ();

}

Class B: a

{

Char get () {return 'b';}

}

void main ()

{

B b = new b ();

B.foo (); // Return 'A'

B.bar (); // Return 'b'

}

Super in the non-statist member function,

Super is a pointer to the object called this function, and this pointer is converted to its base class type pointer. If there is no corresponding base class, it will be considered an error.

Super is not allowed to appear in a member function of the structure. If the member function is explicitly referenced

SUPER calls will generate a non-virtual function call.

Null Keyword NULL represents an empty pointer; from the technical say, its type is (void *). It can be implicitly converted to any pointer type. Integer 0 cannot be converted to an empty pointer. NULL is also used in an empty array.

True, False They are all

The Bit type, the values ​​are 1 and 0, respectively.

Character text quantity character character is a single character, the type is

CHAR,

Wchar or

Dchar. If the volume is / u escape sequence, the type is

Wchar. If the volume is / u escape sequence, the type is

Dchar. Otherwise, its type is capable of accommodating the minimum type.

Functional volume

Functional volume

Function function body

Function (parameter list) function body

Function type (parameter list) function body

Delegate function

DELEGATE (parameter list) function body

Delegate type (parameter list) function body

FunctionLiteral

Function functionBody

Function (parameterlist) FunctionBody

Function Type (Parameterlist) FunctionBody

Delegate functionBody

Delegate (parameterlist) FunctionBody

Delegate Type (ParameterList) FunctionBody

Have

Functional volume, you can directly embed anonymous function and anonymous delegate into an expression.

The type is a function or a return type of the commission. If ignored, it will be considered

Void.

(

parameter list

) Is the parameter passed to the function. If ignored, it will be considered a list of empty parameters.

(). The type of function text is a pointer to a function or a delegate.

E.g:

INT function (char c) fp; // declared function pointer

Void test ()

{

Static int foo (char c) {RETURN 6;}

FP = & foo;

}

Accurate equivalent:

INT function (char C) fp;

Void test ()

{

FP = Function Int (char C) {RETURN 6;

}

and:

INT ABC (Int delegate (long i));

Void test ()

{INT B = 3;

INT foo (long c) {return 6 b;}

ABC (& foo);

}

Accurate equivalent:

INT ABC (Int delegate (long i));

Void test ()

{INT B = 3;

ABC (DELEGATE INT (LONG C) {RETURN 6 B;});

}

Anonymous delegate behavior is like any statement. For example, the following LOOP can perform any statement:

Double test ()

{Double D = 7.6;

Float f = 2.3;

Void Loop (Int K, Int J, Void delegate () statement)

{

For (INT i = K; I

{

STATEMENT ();

}

}

LOOP (5, 100, delegate {D = 1;});

LOOP (3, 10, delegate {f = 1;});

RETURN D F;

}

Compared with the nested function,

Function form is similar to static or non-nested functions, and

Delegate form is similar to the non-static nested function. In other words, the delegate volume can access the stack of the peripheral function, and the function of the function is not available. Assertion expression

Aspect:

askERT (expression)

AskERTEXPISSION:

askERT (Expression)

Assertion

Expression. If the result is a false, an Asserterror exception will be thrown. If the result is true, no exception will be thrown. in case

The expression contains any side effects dependent on the program, is an error. By compiling command line options, the compiler may not evaluate the assertion expression at all. The type of assertion expression is

Void. As an assertion is a foundation for D supporting contractual programming.

TypeID expression

TypeID expression:

TYPEID (Type)

TYPEIDEXPIPRESSION:

TypeId (Type)

Returned

Type corresponding to

An example of the TypeInfo class.

转载请注明原文地址:https://www.9cbs.com/read-58831.html

New Post(0)