Ast development
This commit is contained in:
@@ -32,6 +32,7 @@ type
|
||||
function VisitVariableDeclaration(const Node: IVariableDeclarationNode): TAstValue; virtual;
|
||||
function VisitAssignment(const Node: IAssignmentNode): TAstValue; virtual;
|
||||
function VisitIndexer(const Node: IIndexerNode): TAstValue; virtual;
|
||||
function VisitMemberAccess(const Node: IMemberAccessNode): TAstValue; virtual;
|
||||
end;
|
||||
|
||||
// TDebugEvaluatorVisitor now overrides all visit methods for full tracing
|
||||
@@ -61,6 +62,7 @@ type
|
||||
function VisitVariableDeclaration(const Node: IVariableDeclarationNode): TAstValue; override;
|
||||
function VisitAssignment(const Node: IAssignmentNode): TAstValue; override;
|
||||
function VisitIndexer(const Node: IIndexerNode): TAstValue; override;
|
||||
function VisitMemberAccess(const Node: IMemberAccessNode): TAstValue; override;
|
||||
end;
|
||||
|
||||
// Registers all native core functions in the given scope.
|
||||
@@ -208,7 +210,7 @@ begin
|
||||
skInteger: Result := (AValue.AsScalar.Value.AsInteger <> 0);
|
||||
skInt64: Result := (AValue.AsScalar.Value.AsInt64 <> 0);
|
||||
skUInt64: Result := (AValue.AsScalar.Value.AsUInt64 <> 0);
|
||||
skBoolean: Result := AValue.AsScalar.Value.AsBoolean;
|
||||
skBoolean: Result := (AValue.AsScalar.Value.AsBoolean);
|
||||
else
|
||||
Result := False;
|
||||
end;
|
||||
@@ -248,17 +250,13 @@ end;
|
||||
function TEvaluatorVisitor.VisitIndexer(const Node: IIndexerNode): TAstValue;
|
||||
var
|
||||
baseValue, indexValue: TAstValue;
|
||||
series: TScalarRecordSeries;
|
||||
rec: TScalarRecord;
|
||||
index: Int64;
|
||||
indexScalar: TScalar;
|
||||
begin
|
||||
baseValue := Node.Base.Accept(Self);
|
||||
indexValue := Node.Index.Accept(Self);
|
||||
|
||||
if (baseValue.Kind <> avkRecordSeries) then
|
||||
raise EArgumentException.Create('Indexer `[]` can only be applied to a RecordSeries.');
|
||||
|
||||
// Index validation (common for all indexable types)
|
||||
if (indexValue.Kind <> avkScalar) then
|
||||
raise EArgumentException.Create('Indexer `[]` requires a scalar integer argument.');
|
||||
|
||||
@@ -270,13 +268,54 @@ begin
|
||||
raise EArgumentException.Create('Indexer `[]` requires an integer type argument.');
|
||||
end;
|
||||
|
||||
series := baseValue.AsRecordSeries;
|
||||
// Base type dispatching
|
||||
case baseValue.Kind of
|
||||
avkRecordSeries:
|
||||
begin
|
||||
var series := baseValue.AsRecordSeries;
|
||||
if (index < 0) or (index >= series.TotalCount) then
|
||||
raise EArgumentException.CreateFmt('Index %d is out of bounds for series with %d elements.', [index, series.TotalCount]);
|
||||
|
||||
if (index < 0) or (index >= series.TotalCount) then
|
||||
raise EArgumentException.CreateFmt('Index %d is out of bounds for series with %d elements.', [index, series.TotalCount]);
|
||||
var recordValue := series.Items[Integer(index)];
|
||||
Result := TAstValue.FromRecord(recordValue);
|
||||
end;
|
||||
avkMemberSeries:
|
||||
begin
|
||||
var memberSeries := baseValue.AsMemberSeries;
|
||||
if (index < 0) or (index >= memberSeries.Count) then
|
||||
raise EArgumentException
|
||||
.CreateFmt('Index %d is out of bounds for member series with %d elements.', [index, memberSeries.Count]);
|
||||
|
||||
rec := series.Items[Integer(index)];
|
||||
Result := TAstValue.FromRecord(rec);
|
||||
var scalarValue := memberSeries.Items[Integer(index)];
|
||||
Result := TAstValue.FromScalar(scalarValue);
|
||||
end;
|
||||
else
|
||||
raise EArgumentException.Create('Indexer `[]` is not supported for this value type.');
|
||||
end;
|
||||
end;
|
||||
|
||||
function TEvaluatorVisitor.VisitMemberAccess(const Node: IMemberAccessNode): TAstValue;
|
||||
var
|
||||
baseValue: TAstValue;
|
||||
memberName: string;
|
||||
begin
|
||||
baseValue := Node.Base.Accept(Self);
|
||||
memberName := Node.Member.Name;
|
||||
|
||||
case baseValue.Kind of
|
||||
avkRecordSeries:
|
||||
begin
|
||||
var series := baseValue.AsRecordSeries;
|
||||
Result := TAstValue.FromMemberSeries(series.CreateMemberSeries(memberName));
|
||||
end;
|
||||
avkRecord:
|
||||
begin
|
||||
var recordValue := baseValue.AsRecord;
|
||||
Result := TAstValue.FromScalar(recordValue.Items[memberName]);
|
||||
end;
|
||||
else
|
||||
raise EArgumentException.Create('Member access operator `.` is not supported for this value type.');
|
||||
end;
|
||||
end;
|
||||
|
||||
function TEvaluatorVisitor.VisitVariableDeclaration(const Node: IVariableDeclarationNode): TAstValue;
|
||||
@@ -607,6 +646,18 @@ begin
|
||||
AppendLine(Format('} -> %s', [Result.ToString]));
|
||||
end;
|
||||
|
||||
function TDebugEvaluatorVisitor.VisitMemberAccess(const Node: IMemberAccessNode): TAstValue;
|
||||
begin
|
||||
AppendLine(Format('MemberAccess (Member: %s) {', [Node.Member.Name]));
|
||||
Indent;
|
||||
try
|
||||
Result := inherited VisitMemberAccess(Node);
|
||||
finally
|
||||
Unindent;
|
||||
end;
|
||||
AppendLine(Format('} -> %s', [Result.ToString]));
|
||||
end;
|
||||
|
||||
function TDebugEvaluatorVisitor.VisitTernaryExpression(const Node: ITernaryExpressionNode): TAstValue;
|
||||
begin
|
||||
AppendLine('TernaryExpr{');
|
||||
|
||||
@@ -20,8 +20,8 @@ type
|
||||
function ToString: string;
|
||||
end;
|
||||
|
||||
// Added avkRecord to represent a TScalarRecord value.
|
||||
TAstValueKind = (avkUndefined, avkScalar, avkClosure, avkText, avkRecordSeries, avkRecord);
|
||||
// Added avkMemberSeries to represent a TScalarMemberSeries value.
|
||||
TAstValueKind = (avkUndefined, avkScalar, avkClosure, avkText, avkRecordSeries, avkRecord, avkMemberSeries);
|
||||
|
||||
// --- Forward Declarations to break cycles ---
|
||||
IExecutionScope = interface;
|
||||
@@ -33,15 +33,15 @@ type
|
||||
IBinaryExpressionNode = interface;
|
||||
IUnaryExpressionNode = interface;
|
||||
IIfExpressionNode = interface;
|
||||
// Added forward declaration for the new ternary expression node.
|
||||
ITernaryExpressionNode = interface;
|
||||
ILambdaExpressionNode = interface;
|
||||
IFunctionCallNode = interface;
|
||||
IBlockExpressionNode = interface;
|
||||
IVariableDeclarationNode = interface;
|
||||
IAssignmentNode = interface;
|
||||
// Added forward declaration for the new indexer node.
|
||||
IIndexerNode = interface;
|
||||
// Added forward declaration for the new member access node.
|
||||
IMemberAccessNode = interface;
|
||||
IEvaluatorClosure = interface;
|
||||
|
||||
// --- Concrete Type Definitions ---
|
||||
@@ -88,11 +88,13 @@ type
|
||||
class function FromText(const AValue: String): TAstValue; static;
|
||||
class function FromRecordSeries(const AValue: TScalarRecordSeries): TAstValue; static;
|
||||
class function FromRecord(const AValue: TScalarRecord): TAstValue; static;
|
||||
class function FromMemberSeries(const AValue: TScalarMemberSeries): TAstValue; static;
|
||||
function AsScalar: TScalar;
|
||||
function AsClosure: IEvaluatorClosure;
|
||||
function AsText: String;
|
||||
function AsRecordSeries: TScalarRecordSeries;
|
||||
function AsRecord: TScalarRecord;
|
||||
function AsMemberSeries: TScalarMemberSeries;
|
||||
function ToString: String;
|
||||
property IsVoid: Boolean read GetIsVoid;
|
||||
property Kind: TAstValueKind read GetKind;
|
||||
@@ -123,6 +125,7 @@ type
|
||||
function VisitVariableDeclaration(const Node: IVariableDeclarationNode): TAstValue;
|
||||
function VisitAssignment(const Node: IAssignmentNode): TAstValue;
|
||||
function VisitIndexer(const Node: IIndexerNode): TAstValue;
|
||||
function VisitMemberAccess(const Node: IMemberAccessNode): TAstValue;
|
||||
end;
|
||||
|
||||
IAstNode = interface(IInterface)
|
||||
@@ -243,6 +246,16 @@ type
|
||||
property Index: IExpressionNode read GetIndex;
|
||||
end;
|
||||
|
||||
// Represents a member access expression (e.g., series.field or record.field).
|
||||
IMemberAccessNode = interface(IExpressionNode)
|
||||
{$region 'private'}
|
||||
function GetBase: IExpressionNode;
|
||||
function GetMember: IIdentifierNode;
|
||||
{$endregion}
|
||||
property Base: IExpressionNode read GetBase;
|
||||
property Member: IIdentifierNode read GetMember;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
@@ -276,6 +289,13 @@ begin
|
||||
Result := IEvaluatorClosure(FInterface);
|
||||
end;
|
||||
|
||||
function TAstValue.AsMemberSeries: TScalarMemberSeries;
|
||||
begin
|
||||
if (FKind <> avkMemberSeries) then
|
||||
raise EInvalidCast.Create('Cannot read value as MemberSeries.');
|
||||
Result := (FInterface as IVal<TScalarMemberSeries>).Value;
|
||||
end;
|
||||
|
||||
function TAstValue.AsRecord: TScalarRecord;
|
||||
begin
|
||||
if (FKind <> avkRecord) then
|
||||
@@ -312,6 +332,13 @@ begin
|
||||
Result.FScalar := Default(TScalar);
|
||||
end;
|
||||
|
||||
class function TAstValue.FromMemberSeries(const AValue: TScalarMemberSeries): TAstValue;
|
||||
begin
|
||||
Result.FKind := avkMemberSeries;
|
||||
Result.FInterface := TVal<TScalarMemberSeries>.Create(AValue);
|
||||
Result.FScalar := Default(TScalar);
|
||||
end;
|
||||
|
||||
class function TAstValue.FromRecord(const AValue: TScalarRecord): TAstValue;
|
||||
begin
|
||||
Result.FKind := avkRecord;
|
||||
@@ -358,6 +385,7 @@ begin
|
||||
avkText: Result := AsText;
|
||||
avkRecordSeries: Result := '<record_series>';
|
||||
avkRecord: Result := '<record>';
|
||||
avkMemberSeries: Result := '<member_series>';
|
||||
avkUndefined: Result := '<void>';
|
||||
else
|
||||
Result := '[Unknown AstValue]';
|
||||
|
||||
@@ -34,6 +34,7 @@ type
|
||||
function VisitVariableDeclaration(const Node: IVariableDeclarationNode): TAstValue;
|
||||
function VisitAssignment(const Node: IAssignmentNode): TAstValue;
|
||||
function VisitIndexer(const Node: IIndexerNode): TAstValue;
|
||||
function VisitMemberAccess(const Node: IMemberAccessNode): TAstValue;
|
||||
end;
|
||||
|
||||
implementation
|
||||
@@ -242,4 +243,16 @@ begin
|
||||
Result := TAstValue.Void;
|
||||
end;
|
||||
|
||||
function TPrettyPrintVisitor.VisitMemberAccess(const Node: IMemberAccessNode): TAstValue;
|
||||
begin
|
||||
AppendLine(Format('MemberAccess (Member: %s)', [Node.Member.Name]));
|
||||
Indent;
|
||||
AppendLine('Base:');
|
||||
Indent;
|
||||
Node.Base.Accept(Self);
|
||||
Unindent;
|
||||
Unindent;
|
||||
Result := TAstValue.Void;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
+45
-4
@@ -22,7 +22,6 @@ type
|
||||
const ACondition: IExpressionNode;
|
||||
const AThenBranch, AElseBranch: IExpressionNode
|
||||
): IIfExpressionNode; static;
|
||||
// Added factory function for the new ternary expression node.
|
||||
class function TernaryExpr(
|
||||
const ACondition: IExpressionNode;
|
||||
const AThenBranch, AElseBranch: IExpressionNode
|
||||
@@ -33,8 +32,9 @@ type
|
||||
class function VarDecl(const AIdentifier: IIdentifierNode; AInitializer: IExpressionNode): IVariableDeclarationNode; static;
|
||||
class function Assign(const AIdentifier: IIdentifierNode; const AValue: IExpressionNode): IAssignmentNode; static;
|
||||
class function AssignResult(const AValue: IExpressionNode): IAssignmentNode; static;
|
||||
// Added factory for the new indexer node.
|
||||
class function Indexer(const ABase: IExpressionNode; const AIndex: IExpressionNode): IIndexerNode; static;
|
||||
// Added factory for the new member access node.
|
||||
class function MemberAccess(const ABase: IExpressionNode; const AMember: IIdentifierNode): IMemberAccessNode; static;
|
||||
end;
|
||||
|
||||
implementation
|
||||
@@ -186,7 +186,6 @@ type
|
||||
end;
|
||||
|
||||
{ TIndexerNode }
|
||||
// Concrete implementation for the indexer node.
|
||||
TIndexerNode = class(TAstNode, IIndexerNode)
|
||||
private
|
||||
FBase: IExpressionNode;
|
||||
@@ -198,6 +197,19 @@ type
|
||||
function Accept(const Visitor: IAstVisitor): TAstValue; override;
|
||||
end;
|
||||
|
||||
{ TMemberAccessNode }
|
||||
// Concrete implementation for the member access node.
|
||||
TMemberAccessNode = class(TAstNode, IMemberAccessNode)
|
||||
private
|
||||
FBase: IExpressionNode;
|
||||
FMember: IIdentifierNode;
|
||||
function GetBase: IExpressionNode;
|
||||
function GetMember: IIdentifierNode;
|
||||
public
|
||||
constructor Create(const ABase: IExpressionNode; const AMember: IIdentifierNode);
|
||||
function Accept(const Visitor: IAstVisitor): TAstValue; override;
|
||||
end;
|
||||
|
||||
{ TConstantNode }
|
||||
|
||||
constructor TConstantNode.Create(AValue: TScalar);
|
||||
@@ -506,6 +518,30 @@ begin
|
||||
Result := FIndex;
|
||||
end;
|
||||
|
||||
{ TMemberAccessNode }
|
||||
|
||||
constructor TMemberAccessNode.Create(const ABase: IExpressionNode; const AMember: IIdentifierNode);
|
||||
begin
|
||||
inherited Create;
|
||||
FBase := ABase;
|
||||
FMember := AMember;
|
||||
end;
|
||||
|
||||
function TMemberAccessNode.Accept(const Visitor: IAstVisitor): TAstValue;
|
||||
begin
|
||||
Result := Visitor.VisitMemberAccess(Self);
|
||||
end;
|
||||
|
||||
function TMemberAccessNode.GetBase: IExpressionNode;
|
||||
begin
|
||||
Result := FBase;
|
||||
end;
|
||||
|
||||
function TMemberAccessNode.GetMember: IIdentifierNode;
|
||||
begin
|
||||
Result := FMember;
|
||||
end;
|
||||
|
||||
{ TAst }
|
||||
|
||||
class function TAst.Assign(const AIdentifier: IIdentifierNode; const AValue: IExpressionNode): IAssignmentNode;
|
||||
@@ -553,7 +589,12 @@ begin
|
||||
Result := TIndexerNode.Create(ABase, AIndex);
|
||||
end;
|
||||
|
||||
class function TAst.TernaryExpr(const ACondition, AThenBranch, AElseBranch: IExpressionNode): ITernaryExpressionNode;
|
||||
class function TAst.MemberAccess(const ABase: IExpressionNode; const AMember: IIdentifierNode): IMemberAccessNode;
|
||||
begin
|
||||
Result := TMemberAccessNode.Create(ABase, AMember);
|
||||
end;
|
||||
|
||||
class function TAst.TernaryExpr(const ACondition: IExpressionNode; const AThenBranch, AElseBranch: IExpressionNode): ITernaryExpressionNode;
|
||||
begin
|
||||
Result := TTernaryExpressionNode.Create(ACondition, AThenBranch, AElseBranch);
|
||||
end;
|
||||
|
||||
+22
-27
@@ -160,17 +160,21 @@ type
|
||||
end;
|
||||
|
||||
TScalarMemberSeries = record
|
||||
strict private
|
||||
FKind: TScalarKind;
|
||||
private
|
||||
FDef: TScalarRecordDefinition;
|
||||
FArray: TChunkArray<TScalarValue>;
|
||||
FElement: Integer;
|
||||
function GetCount: Int64; inline;
|
||||
function GetItems(Idx: Integer): TScalarValue;
|
||||
function GetItems(Idx: Integer): TScalar;
|
||||
public
|
||||
constructor Create(const ADef: TScalarRecordDefinition; const AArray: TChunkArray<TScalarValue>; AElement: Integer);
|
||||
function GetKind: TScalarKind; inline;
|
||||
property Count: Int64 read GetCount;
|
||||
property Element: Integer read FElement;
|
||||
property Items[Idx: Integer]: TScalarValue read GetItems; default;
|
||||
property Items[Idx: Integer]: TScalar read GetItems; default;
|
||||
property Kind: TScalarKind read GetKind;
|
||||
end;
|
||||
|
||||
// A time series of scalar records, optimized for memory and access speed.
|
||||
@@ -423,23 +427,13 @@ end;
|
||||
|
||||
function TScalarRecord.GetItems(const Name: String): TScalar;
|
||||
var
|
||||
i: Integer;
|
||||
fieldDef: TScalarRecordField;
|
||||
idx: Integer;
|
||||
begin
|
||||
// Find the index of the field by name (case-insensitive)
|
||||
for i := 0 to High(FDef.Fields) do
|
||||
begin
|
||||
fieldDef := FDef.Fields[i];
|
||||
if (CompareText(fieldDef.Name, Name) = 0) then
|
||||
begin
|
||||
// Found it. Construct the TScalar result from the kind and value.
|
||||
Result.Create(fieldDef.Kind, FFields[i]);
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
idx := FDef.IndexOf(Name);
|
||||
if idx < 0 then
|
||||
raise EArgumentException.CreateFmt('Field "%s" not found in record definition.', [Name]);
|
||||
|
||||
// If we get here, the field was not found.
|
||||
raise EArgumentException.CreateFmt('Field "%s" not found in record definition.', [Name]);
|
||||
Result.Create(FDef.Fields[idx].Kind, FFields[idx]);
|
||||
end;
|
||||
|
||||
{ TScalarSeries }
|
||||
@@ -513,17 +507,20 @@ end;
|
||||
|
||||
function TScalarMemberSeries.GetCount: Int64;
|
||||
begin
|
||||
Result := FArray.Count;
|
||||
Result := FArray.Count div Length(FDef.Fields);
|
||||
end;
|
||||
|
||||
function TScalarMemberSeries.GetItems(Idx: Integer): TScalarValue;
|
||||
function TScalarMemberSeries.GetItems(Idx: Integer): TScalar;
|
||||
var
|
||||
values: TArray<TScalarValue>;
|
||||
fieldCount: Integer;
|
||||
begin
|
||||
fieldCount := Length(FDef.Fields);
|
||||
SetLength(values, fieldCount);
|
||||
Result := FArray.ItemRef[(FArray.Count - fieldCount) - (Idx * fieldCount)]^;
|
||||
Result.Create(FDef.Fields[FElement].Kind, FArray.Items[(FArray.Count - fieldCount) - (Idx * fieldCount) + FElement]);
|
||||
end;
|
||||
|
||||
function TScalarMemberSeries.GetKind: TScalarKind;
|
||||
begin
|
||||
Result := FDef.Fields[FElement].Kind;
|
||||
end;
|
||||
|
||||
constructor TScalarRecordDefinition.Create(const AFields: TArray<TScalarRecordField>);
|
||||
@@ -534,15 +531,13 @@ end;
|
||||
function TScalarRecordDefinition.IndexOf(const Name: String): Integer;
|
||||
var
|
||||
i: Integer;
|
||||
fieldDef: TScalarRecordField;
|
||||
begin
|
||||
// Find the index of the field by name (case-insensitive)
|
||||
for i := 0 to High(FFields) do
|
||||
begin
|
||||
if (CompareText(FFields[i].Name, Name) = 0) then
|
||||
exit(i);
|
||||
|
||||
// If we get here, the field was not found.
|
||||
raise EArgumentException.CreateFmt('Field "%s" not found in record definition.', [Name]);
|
||||
end;
|
||||
Result := -1;
|
||||
end;
|
||||
|
||||
initialization
|
||||
|
||||
Reference in New Issue
Block a user