📄 interpreterunit2.pas
字号:
unit InterpreterUnit2;
interface
type
TboolArray = array[0..25] of boolean;
TContext = class;
TBooleanExp = class
public
constructor Create;
destructor Destroy; virtual;
function Evaluate(c: TContext): Boolean; virtual; abstract;
function Replace(const c: PChar; b: TBooleanExp): TBooleanExp; virtual; abstract;
function copy(): TBooleanExp; virtual; abstract;
end;
TVariableExp = class(TBooleanExp)
private
f_Name: PChar;
public
constructor Create(const name: PChar);
destructor Destroy; virtual;
function Evaluate(aContext: TContext): Boolean; override;
function Replace(const name: PChar; exp: TBooleanExp): TBooleanExp; override;
function copy(): TBooleanExp; override;
end;
TContext = class
private
f_var: TboolArray;
public
function Lookup(const c: PChar): Boolean;
procedure Assign(v: TVariableExp; b: Boolean);
end;
TAndExp = class(TBooleanExp)
private
f_operand1: TBooleanExp;
f_operand2: TBooleanExp;
public
constructor Create(op1, op2: TBooleanExp);
destructor Destroy; virtual;
function Evaluate(aContext: TContext): Boolean; override;
function Replace(const name: PChar; exp: TBooleanExp): TBooleanExp; override;
function Copy(): TBooleanExp; override;
end;
TOrExp = class(TBooleanExp)
private
f_operand1: TBooleanExp;
f_operand2: TBooleanExp;
public
constructor Create(op1, op2: TBooleanExp);
destructor Destroy; virtual;
function Evaluate(aContext: TContext): Boolean; override;
function Replace(const name: PChar; exp: TBooleanExp): TBooleanExp; override;
function Copy(): TBooleanExp; override;
end;
TNotExp = class(TBooleanExp)
private
f_operand: TBooleanExp;
public
constructor Create(op: TBooleanExp);
destructor Destroy; virtual;
function Evaluate(aContext: TContext): Boolean; override;
function Replace(const name: PChar; exp: TBooleanExp): TBooleanExp; override;
function Copy(): TBooleanExp; override;
end;
TConstantExp = class(TBooleanExp)
private
f_const: Boolean;
public
constructor Create(op: Boolean);
destructor Destroy; virtual;
function Evaluate(c: TContext): Boolean;override;
function Replace(const name: PChar; exp: TBooleanExp): TBooleanExp; override;
function Copy(): TBooleanExp; override;
end;
implementation
constructor TBooleanExp.Create;
begin
//.....
end;
destructor TBooleanExp.Destroy;
begin
//.....
end;
function TContext.Lookup(const c: PChar): Boolean;
begin
Result := f_var[ord(c^) - 65];
end;
procedure TContext.Assign(v: TVariableExp; b: Boolean);
begin
f_var[Ord(v.f_Name^) - 65] := b;
end;
constructor TVariableExp.Create(const name: PChar);
begin
f_name := name;
end;
destructor TVariableExp.Destroy;
begin
//.....
end;
function TVariableExp.Evaluate(aContext: TContext): Boolean;
begin
Result := aContext.Lookup(f_Name);
end;
function TVariableExp.Replace(const name: PChar; exp: TBooleanExp): TBooleanExp;
begin
if name = f_Name then
Result := Exp.copy()
else
Result := TBooleanExp(TVariableExp.Create(f_name));
end;
function TVariableExp.copy(): TBooleanExp;
begin
Result := TVariableExp.Create(f_Name);
end;
constructor TAndExp.Create(op1, op2: TBooleanExp);
begin
f_operand1 := op1;
f_operand2 := op2;
end;
destructor TAndExp.Destroy;
begin
//.....
end;
function TAndExp.Evaluate(aContext: TContext): Boolean;
begin
result := (f_operand1.Evaluate(aContext) and (f_operand2.Evaluate(aContext)));
end;
function TAndExp.Replace(const name: PChar; exp: TBooleanExp): TBooleanExp;
begin
result := TAndExp.Create(f_operand1.Replace(name, exp), f_operand2.Replace(name, exp));
end;
function TAndExp.Copy(): TBooleanExp;
begin
result := TAndExp.Create(f_operand1.copy(), f_operand2.copy());
end;
constructor TOrExp.Create(op1, op2: TBooleanExp);
begin
f_operand1 := op1;
f_operand2 := op2;
end;
destructor TOrExp.Destroy;
begin
//.....
end;
function TOrExp.Evaluate(aContext: TContext): Boolean;
begin
result := (f_operand1.Evaluate(aContext) or (f_operand2.Evaluate(aContext)));
end;
function TOrExp.Replace(const name: PChar; exp: TBooleanExp): TBooleanExp;
begin
result := TOrExp.Create(f_operand1.Replace(name, exp), f_operand2.Replace(name, exp));
end;
function TOrExp.Copy(): TBooleanExp;
begin
result := TOrExp.Create(f_operand1.copy(), f_operand2.copy());
end;
constructor TNotExp.Create(op: TBooleanExp);
begin
f_operand := op;
end;
destructor TNotExp.Destroy;
begin
//.....
end;
function TNotExp.Evaluate(aContext: TContext): Boolean;
begin
result := not (f_operand.Evaluate(aContext));
end;
function TNotExp.Replace(const name: PChar; exp: TBooleanExp): TBooleanExp;
begin
result := TNotExp.Create(f_operand.Replace(name, exp));
end;
function TNotExp.Copy(): TBooleanExp;
begin
result := TNotExp.Create(f_operand.copy());
end;
constructor TConstantExp.Create(op: Boolean);
begin
f_const := op;
end;
destructor TConstantExp.Destroy;
begin
//.........
end;
function TConstantExp.Evaluate(c: TContext): Boolean;
begin
Result := f_const;
end;
function TConstantExp.Replace(const name: PChar; exp: TBooleanExp): TBooleanExp;
begin
result := Self;
end;
function TConstantExp.Copy(): TBooleanExp;
begin
Result := TConstantExp.Create(f_const);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -