openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
achandmsft-patch-2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

codegen/generator/src/Visitors/VisitorHelpers.cs

129lines · modecode

1using Microsoft.TypeSpec.Generator.Expressions;
2using Microsoft.TypeSpec.Generator.Snippets;
3using Microsoft.TypeSpec.Generator.Statements;
4using System;
5using System.Collections.Generic;
6using System.Linq;
7
8namespace OpenAILibraryPlugin.Visitors;
9
10internal static class VisitorHelpers
11{
12 internal static void VisitExplodedMethodBodyStatements(
13 IList<MethodBodyStatement?> statements,
14 Func<MethodBodyStatement?, MethodBodyStatement?> visitorFunc)
15 {
16 for (int i = 0; i < statements.Count; i++)
17 {
18 statements[i] = visitorFunc.Invoke(statements[i]);
19
20 if (statements[i] is ForEachStatement foreachStatement)
21 {
22 List<MethodBodyStatement> foreachBodyStatements
23 = foreachStatement.Body
24 .SelectMany(bodyStatement => bodyStatement)
25 .ToList();
26 VisitExplodedMethodBodyStatements(foreachBodyStatements!, visitorFunc);
27 foreachStatement.Body.Clear();
28 foreachStatement.Body.Add(new MethodBodyStatements(foreachBodyStatements));
29 }
30 else if (statements[i] is IfStatement ifStatement)
31 {
32 List<MethodBodyStatement> ifBodyStatements
33 = ifStatement.Body
34 .SelectMany(bodyStatement => bodyStatement)
35 .ToList();
36 VisitExplodedMethodBodyStatements(ifBodyStatements!, visitorFunc);
37 var newIfStatement = new IfStatement(ifStatement.Condition);
38 foreach (MethodBodyStatement bodyStatement in ifBodyStatements)
39 {
40 newIfStatement.Add(bodyStatement);
41 }
42 statements[i] = newIfStatement;
43 }
44 else if (statements[i] is ForStatement forStatement)
45 {
46 // To do: traverse inside of "for"
47 }
48 else if (statements[i] is WhileStatement whileStatement)
49 {
50 List<MethodBodyStatement> whileBodyStatements
51 = whileStatement.Body
52 .SelectMany(bodyStatement => bodyStatement)
53 .ToList();
54 VisitExplodedMethodBodyStatements(whileBodyStatements!, visitorFunc);
55 var newWhileStatement = new WhileStatement(whileStatement.Condition);
56 foreach (MethodBodyStatement bodyStatement in whileBodyStatements)
57 {
58 newWhileStatement.Add(bodyStatement);
59 }
60 statements[i] = newWhileStatement;
61 }
62 }
63 }
64
65 internal static MethodBodyStatement? GetUpdatedIfStatement(
66 MethodBodyStatement? statement,
67 Func<ValueExpression, ValueExpression?> conditionUpdateFunc,
68 string comment)
69 {
70 if (statement is not IfStatement ifStatement)
71 {
72 return statement;
73 }
74
75 List<MethodBodyStatement> statements =
76 [
77 new SingleLineCommentStatement(comment)
78 ];
79
80 ValueExpression? newCondition = GetRecursivelyUpdatedCondition(ifStatement.Condition, conditionUpdateFunc);
81 if (newCondition == ifStatement.Condition)
82 {
83 return ifStatement;
84 }
85 else if (newCondition is null)
86 {
87 statements.Add(ifStatement.Body);
88 }
89 else
90 {
91 var updatedIfStatement = new IfStatement(newCondition, ifStatement.Inline, ifStatement.AddBraces)
92 {
93 ifStatement.Body,
94 };
95
96 statements.Add(updatedIfStatement);
97 }
98
99 return new MethodBodyStatements(statements);
100 }
101
102 private static ValueExpression? GetRecursivelyUpdatedCondition(
103 ValueExpression expression,
104 Func<ValueExpression, ValueExpression?> visitorFunc)
105 {
106 ValueExpression? newExpression = visitorFunc.Invoke(expression);
107 BinaryOperatorExpression? binaryOperatorExpression
108 = expression as BinaryOperatorExpression ?? (expression as ScopedApi<bool>)?.Original as BinaryOperatorExpression;
109
110 if (newExpression == expression && binaryOperatorExpression is not null)
111 {
112 ValueExpression? newLeft = GetRecursivelyUpdatedCondition(binaryOperatorExpression.Left, visitorFunc);
113 ValueExpression? newRight = GetRecursivelyUpdatedCondition(binaryOperatorExpression.Right, visitorFunc);
114 if (newLeft is null)
115 {
116 return newRight;
117 }
118 else if (newRight is null)
119 {
120 return newLeft;
121 }
122 else if (newLeft != binaryOperatorExpression.Left || newRight != binaryOperatorExpression.Right)
123 {
124 return new BinaryOperatorExpression(binaryOperatorExpression.Operator, newLeft, newRight);
125 }
126 }
127 return newExpression;
128 }
129}