munificent / magpie

The Magpie programming language.

Clone this repository (size: 897.6 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/munificent/magpie/
commit 36: 46d09f708b11
parent 35: d78a20a8a165
branch: default
Random intrinsic.
Robert Nystrom / munificent
7 months ago
r36:46d09f708b11 474 loc 18.5 KB embed / history / annotate / raw /
  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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Magpie.Compilation
{
    /// <summary>
    /// Generates a bound function body where all names have been resolved.
    /// </summary>
    public class FunctionBinder : IUnboundExprVisitor<IBoundExpr>
    {
        public static void Bind(BindingContext context, Function function)
        {
            if (function.Type.Return == null) throw new InvalidOperationException("Can only bind functions whose type is already bound.");

            var scope = new Scope();

            // create a local slot for the arg if there is one
            if (function.Type.Parameter.Unbound != Decl.Unit)
            {
                scope.Define("__arg", Decl.Unit /* ignored */, false);
            }

            var binder = new FunctionBinder(function, context, scope);

            // bind the function
            function.Bind(binder);

            // make sure declared return type matches actual return type
            if (!DeclComparer.TypesMatch(function.Type.Return.Bound, function.Body.Bound.Type))
            {
                if (function.Body.Bound.Type == Decl.EarlyReturn)
                {
                    //### bob: should find the return expr
                    throw new CompileException(function.Position, "Unneeded explicit \"return\".");
                }
                else
                {
                    throw new CompileException(function.Position, String.Format("{0} is declared to return {1} but is returning {2}.",
                        function.Name, function.Type.Return.Bound, function.Body.Bound.Type));
                }
            }
        }

        public Scope Scope { get; private set; }

        #region IUnboundExprVisitor<IBoundExpr> Members

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(CallExpr expr)
        {
            var boundArg = expr.Arg.Accept(this);

            var namedTarget = expr.Target as NameExpr;
            if (namedTarget != null)
            {
                return mContext.Compiler.ResolveName(mFunction, Scope, namedTarget.Position,
                    namedTarget.Name, namedTarget.TypeArgs, boundArg);
            }

            IBoundExpr target = expr.Target.Accept(this);

            // see if we're calling a function
            FuncType funcType = target.Type as FuncType;
            if (funcType != null)
            {
                // check that args match
                if (!DeclComparer.TypesMatch(funcType.Parameter.Bound, boundArg.Type))
                {
                    throw new CompileException(expr.Position, "Argument types passed to evaluated function reference do not match function's parameter types.");
                }

                // simply apply the arg to the bound expression
                return new BoundCallExpr(target, boundArg);
            }

            // not calling a function, so try to desugar to a __Call
            var callArg = new BoundTupleExpr(new IBoundExpr[] { target, boundArg });

            var call = mContext.Compiler.ResolveFunction(mFunction, expr.Target.Position,
                "__Call", new IUnboundDecl[0], callArg);

            if (call != null) return call;

            throw new CompileException(expr.Position, "Target of call is not a function.");
        }

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(ArrayExpr expr)
        {
            var elementType = (IBoundDecl)null;

            if (expr.ElementType != null)
            {
                elementType = TypeBinder.Bind(mContext, expr.ElementType);
            }

            var elements = expr.Elements.Accept(this);

            // infer the type from the elements
            if (elementType == null)
            {
                var index = 0;
                foreach (var element in elements)
                {
                    if (elementType == null)
                    {
                        // take the type of the first
                        elementType = element.Type;
                    }
                    else
                    {
                        // make sure the others match
                        if (!DeclComparer.TypesMatch(elementType, element.Type))
                            throw new CompileException(expr.Position, String.Format("Array elements must all be the same type. Array is type {0}, but element {1} is type {2}.",
                                elementType, index, element.Type));
                    }

                    index++;
                }
            }

            return new BoundArrayExpr(elementType, elements, expr.IsMutable);
        }

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(AssignExpr expr)
        {
            var value = expr.Value.Accept(this);

            // handle a name target: foo <- 3
            var nameTarget = expr.Target as NameExpr;
            if (nameTarget != null)
            {
                // see if it's a local
                if (Scope.Contains(nameTarget.Name))
                {
                    if (!Scope[nameTarget.Name].IsMutable) throw new CompileException(expr.Position, "Cannot assign to immutable local.");

                    // direct assign to local
                    return new StoreExpr(new LocalsExpr(), Scope[nameTarget.Name], value);
                }

                // look for an assignment function
                return TranslateAssignment(nameTarget.Position, nameTarget.Name, nameTarget.TypeArgs, new UnitExpr(Position.None), value);
            }

            // handle a function apply target: Foo 1 <- 3  ==> Foo<- (1, 3)
            var callTarget = expr.Target as CallExpr;
            if (callTarget != null)
            {
                var callArg = callTarget.Arg.Accept(this);

                // see if it's a direct function call
                var funcName = callTarget.Target as NameExpr;
                if ((funcName != null) && !mContext.Compiler.IsLocal(mFunction, Scope, funcName.Name))
                {
                    // translate the call
                    return TranslateAssignment(callTarget.Position, funcName.Name, funcName.TypeArgs, callArg, value);
                }

                // not calling a function, so try to desugar to a __Call<-
                var desugaredCallTarget = callTarget.Target.Accept(this);
                var desugaredCallArg = new BoundTupleExpr(new IBoundExpr[] { desugaredCallTarget, callArg, value });

                var call = mContext.Compiler.ResolveFunction(mFunction, expr.Target.Position,
                    "__Call<-", new IUnboundDecl[0], desugaredCallArg);

                if (call != null) return call;

                throw new CompileException(expr.Position, "Couldn't figure out what you're trying to do on the left side of an assignment.");
            }

            // handle an operator target: 1 $$ 2 <- 3  ==> $$<- (1, 2, 3)
            var operatorTarget = expr.Target as OperatorExpr;
            if (operatorTarget != null)
            {
                var opArg = new BoundTupleExpr(new IBoundExpr[]
                    { operatorTarget.Left.Accept(this),
                      operatorTarget.Right.Accept(this) });

                return TranslateAssignment(operatorTarget.Position, operatorTarget.Name, null /* no operator generics yet */, opArg, value);
            }

            var tupleTarget = expr.Target as TupleExpr;
            if (tupleTarget != null)
            {
                //### bob: need to handle tuple decomposition here:
                //         a, b <- (1, 2)
                throw new NotImplementedException();
            }

            // if we got here, it's not a valid assignment expression
            throw new CompileException(expr.Position, "Cannot assign to " + expr.Target);
        }

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(BlockExpr block)
        {
            // create an inner scope
            Scope.Push();

            var exprs = new List<IBoundExpr>();
            var index = 0;
            foreach (IUnboundExpr expr in block.Exprs)
            {
                var bound = expr.Accept(this);

                // all but last expression must be void
                if (index < block.Exprs.Count - 1)
                {
                    if (bound.Type != Decl.Unit) throw new CompileException(expr.Position, "All expressions in a block except the last must be of type Unit. " + block.ToString());
                }

                index++;

                exprs.Add(bound);
            }

            Scope.Pop();

            return new BoundBlockExpr(exprs);
        }

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(DefineExpr expr)
        {
            if (Scope.Contains(expr.Name)) throw new CompileException(expr.Position, "A local variable named \"" + expr.Name + "\" is already defined in this scope.");

            var value = expr.Value.Accept(this);

            // add it to the scope
            Scope.Define(expr.Name, value.Type, expr.IsMutable);

            // assign it
            return new StoreExpr(new LocalsExpr(), Scope[expr.Name], value);
        }

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(FuncRefExpr expr)
        {
            IBoundDecl paramType = null;
            if (expr.ParamType != null)
            {
                paramType = TypeBinder.Bind(mContext, expr.ParamType);
            }

            var callable = mContext.Compiler.Functions.Find(mContext.Compiler, mFunction.SearchSpace, 
                expr.Name.Name, expr.Name.TypeArgs, paramType);

            var function = callable as Function;

            //### bob: to support intrinsics, we'll need to basically create wrapper functions
            // that have the same type signature as the intrinsic and that do nothing but
            // call the intrinsic and return. then, we can get a reference to that wrapper.
            // 
            // to support foreign functions, we can either do the same thing, or change the
            // way function references work. if a function reference can be distinguished
            // between being a regular function, a foreign one (or later a closure), then
            // we can get rid of ForeignFuncCallExpr and just use CallExpr for foreign calls
            // too.
            if (function == null) throw new NotImplementedException("Can only get references to user-defined functions. Intrinsics, auto-generated, and foreign function references aren't supported yet.");

            return new BoundFuncRefExpr(function);
        }

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(IfThenExpr expr)
        {
            var bound = new BoundIfThenExpr(
                expr.Condition.Accept(this),
                expr.Body.Accept(this));

            if (bound.Condition.Type != Decl.Bool)
            {
                throw new CompileException(expr.Position, String.Format(
                    "Condition of if/then is returning type {0} but should be Bool.",
                    bound.Condition.Type));
            }

            if ((bound.Body.Type != Decl.Unit) && (bound.Body.Type != Decl.EarlyReturn))
            {
                throw new CompileException(expr.Position, String.Format(
                    "Body of if/then is returning type {0} but must be Unit (or a return) if there is no else branch.",
                    bound.Body.Type));
            }

            return bound;
        }

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(IfThenElseExpr expr)
        {
            var bound = new BoundIfThenElseExpr(
                expr.Condition.Accept(this),
                expr.ThenBody.Accept(this),
                expr.ElseBody.Accept(this));

            if (bound.Condition.Type != Decl.Bool)
            {
                throw new CompileException(expr.Position, String.Format(
                    "Condition of if/then/else is returning type {0} but should be Bool.",
                    bound.Condition.Type));
            }

            if (!DeclComparer.TypesMatch(bound.ThenBody.Type, bound.ElseBody.Type))
            {
                throw new CompileException(expr.Position, String.Format(
                    "Branches of if/then/else do not return the same type. Then arm returns {0} while else arm returns {1}.",
                    bound.ThenBody.Type, bound.ElseBody.Type));
            }

            return bound;
        }

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(NameExpr expr)
        {
            return mContext.Compiler.ResolveName(mFunction, Scope,
                expr.Position, expr.Name, expr.TypeArgs, null);
        }

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(OperatorExpr expr)
        {
            // an operator is just function application
            var apply = new CallExpr(new NameExpr(expr.Position, expr.Name), new TupleExpr(new IUnboundExpr[] { expr.Left, expr.Right }));

            return apply.Accept(this);
        }

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(TupleExpr expr)
        {
            return new BoundTupleExpr(expr.Fields.Accept(this));
        }

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(IntExpr expr)
        {
            return expr;
        }

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(BoolExpr expr)
        {
            return expr;
        }

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(StringExpr expr)
        {
            return expr;
        }

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(UnitExpr expr)
        {
            return expr;
        }

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(ReturnExpr expr)
        {
            return new BoundReturnExpr(expr.Value.Accept(this));
        }

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(WhileExpr expr)
        {
            var bound = new BoundWhileExpr(expr.Condition.Accept(this), expr.Body.Accept(this));

            if (bound.Condition.Type != Decl.Bool)
            {
                throw new CompileException(expr.Position, String.Format(
                    "Condition of while/do is returning type {0} but should be Bool.",
                    bound.Condition.Type));
            }

            if (bound.Body.Type != Decl.Unit)
            {
                throw new CompileException(expr.Position, String.Format(
                    "Body of while/do is returning type {0} but should be Unit.",
                    bound.Body.Type));
            }

            return bound;
        }

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(ForExpr expr)
        {
            // a for expression is basically syntactic sugar for a while expression
            // and one or more iterators. for example, the following:
            //
            // for foo <- bar do
            //     Print foo
            // end
            //
            // is equivalent to:
            //
            // def _fooIter <- Iterate bar
            // while MoveNext _fooIter do
            //     def foo <- Current _fooIter
            //     Print foo
            // end
            //
            // so, to bind a for expression, we just desugar it, then bind that.

            var topExprs = new List<IUnboundExpr>();
            var conditionExpr = (IUnboundExpr)null;
            var whileExprs = new List<IUnboundExpr>();

            // instantiate each iterator
            foreach (var iterator in expr.Iterators)
            {
                // note: the iterator variable includes a space to ensure it can't collide with a
                // user-defined variable.
                var createIterator = new CallExpr(new NameExpr(iterator.Position, "Iterate"), iterator.Iterator);
                topExprs.Add(new DefineExpr(iterator.Position, iterator.Name + " iter", createIterator, false));

                var condition = new CallExpr(new NameExpr(iterator.Position, "MoveNext"), new NameExpr(iterator.Position, iterator.Name + " iter"));
                if (conditionExpr == null)
                {
                    conditionExpr = condition;
                }
                else
                {
                    // combine with previous condition(s)
                    conditionExpr = new OperatorExpr(iterator.Position, conditionExpr, "&", condition);
                }

                var currentValue = new CallExpr(new NameExpr(iterator.Position, "Current"), new NameExpr(iterator.Position, iterator.Name + " iter"));
                whileExprs.Add(new DefineExpr(iterator.Position, iterator.Name, currentValue, false));
            }

            // create the while loop
            whileExprs.Add(expr.Body);
            var whileExpr = new WhileExpr(expr.Position,
                conditionExpr,
                new BlockExpr(whileExprs));
            topExprs.Add(whileExpr);

            // build the whole block
            var block = new BlockExpr(topExprs);

            // now bind the whole thing
            return block.Accept(this);
        }

        IBoundExpr IUnboundExprVisitor<IBoundExpr>.Visit(MatchExpr expr)
        {
            // bind the match
            var match = expr.Match.Accept(this);

            // make sure the patterns are the right shape
            foreach (var matchCase in expr.Cases)
            {
                if (!CaseDeclMatcher.Matches(mContext.Compiler, match.Type, matchCase.Case))
                {
                    throw new CompileException(matchCase.Position,
                        String.Format("The match case {0} is not the right shape to match a value of type {1}.", matchCase, match.Type));
                }
            }

            throw new NotImplementedException();
        }

        #endregion

        private IBoundExpr TranslateAssignment(Position position, string baseName, IList<IUnboundDecl> typeArgs, IBoundExpr arg, IBoundExpr value)
        {
            var name = baseName + "<-";

            // add the value argument
            arg = arg.AppendArg(value);

            return mContext.Compiler.ResolveFunction(mFunction, position, name, typeArgs, arg);
        }

        private FunctionBinder(Function function, BindingContext context, Scope scope)
        {
            mFunction = function;
            mContext = context;
            Scope = scope;
        }

        private BindingContext mContext;
        private Function mFunction;
    }
}