Fix constructors like True and False not being parsed

This commit is contained in:
Sam Vervaeck 2023-05-21 14:50:28 +02:00
parent 1f94b7f799
commit b6706dd801
Signed by: samvv
SSH key fingerprint: SHA256:dIg0ywU1OP+ZYifrYxy8c5esO72cIKB+4/9wkZj1VaY
5 changed files with 64 additions and 30 deletions

View file

@ -707,13 +707,30 @@ namespace bolt {
}; };
class Identifier : public Token { class Symbol : public Token {
public:
inline Symbol(NodeKind Kind, TextLoc StartLoc):
Token(Kind, StartLoc) {}
virtual ByteString getCanonicalText() = 0;
static bool classof(const Node* N) {
return N->getKind() == NodeKind::Identifier
|| N->getKind() == NodeKind::IdentifierAlt;
}
};
class Identifier : public Symbol {
public: public:
ByteString Text; ByteString Text;
Identifier(ByteString Text, TextLoc StartLoc): Identifier(ByteString Text, TextLoc StartLoc):
Token(NodeKind::Identifier, StartLoc), Text(Text) {} Symbol(NodeKind::Identifier, StartLoc), Text(Text) {}
ByteString getCanonicalText() override;
std::string getText() const override; std::string getText() const override;
@ -725,13 +742,15 @@ namespace bolt {
}; };
class IdentifierAlt : public Token { class IdentifierAlt : public Symbol {
public: public:
ByteString Text; ByteString Text;
IdentifierAlt(ByteString Text, TextLoc StartLoc): IdentifierAlt(ByteString Text, TextLoc StartLoc):
Token(NodeKind::IdentifierAlt, StartLoc), Text(Text) {} Symbol(NodeKind::IdentifierAlt, StartLoc), Text(Text) {}
ByteString getCanonicalText() override;
std::string getText() const override; std::string getText() const override;
@ -970,11 +989,11 @@ namespace bolt {
public: public:
std::vector<std::tuple<IdentifierAlt*, Dot*>> ModulePath; std::vector<std::tuple<IdentifierAlt*, Dot*>> ModulePath;
Identifier* Name; Symbol* Name;
ReferenceExpression( ReferenceExpression(
std::vector<std::tuple<IdentifierAlt*, Dot*>> ModulePath, std::vector<std::tuple<IdentifierAlt*, Dot*>> ModulePath,
Identifier* Name Symbol* Name
): Expression(NodeKind::ReferenceExpression), ): Expression(NodeKind::ReferenceExpression),
ModulePath(ModulePath), ModulePath(ModulePath),
Name(Name) {} Name(Name) {}
@ -1007,7 +1026,6 @@ namespace bolt {
}; };
class MatchExpression : public Expression { class MatchExpression : public Expression {
public: public:

View file

@ -605,12 +605,20 @@ namespace bolt {
return "instance"; return "instance";
} }
ByteString Identifier::getCanonicalText() {
return Text;
}
ByteString IdentifierAlt::getCanonicalText() {
return Text;
}
SymbolPath ReferenceExpression::getSymbolPath() const { SymbolPath ReferenceExpression::getSymbolPath() const {
std::vector<ByteString> ModuleNames; std::vector<ByteString> ModuleNames;
for (auto [Name, Dot]: ModulePath) { for (auto [Name, Dot]: ModulePath) {
ModuleNames.push_back(Name->Text); ModuleNames.push_back(Name->getCanonicalText());
} }
return SymbolPath { ModuleNames, Name->Text }; return SymbolPath { ModuleNames, Name->getCanonicalText() };
} }
} }

View file

@ -331,8 +331,8 @@ namespace bolt {
{ {
auto Class = static_cast<ClassDeclaration*>(X); auto Class = static_cast<ClassDeclaration*>(X);
for (auto TE: Class->TypeVars) { for (auto TE: Class->TypeVars) {
auto TV = createRigidVar(TE->Name->Text); auto TV = createRigidVar(TE->Name->getCanonicalText());
TV->Contexts.emplace(Class->Name->Text); TV->Contexts.emplace(Class->Name->getCanonicalText());
TE->setType(TV); TE->setType(TV);
} }
for (auto Element: Class->Elements) { for (auto Element: Class->Elements) {
@ -344,9 +344,9 @@ namespace bolt {
case NodeKind::InstanceDeclaration: case NodeKind::InstanceDeclaration:
{ {
auto Decl = static_cast<InstanceDeclaration*>(X); auto Decl = static_cast<InstanceDeclaration*>(X);
auto Match = InstanceMap.find(Decl->Name->Text); auto Match = InstanceMap.find(Decl->Name->getCanonicalText());
if (Match == InstanceMap.end()) { if (Match == InstanceMap.end()) {
InstanceMap.emplace(Decl->Name->Text, std::vector { Decl }); InstanceMap.emplace(Decl->Name->getCanonicalText(), std::vector { Decl });
} else { } else {
Match->second.push_back(Decl); Match->second.push_back(Decl);
} }
@ -376,7 +376,7 @@ namespace bolt {
auto Decl = static_cast<ClassDeclaration*>(Let->Parent); auto Decl = static_cast<ClassDeclaration*>(Let->Parent);
for (auto TE: Decl->TypeVars) { for (auto TE: Decl->TypeVars) {
auto TV = llvm::cast<TVar>(TE->getType()); auto TV = llvm::cast<TVar>(TE->getType());
NewCtx->Env.emplace(TE->Name->Text, new Forall(TV)); NewCtx->Env.emplace(TE->Name->getCanonicalText(), new Forall(TV));
NewCtx->TVs->emplace(TV); NewCtx->TVs->emplace(TV);
} }
} }
@ -617,7 +617,7 @@ namespace bolt {
for (auto TE: D->TEs) { for (auto TE: D->TEs) {
Types.push_back(inferTypeExpression(TE)); Types.push_back(inferTypeExpression(TE));
} }
return new CClass(D->Name->Text, Types); return new CClass(D->Name->getCanonicalText(), Types);
} }
case NodeKind::EqualityConstraintExpression: case NodeKind::EqualityConstraintExpression:
{ {
@ -636,10 +636,10 @@ namespace bolt {
case NodeKind::ReferenceTypeExpression: case NodeKind::ReferenceTypeExpression:
{ {
auto RefTE = static_cast<ReferenceTypeExpression*>(N); auto RefTE = static_cast<ReferenceTypeExpression*>(N);
auto Ty = lookupMono(RefTE->Name->Text); auto Ty = lookupMono(RefTE->Name->getCanonicalText());
if (Ty == nullptr) { if (Ty == nullptr) {
if (Config.typeVarsRequireForall()) { if (Config.typeVarsRequireForall()) {
DE.add<BindingNotFoundDiagnostic>(RefTE->Name->Text, RefTE->Name); DE.add<BindingNotFoundDiagnostic>(RefTE->Name->getCanonicalText(), RefTE->Name);
} }
Ty = createTypeVar(); Ty = createTypeVar();
} }
@ -650,13 +650,13 @@ namespace bolt {
case NodeKind::VarTypeExpression: case NodeKind::VarTypeExpression:
{ {
auto VarTE = static_cast<VarTypeExpression*>(N); auto VarTE = static_cast<VarTypeExpression*>(N);
auto Ty = lookupMono(VarTE->Name->Text); auto Ty = lookupMono(VarTE->Name->getCanonicalText());
if (Ty == nullptr) { if (Ty == nullptr) {
if (Config.typeVarsRequireForall()) { if (Config.typeVarsRequireForall()) {
DE.add<BindingNotFoundDiagnostic>(VarTE->Name->Text, VarTE->Name); DE.add<BindingNotFoundDiagnostic>(VarTE->Name->getCanonicalText(), VarTE->Name);
} }
Ty = createRigidVar(VarTE->Name->Text); Ty = createRigidVar(VarTE->Name->getCanonicalText());
addBinding(VarTE->Name->Text, new Forall(Ty)); addBinding(VarTE->Name->getCanonicalText(), new Forall(Ty));
} }
N->setType(Ty); N->setType(Ty);
return Ty; return Ty;
@ -725,9 +725,9 @@ namespace bolt {
ZEN_ASSERT(Ctx->ReturnType != nullptr); ZEN_ASSERT(Ctx->ReturnType != nullptr);
return Ctx->ReturnType; return Ctx->ReturnType;
} }
auto Scm = lookup(Ref->Name->Text); auto Scm = lookup(Ref->Name->getCanonicalText());
if (Scm == nullptr) { if (Scm == nullptr) {
DE.add<BindingNotFoundDiagnostic>(Ref->Name->Text, Ref->Name); DE.add<BindingNotFoundDiagnostic>(Ref->Name->getCanonicalText(), Ref->Name);
return createTypeVar(); return createTypeVar();
} }
auto Ty = instantiate(Scm, X); auto Ty = instantiate(Scm, X);
@ -791,7 +791,7 @@ namespace bolt {
case NodeKind::BindPattern: case NodeKind::BindPattern:
{ {
addBinding(static_cast<BindPattern*>(Pattern)->Name->Text, new Forall(TVs, Constraints, Type)); addBinding(static_cast<BindPattern*>(Pattern)->Name->getCanonicalText(), new Forall(TVs, Constraints, Type));
break; break;
} }
@ -813,7 +813,7 @@ namespace bolt {
for (auto TE: Class->TypeVars) { for (auto TE: Class->TypeVars) {
Tys.push_back(llvm::cast<TVar>(TE->getType())); Tys.push_back(llvm::cast<TVar>(TE->getType()));
} }
Out.push_back(TypeclassSignature { Class->Name->Text, Tys }); Out.push_back(TypeclassSignature { Class->Name->getCanonicalText(), Tys });
} }
if (Decl->TypeAssert != nullptr) { if (Decl->TypeAssert != nullptr) {
if (llvm::isa<QualifiedTypeExpression>(Decl->TypeAssert->TypeExpression)) { if (llvm::isa<QualifiedTypeExpression>(Decl->TypeAssert->TypeExpression)) {
@ -827,7 +827,7 @@ namespace bolt {
ZEN_ASSERT(llvm::isa<TVar>(TV)); ZEN_ASSERT(llvm::isa<TVar>(TV));
Tys.push_back(static_cast<TVar*>(TV)); Tys.push_back(static_cast<TVar*>(TV));
} }
Out.push_back(TypeclassSignature { TCE->Name->Text, Tys }); Out.push_back(TypeclassSignature { TCE->Name->getCanonicalText(), Tys });
} }
} }
} }

View file

@ -47,7 +47,9 @@ namespace bolt {
static std::string describe(NodeKind Type) { static std::string describe(NodeKind Type) {
switch (Type) { switch (Type) {
case NodeKind::Identifier: case NodeKind::Identifier:
return "an identifier"; return "an identifier starting with a lowercase letter";
case NodeKind::IdentifierAlt:
return "an identifier starting with a capital letter";
case NodeKind::CustomOperator: case NodeKind::CustomOperator:
return "an operator"; return "an operator";
case NodeKind::IntegerLiteral: case NodeKind::IntegerLiteral:

View file

@ -211,18 +211,24 @@ after_constraints:
auto T0 = Tokens.peek(); auto T0 = Tokens.peek();
switch (T0->getKind()) { switch (T0->getKind()) {
case NodeKind::Identifier: case NodeKind::Identifier:
case NodeKind::IdentifierAlt:
{ {
std::vector<std::tuple<IdentifierAlt*, Dot*>> ModulePath; std::vector<std::tuple<IdentifierAlt*, Dot*>> ModulePath;
for (;;) { for (;;) {
auto T1 = Tokens.peek(); auto T1 = Tokens.peek(0);
if (T1->getKind() != NodeKind::IdentifierAlt) { auto T2 = Tokens.peek(1);
if (!llvm::isa<IdentifierAlt>(T1) || !llvm::isa<Dot>(T2)) {
break; break;
} }
Tokens.get(); Tokens.get();
auto Dot = expectToken<class Dot>(); auto Dot = expectToken<class Dot>();
ModulePath.push_back(std::make_tuple(static_cast<IdentifierAlt*>(T1), Dot)); ModulePath.push_back(std::make_tuple(static_cast<IdentifierAlt*>(T1), Dot));
} }
return new ReferenceExpression(ModulePath, expectToken<Identifier>()); auto T3 = Tokens.get();
if (!llvm::isa<Symbol>(T3)) {
throw UnexpectedTokenDiagnostic(File, T3, { NodeKind::Identifier, NodeKind::IdentifierAlt });
}
return new ReferenceExpression(ModulePath, static_cast<Symbol*>(T3));
} }
case NodeKind::LParen: case NodeKind::LParen:
{ {