?? hwexprextliteral.pas
字號:
unit hwExprExtLiteral;
interface
uses
TypInfo,
Classes,
SysUtils,
hwExpr;
const
Err_OnlyBoolean = '只能返回邏輯值';
Err_OnlyInteger = '只能返回整數值';
Err_OnlyFloat = '只能返回浮點數';
Err_OnlyString = '只能返回字符串';
Err_OnlyObject = '只能返回對象';
Err_OnlyEnum = '只能返回枚舉值';
Err_CanntBoolean = '不能返回邏輯值';
Err_CanntInteger = '不能返回整數值';
Err_CanntFloat = '不能返回浮點值';
Err_CanntString = '不能返回字符串';
Err_CanntObject = '不能返回對象';
Err_OutOfIndex = '索引值超出范圍';
type
{*** Charmer Create Begin ***}
{ 對外部定義符號的引用,如引用外部定義的常量、變量等。這些變量在程序中
以Delphi定義變量的標準進行定義,如 var i: integer; i := 100; }
TExtRefSymLiteral = class(TExpression)
Private
FExtSym: Pointer; //指向外部量的指針
FSymType: TExprType;
FSymbolName: string;
Public
constructor Create(const AIdentifier: string;
PSymAddr: Pointer;
const AType: TExprType);
destructor Destroy; Override;
function AsBoolean: boolean; override;
function AsFloat: double; override;
function AsInteger: integer; override;
function AsObject: TObject; override;
function AsString: string; override;
function ExprType: TExprType; override;
property SymbolName: string read FSymbolName;
end;
{ 定義的變量符號,用于CompileFunction }
TVarLiteral = class(TExpression)
Private
FSymbolName: string;
//FValue: Pointer;
FBlnValue: Boolean;
FIntValue: Integer;
FDblValue: double;
FStrValue: String;
FObjValue: TObject;
FExprType: TExprType;
//FTypeInfo: PTypeInfo;
FHasValue: Boolean;
FInitialized: Boolean;
Public
constructor Create(const AIdentifier: string; const AType: TExprType);
destructor Destroy; override;
function AsBoolean: Boolean; override;
function AsInteger: Integer; override;
function AsString: string; override;
function AsObject: TObject; override;
function AsFloat: double; override;
//function CanReadAs(aType: TExprType): Boolean;
function ExprType: TExprType; override;
procedure SetValue(const Value: Boolean); overload;
procedure SetValue(const Value: Integer); overload;
procedure SetValue(const Value: double); overload;
procedure SetValue(const Value: string); overload;
procedure SetValue(const Value: TObject); overload;
function IsInitialized: Boolean;
property HasValue: Boolean read FHasValue;
property SymbolName: string read FSymbolName;
end;
implementation
{ ============================================================================
>>>> Class Implementation Begin <<<<
>>>> Class Name : TExtSymbolLiteral
>>>> Description :
>>>> Create Date :
---------------------------------------------------------------------------- }
constructor TExtRefSymLiteral.Create(const AIdentifier: string;
PSymAddr: Pointer; const AType: TExprType);
begin
inherited Create;
FSymbolName := AIdentifier;
FSymType := AType;
FExtSym := PSymAddr;
end;
destructor TExtRefSymLiteral.Destroy;
begin
inherited Destroy;
end;
function TExtRefSymLiteral.AsBoolean: Boolean;
begin
if FSymType = ttBoolean then
Result := Boolean(FExtSym^)
else
Raise EExpression.Create(Err_CanntBoolean);
end;
function TExtRefSymLiteral.AsFloat: Double;
begin
if FSymType = ttFloat then
Result := Double(FExtSym^)
else
Result := inherited AsFloat;
//Raise EExpression.Create(Err_OnlyFloat);
end;
function TExtRefSymLiteral.AsInteger: integer;
begin
if FSymType = ttInteger then
Result := Integer(FExtSym^)
else
Result := Inherited AsInteger;
//Raise EExpression.Create(Err_OnlyInteger);
end;
function TExtRefSymLiteral.AsObject: TObject;
begin
if FSymType = ttObject then
Result := TObject(FExtSym^)
else
raise EExpression.Create(Err_CanntObject);
end;
function TExtRefSymLiteral.AsString: string;
begin
if FSymType = ttString then
Result := String(FExtSym^)
else
Result := inherited AsString;
//raise EExpression.Create(Err_OnlyString);
end;
function TExtRefSymLiteral.ExprType: TExprType;
begin
Result := FSymType;
end;
{ ============================================================================
>>>> Class Implementation Begin <<<<
>>>> Class Name : TVarLiteral
>>>> Description :
>>>> Create Date :
---------------------------------------------------------------------------- }
constructor TVarLiteral.Create(const AIdentifier: string; const AType: TExprType);
begin
inherited Create;
FSymbolName := AIdentifier;
FHasValue := False;
FInitialized := True;
FExprType := AType;
end;
destructor TVarLiteral.Destroy;
begin
inherited Destroy;
end;
//function TVarLiteral.CanReadAs(aType: TExprType):Boolean;
//begin
// Result := aType = FExprType;
//end;
function TVarLiteral.ExprType: TExprType;
begin
Result := FExprType;
end;
function TVarLiteral.IsInitialized: Boolean;
begin
result := FInitialized;
end;
function TVarLiteral.AsBoolean: Boolean;
begin
if FExprType = ttBoolean then Result := FBlnValue
else raise EExpression.Create(Err_CanntBoolean);
end;
function TVarLiteral.AsInteger: integer;
begin
if FExprType = ttInteger then Result := FIntValue
else Result := inherited AsInteger;
end;
function TVarLiteral.AsString: string;
begin
if FExprType = ttString then Result := FStrValue
else Result := inherited AsString;
end;
function TVarLiteral.AsObject: TObject;
begin
if FExprType = ttObject then Result := FObjValue
else raise EExpression.Create(Err_CanntObject);
end;
function TVarLiteral.AsFloat: double;
begin
if FExprType = ttFloat then REsult := FDblValue
else Result := inherited AsFloat;
end;
const Err_SetValue = '變量%s的數據類型為%s,不能將%s類型的數據賦值給它';
procedure TVarLiteral.SetValue(const Value: Boolean);
begin
if FExprType = ttBoolean then FBlnValue:= Value
else
raise EExpression.CreateFmt(Err_SetValue,
[FSymbolName,
ExprTypeName[FExprType],
ExprTypeName[ttBoolean]]);
end;
procedure TVarLiteral.SetValue(const Value: Integer);
begin
if FExprType = ttInteger then FIntValue:= Value
else
raise EExpression.CreateFmt(Err_SetValue,
[FSymbolName,
ExprTypeName[FExprType],
ExprTypeName[ttInteger]]);
end;
procedure TVarLiteral.SetValue(const Value: double);
begin
if FExprType = ttFloat then FDblValue := Value
else
raise EExpression.CreateFmt(Err_SetValue,
[FSymbolName,
ExprTypeName[FExprType],
ExprTypeName[ttFloat]]);
end;
procedure TVarLiteral.SetValue(const Value: string);
begin
if FExprType = ttString then FStrValue := Value
else
raise EExpression.CreateFmt(Err_SetValue,
[FSymbolName,
ExprTypeName[FExprType],
ExprTypeName[ttString]]);
end;
procedure TVarLiteral.SetValue(const Value: TObject);
begin
if FExprType = ttObject then FObjValue := Value
else
raise EExpression.CreateFmt(Err_SetValue,
[FSymbolName,
ExprTypeName[FExprType],
ExprTypeName[ttObject]]);
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -