Pipe Parameter is now a Tuple

This commit is contained in:
Michael Schimmel
2026-01-05 00:13:51 +01:00
parent 242ec9a56e
commit 264314cd93
15 changed files with 322 additions and 1244 deletions
+84 -257
View File
@@ -18,8 +18,6 @@ type
function Deserialize(const AJson: TJSONObject): IAstNode;
end;
// TJsonAstConverter implements the visitor pattern for serialization
// and uses factory functions for deserialization.
TJsonAstConverter = class(TAstVisitor<TJSONObject>, IJsonAstConverter)
private
procedure DataValueToJson(const AValue: TDataValue; const AParent: TJSONObject; const AName: string);
@@ -52,16 +50,15 @@ type
function JsonToSeriesLengthNode(const AObj: TJSONObject): ISeriesLengthNode;
function JsonToNopNode(const AObj: TJSONObject): INopNode;
function JsonToTupleNode(const AObj: TJSONObject): ITupleNode;
function JsonToPipeNode(const AObj: TJSONObject): IPipeNode;
strict private
// Serialization Visitors (IAstNode signature)
// Serialization Visitors
function VisitConstant(const Node: IAstNode): TJSONObject;
function VisitIdentifier(const Node: IAstNode): TJSONObject;
function VisitKeyword(const Node: IAstNode): TJSONObject;
function VisitTuple(const Node: IAstNode): TJSONObject;
function VisitRecordField(const Node: IAstNode): TJSONObject;
function VisitIfExpression(const Node: IAstNode): TJSONObject;
function VisitCondExpression(const Node: IAstNode): TJSONObject;
function VisitLambdaExpression(const Node: IAstNode): TJSONObject;
@@ -82,10 +79,6 @@ type
function VisitAddSeriesItem(const Node: IAstNode): TJSONObject;
function VisitSeriesLength(const Node: IAstNode): TJSONObject;
function VisitNop(const Node: IAstNode): TJSONObject;
function VisitPipeInput(const Node: IAstNode): TJSONObject;
function VisitPipeSelectorList(const Node: IAstNode): TJSONObject;
function VisitPipeInputList(const Node: IAstNode): TJSONObject;
function VisitPipe(const Node: IAstNode): TJSONObject;
protected
@@ -100,7 +93,8 @@ type
implementation
uses
Myc.Data.Keyword;
Myc.Data.Keyword,
Myc.Ast.Identities;
{ TJsonAstConverter }
@@ -114,10 +108,8 @@ begin
Register(akConstant, VisitConstant);
Register(akIdentifier, VisitIdentifier);
Register(akKeyword, VisitKeyword);
Register(akTuple, VisitTuple);
Register(akRecordField, VisitRecordField);
Register(akIfExpression, VisitIfExpression);
Register(akCondExpression, VisitCondExpression);
Register(akLambdaExpression, VisitLambdaExpression);
@@ -138,19 +130,13 @@ begin
Register(akSeriesLength, VisitSeriesLength);
Register(akRecur, VisitRecurNode);
Register(akNop, VisitNop);
Register(akPipeInput, VisitPipeInput);
Register(akPipeSelectorList, VisitPipeSelectorList);
Register(akPipeInputList, VisitPipeInputList);
Register(akPipe, VisitPipe);
end;
function TJsonAstConverter.Serialize(const RootNode: IAstNode): TJSONObject;
begin
if not Assigned(RootNode) then
begin
exit(nil);
end;
Result := Visit(RootNode);
end;
@@ -159,8 +145,6 @@ begin
Result := JsonToNode(AJson);
end;
{ Serialization Visitors }
procedure TJsonAstConverter.DataValueToJson(const AValue: TDataValue; const AParent: TJSONObject; const AName: string);
var
valObj, scalarObj: TJSONObject;
@@ -222,12 +206,9 @@ begin
T := Node.AsTuple;
Result := TJSONObject.Create;
Result.AddPair('NodeType', TJSONString.Create('Tuple'));
elemsArray := TJSONArray.Create;
for i := 0 to T.Count - 1 do
begin
elemsArray.Add(Visit(T.Items[i]));
end;
Result.AddPair('Elements', elemsArray);
end;
@@ -241,15 +222,10 @@ begin
Result.AddPair('NodeType', TJSONString.Create('IfExpr'));
Result.AddPair('Condition', Visit(E.Condition));
Result.AddPair('ThenBranch', Visit(E.ThenBranch));
if Assigned(E.ElseBranch) then
begin
elseObj := Visit(E.ElseBranch);
end
elseObj := Visit(E.ElseBranch)
else
begin
elseObj := TJSONNull.Create;
end;
Result.AddPair('ElseBranch', elseObj);
end;
@@ -270,16 +246,10 @@ begin
pairObj.AddPair('Branch', Visit(E.Pairs[i].Branch));
pairsArray.Add(pairObj);
end;
if Assigned(E.ElseBranch) then
begin
elseObj := Visit(E.ElseBranch);
end
elseObj := Visit(E.ElseBranch)
else
begin
elseObj := TJSONNull.Create;
end;
Result := TJSONObject.Create;
Result.AddPair('NodeType', TJSONString.Create('CondExpr'));
Result.AddPair('Pairs', pairsArray);
@@ -289,41 +259,23 @@ end;
function TJsonAstConverter.VisitLambdaExpression(const Node: IAstNode): TJSONObject;
var
L: ILambdaExpressionNode;
paramsArray: TJSONArray;
i: Integer;
begin
L := Node.AsLambdaExpression;
paramsArray := TJSONArray.Create;
// Iterate Tuple
for i := 0 to L.Parameters.Count - 1 do
begin
paramsArray.Add(Visit(L.Parameters.Items[i]));
end;
Result := TJSONObject.Create;
Result.AddPair('NodeType', TJSONString.Create('LambdaExpr'));
Result.AddPair('Parameters', paramsArray);
Result.AddPair('Parameters', Visit(L.Parameters));
Result.AddPair('Body', Visit(L.Body));
end;
function TJsonAstConverter.VisitMacroDefinition(const Node: IAstNode): TJSONObject;
var
M: IMacroDefinitionNode;
paramsArray: TJSONArray;
i: Integer;
begin
M := Node.AsMacroDefinition;
paramsArray := TJSONArray.Create;
// Iterate Tuple
for i := 0 to M.Parameters.Count - 1 do
begin
paramsArray.Add(Visit(M.Parameters.Items[i]));
end;
Result := TJSONObject.Create;
Result.AddPair('NodeType', TJSONString.Create('MacroDef'));
Result.AddPair('Name', Visit(M.Name));
Result.AddPair('Parameters', paramsArray);
Result.AddPair('Parameters', Visit(M.Parameters));
Result.AddPair('Body', Visit(M.Body));
end;
@@ -351,80 +303,38 @@ end;
function TJsonAstConverter.VisitFunctionCall(const Node: IAstNode): TJSONObject;
var
C: IFunctionCallNode;
argsArray: TJSONArray;
i: Integer;
begin
C := Node.AsFunctionCall;
argsArray := TJSONArray.Create;
// Iterate Tuple
for i := 0 to C.Arguments.Count - 1 do
begin
argsArray.Add(Visit(C.Arguments.Items[i]));
end;
Result := TJSONObject.Create;
Result.AddPair('NodeType', TJSONString.Create('FunctionCall'));
Result.AddPair('Callee', Visit(C.Callee));
Result.AddPair('Arguments', argsArray);
Result.AddPair('Arguments', Visit(C.Arguments));
end;
function TJsonAstConverter.VisitMacroExpansionNode(const Node: IAstNode): TJSONObject;
var
M: IMacroExpansionNode;
argsArray: TJSONArray;
i: Integer;
begin
M := Node.AsMacroExpansion;
argsArray := TJSONArray.Create;
// Iterate Tuple
for i := 0 to M.CallNode.Arguments.Count - 1 do
begin
argsArray.Add(Visit(M.CallNode.Arguments.Items[i]));
end;
Result := TJSONObject.Create;
Result.AddPair('NodeType', TJSONString.Create('MacroExpansion'));
Result.AddPair('Callee', Visit(M.CallNode.Callee));
Result.AddPair('Arguments', argsArray);
Result.AddPair('Arguments', Visit(M.CallNode.Arguments));
Result.AddPair('ExpandedBody', Visit(M.ExpandedBody));
end;
function TJsonAstConverter.VisitRecurNode(const Node: IAstNode): TJSONObject;
var
R: IRecurNode;
argsArray: TJSONArray;
i: Integer;
begin
R := Node.AsRecur;
argsArray := TJSONArray.Create;
// Iterate Tuple
for i := 0 to R.Arguments.Count - 1 do
begin
argsArray.Add(Visit(R.Arguments.Items[i]));
end;
Result := TJSONObject.Create;
Result.AddPair('NodeType', TJSONString.Create('Recur'));
Result.AddPair('Arguments', argsArray);
Result.AddPair('Arguments', Visit(Node.AsRecur.Arguments));
end;
function TJsonAstConverter.VisitBlockExpression(const Node: IAstNode): TJSONObject;
var
B: IBlockExpressionNode;
exprsArray: TJSONArray;
i: Integer;
begin
B := Node.AsBlockExpression;
exprsArray := TJSONArray.Create;
// Iterate Tuple
for i := 0 to B.Expressions.Count - 1 do
begin
exprsArray.Add(Visit(B.Expressions.Items[i]));
end;
Result := TJSONObject.Create;
Result.AddPair('NodeType', TJSONString.Create('Block'));
Result.AddPair('Expressions', exprsArray);
Result.AddPair('Expressions', Visit(Node.AsBlockExpression.Expressions));
end;
function TJsonAstConverter.VisitVariableDeclaration(const Node: IAstNode): TJSONObject;
@@ -434,14 +344,9 @@ var
begin
V := Node.AsVariableDeclaration;
if Assigned(V.Initializer) then
begin
initObj := Visit(V.Initializer);
end
initObj := Visit(V.Initializer)
else
begin
initObj := TJSONNull.Create;
end;
Result := TJSONObject.Create;
Result.AddPair('NodeType', TJSONString.Create('VarDecl'));
Result.AddPair('Identifier', Visit(V.Target));
@@ -482,21 +387,10 @@ begin
end;
function TJsonAstConverter.VisitRecordLiteral(const Node: IAstNode): TJSONObject;
var
R: IRecordLiteralNode;
fieldsArray: TJSONArray;
i: Integer;
begin
R := Node.AsRecordLiteral;
Result := TJSONObject.Create;
Result.AddPair('NodeType', TJSONString.Create('RecordLiteral'));
fieldsArray := TJSONArray.Create;
// Iterate Tuple
for i := 0 to R.Fields.Count - 1 do
begin
fieldsArray.Add(Visit(R.Fields.Items[i]));
end;
Result.AddPair('Fields', fieldsArray);
Result.AddPair('Fields', Visit(Node.AsRecordLiteral.Fields));
end;
function TJsonAstConverter.VisitRecordField(const Node: IAstNode): TJSONObject;
@@ -523,14 +417,9 @@ var
begin
A := Node.AsAddSeriesItem;
if Assigned(A.Lookback) then
begin
lookbackObj := Visit(A.Lookback);
end
lookbackObj := Visit(A.Lookback)
else
begin
lookbackObj := TJSONNull.Create;
end;
Result := TJSONObject.Create;
Result.AddPair('NodeType', TJSONString.Create('AddSeriesItem'));
Result.AddPair('Series', Visit(A.Series));
@@ -551,21 +440,15 @@ begin
Result.AddPair('NodeType', TJSONString.Create('Nop'));
end;
function TJsonAstConverter.VisitPipeInput(const Node: IAstNode): TJSONObject;
begin
raise ENotSupportedException.Create('JSON Pipe Serialization not implemented.');
end;
function TJsonAstConverter.VisitPipeSelectorList(const Node: IAstNode): TJSONObject;
begin
raise ENotSupportedException.Create('JSON Pipe Serialization not implemented.');
end;
function TJsonAstConverter.VisitPipeInputList(const Node: IAstNode): TJSONObject;
begin
raise ENotSupportedException.Create('JSON Pipe Serialization not implemented.');
end;
function TJsonAstConverter.VisitPipe(const Node: IAstNode): TJSONObject;
var
P: IPipeNode;
begin
raise ENotSupportedException.Create('JSON Pipe Serialization not implemented.');
P := Node.AsPipe;
Result := TJSONObject.Create;
Result.AddPair('NodeType', TJSONString.Create('Pipe'));
Result.AddPair('Inputs', Visit(P.Inputs));
Result.AddPair('Transformation', Visit(P.Transformation));
end;
{ Deserialization Handlers }
@@ -590,17 +473,11 @@ begin
end;
end
else if SameText(kindStr, 'Text') then
begin
Result := valObj.GetValue<string>('Value');
end
Result := valObj.GetValue<string>('Value')
else if SameText(kindStr, 'Void') then
begin
Result := TDataValue.Void;
end
Result := TDataValue.Void
else
begin
raise ENotSupportedException.Create('Unsupported TDataValue kind.');
end;
end;
function TJsonAstConverter.JsonToNode(const AJson: TJSONValue; const ExpectedType: string): IAstNode;
@@ -609,9 +486,7 @@ var
nodeType: string;
begin
if not (AJson is TJSONObject) then
begin
raise EInvalidCast.Create('Expected JSON object.');
end;
obj := AJson as TJSONObject;
nodeType := obj.GetValue<string>('NodeType');
@@ -665,6 +540,8 @@ begin
Result := JsonToNopNode(obj)
else if nodeType = 'Tuple' then
Result := JsonToTupleNode(obj)
else if nodeType = 'Pipe' then
Result := JsonToPipeNode(obj)
else
raise ENotSupportedException.CreateFmt('Unsupported NodeType "%s".', [nodeType]);
end;
@@ -689,14 +566,9 @@ var
elseNode: IAstNode;
begin
if AObj.GetValue('ElseBranch') is TJSONNull then
begin
elseNode := nil;
end
elseNode := nil
else
begin
elseNode := JsonToNode(AObj.GetValue('ElseBranch'));
end;
Result := TAst.IfExpr(JsonToNode(AObj.GetValue('Condition')), JsonToNode(AObj.GetValue('ThenBranch')), elseNode);
end;
@@ -710,23 +582,15 @@ begin
pairsArray := AObj.GetValue<TJSONArray>('Pairs');
SetLength(pairs, pairsArray.Count);
for i := 0 to pairsArray.Count - 1 do
begin
pairs[i] :=
TCondPair.Create(
JsonToNode((pairsArray.Items[i] as TJSONObject).GetValue('Condition')),
JsonToNode((pairsArray.Items[i] as TJSONObject).GetValue('Branch'))
);
end;
if AObj.GetValue('ElseBranch') is TJSONNull then
begin
elseNode := nil;
end
elseNode := nil
else
begin
elseNode := JsonToNode(AObj.GetValue('ElseBranch'));
end;
Result := TAst.CondExpr(pairs, elseNode);
end;
@@ -742,32 +606,37 @@ end;
function TJsonAstConverter.JsonToLambdaExprNode(const AObj: TJSONObject): ILambdaExpressionNode;
var
params: TArray<IIdentifierNode>;
paramArray: TJSONArray;
i: Integer;
identNodes: TList<IIdentifierNode>;
node: IAstNode;
begin
paramArray := AObj.GetValue<TJSONArray>('Parameters');
SetLength(params, paramArray.Count);
for i := 0 to paramArray.Count - 1 do
begin
params[i] := JsonToIdentifierNode(paramArray.Items[i] as TJSONObject);
identNodes := TList<IIdentifierNode>.Create;
try
for node in JsonToNode(AObj.GetValue('Parameters')).AsTuple.ToArray do
identNodes.Add(node.AsIdentifier);
Result := TAst.LambdaExpr(identNodes.ToArray, JsonToNode(AObj.GetValue('Body')));
finally
identNodes.Free;
end;
Result := TAst.LambdaExpr(params, JsonToNode(AObj.GetValue('Body')));
end;
function TJsonAstConverter.JsonToMacroDefNode(const AObj: TJSONObject): IMacroDefinitionNode;
var
params: TArray<IIdentifierNode>;
paramArray: TJSONArray;
i: Integer;
identNodes: TList<IIdentifierNode>;
node: IAstNode;
begin
paramArray := AObj.GetValue<TJSONArray>('Parameters');
SetLength(params, paramArray.Count);
for i := 0 to paramArray.Count - 1 do
begin
params[i] := JsonToIdentifierNode(paramArray.Items[i] as TJSONObject);
identNodes := TList<IIdentifierNode>.Create;
try
for node in JsonToNode(AObj.GetValue('Parameters')).AsTuple.ToArray do
identNodes.Add(node.AsIdentifier);
Result :=
TAst.MacroDef(
JsonToIdentifierNode(AObj.GetValue('Name') as TJSONObject),
identNodes.ToArray,
JsonToNode(AObj.GetValue('Body'))
);
finally
identNodes.Free;
end;
Result := TAst.MacroDef(JsonToIdentifierNode(AObj.GetValue('Name') as TJSONObject), params, JsonToNode(AObj.GetValue('Body')));
end;
function TJsonAstConverter.JsonToQuasiquoteNode(const AObj: TJSONObject): IQuasiquoteNode;
@@ -786,69 +655,29 @@ begin
end;
function TJsonAstConverter.JsonToFunctionCallNode(const AObj: TJSONObject): IFunctionCallNode;
var
args: TArray<IAstNode>;
argsArray: TJSONArray;
i: Integer;
begin
argsArray := AObj.GetValue<TJSONArray>('Arguments');
SetLength(args, argsArray.Count);
for i := 0 to argsArray.Count - 1 do
begin
args[i] := JsonToNode(argsArray.Items[i]);
end;
Result := TAst.FunctionCall(JsonToNode(AObj.GetValue('Callee')), args);
Result := TAst.FunctionCall(JsonToNode(AObj.GetValue('Callee')), JsonToNode(AObj.GetValue('Arguments')).AsTuple.ToArray);
end;
function TJsonAstConverter.JsonToMacroExpansionNode(const AObj: TJSONObject): IMacroExpansionNode;
var
callee, expandedBody: IAstNode;
args: TArray<IAstNode>;
argsArray: TJSONArray;
i: Integer;
tempCallNode: IFunctionCallNode;
argsTuple: ITupleNode;
begin
callee := JsonToNode(AObj.GetValue('Callee'));
expandedBody := JsonToNode(AObj.GetValue('ExpandedBody'));
argsArray := AObj.GetValue<TJSONArray>('Arguments');
SetLength(args, argsArray.Count);
for i := 0 to argsArray.Count - 1 do
begin
args[i] := JsonToNode(argsArray.Items[i]);
end;
tempCallNode := TAst.FunctionCall(callee, args);
Result := TAst.MacroExpansionNode(tempCallNode, expandedBody);
argsTuple := JsonToNode(AObj.GetValue('Arguments')).AsTuple;
Result := TAst.MacroExpansionNode(TAst.FunctionCall(callee, argsTuple.ToArray), expandedBody);
end;
function TJsonAstConverter.JsonToRecurNode(const AObj: TJSONObject): IRecurNode;
var
args: TArray<IAstNode>;
argsArray: TJSONArray;
i: Integer;
begin
argsArray := AObj.GetValue<TJSONArray>('Arguments');
SetLength(args, argsArray.Count);
for i := 0 to argsArray.Count - 1 do
begin
args[i] := JsonToNode(argsArray.Items[i]);
end;
Result := TAst.Recur(args);
Result := TAst.Recur(JsonToNode(AObj.GetValue('Arguments')).AsTuple.ToArray);
end;
function TJsonAstConverter.JsonToBlockNode(const AObj: TJSONObject): IBlockExpressionNode;
var
exprs: TArray<IAstNode>;
exprsArray: TJSONArray;
i: Integer;
begin
exprsArray := AObj.GetValue<TJSONArray>('Expressions');
SetLength(exprs, exprsArray.Count);
for i := 0 to exprsArray.Count - 1 do
begin
exprs[i] := JsonToNode(exprsArray.Items[i]);
end;
Result := TAst.Block(exprs);
Result := TAst.Block(JsonToNode(AObj.GetValue('Expressions')).AsTuple.ToArray);
end;
function TJsonAstConverter.JsonToVarDeclNode(const AObj: TJSONObject): IVariableDeclarationNode;
@@ -856,14 +685,9 @@ var
initNode: IAstNode;
begin
if AObj.GetValue('Initializer') is TJSONNull then
begin
initNode := nil;
end
initNode := nil
else
begin
initNode := JsonToNode(AObj.GetValue('Initializer'));
end;
Result := TAst.VarDecl(JsonToIdentifierNode(AObj.GetValue('Identifier') as TJSONObject), initNode);
end;
@@ -879,26 +703,27 @@ end;
function TJsonAstConverter.JsonToMemberAccessNode(const AObj: TJSONObject): IMemberAccessNode;
begin
Result := TAst.MemberAccess(JsonToNode(AObj.GetValue('Base')), JsonToKeywordNode(AObj.GetValue('Member') as TJSONObject));
Result := TAst.MemberAccess(JsonToNode(AObj.GetValue('Base')), JsonToKeywordNode(AObj.GetValue('Member') as TJSONObject).AsKeyword);
end;
function TJsonAstConverter.JsonToRecordLiteralNode(const AObj: TJSONObject): IRecordLiteralNode;
var
fields: TArray<IRecordFieldNode>;
fieldsArray: TJSONArray;
fields: TList<IRecordFieldNode>;
i: Integer;
begin
fieldsArray := AObj.GetValue<TJSONArray>('Fields');
SetLength(fields, fieldsArray.Count);
for i := 0 to fieldsArray.Count - 1 do
begin
fields[i] :=
TAst.RecordField(
TAst.Keyword((fieldsArray.Items[i] as TJSONObject).GetValue<string>('Name')),
JsonToNode((fieldsArray.Items[i] as TJSONObject).GetValue('Value'))
);
fields := TList<IRecordFieldNode>.Create;
try
for i := 0 to fieldsArray.Count - 1 do
begin
var fieldObj := fieldsArray.Items[i] as TJSONObject;
fields.Add(TAst.RecordField(TAst.Keyword(fieldObj.GetValue<string>('Name')), JsonToNode(fieldObj.GetValue('Value'))));
end;
Result := TAst.RecordLiteral(fields.ToArray);
finally
fields.Free;
end;
Result := TAst.RecordLiteral(fields);
end;
function TJsonAstConverter.JsonToCreateSeriesNode(const AObj: TJSONObject): ICreateSeriesNode;
@@ -911,14 +736,9 @@ var
lookNode: IAstNode;
begin
if AObj.GetValue('Lookback') is TJSONNull then
begin
lookNode := nil;
end
lookNode := nil
else
begin
lookNode := JsonToNode(AObj.GetValue('Lookback'));
end;
Result :=
TAst.AddSeriesItem(JsonToIdentifierNode(AObj.GetValue('Series') as TJSONObject), JsonToNode(AObj.GetValue('Value')), lookNode);
end;
@@ -935,17 +755,24 @@ end;
function TJsonAstConverter.JsonToTupleNode(const AObj: TJSONObject): ITupleNode;
var
elems: TArray<IAstNode>;
elemsArray: TJSONArray;
elems: TList<IAstNode>;
i: Integer;
begin
elemsArray := AObj.GetValue<TJSONArray>('Elements');
SetLength(elems, elemsArray.Count);
for i := 0 to elemsArray.Count - 1 do
begin
elems[i] := JsonToNode(elemsArray.Items[i]);
elems := TList<IAstNode>.Create;
try
for i := 0 to elemsArray.Count - 1 do
elems.Add(JsonToNode(elemsArray.Items[i]));
Result := TAst.Tuple(elems.ToArray);
finally
elems.Free;
end;
Result := TAst.Tuple(elems); // Factory handles identity/type defaults
end;
function TJsonAstConverter.JsonToPipeNode(const AObj: TJSONObject): IPipeNode;
begin
Result := TAst.Pipe(JsonToNode(AObj.GetValue('Inputs')).AsTuple, JsonToNode(AObj.GetValue('Transformation')).AsLambdaExpression);
end;
end.