source: branches/Pascal Compiler/UWideInteger.pas

Last change on this file was 15, checked in by george, 15 years ago
  • Přidáno: Další pokusný projekt překladače.
File size: 3.3 KB
Line 
1unit UWideInteger;
2
3interface
4
5type
6 IWideInteger = interface
7 function GetWidth: Integer;
8 procedure SetWidth(const Value: Integer);
9 function GetAsInteger: Integer;
10 procedure SetAsInteger(const Value: Integer);
11 function GetAsString: string;
12 procedure SetAsString(const Value: string);
13 function GetSign: Boolean;
14 procedure SetSign(const Value: Boolean);
15
16 procedure Assign(Source: IWideInteger);
17 procedure Increment;
18 procedure Decrement;
19 procedure Add(Value: IWideInteger);
20 procedure Subtract(Value: IWideInteger);
21 property Width: Integer read GetWidth write SetWidth;
22 property AsInteger: Integer read GetAsInteger write SetAsInteger;
23 property AsString: string read GetAsString write SetAsString;
24 property Sign: Boolean read GetSign write SetSign;
25 end;
26
27 TWideInteger = class(TInterfacedObject, IWideInteger)
28 private
29 Data: array of Byte;
30 FSign: Boolean;
31 function GetWidth: Integer;
32 procedure SetWidth(const Value: Integer);
33 function GetAsInteger: Integer;
34 procedure SetAsInteger(const Value: Integer);
35 function GetAsString: string;
36 procedure SetAsString(const Value: string);
37 function GetSign: Boolean;
38 procedure SetSign(const Value: Boolean);
39 public
40 procedure Assign(Source: IWideInteger);
41 procedure Increment;
42 procedure Decrement;
43 procedure Add(Value: IWideInteger);
44 procedure Subtract(Value: IWideInteger);
45 property Width: Integer read GetWidth write SetWidth;
46 property AsInteger: Integer read GetAsInteger write SetAsInteger;
47 property AsString: string read GetAsString write SetAsString;
48 property Sign: Boolean read GetSign write SetSign;
49 end;
50
51implementation
52
53{ TWideInteger }
54
55procedure TWideInteger.Add(Value: IWideInteger);
56var
57 I: Integer;
58 Carry: Integer;
59begin
60 Carry := 0;
61 for I := 0 to Width - 1 do begin
62 Carry := Carry + Value.Data[I];
63 Data[I] := Carry;
64 Carry := Carry shr 8;
65 end;
66end;
67
68procedure TWideInteger.Assign(Source: TWideInteger);
69var
70 I: Integer;
71begin
72 Width := Source.Width;
73 for I := 0 to Width - 1 do
74 Data[I] := Source.Data[I];
75end;
76
77procedure TWideInteger.Decrement;
78begin
79 Subtract(1);
80end;
81
82function TWideInteger.GetAsInteger: Integer;
83begin
84 Result := Data[0] or (Data[1] shl 8) or (Data[2] shl 16) or (Data[3] shl 24)
85 or (Sign shl 31);
86end;
87
88function TWideInteger.GetAsString: string;
89begin
90 Result := '';
91end;
92
93function TWideInteger.GetSign: Boolean;
94begin
95 Result := FSign;
96end;
97
98function TWideInteger.GetWidth: Integer;
99begin
100 Result := Length(Data);
101end;
102
103procedure TWideInteger.Increment;
104begin
105 Add(1);
106end;
107
108procedure TWideInteger.SetAsInteger(const Value: Integer);
109begin
110 Width := 4;
111 Data[0] := Value and $ff;
112 Data[1] := (Value shr 8) and $ff;
113 Data[2] := (Value shr 16) and $ff;
114 Data[3] := (Value shr 24) and $3f;
115end;
116
117procedure TWideInteger.SetAsString(const Value: string);
118begin
119
120end;
121
122procedure TWideInteger.SetSign(const Value: Boolean);
123begin
124 FSign := Value;
125end;
126
127procedure TWideInteger.SetWidth(const Value: Integer);
128begin
129 SetLength(Data, Value);
130end;
131
132procedure TWideInteger.Subtract(Value: TWideInteger);
133begin
134
135 Add(Value);
136end;
137
138end.
Note: See TracBrowser for help on using the repository browser.