Compare commits
No commits in common. "b1d685bdaf27d8d5434b0b96335cf6b032425abf" and "d8a819aebab965a52368130c2f86af75c822524f" have entirely different histories.
b1d685bdaf
...
d8a819aeba
8 changed files with 71 additions and 167 deletions
|
@ -49,7 +49,6 @@ add_library(
|
|||
src/Scanner.cc
|
||||
src/Parser.cc
|
||||
src/Type.cc
|
||||
src/Constraint.cc
|
||||
src/Checker.cc
|
||||
src/Evaluator.cc
|
||||
src/Scope.cc
|
||||
|
|
|
@ -11,10 +11,55 @@
|
|||
#include "bolt/CST.hpp"
|
||||
#include "bolt/DiagnosticEngine.hpp"
|
||||
#include "bolt/Type.hpp"
|
||||
#include "bolt/Constraint.hpp"
|
||||
|
||||
namespace bolt {
|
||||
|
||||
enum class ConstraintKind {
|
||||
TypesEqual,
|
||||
};
|
||||
|
||||
class Constraint {
|
||||
|
||||
ConstraintKind Kind;
|
||||
|
||||
protected:
|
||||
|
||||
Constraint(ConstraintKind Kind):
|
||||
Kind(Kind) {}
|
||||
|
||||
public:
|
||||
|
||||
inline ConstraintKind getKind() const {
|
||||
return Kind;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class CTypesEqual : public Constraint {
|
||||
|
||||
Type* A;
|
||||
Type* B;
|
||||
Node* Origin;
|
||||
|
||||
public:
|
||||
|
||||
CTypesEqual(Type* A, Type* B, Node* Origin):
|
||||
Constraint(ConstraintKind::TypesEqual), A(A), B(B), Origin(Origin) {}
|
||||
|
||||
Type* getLeft() const {
|
||||
return A;
|
||||
}
|
||||
|
||||
Type* getRight() const {
|
||||
return B;
|
||||
}
|
||||
|
||||
Node* getOrigin() const {
|
||||
return Origin;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class TypeEnv {
|
||||
|
||||
TypeEnv* Parent;
|
||||
|
@ -31,8 +76,6 @@ public:
|
|||
|
||||
bool hasVar(TVar* TV) const;
|
||||
|
||||
void dump() const;
|
||||
|
||||
TypeScheme* lookup(ByteString Name, SymbolKind Kind);
|
||||
|
||||
};
|
||||
|
@ -48,8 +91,6 @@ class Checker {
|
|||
Type* StringType;
|
||||
Type* UnitType;
|
||||
|
||||
unsigned NextVarId = 0;
|
||||
|
||||
public:
|
||||
|
||||
Checker(DiagnosticEngine& DE);
|
||||
|
@ -71,7 +112,7 @@ public:
|
|||
}
|
||||
|
||||
TVar* createTVar() {
|
||||
return new TVar("a" + std::to_string(NextVarId++));
|
||||
return new TVar();
|
||||
}
|
||||
|
||||
Type* instantiate(TypeScheme* Scm);
|
||||
|
|
|
@ -1,60 +0,0 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "bolt/Type.hpp"
|
||||
|
||||
namespace bolt {
|
||||
|
||||
class Node;
|
||||
|
||||
enum class ConstraintKind {
|
||||
TypesEqual,
|
||||
};
|
||||
|
||||
class Constraint {
|
||||
|
||||
ConstraintKind Kind;
|
||||
|
||||
protected:
|
||||
|
||||
Constraint(ConstraintKind Kind):
|
||||
Kind(Kind) {}
|
||||
|
||||
public:
|
||||
|
||||
inline ConstraintKind getKind() const {
|
||||
return Kind;
|
||||
}
|
||||
|
||||
std::string toString() const;
|
||||
|
||||
};
|
||||
|
||||
class CTypesEqual : public Constraint {
|
||||
|
||||
Type* A;
|
||||
Type* B;
|
||||
Node* Origin;
|
||||
|
||||
public:
|
||||
|
||||
CTypesEqual(Type* A, Type* B, Node* Origin):
|
||||
Constraint(ConstraintKind::TypesEqual), A(A), B(B), Origin(Origin) {}
|
||||
|
||||
Type* getLeft() const {
|
||||
return A;
|
||||
}
|
||||
|
||||
Type* getRight() const {
|
||||
return B;
|
||||
}
|
||||
|
||||
Node* getOrigin() const {
|
||||
return Origin;
|
||||
}
|
||||
|
||||
std::string toString() const;
|
||||
|
||||
};
|
||||
|
||||
}
|
|
@ -12,8 +12,6 @@
|
|||
|
||||
namespace bolt {
|
||||
|
||||
class Constraint;
|
||||
|
||||
enum class TypeIndexKind {
|
||||
AppOp,
|
||||
AppArg,
|
||||
|
@ -125,10 +123,8 @@ class TVar : public Type {
|
|||
|
||||
public:
|
||||
|
||||
std::string Name;
|
||||
|
||||
TVar(std::string Name):
|
||||
Type(TypeKind::Var), Name(Name) {}
|
||||
TVar():
|
||||
Type(TypeKind::Var) {}
|
||||
|
||||
void set(Type* Ty) {
|
||||
auto Root = find();
|
||||
|
@ -140,11 +136,11 @@ public:
|
|||
Type* find() const override {
|
||||
TVar* Curr = const_cast<TVar*>(this);
|
||||
for (;;) {
|
||||
auto Parent = Curr->Parent;
|
||||
if (Parent == Curr || !Parent->isVar()) {
|
||||
return Parent;
|
||||
auto Keep = Curr->Parent;
|
||||
if (Keep == Curr || !Keep->isVar()) {
|
||||
return Keep;
|
||||
}
|
||||
auto Keep2 = static_cast<TVar*>(Parent);
|
||||
auto Keep2 = static_cast<TVar*>(Keep);
|
||||
Curr->Parent = Keep2->Parent;
|
||||
Curr = Keep2;
|
||||
}
|
||||
|
@ -218,15 +214,12 @@ public:
|
|||
struct TypeScheme {
|
||||
|
||||
std::unordered_set<TVar*> Unbound;
|
||||
std::vector<Constraint*> Constraints;
|
||||
Type* Ty;
|
||||
|
||||
Type* getType() const {
|
||||
return Ty;
|
||||
}
|
||||
|
||||
std::string toString() const;
|
||||
|
||||
};
|
||||
|
||||
class TypeVisitor {
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
#include <unordered_set>
|
||||
#include <cwchar>
|
||||
#include <functional>
|
||||
|
||||
#include "bolt/CSTVisitor.hpp"
|
||||
#include "zen/graph.hpp"
|
||||
|
||||
#include "bolt/ByteString.hpp"
|
||||
#include "bolt/CST.hpp"
|
||||
#include "bolt/CSTVisitor.hpp"
|
||||
#include "bolt/Type.hpp"
|
||||
#include "bolt/Diagnostics.hpp"
|
||||
#include <algorithm>
|
||||
#include <cwchar>
|
||||
#include <functional>
|
||||
#include <variant>
|
||||
#include "bolt/Checker.hpp"
|
||||
|
||||
namespace bolt {
|
||||
|
@ -32,30 +33,11 @@ TypeScheme* TypeEnv::lookup(ByteString Name, SymbolKind Kind) {
|
|||
}
|
||||
|
||||
void TypeEnv::add(ByteString Name, TypeScheme* Scm, SymbolKind Kind) {
|
||||
Mapping[std::make_tuple(Name, Kind)] = Scm;
|
||||
Mapping.emplace(std::make_tuple(Name, Kind), Scm);
|
||||
}
|
||||
|
||||
void TypeEnv::add(ByteString Name, Type* Ty, SymbolKind Kind) {
|
||||
add(Name, new TypeScheme { {}, {}, Ty }, Kind);
|
||||
}
|
||||
|
||||
void TypeEnv::dump() const {
|
||||
for (auto [Tuple, Scm]: Mapping) {
|
||||
auto Name = std::get<0>(Tuple);
|
||||
auto Kind = std::get<1>(Tuple);
|
||||
switch (Kind) {
|
||||
case SymbolKind::Var:
|
||||
std::cerr << "let " << Name << " : " << Scm->toString() << "\n";
|
||||
break;
|
||||
case SymbolKind::Type:
|
||||
std::cerr << "type " << Name << " = " << Scm->toString() << "\n";
|
||||
break;
|
||||
case SymbolKind::Class:
|
||||
ZEN_UNREACHABLE // TODO
|
||||
case SymbolKind::Constructor:
|
||||
ZEN_UNREACHABLE // TODO
|
||||
}
|
||||
}
|
||||
add(Name, new TypeScheme { {}, Ty }, Kind);
|
||||
}
|
||||
|
||||
using TVSub = std::unordered_map<TVar*, Type*>;
|
||||
|
@ -112,7 +94,6 @@ Type* Checker::instantiate(TypeScheme* Scm) {
|
|||
auto Fresh = createTVar();
|
||||
Sub[TV] = Fresh;
|
||||
}
|
||||
// TODO instantiate constraints
|
||||
return substituteType(Scm->getType(), Sub);
|
||||
}
|
||||
|
||||
|
@ -486,45 +467,26 @@ bool TypeEnv::hasVar(TVar* TV) const {
|
|||
return false;
|
||||
}
|
||||
|
||||
static void addUnbound(Type* Ty, const TypeEnv& Env, std::unordered_set<TVar*>& Vars) {
|
||||
auto getUnbound(const TypeEnv& Env, Type* Ty) {
|
||||
struct Visitor : public TypeVisitor {
|
||||
const TypeEnv& Env;
|
||||
std::unordered_set<TVar*>& Vars;
|
||||
Visitor(const TypeEnv& Env, std::unordered_set<TVar*>& Vars):
|
||||
Env(Env), Vars(Vars) {}
|
||||
Visitor(const TypeEnv& Env):
|
||||
Env(Env) {}
|
||||
std::vector<TVar*> Out;
|
||||
void visitVar(TVar* TV) {
|
||||
auto Solved = TV->find();
|
||||
if (isa<TVar>(Solved)) {
|
||||
auto Var = static_cast<TVar*>(Solved);
|
||||
if (!Env.hasVar(Var)) {
|
||||
Vars.emplace(Var);
|
||||
Out.push_back(Var);
|
||||
}
|
||||
} else {
|
||||
visit(Solved);
|
||||
}
|
||||
}
|
||||
} V { Env, Vars };
|
||||
} V { Env };
|
||||
V.visit(Ty);
|
||||
}
|
||||
|
||||
static void addUnbound(const Constraint& C, const TypeEnv& Env, std::unordered_set<TVar*>& Vars) {
|
||||
switch (C.getKind()) {
|
||||
case ConstraintKind::TypesEqual:
|
||||
{
|
||||
auto TE = static_cast<const CTypesEqual&>(C);
|
||||
addUnbound(TE.getLeft(), Env, Vars);
|
||||
addUnbound(TE.getRight(), Env, Vars);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static TypeScheme* generalize(const TypeEnv& Env, const ConstraintSet& Constraints, Type* Ty) {
|
||||
std::unordered_set<TVar*> Vars;
|
||||
for (const auto C: Constraints) {
|
||||
addUnbound(*C, Env, Vars);
|
||||
}
|
||||
return new TypeScheme { Vars, Constraints, Ty };
|
||||
return V.Out;
|
||||
}
|
||||
|
||||
ConstraintSet Checker::inferMany(TypeEnv& Env, std::vector<Node*>& Elements, Type* RetTy) {
|
||||
|
@ -602,9 +564,10 @@ ConstraintSet Checker::inferMany(TypeEnv& Env, std::vector<Node*>& Elements, Typ
|
|||
for (auto N: Mutual) {
|
||||
if (isa<FunctionDeclaration>(N)) {
|
||||
auto Func = static_cast<FunctionDeclaration*>(N);
|
||||
auto Unbound = getUnbound(Env, Func->getType());
|
||||
Env.add(
|
||||
Func->getNameAsString(),
|
||||
generalize(Env, Out, Func->getType()->find()),
|
||||
new TypeScheme { { Unbound.begin(), Unbound.end() }, Func->getType()->find() },
|
||||
SymbolKind::Var
|
||||
);
|
||||
}
|
||||
|
@ -774,7 +737,7 @@ void Checker::run(SourceFile* SF) {
|
|||
Env.add("not", new TFun(Bool, Bool), SymbolKind::Var);
|
||||
Env.add("+", new TFun(Int, new TFun(Int, Int)), SymbolKind::Var);
|
||||
Env.add("-", new TFun(Int, new TFun(Int, Int)), SymbolKind::Var);
|
||||
Env.add("$", new TypeScheme({ A, B }, {}, new TFun(new TFun(A, B), new TFun(A, B))), SymbolKind::Var);
|
||||
Env.add("$", new TypeScheme({ A, B }, new TFun(new TFun(A, B), new TFun(A, B))), SymbolKind::Var);
|
||||
auto Out = inferSourceFile(Env, SF);
|
||||
solve(Out);
|
||||
}
|
||||
|
|
|
@ -616,7 +616,6 @@ void ConsolePrinter::writeDiagnostic(const Diagnostic& D) {
|
|||
write(E.Filename.c_str());
|
||||
write(": ");
|
||||
write(strerror(E.Code.value()));
|
||||
write("\n");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
|
||||
#include "bolt/Constraint.hpp"
|
||||
|
||||
namespace bolt {
|
||||
|
||||
std::string Constraint::toString() const {
|
||||
switch (Kind) {
|
||||
case ConstraintKind::TypesEqual:
|
||||
return static_cast<const CTypesEqual*>(this)->toString();
|
||||
}
|
||||
}
|
||||
|
||||
std::string CTypesEqual::toString() const {
|
||||
return A->toString() + " ~ " + B->toString();
|
||||
}
|
||||
|
||||
}
|
16
src/Type.cc
16
src/Type.cc
|
@ -1,6 +1,5 @@
|
|||
|
||||
#include "zen/config.hpp"
|
||||
#include <sstream>
|
||||
|
||||
#include "bolt/Type.hpp"
|
||||
|
||||
|
@ -75,7 +74,7 @@ std::string Type::toString() const {
|
|||
return F->getLeft()->toString() + " -> " + F->getRight()->toString();
|
||||
}
|
||||
case TypeKind::Var:
|
||||
return static_cast<const TVar*>(this)->Name;
|
||||
return "α";
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -100,17 +99,4 @@ void TypeVisitor::visit(Type* Ty) {
|
|||
}
|
||||
}
|
||||
|
||||
std::string TypeScheme::toString() const {
|
||||
std::ostringstream Out;
|
||||
if (!Unbound.empty()) {
|
||||
Out << "forall";
|
||||
for (auto TV: Unbound) {
|
||||
Out << " " << TV->toString();
|
||||
}
|
||||
Out << ". ";
|
||||
}
|
||||
Out << Ty->toString();
|
||||
return Out.str();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue