![Gwendolyn](/assets/img/avatar_default.png)
the typechecker project can collect all the top level types from a file, which is pretty cool I think (except for pointers, those aren't implemented yet...)
60 lines
1.9 KiB
C#
60 lines
1.9 KiB
C#
using meowlang.parser;
|
|
|
|
namespace meowlang.typechecker;
|
|
|
|
class TypeDescriptionVisitor : MeowModelVisitor<TypeDescription>
|
|
{
|
|
private readonly Scope _scope;
|
|
private readonly string? _name;
|
|
|
|
public TypeDescriptionVisitor(Scope scope)
|
|
{
|
|
_scope = scope;
|
|
}
|
|
public TypeDescriptionVisitor(Scope scope, string? name)
|
|
{
|
|
_scope = scope;
|
|
_name = name;
|
|
}
|
|
|
|
public override TypeDescription VisitStructType(StructTypeModel m)
|
|
{
|
|
var members = new List<(string, TypeId)>();
|
|
foreach (var (_, typeReferenceModel, name) in m.Members)
|
|
{
|
|
var fullName = _name == null ? null : $"{_name}@{name}";
|
|
members.Add((name, new TypeIdVisitor(_scope, fullName).Visit(typeReferenceModel)));
|
|
}
|
|
|
|
return new StructTypeDescription(members);
|
|
}
|
|
|
|
public override TypeDescription VisitEnumType(EnumTypeModel m)
|
|
{
|
|
var members = new List<(string, TypeId?)>();
|
|
foreach (var (_, name, typeReferenceModel) in m.Members)
|
|
{
|
|
var fullName = _name == null ? null : $"{_name}@{name}";
|
|
members.Add((name, new TypeIdVisitor(_scope, fullName).TryVisit(typeReferenceModel)));
|
|
}
|
|
|
|
return new EnumTypeDescription(members);
|
|
}
|
|
|
|
public override TypeDescription VisitTupleType(TupleTypeModel m)
|
|
{
|
|
var members = m.Types.Select((t, i) => new TypeIdVisitor(_scope, $"{_name}@{i}").Visit(t)).ToList();
|
|
return new TupleTypeDescription(members);
|
|
}
|
|
|
|
public override TypeDescription VisitArrayType(ArrayTypeModel m)
|
|
{
|
|
var elementType = new TypeIdVisitor(_scope, $"{_name}@inner").Visit(m.Type);
|
|
return new ArrayTypeDescription(elementType, m.Size);
|
|
}
|
|
public override TypeDescription VisitSliceType(SliceTypeModel m)
|
|
{
|
|
var elementType = new TypeIdVisitor(_scope, $"{_name}@inner").Visit(m.Type);
|
|
return new SliceTypeDescription(elementType);
|
|
}
|
|
} |