d6bdd08002
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...)
77 lines
2.2 KiB
C#
77 lines
2.2 KiB
C#
using meowlang.parser;
|
|
|
|
namespace meowlang.typechecker;
|
|
|
|
public class CollectTypesVisitor : MeowModelVisitor
|
|
{
|
|
private readonly Scope _scope;
|
|
|
|
public CollectTypesVisitor(Scope scope)
|
|
{
|
|
_scope = scope;
|
|
}
|
|
|
|
public override void VisitStructDeclaration(StructDeclarationModel m)
|
|
{
|
|
TypeDescription structDescription;
|
|
var arity = m.TypeParameters.Count;
|
|
if (arity > 0)
|
|
{
|
|
structDescription = new GenericTypeDescriptionVisitor(_scope, m.TypeParameters, m.Name).Visit(m);
|
|
}
|
|
else
|
|
{
|
|
structDescription = new TypeDescriptionVisitor(_scope, m.Name).Visit(m);
|
|
}
|
|
|
|
_scope.AddNamedType(m.Name, arity, structDescription);
|
|
}
|
|
|
|
public override void VisitEnumDeclaration(EnumDeclarationModel m)
|
|
{
|
|
TypeDescription enumDescription;
|
|
var arity = m.TypeParameters.Count;
|
|
if (arity > 0)
|
|
{
|
|
enumDescription = new GenericTypeDescriptionVisitor(_scope, m.TypeParameters, m.Name).Visit(m);
|
|
}
|
|
else
|
|
{
|
|
enumDescription = new TypeDescriptionVisitor(_scope, m.Name).Visit(m);
|
|
}
|
|
|
|
_scope.AddNamedType(m.Name, arity, enumDescription);
|
|
}
|
|
|
|
public override void VisitTupleDeclaration(TupleDeclarationModel m)
|
|
{
|
|
TypeDescription tupleDescription;
|
|
var arity = m.TypeParameters.Count;
|
|
if (arity > 0)
|
|
{
|
|
tupleDescription = new GenericTypeDescriptionVisitor(_scope, m.TypeParameters, m.Name).Visit(m);
|
|
}
|
|
else
|
|
{
|
|
tupleDescription = new TypeDescriptionVisitor(_scope, m.Name).Visit(m);
|
|
}
|
|
|
|
_scope.AddNamedType(m.Name, arity, tupleDescription);
|
|
}
|
|
|
|
public override void VisitTypeAlias(TypeAliasModel m)
|
|
{
|
|
TypeDescription typeDescription;
|
|
var arity = m.TypeParameters.Count;
|
|
if (arity > 0)
|
|
{
|
|
typeDescription = new GenericTypeDescriptionVisitor(_scope, m.TypeParameters, m.Name).Visit(m);
|
|
}
|
|
else
|
|
{
|
|
typeDescription = new TypeDescriptionVisitor(_scope, m.Name).Visit(m);
|
|
}
|
|
|
|
_scope.AddNamedType(m.Name, arity, typeDescription);
|
|
}
|
|
} |