2023-10-04 05:32:24 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Immutable;
|
2023-10-04 18:31:10 +00:00
|
|
|
using System.Linq;
|
2023-10-04 05:32:24 +00:00
|
|
|
using Finn;
|
|
|
|
using Finn.AST;
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
using ScopeStack = System.Collections.Immutable.ImmutableStack<System.Collections.Immutable.ImmutableHashSet<string>>;
|
|
|
|
using Resolutions = System.Collections.Immutable.ImmutableDictionary<Finn.AST.Expr, int>;
|
|
|
|
using System.Collections.Generic;
|
2023-10-04 05:32:24 +00:00
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
class Resolver : IExprVisitor<ScopeStack, Resolutions>
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
|
|
|
private class NameCollection
|
|
|
|
{
|
|
|
|
public ImmutableHashSet<string> Names { get; private set; } = ImmutableHashSet<string>.Empty;
|
|
|
|
|
|
|
|
public void Add(Token name)
|
|
|
|
{
|
|
|
|
var value = (string)name.Literal!;
|
|
|
|
if (Names.Contains(value))
|
|
|
|
{
|
|
|
|
Program.error(name.Start, $"Duplicate name declared in scope: {name.Lexeme}");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Names = Names.Add(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class PatternNameCollector : IPatternVisitor<NameCollection, ValueTuple>
|
|
|
|
{
|
|
|
|
public readonly static PatternNameCollector Instance = new();
|
|
|
|
private static readonly ValueTuple Void = ValueTuple.Create();
|
|
|
|
|
|
|
|
private PatternNameCollector() { }
|
|
|
|
|
|
|
|
public ValueTuple visitFieldPatternPattern(NameCollection names, FieldPattern field)
|
|
|
|
{
|
|
|
|
if (field.Pattern is null)
|
|
|
|
{
|
|
|
|
names.Add(field.Name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
field.Pattern.accept(names, this);
|
|
|
|
}
|
|
|
|
return Void;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ValueTuple visitRecordPatternPattern(NameCollection names, RecordPattern record)
|
|
|
|
{
|
|
|
|
record.Rest?.accept(names, this);
|
|
|
|
foreach (var field in record.Fields)
|
|
|
|
{
|
|
|
|
field.accept(names, this);
|
|
|
|
}
|
|
|
|
return Void;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ValueTuple visitSimplePatternPattern(NameCollection names, SimplePattern simple)
|
|
|
|
{
|
|
|
|
if (simple.Identifier is not null)
|
|
|
|
{
|
|
|
|
names.Add(simple.Identifier);
|
|
|
|
}
|
|
|
|
return Void;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ValueTuple visitVariantPatternPattern(NameCollection names, VariantPattern variant)
|
|
|
|
{
|
|
|
|
variant.Argument?.accept(names, this);
|
|
|
|
return Void;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
private static readonly Resolutions EmptyResolutions = Resolutions.Empty.WithComparers(ReferenceEqualityComparer.Instance);
|
|
|
|
public static readonly Resolver Instance = new Resolver();
|
2023-10-04 05:32:24 +00:00
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
private Resolver() { }
|
2023-10-04 05:32:24 +00:00
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
private static Resolutions AddRange(Resolutions r1, Resolutions r2) => r1.AddRange(r2);
|
|
|
|
|
|
|
|
private Resolutions Resolve(ScopeStack scopes, Expr expr)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
2023-10-04 18:31:10 +00:00
|
|
|
return expr.accept(scopes, this);
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
private Resolutions Resolve(ScopeStack scopes, ImmutableHashSet<string> newScope, Expr value)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
2023-10-04 18:31:10 +00:00
|
|
|
return Resolve(scopes.Push(newScope), value);
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
public Resolutions visitBinaryExpr(ScopeStack scopes, Binary expr)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
2023-10-04 18:31:10 +00:00
|
|
|
var left = Resolve(scopes, expr.Left);
|
|
|
|
var right = Resolve(scopes, expr.Right);
|
|
|
|
return left.AddRange(right);
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
public Resolutions visitCallExpr(ScopeStack scopes, Call expr)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
2023-10-04 18:31:10 +00:00
|
|
|
return (from arg in expr.Arguments
|
|
|
|
where arg is not null
|
|
|
|
select Resolve(scopes, arg))
|
|
|
|
.Aggregate(Resolve(scopes, expr.Left), AddRange);
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
public Resolutions visitGroupingExpr(ScopeStack scopes, Grouping expr)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
2023-10-04 18:31:10 +00:00
|
|
|
return Resolve(scopes, expr.Expression);
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
public Resolutions visitIfExpr(ScopeStack scopes, If expr)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
2023-10-04 18:31:10 +00:00
|
|
|
var condition = Resolve(scopes, expr.Condition);
|
|
|
|
var _then = Resolve(scopes, expr.Then);
|
|
|
|
var _else = Resolve(scopes, expr.Else);
|
|
|
|
return condition.AddRange(_then).AddRange(_else);
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
public Resolutions visitIndexerExpr(ScopeStack scopes, Indexer expr)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
2023-10-04 18:31:10 +00:00
|
|
|
var left = Resolve(scopes, expr.Left);
|
|
|
|
var index = Resolve(scopes, expr.Index);
|
|
|
|
return left.AddRange(index);
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
public Resolutions visitLetExpr(ScopeStack scopes, Let expr)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
|
|
|
var names = new NameCollection();
|
|
|
|
foreach (var binding in expr.Bindings)
|
|
|
|
{
|
|
|
|
switch (binding)
|
|
|
|
{
|
|
|
|
case FuncBinding fb:
|
|
|
|
names.Add(fb.Name);
|
|
|
|
break;
|
|
|
|
case VarBinding vb:
|
|
|
|
vb.Pattern.accept(names, PatternNameCollector.Instance);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception("wtf there are no other binding types");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 06:05:10 +00:00
|
|
|
var newScopes = scopes.Push(names.Names);
|
2023-10-04 05:32:24 +00:00
|
|
|
|
|
|
|
// TODO Put the static constructivity check here.
|
|
|
|
// For now, just check that a definition only uses names that come before it.
|
|
|
|
// Note that this will preclude using shadowed variables in definitions.
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
return (from binding in expr.Bindings
|
|
|
|
select (binding switch
|
|
|
|
{
|
|
|
|
FuncBinding fb => ResolveFunction(newScopes, fb),
|
|
|
|
VarBinding vb => Resolve(newScopes, vb.Value),
|
|
|
|
_ => throw new Exception("wtf there are no other binding types"),
|
|
|
|
}))
|
|
|
|
.Aggregate(Resolve(newScopes, expr.Body), AddRange);
|
2023-10-04 05:32:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
private Resolutions ResolveFunction(ScopeStack scopes, FuncBinding fb)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
|
|
|
NameCollection names = new();
|
|
|
|
foreach (var param in fb.Params)
|
|
|
|
{
|
|
|
|
param.accept(names, PatternNameCollector.Instance);
|
|
|
|
}
|
2023-10-04 18:31:10 +00:00
|
|
|
return Resolve(scopes, names.Names, fb.Value);
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
public Resolutions visitListExpr(ScopeStack scopes, List expr)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
2023-10-04 18:31:10 +00:00
|
|
|
return (from element in expr.Elements
|
|
|
|
select Resolve(scopes, element))
|
|
|
|
.Aggregate(EmptyResolutions, AddRange);
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
public Resolutions visitLiteralExpr(ScopeStack scopes, Literal expr)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
2023-10-04 18:31:10 +00:00
|
|
|
return EmptyResolutions;
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
public Resolutions visitRecordExpr(ScopeStack scopes, Record expr)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
2023-10-04 18:31:10 +00:00
|
|
|
var resolutions = (from extension in expr.Extensions
|
|
|
|
select Resolve(scopes, extension.Value))
|
|
|
|
.Aggregate(EmptyResolutions, AddRange);
|
2023-10-04 05:32:24 +00:00
|
|
|
if (expr.Base is not null)
|
|
|
|
{
|
2023-10-04 18:31:10 +00:00
|
|
|
resolutions = resolutions.AddRange(Resolve(scopes, expr.Base.Value));
|
|
|
|
resolutions = (from update in expr.Base.Updates
|
|
|
|
select Resolve(scopes, update.Value))
|
|
|
|
.Aggregate(resolutions, AddRange);
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
return resolutions;
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
public Resolutions visitSelectorExpr(ScopeStack scopes, Selector expr)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
2023-10-04 18:31:10 +00:00
|
|
|
return Resolve(scopes, expr.Left);
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
public Resolutions visitSequenceExpr(ScopeStack scopes, Sequence expr)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
2023-10-04 18:31:10 +00:00
|
|
|
return AddRange(Resolve(scopes, expr.Left), Resolve(scopes, expr.Right));
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
public Resolutions visitUnaryExpr(ScopeStack scopes, Unary expr)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
2023-10-04 18:31:10 +00:00
|
|
|
return Resolve(scopes, expr.Right);
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
public Resolutions visitVariableExpr(ScopeStack scopes, Variable expr)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
2023-10-04 06:05:10 +00:00
|
|
|
int depth = 0;
|
|
|
|
while (!scopes.IsEmpty)
|
|
|
|
{
|
|
|
|
scopes = scopes.Pop(out var scope);
|
2023-10-04 18:31:10 +00:00
|
|
|
if (scope.Contains((string)expr.Value.Literal!))
|
2023-10-04 06:05:10 +00:00
|
|
|
{
|
2023-10-04 18:31:10 +00:00
|
|
|
return EmptyResolutions.Add(expr, depth);
|
2023-10-04 06:05:10 +00:00
|
|
|
}
|
|
|
|
depth++;
|
|
|
|
}
|
|
|
|
// At this point, it must be a global. I don't like that we leave that resolution for runtime.
|
|
|
|
// TODO Don't leave that resolution for runtime.
|
2023-10-04 18:31:10 +00:00
|
|
|
// But I think that can wait until I make the resolver nameless.
|
|
|
|
return EmptyResolutions.Add(expr, depth);
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
public Resolutions visitVariantExpr(ScopeStack scopes, Variant expr)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
|
|
|
if (expr.Argument is not null)
|
|
|
|
{
|
2023-10-04 18:31:10 +00:00
|
|
|
return Resolve(scopes, expr.Argument);
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
2023-10-04 18:31:10 +00:00
|
|
|
return EmptyResolutions;
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:31:10 +00:00
|
|
|
public Resolutions visitWhenExpr(ScopeStack scopes, When expr)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
2023-10-04 18:31:10 +00:00
|
|
|
return (from _case in expr.Cases
|
|
|
|
select ResolveCase(_case))
|
|
|
|
.Aggregate(Resolve(scopes, expr.Head), AddRange);
|
|
|
|
|
|
|
|
Resolutions ResolveCase(VarBinding _case)
|
2023-10-04 05:32:24 +00:00
|
|
|
{
|
|
|
|
var names = new NameCollection();
|
|
|
|
_case.Pattern.accept(names, PatternNameCollector.Instance);
|
2023-10-04 18:31:10 +00:00
|
|
|
return Resolve(scopes, names.Names, _case.Value);
|
2023-10-04 05:32:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|