📄 rm_pars.pas
字号:
{*****************************************}
{ }
{ Report Machine v2.0 }
{ Report Pars }
{ }
{*****************************************}
unit RM_pars;
{$I RM.INC}
interface
uses
Windows, SysUtils, Classes, Forms, DB, RM_Const, RM_DBRel
{$IFDEF Delphi6}, Variants{$ENDIF};
type
TGetPValueEvent = procedure(const s: string; var v: Variant) of object;
TFunctionEvent = procedure(const Name: string; p1, p2, p3: Variant;
var Val: Variant) of object;
// TRMParser is intended for calculating expressions passed as string
// parameter like '1 + 2 * (a + b)'. Expression can contain variables and
// functions. There is two events in TfrParser: OnGetValue and OnFunction
// intended for substitute var/func value instead of var/func name.
// Call TRMParser.Calc(Expression) to get expression value.
{ TRMParser }
TRMParser = class
private
FOnGetValue: TGetPValueEvent;
FOnFunction: TFunctionEvent;
function GetIdentify(const s: string; var i: Integer): string;
function GetString(const s: string; var i: Integer): string;
procedure Get3Parameters(const s: string; var i: Integer;
var s1, s2, s3: string);
public
function Str2OPZ(s: string): string;
function CalcOPZ(const s: string): Variant;
function Calc(const s: string): Variant;
property OnGetValue: TGetPValueEvent read FOnGetValue write FOnGetValue;
property OnFunction: TFunctionEvent read FOnFunction write FOnFunction;
end;
// TRMVariables is tool class intended for storing variable name and its
// value. Value is of type Variant.
// Call TRMVariables['VarName'] := VarValue to set variable value and
// VarValue := TRMVariables['VarName'] to retrieve it.
{ TRMVariables }
TRMVariables = class(TObject)
private
FList: TStringList;
function GetVariable(const Name: string): Variant;
function GetValue(Index: Integer): Variant;
function GetName(Index: Integer): string;
function GetCount: Integer;
function GetSorted: Boolean;
procedure SetVariable(const Name: string; Value: Variant);
procedure SetValue(Index: Integer; Value: Variant);
procedure SetName(Index: Integer; Value: string);
procedure SetSorted(Value: Boolean);
public
constructor Create;
destructor Destroy; override;
function IndexOf(const Name: string): Integer;
procedure Assign(Value: TRMVariables);
procedure Clear;
procedure Delete(Index: Integer);
procedure Insert(Position: Integer; const Name: string);
property Variable[const Name: string]: Variant read GetVariable write SetVariable; default;
property Value[Index: Integer]: Variant read GetValue write SetValue;
property Name[Index: Integer]: string read GetName write SetName;
property Count: Integer read GetCount;
property Sorted: Boolean read GetSorted write SetSorted;
end;
// TRMFunctionSplitter is internal class, you typically don't need to use it.
// It intended for splitting expression onto several parts and checking
// if it contains some specified functions.
// TRMFunctionSplitter used when checking if objects has aggregate functions
// inside.
TRMFunctionSplitter = class
protected
FMatchFuncs, FSplitTo: TStrings;
FParser: TRMParser;
FVariables: TRMVariables;
public
constructor Create(MatchFuncs, SplitTo: TStrings; Variables: TRMVariables);
destructor Destroy; override;
procedure Split(s: string);
end;
implementation
uses RM_Utils;
type
PVariable = ^TVariable;
TVariable = record
Value: Variant;
end;
const
ttGe = #1; ttLe = #2;
ttNe = #3; ttOr = #4; ttAnd = #5;
ttInt = #6; ttFrac = #7;
ttUnMinus = #9; ttUnPlus = #10; ttStr = #11;
ttNot = #12; ttMod = #13; ttRound = #14;
{------------------------------------------------------------------------------}
{------------------------------------------------------------------------------}
{TRMVariables}
constructor TRMVariables.Create;
begin
inherited Create;
FList := TStringList.Create;
FList.Duplicates := dupIgnore;
end;
destructor TRMVariables.Destroy;
begin
Clear;
FList.Free;
inherited Destroy;
end;
procedure TRMVariables.Assign(Value: TRMVariables);
var
i: Integer;
begin
Clear;
for i := 0 to Value.Count - 1 do
SetVariable(Value.Name[i], Value.Value[i]);
end;
procedure TRMVariables.Clear;
begin
while FList.Count > 0 do
Delete(0);
end;
procedure TRMVariables.SetVariable(const Name: string; Value: Variant);
var
i: Integer;
p: PVariable;
begin
i := IndexOf(Name);
if i <> -1 then
PVariable(FList.Objects[i]).Value := Value
else
begin
New(p);
p^.Value := Value;
FList.AddObject(Name, TObject(p));
end;
end;
function TRMVariables.GetVariable(const Name: string): Variant;
var
i: Integer;
begin
Result := Null;
i := IndexOf(Name);
if i <> -1 then
Result := PVariable(FList.Objects[i]).Value;
end;
procedure TRMVariables.SetValue(Index: Integer; Value: Variant);
begin
if (Index < 0) or (Index >= FList.Count) then Exit;
PVariable(FList.Objects[Index])^.Value := Value;
end;
function TRMVariables.GetValue(Index: Integer): Variant;
begin
Result := 0;
if (Index < 0) or (Index >= FList.Count) then Exit;
Result := PVariable(FList.Objects[Index])^.Value;
end;
function TRMVariables.IndexOf(const Name: string): Integer;
begin
Result := FList.IndexOf(Name);
end;
procedure TRMVariables.Insert(Position: Integer; const Name: string);
begin
SetVariable(Name, 0);
FList.Move(FList.IndexOf(Name), Position);
end;
function TRMVariables.GetCount: Integer;
begin
Result := FList.Count;
end;
procedure TRMVariables.SetName(Index: Integer; Value: string);
begin
if (Index < 0) or (Index >= FList.Count) then Exit;
FList[Index] := Value;
end;
function TRMVariables.GetName(Index: Integer): string;
begin
Result := '';
if (Index < 0) or (Index >= FList.Count) then Exit;
Result := FList[Index];
end;
procedure TRMVariables.Delete(Index: Integer);
var
p: PVariable;
begin
if (Index < 0) or (Index >= FList.Count) then Exit;
p := PVariable(FList.Objects[Index]);
Dispose(p);
FList.Delete(Index);
end;
procedure TRMVariables.SetSorted(Value: Boolean);
begin
FList.Sorted := Value;
end;
function TRMVariables.GetSorted: Boolean;
begin
Result := FList.Sorted;
end;
{------------------------------------------------------------------------------}
{------------------------------------------------------------------------------}
{TRMParser}
function TRMParser.CalcOPZ(const s: string): Variant;
var
i, j, k, i1, st, ci, cn: Integer;
s1, s2, s3, s4: string;
nm: array[1..8] of Variant;
v: Double;
begin
st := 1;
i := 1;
nm[1] := 0;
while i <= Length(s) do
begin
j := i;
case s[i] of
'+', ttOr:
nm[st - 2] := nm[st - 2] + nm[st - 1];
'-':
nm[st - 2] := nm[st - 2] - nm[st - 1];
'*', ttAnd:
nm[st - 2] := nm[st - 2] * nm[st - 1];
'/':
if nm[st - 1] <> 0 then
nm[st - 2] := nm[st - 2] / nm[st - 1]
else
nm[st - 2] := 0;
'>':
if nm[st - 2] > nm[st - 1] then
nm[st - 2] := 1
else
nm[st - 2] := 0;
'<':
if nm[st - 2] < nm[st - 1] then
nm[st - 2] := 1
else
nm[st - 2] := 0;
'=':
if nm[st - 2] = nm[st - 1] then
nm[st - 2] := 1
else
nm[st - 2] := 0;
ttNe:
if nm[st - 2] <> nm[st - 1] then
nm[st - 2] := 1
else
nm[st - 2] := 0;
ttGe:
if nm[st - 2] >= nm[st - 1] then
nm[st - 2] := 1
else
nm[st - 2] := 0;
ttLe:
if nm[st - 2] <= nm[st - 1] then
nm[st - 2] := 1
else
nm[st - 2] := 0;
ttInt:
begin
v := nm[st - 1];
if Abs(Round(v) - v) < 1E-10 then
v := Round(v)
else
v := Int(v);
nm[st - 1] := v;
end;
ttFrac:
begin
v := nm[st - 1];
if Abs(Round(v) - v) < 1E-10 then
v := Round(v);
nm[st - 1] := Frac(v);
end;
ttRound:
nm[st - 1] := Integer(Round(nm[st - 1]));
ttUnMinus:
nm[st - 1] := -nm[st - 1];
ttUnPlus: ;
ttStr:
begin
if nm[st - 1] <> Null then
s1 := nm[st - 1]
else
s1 := '';
nm[st - 1] := s1;
end;
ttNot:
if nm[st - 1] = 0 then
nm[st - 1] := 1
else
nm[st - 1] := 0;
ttMod:
nm[st - 2] := nm[st - 2] mod nm[st - 1];
' ': ;
'[':
begin
k := i;
s1 := RMGetBrackedVariable(s, k, i);
if Assigned(FOnGetValue) then
FOnGetValue(AnsiUpperCase(s1), nm[st]);
Inc(st);
end
else //case else
if s[i] = '''' then
begin
s1 := GetString(s, i);
s1 := Copy(s1, 2, Length(s1) - 2);
while Pos('''' + '''', s1) <> 0 do
Delete(s1, Pos('''' + '''', s1), 1);
nm[st] := s1;
k := i;
end
else
begin
k := i;
s1 := GetIdentify(s, k);
if (s1 <> '') and (s1[1] in ['0'..'9', '.', ',']) then
begin
for i1 := 1 to Length(s1) do
begin
if s1[i1] in ['.', ','] then s1[i1] := DecimalSeparator;
end;
nm[st] := StrToFloat(s1);
end
else if AnsiCompareText(s1, 'TRUE') = 0 then
nm[st] := 1
else if AnsiCompareText(s1, 'FALSE') = 0 then
nm[st] := 0
else if s[k] = '[' then
begin
s1 := 'GETARRAY(' + s1 + ', ' + RMGetBrackedVariable(s, k, i) + ')';
nm[st] := Calc(s1);
k := i;
end
else if s[k] = '(' then
begin
s1 := AnsiUpperCase(s1);
Get3Parameters(s, k, s2, s3, s4);
if s1 = 'COPY' then
begin
ci := StrToInt(Calc(s3));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -