跳轉到內容

註釋國王參考手冊/型別

來自華夏公益教科書

此頁面正在建設中。

型別宣告

[編輯 | 編輯原始碼]

一些一般原則

  • 所有標量子型別都有一個預設值。對於有界數值和列舉子型別,這是子型別的“第一個”,除非在 Default_Value 方面指定了不同的值。對於無界數值子型別,必須指定 Default_Value 方面。請注意,無界數值型別可以有有界子型別。
  • 可以為所有子型別指定 Predicate 方面。
  • 所有標量型別都具有屬性 'Image、'Value、'Min、'Max、'Previous 和 'Next。
  • 整數和列舉型別具有屬性 'Position、'Value (integer)、'Representation 和 'From_Representation。對於整數型別,'Position 和 'Representation 都給出引數的值,'Value (integer) 和 'From_Representation 給出具有引數值的型別的值(如果存在);這類似於 Ada 中整數型別的 'Pos 和 "Val。
  • 'Image <=>、'Value (string) 互為反函式,'Position <=> 'Value (integer) 也是如此。
type_declaration ::= full_type_declaration | hidden_type_declaration

full_type_declaration ::=
          type defining_identifier [known_discriminant_part] is type_definition
             [aspect_specification];

hidden_type_declaration ::=
          type defining_identifier [discriminant_part] is hidden
             [aspect_specification];

discriminant_part ::= unknown_discriminant_part | known_discriminant_part

unknown_discriminant_part ::= (<>)

known_discriminant_part ::= (discriminant_specification {; discriminant_specification})

discriminant_specification ::=
          defining_identifier : subtype_mark [<- default_expression]  

type_definition ::=
            enumeration_type_definition
          | integer_type_definition
          | real_type_definition
          | map_type_definition
          | sequence_type_definition
          | set_type_definition
          | record_type_definition
          | derived_type_definition
          | module_type_definition
          | task_type_definition

-

-

列舉型別

[編輯 | 編輯原始碼]
type Suit is (Spade, Club, Heart, Diamond);

type Philosopher_ID is (Archimedes, Descartes, Hegel, Kant, Socrates);
enumeration_type_definition ::=
         (enumeration_literal_specification {, enumeration_literal_specification})

enumeration_literal_specification ::= defining_identifier | defining_character_literal

運算子

[編輯 | 編輯原始碼]

= /= < <= > >=

First, Last, Position, From_Representation, Representation, Image, Value, Min, Max, Previous, Next

Bit_Size, Byte_Size

物件宣告

[編輯 | 編輯原始碼]
My_Card : Suit;

-

-

整數型別

[編輯 | 編輯原始碼]

有界整數型別

[編輯 | 編輯原始碼]
type Day is range 1 .. 31;

無界整數型別

[編輯 | 編輯原始碼]
type Factorial is range <> with Default_Value => 0;
integer_type_definition ::= bounded_integer_type_definition | unbounded_integer_type_definition

bounded_integer_type_definition ::= range static_simple_expression .. static_simple_expression

unbounded_integer_type_definition ::= range <>

運算子

[編輯 | 編輯原始碼]

+ - * / rem mod ^ = /= < <= > >= and or xor not

First, Last, Bit_Wise_Operators, Image, Value, Min, Max, Previous, Next, Valid

具有 Bit_Wise_Operators = True 的附加屬性

Shift_Left, Shift_Right, Rotate_Left, Rotate_Right, Mod

Default_Value, Predicate, Signed_Representation, Overflow_Checking

物件宣告

[編輯 | 編輯原始碼]
N : Factorial;

所有適合單個硬體整數的有界整數型別都可用它的基本型別的位運算。

-

實數型別

[編輯 | 編輯原始碼]
real_type_definition ::= floating_point_definition | fixed_point_definition

浮點型別

[編輯 | 編輯原始碼]
有界浮點型別
[編輯 | 編輯原始碼]
type Risk is digits 7 range 0 .. 1;
無界浮點型別
[編輯 | 編輯原始碼]
type Rational is digits <> with Default_Value => 0;
floating_point_definition ::=
          bounded_floating_point_definition | unbounded_floating_point_definition

bounded_floating_point_definition ::= digits static_expression [real_range_specification]

real_range_specification ::= range static_simple_expression .. static_simple_expression

unbounded_floating_point_definition ::= digits <>

運算子

[編輯 | 編輯原始碼]

+ - * / ^ = /= < <= > >=

Image, Value, Min, Max, Previous, Next, Valid, digits

Default_Value, Predicate

物件宣告

[編輯 | 編輯原始碼]
Speed : Rational;

-

-

定點型別

[編輯 | 編輯原始碼]
有界定點型別
[編輯 | 編輯原始碼]
type Angle is delta 0.01 range -3.14 .. 3.14;
無界定點型別
[編輯 | 編輯原始碼]
type Duration is delta 10.0 ^ -9 range <> with Default_Value => 0;
fixed_point_definition ::= bounded_fixed_point_definition | unbounded_fixed_point_definition

bounded_fixed_point_definition ::= delta static_expression real_range_specification

unbounded_fixed_point_definition ::= delta static_expression range <>

運算子

[編輯 | 編輯原始碼]

+ - * / ^ = /= < <= > >=

影像、值、最小值、最大值、前一個、下一個、有效、增量

Default_Value, Predicate

物件宣告

[編輯 | 編輯原始碼]
Bearing : Angle;

不應該提供小數型別,以涵蓋 Ada 中相應的可用型別嗎?

增量為 10 的冪的 King 定點型別等效於 Ada 小數定點型別。

type Money is delta 0.01 digits 14;

上面的程式碼等效於

type Money is delta 0.01 range -999_999_999_999.99 .. 999_999_999_999.99;

-

對映型別

[編輯 | 編輯原始碼]
type Store is map Part_Key => Stock_Info;
map_type_definition := map key_subtype_mark => value_subtype_mark

運算子

[編輯 | 編輯原始碼]

= /=

"=" 只有在為值子型別定義時才定義。

-

刪除、清除、定義、大小

-

物件宣告

[編輯 | 編輯原始碼]
My_Store : Store;

元件解引用

[編輯 | 編輯原始碼]
My_Store (K99)
Store'(K1 => SI1, KE => SIE)

Store'(null) -- Empty map
for K in My_Store'range

for E of My_Store

對映也可以透過函式實現,因此 King 對兩者使用相同的符號。

-

序列型別

[編輯 | 編輯原始碼]
type Stack is sequence of Message;
sequence_type_definition := sequence of element_subtype_mark

運算子

[編輯 | 編輯原始碼]

= /= &

"=" 只有在為值子型別定義時才定義。

-

刪除、清除、長度

-

物件宣告

[編輯 | 編輯原始碼]
My_Stack : Stack;

元件解引用

[編輯 | 編輯原始碼]
My_Stack [Curent]
My_Stack'[MC, ML, MZ]

My_Stack'[null] -- Empty sequence
for P in [reverse] My_Stack'range

for E of [reverse] My_Stack

-

-

集合型別

[編輯 | 編輯原始碼]
type File_Mask is set of File_Mode;
set_type_definition := set of element_subtype_mark

運算子

[編輯 | 編輯原始碼]

+ - * / = /= < <= > >= [not] in

-

大小

-

物件宣告

[編輯 | 編輯原始碼]
My_Mask : File_Mask:

元件解引用

[編輯 | 編輯原始碼]

-

My_Mask'{Read, Exec}

My_Mask'{null} -- Empty set

My_Mask'{all} -- Full set
for M of My_Mask

-

-

記錄型別

[編輯 | 編輯原始碼]
type Complex is record
   Re : Float;
   Im : Float;
end record Complex;
record_type_definition ::= record_definition

record_definition ::=
           record
              component_list
           end record record_identifier

component_list ::=
            component_item {component_item}
          | {component_item} variant_part
          | null;

component_item ::= component_declaration

component_declaration ::= defining_identifier : component_definition [<- default_expression]
                          [aspect_specification];

component_definition ::= subtype_indication

variant_part ::= case discriminant_direct_name is
                   variant
                   {variant}
                 end case;

variant ::= when discrete_choice_list =>
               component_list

discrete_choice_list ::= discrete_choice {| discrete_choice}

discrete_choice ::= choice_expression | discrete_subtype_indication | range_specification | others

運算子

[編輯 | 編輯原始碼]

= /=

-

-

物件宣告

[編輯 | 編輯原始碼]
Z : Complex;

元件解引用

[編輯 | 編輯原始碼]
Z.Re

-

-

派生型別

[編輯 | 編輯原始碼]
type Boxes (Length : Position_Value) is record
   Box_A : Receive_Box (Max_Length => Length);
   Box_B : Receive_Box (Max_Length => Length);
end record;
type Boxes_4 is new Boxes (Length => 4);
derived_type_definition ::= new parent_subtype_indication

運算子

[編輯 | 編輯原始碼]

-

-

-

操作、屬性等基於父型別。

-

模組型別

[編輯 | 編輯原始碼]
type Strings is module
   Set : procedure (S : String);
   Index : function (Pattern : String; Going : Direction <- Forward) return Position_Value;
   Head : function (Count : Natural; Pad : Unicode <- Space) return String;
   -- ...
end module Strings;
module_type_definition ::= module
                              subprogram_declaration {subprogram_declaration}
                           end module module_identifier

運算子

[編輯 | 編輯原始碼]

-

-

-

-

-

主動任務型別

[編輯 | 編輯原始碼]
type Philosopher (ID : Philosopher_ID <- Archimedes) is task;
task_type_definition ::= task

運算子

[編輯 | 編輯原始碼]

= /=

-

-

物件宣告

[編輯 | 編輯原始碼]
T1 : Philosopher;

-

-

被動任務型別

[編輯 | 編輯原始碼]
type Semaphore is task
   Secure : procedure;
   Release : procedure;
end task Semaphore;
task_type_definition ::= task
                            subprogram_declaration {subprogram_declaration}
                         end task task_identifier

運算子

[編輯 | 編輯原始碼]

= /=

-

-

物件宣告

[編輯 | 編輯原始碼]
S1 : Semaphore;

被動任務是主動任務之間唯一可以進行通訊的方式。

-

華夏公益教科書