-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionBinder.cs
201 lines (178 loc) · 7.92 KB
/
FunctionBinder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System.Reflection;
using PseudoCode.Core.Runtime.Instances;
using PseudoCode.Core.Runtime.Operations;
using PseudoCode.Core.Runtime.Types;
using PseudoCode.Core.Runtime.Types.Descriptor;
using Type = System.Type;
namespace PseudoCode.Core.Runtime.Reflection;
public class FunctionBinder
{
public delegate Instance BuiltinFunction(Scope parentScope, PseudoProgram program, Instance[] arguments);
public static readonly Dictionary<Type, string> TypeMap = new()
{
[typeof(int)] = "INTEGER",
[typeof(RealNumberType)] = "REAL",
[typeof(string)] = "STRING",
[typeof(char)] = "CHAR",
[typeof(bool)] = "BOOLEAN",
[typeof(DateOnly)] = "DATE",
[typeof(void)] = "NULL",
[typeof(object)] = "ANY",
[typeof(HashSet<Instance>)] = "SET"
};
// Will be extended to support arrays
public static ITypeDescriptor GetTypeDescriptorFromSystemType(Type type)
{
return new PlainTypeDescriptor(TypeMap[type]);
}
public static void AddBuiltinFunctionOperations(Type type, Scope parentScope, PseudoProgram program)
{
foreach (var (definition, func) in MakeDefinition(type, parentScope, program))
parentScope.AddOperation(new MakeBuiltinFunctionOperation(parentScope, program)
{
Name = definition.Name,
Definition = definition,
Func = func,
PoiLocation = SourceLocation.Identity,
SourceRange = SourceRange.Identity
});
}
public static IEnumerable<(Definition, BuiltinFunction)> MakeDefinition(Type type, Scope parentScope,
PseudoProgram program)
{
var methods = type.GetMethods();
foreach (var methodInfo in methods)
if (MakeDefinitionOfMethod(parentScope, program, methodInfo, out var definition))
yield return (definition, (BuiltinFunction)methodInfo.CreateDelegate(typeof(BuiltinFunction)));
else if (MakeDefinitionOfNativeMethod(parentScope, program, methodInfo, out var nativeDefinition))
yield return (nativeDefinition, (scope, pseudoProgram, arguments) =>
{
var nativeDefinitionType = (BuiltinFunctionType)nativeDefinition.Type;
var res = methodInfo.Invoke(null,
arguments.Select(instance => instance.Get<object>()).ToArray());
return res == null ? Instance.Null : nativeDefinitionType.ReturnType.Type.Instance(res);
});
}
private static bool MakeDefinitionOfMethod(Scope parentScope, PseudoProgram program, MethodInfo methodInfo,
out Definition definition)
{
if (methodInfo.GetCustomAttributes(typeof(BuiltinFunctionAttribute), true).Length == 0)
{
definition = null;
return false;
}
var functionName = GetFunctionName(methodInfo);
var documentation = GetFunctionDocumentation(methodInfo);
var paramList = GetMethodParamList(parentScope, program, methodInfo).ToArray();
var returnDef = GetMethodReturnDefinition(parentScope, program, methodInfo);
definition = new Definition(parentScope, program)
{
Name = functionName,
References = new List<SourceRange>(),
SourceRange = SourceRange.Identity,
Type = new BuiltinFunctionType(parentScope, program)
{
ParameterInfos = paramList,
ReturnType = returnDef
},
TypeDescriptor = new FunctionDescriptor(returnDef?.TypeDescriptor, paramList),
Documentation = documentation
};
return true;
}
private static bool MakeDefinitionOfNativeMethod(Scope parentScope, PseudoProgram program, MethodInfo methodInfo,
out Definition definition)
{
if (methodInfo.GetCustomAttributes(typeof(BuiltinNativeFunctionAttribute), true).Length == 0)
{
definition = null;
return false;
}
var functionName = GetFunctionName(methodInfo);
var documentation = GetFunctionDocumentation(methodInfo);
var paramList = GetNativeMethodParamList(parentScope, program, methodInfo).ToArray();
var returnDef = GetNativeMethodReturnDefinition(parentScope, program, methodInfo);
definition = new Definition(parentScope, program)
{
Name = functionName,
References = new List<SourceRange>(),
SourceRange = SourceRange.Identity,
Type = new BuiltinFunctionType(parentScope, program)
{
ParameterInfos = paramList,
ReturnType = returnDef
},
TypeDescriptor = new FunctionDescriptor(returnDef?.TypeDescriptor, paramList),
Documentation = documentation
};
return true;
}
private static string GetFunctionDocumentation(MethodInfo methodInfo)
{
if (methodInfo.GetCustomAttribute(typeof(DocumentationAttribute)) is DocumentationAttribute attribute)
{
return attribute.Documentation;
}
return "";
}
private static string GetFunctionName(MethodInfo methodInfo)
{
if (methodInfo.GetCustomAttributes(typeof(BuiltinFunctionAttribute)) is BuiltinFunctionAttribute[] nameAttrs &&
nameAttrs.Length != 0)
return nameAttrs[0].Name;
if (methodInfo.GetCustomAttributes(typeof(BuiltinNativeFunctionAttribute)) is BuiltinNativeFunctionAttribute[]
nativeNameAttrs &&
nativeNameAttrs.Length != 0)
return nativeNameAttrs[0].Name;
return methodInfo.Name;
}
private static List<Definition> GetMethodParamList(Scope parentScope, PseudoProgram program, MethodInfo methodInfo)
{
return methodInfo.GetCustomAttributes(typeof(ParamTypeAttribute))
.Cast<ParamTypeAttribute>()
.Select(param => new Definition(parentScope, program)
{
Name = param.Name,
Attributes =
param.IsReference ? Definition.Attribute.Reference : Definition.Attribute.Immutable,
SourceRange = SourceRange.Identity,
TypeDescriptor = param.MakeTypeDescriptor()
}).ToList();
}
private static Definition GetMethodReturnDefinition(Scope parentScope, PseudoProgram program, MethodInfo methodInfo)
{
if (methodInfo.GetCustomAttributes(typeof(ReturnTypeAttribute)) is not ReturnTypeAttribute[]
returnTypeAttributes || returnTypeAttributes.Length == 0) return null;
var typeDescriptor = returnTypeAttributes[0].MakeTypeDescriptor();
return new Definition(parentScope, program)
{
Name = typeDescriptor.ToString(),
TypeDescriptor = typeDescriptor,
SourceRange = SourceRange.Identity,
Attributes = Definition.Attribute.Type | Definition.Attribute.Immutable
};
}
private static List<Definition> GetNativeMethodParamList(Scope parentScope, PseudoProgram program,
MethodInfo methodInfo)
{
return methodInfo.GetParameters().Select(info => new Definition(parentScope, program)
{
Name = info.Name,
TypeDescriptor = GetTypeDescriptorFromSystemType(info.ParameterType),
Attributes = Definition.Attribute.Immutable,
SourceRange = SourceRange.Identity
}).ToList();
}
private static Definition GetNativeMethodReturnDefinition(Scope parentScope, PseudoProgram program,
MethodInfo methodInfo)
{
var typeDescriptor = GetTypeDescriptorFromSystemType(methodInfo.ReturnType);
return new Definition(parentScope, program)
{
Name = typeDescriptor.ToString(),
TypeDescriptor = typeDescriptor,
SourceRange = SourceRange.Identity,
Attributes = Definition.Attribute.Type | Definition.Attribute.Immutable
};
}
}