This repository was archived by the owner on Jan 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathProcedure.cs
More file actions
310 lines (285 loc) · 12 KB
/
Procedure.cs
File metadata and controls
310 lines (285 loc) · 12 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Schemy
{
/// <summary>
/// Represents a procedure value in Scheme
/// </summary>
interface ICallable
{
/// <summary>
/// Invokes this procedure
/// </summary>
/// <param name="args">The arguments. These are the `cdr` of the s-expression for the procedure invocation.</param>
/// <returns>the result of the procedure invocation</returns>
object Call(List<object> args);
}
/// <summary>
/// A procedure implemented in Scheme
/// </summary>
/// <seealso cref="Schemy.ICallable" />
public class Procedure : ICallable
{
private readonly Union<Symbol, List<Symbol>> parameters;
private readonly object body;
private readonly Environment env;
public Procedure(Union<Symbol, List<Symbol>> parameters, object body, Environment env)
{
this.parameters = parameters;
this.body = body;
this.env = env;
}
public object Body
{
get { return this.body; }
}
public Union<Symbol, List<Symbol>> Parameters
{
get { return this.parameters; }
}
public Environment Env
{
get { return this.env; }
}
/// <summary>
/// Invokes this procedure
/// </summary>
/// <remarks>
/// Implementation note: under normal function invocation scenarios, this method is not used. Instead,
/// a tail call optimization is used in the interpreter evaluation phase that runs Scheme functions.
///
/// This method is useful however, in macro expansions, and any other occasions where the tail call optimization
/// is not (yet) implemented.
///
/// <see cref="Interpreter.EvaluateExpression(object, Environment)"/>
/// </remarks>
public object Call(List<object> args)
{
// NOTE: This is not needed for regular function invoke after the tail call optimization.
// a (non-native) procedure is now optimized into evaluating the body under the environment
// formed by the (params, args). So the `Call` method will never be used.
return Interpreter.EvaluateExpression(this.body, Environment.FromVariablesAndValues(this.parameters, args, this.env));
}
/// <summary>
/// Prints the implementation of the function.
/// </summary>
public override string ToString()
{
var form = new List<object> { Symbol.LAMBDA, this.parameters.Use(sym => (object)sym, syms => syms.Cast<object>().ToList()), this.body };
return Utils.PrintExpr(form);
}
}
/// <summary>
/// A procedure implemented in .NET
/// </summary>
/// <seealso cref="Schemy.ICallable" />
public class NativeProcedure : ICallable
{
private readonly Func<List<object>, object> func;
private readonly string name;
public NativeProcedure(Func<List<object>, object> func, string name = null)
{
this.func = func;
this.name = name;
}
public object Call(List<object> args)
{
return this.func(args);
}
/// <summary>
/// Convenient function method to create a native procedure and doing arity and type check for inputs. It makes the input function
/// implementation strongly typed.
/// </summary>
/// <see cref="Create{T1, T2}(Func{T1, T2}, string)"/>
public static NativeProcedure Create<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> func, string name = null)
{
return new NativeProcedure(args =>
{
Utils.CheckArity(args, 9);
return func(
Utils.ConvertType<T1>(args[0]),
Utils.ConvertType<T2>(args[1]),
Utils.ConvertType<T3>(args[2]),
Utils.ConvertType<T4>(args[3]),
Utils.ConvertType<T5>(args[4]),
Utils.ConvertType<T6>(args[5]),
Utils.ConvertType<T7>(args[6]),
Utils.ConvertType<T8>(args[7]),
Utils.ConvertType<T9>(args[8])
);
}, name);
}
/// <summary>
/// Convenient function method to create a native procedure and doing arity and type check for inputs. It makes the input function
/// implementation strongly typed.
/// </summary>
/// <see cref="Create{T1, T2}(Func{T1, T2}, string)"/>
public static NativeProcedure Create<T1, T2, T3, T4, T5, T6, T7, T8, T9>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9> func, string name = null)
{
return new NativeProcedure(args =>
{
Utils.CheckArity(args, 8);
return func(
Utils.ConvertType<T1>(args[0]),
Utils.ConvertType<T2>(args[1]),
Utils.ConvertType<T3>(args[2]),
Utils.ConvertType<T4>(args[3]),
Utils.ConvertType<T5>(args[4]),
Utils.ConvertType<T6>(args[5]),
Utils.ConvertType<T7>(args[6]),
Utils.ConvertType<T8>(args[7])
);
}, name);
}
/// <summary>
/// Convenient function method to create a native procedure and doing arity and type check for inputs. It makes the input function
/// implementation strongly typed.
/// </summary>
/// <see cref="Create{T1, T2}(Func{T1, T2}, string)"/>
public static NativeProcedure Create<T1, T2, T3, T4, T5, T6, T7, T8>(Func<T1, T2, T3, T4, T5, T6, T7, T8> func, string name = null)
{
return new NativeProcedure(args =>
{
Utils.CheckArity(args, 7);
return func(
Utils.ConvertType<T1>(args[0]),
Utils.ConvertType<T2>(args[1]),
Utils.ConvertType<T3>(args[2]),
Utils.ConvertType<T4>(args[3]),
Utils.ConvertType<T5>(args[4]),
Utils.ConvertType<T6>(args[5]),
Utils.ConvertType<T7>(args[6])
);
}, name);
}
/// <summary>
/// Convenient function method to create a native procedure and doing arity and type check for inputs. It makes the input function
/// implementation strongly typed.
/// </summary>
/// <see cref="Create{T1, T2}(Func{T1, T2}, string)"/>
public static NativeProcedure Create<T1, T2, T3, T4, T5, T6, T7>(Func<T1, T2, T3, T4, T5, T6, T7> func, string name = null)
{
return new NativeProcedure(args =>
{
Utils.CheckArity(args, 6);
return func(
Utils.ConvertType<T1>(args[0]),
Utils.ConvertType<T2>(args[1]),
Utils.ConvertType<T3>(args[2]),
Utils.ConvertType<T4>(args[3]),
Utils.ConvertType<T5>(args[4]),
Utils.ConvertType<T6>(args[5])
);
}, name);
}
/// <summary>
/// Convenient function method to create a native procedure and doing arity and type check for inputs. It makes the input function
/// implementation strongly typed.
/// </summary>
/// <see cref="Create{T1, T2}(Func{T1, T2}, string)"/>
public static NativeProcedure Create<T1, T2, T3, T4, T5, T6>(Func<T1, T2, T3, T4, T5, T6> func, string name = null)
{
return new NativeProcedure(args =>
{
Utils.CheckArity(args, 5);
return func(
Utils.ConvertType<T1>(args[0]),
Utils.ConvertType<T2>(args[1]),
Utils.ConvertType<T3>(args[2]),
Utils.ConvertType<T4>(args[3]),
Utils.ConvertType<T5>(args[4])
);
}, name);
}
/// <summary>
/// Convenient function method to create a native procedure and doing arity and type check for inputs. It makes the input function
/// implementation strongly typed.
/// </summary>
/// <see cref="Create{T1, T2}(Func{T1, T2}, string)"/>
public static NativeProcedure Create<T1, T2, T3, T4, T5>(Func<T1, T2, T3, T4, T5> func, string name = null)
{
return new NativeProcedure(args =>
{
Utils.CheckArity(args, 4);
return func(
Utils.ConvertType<T1>(args[0]),
Utils.ConvertType<T2>(args[1]),
Utils.ConvertType<T3>(args[2]),
Utils.ConvertType<T4>(args[3]));
}, name);
}
/// <summary>
/// Convenient function method to create a native procedure and doing arity and type check for inputs. It makes the input function
/// implementation strongly typed.
/// </summary>
/// <see cref="Create{T1, T2}(Func{T1, T2}, string)"/>
public static NativeProcedure Create<T1, T2, T3, T4>(Func<T1, T2, T3, T4> func, string name = null)
{
return new NativeProcedure(args =>
{
Utils.CheckArity(args, 3);
return func(
Utils.ConvertType<T1>(args[0]),
Utils.ConvertType<T2>(args[1]),
Utils.ConvertType<T3>(args[2]));
}, name);
}
/// <summary>
/// Convenient function method to create a native procedure and doing arity and type check for inputs. It makes the input function
/// implementation strongly typed.
/// </summary>
/// <see cref="Create{T1, T2}(Func{T1, T2}, string)"/>
public static NativeProcedure Create<T1, T2, T3>(Func<T1, T2, T3> func, string name = null)
{
return new NativeProcedure(args =>
{
Utils.CheckArity(args, 2);
return func(
Utils.ConvertType<T1>(args[0]),
Utils.ConvertType<T2>(args[1]));
}, name);
}
/// <summary>
/// Convenient function method to create a native procedure and doing arity and type check for inputs. It makes the input function
/// implementation strongly typed.
/// </summary>
/// <typeparam name="T1">The type of the 1st argument</typeparam>
/// <typeparam name="T2">The type of the 2nd argument</typeparam>
/// <param name="func">The function implementation</param>
/// <param name="name">The name of the function</param>
public static NativeProcedure Create<T1, T2>(Func<T1, T2> func, string name = null)
{
return new NativeProcedure(args =>
{
Utils.CheckArity(args, 1);
return func(
Utils.ConvertType<T1>(args[0])
);
}, name);
}
/// <summary>
/// Convenient function method to create a native procedure and doing arity and type check for inputs. It makes the input function
/// implementation strongly typed.
/// </summary>
/// <see cref="Create{T1, T2}(Func{T1, T2}, string)"/>
public static NativeProcedure Create<T1>(Func<T1> func, string name = null)
{
return new NativeProcedure(args =>
{
Utils.CheckArity(args, 0);
return func();
}, name);
}
/// <summary>
/// ToString implementation
/// </summary>
/// <returns>the string representation</returns>
public override string ToString()
{
return string.Format("#<NativeProcedure:{0}>", string.IsNullOrEmpty(this.name) ? "noname" : this.name);
}
}
}