Decouple type checking info from CST and refactor checker.ts a bit

This commit is contained in:
Sam Vervaeck 2023-08-12 13:46:19 +02:00
parent aa386f2e79
commit e92e346bad
Signed by: samvv
SSH key fingerprint: SHA256:dIg0ywU1OP+ZYifrYxy8c5esO72cIKB+4/9wkZj1VaY
4 changed files with 365 additions and 284 deletions

View file

@ -23,6 +23,22 @@ nice goodies, including:
- **Cross-platform standard library**, allowing you to write your code for the - **Cross-platform standard library**, allowing you to write your code for the
web and the desktop at the same time. web and the desktop at the same time.
## Examples
_Note that these examples are stil in the design phase and not able to compile._
```ocaml
import "html" ( HtmlComponent )
let app : HtmlComponent.
let { fullname, .. } = perform get_app_state
return match user.
None => h1 [ "Please log in." ]
Some({ fullname, .. }) =>
```
## Core Principles ## Core Principles
Bolt has a few fundamental design principles that we hope in time will make it Bolt has a few fundamental design principles that we hope in time will make it

File diff suppressed because it is too large Load diff

View file

@ -4,7 +4,7 @@ import path from "path"
import { assert, implementationLimitation, IndentWriter, JSONObject, JSONValue, nonenumerable, unreachable } from "./util"; import { assert, implementationLimitation, IndentWriter, JSONObject, JSONValue, nonenumerable, unreachable } from "./util";
import { isNodeWithScope, Scope } from "./scope" import { isNodeWithScope, Scope } from "./scope"
import type { InferContext, Kind, KindEnv, Scheme, TypeEnv } from "./checker" import type { Kind, Scheme } from "./checker"
import type { Type } from "./types"; import type { Type } from "./types";
import { Emitter } from "./emitter"; import { Emitter } from "./emitter";
@ -253,11 +253,6 @@ abstract class SyntaxBase {
@nonenumerable @nonenumerable
public parent: Syntax | null = null; public parent: Syntax | null = null;
@nonenumerable
public inferredKind?: Kind;
@nonenumerable
public inferredType?: Type;
public abstract getFirstToken(): Token; public abstract getFirstToken(): Token;
public abstract getLastToken(): Token; public abstract getLastToken(): Token;
@ -296,6 +291,10 @@ abstract class SyntaxBase {
throw new Error(`Could not find a scope for ${this}. Maybe the parent links are not set?`); throw new Error(`Could not find a scope for ${this}. Maybe the parent links are not set?`);
} }
public getParentScope(): Scope | null {
return this.parent === null ? null : this.parent.getScope();
}
public getEnclosingModule(): ModuleDeclaration | SourceFile { public getEnclosingModule(): ModuleDeclaration | SourceFile {
let curr = this.parent!; let curr = this.parent!;
while (curr !== null) { while (curr !== null) {
@ -2595,9 +2594,6 @@ export class EnumDeclaration extends SyntaxBase {
public readonly kind = SyntaxKind.EnumDeclaration; public readonly kind = SyntaxKind.EnumDeclaration;
@nonenumerable
public typeEnv?: TypeEnv;
public constructor( public constructor(
public pubKeyword: PubKeyword | null, public pubKeyword: PubKeyword | null,
public enumKeyword: EnumKeyword, public enumKeyword: EnumKeyword,
@ -2668,9 +2664,6 @@ export class StructDeclaration extends SyntaxBase {
public readonly kind = SyntaxKind.StructDeclaration; public readonly kind = SyntaxKind.StructDeclaration;
@nonenumerable
public typeEnv?: TypeEnv;
public constructor( public constructor(
public pubKeyword: PubKeyword | null, public pubKeyword: PubKeyword | null,
public structKeyword: StructKeyword, public structKeyword: StructKeyword,
@ -2836,9 +2829,6 @@ export class TypeDeclaration extends SyntaxBase {
public readonly kind = SyntaxKind.TypeDeclaration; public readonly kind = SyntaxKind.TypeDeclaration;
@nonenumerable
public typeEnv?: TypeEnv;
public constructor( public constructor(
public pubKeyword: PubKeyword | null, public pubKeyword: PubKeyword | null,
public typeKeyword: TypeKeyword, public typeKeyword: TypeKeyword,
@ -2881,17 +2871,11 @@ export class LetDeclaration extends SyntaxBase {
@nonenumerable @nonenumerable
public scope?: Scope; public scope?: Scope;
@nonenumerable
public typeEnv?: TypeEnv;
@nonenumerable @nonenumerable
public activeCycle?: boolean; public activeCycle?: boolean;
@nonenumerable @nonenumerable
public visited?: boolean; public visited?: boolean;
@nonenumerable
public context?: InferContext;
public constructor( public constructor(
public pubKeyword: PubKeyword | null, public pubKeyword: PubKeyword | null,
public letKeyword: LetKeyword, public letKeyword: LetKeyword,
@ -3082,9 +3066,6 @@ export class ClassDeclaration extends SyntaxBase {
public readonly kind = SyntaxKind.ClassDeclaration; public readonly kind = SyntaxKind.ClassDeclaration;
@nonenumerable
public typeEnv?: TypeEnv;
public constructor( public constructor(
public pubKeyword: PubKeyword | null, public pubKeyword: PubKeyword | null,
public classKeyword: ClassKeyword, public classKeyword: ClassKeyword,
@ -3186,9 +3167,6 @@ export class InstanceDeclaration extends SyntaxBase {
public readonly kind = SyntaxKind.InstanceDeclaration; public readonly kind = SyntaxKind.InstanceDeclaration;
@nonenumerable
public typeEnv?: TypeEnv;
public constructor( public constructor(
public pubKeyword: PubKeyword | null, public pubKeyword: PubKeyword | null,
public classKeyword: InstanceKeyword, public classKeyword: InstanceKeyword,
@ -3233,11 +3211,6 @@ export class ModuleDeclaration extends SyntaxBase {
public readonly kind = SyntaxKind.ModuleDeclaration; public readonly kind = SyntaxKind.ModuleDeclaration;
@nonenumerable
public typeEnv?: TypeEnv;
@nonenumerable
public kindEnv?: KindEnv;
public constructor( public constructor(
public pubKeyword: PubKeyword | null, public pubKeyword: PubKeyword | null,
public modKeyword: ModKeyword, public modKeyword: ModKeyword,
@ -3287,10 +3260,6 @@ export class SourceFile extends SyntaxBase {
@nonenumerable @nonenumerable
public scope?: Scope; public scope?: Scope;
@nonenumerable
public typeEnv?: TypeEnv;
@nonenumerable
public kindEnv?: KindEnv;
public constructor( public constructor(
private file: TextFile, private file: TextFile,

View file

@ -50,7 +50,7 @@ export abstract class TypeBase {
this.find().parent = newType; this.find().parent = newType;
} }
public hasTypeVar(tv: TUniVar): boolean { public hasTypeVar(tv: TRegularVar): boolean {
for (const other of this.getTypeVars()) { for (const other of this.getTypeVars()) {
if (tv.id === other.id) { if (tv.id === other.id) {
return true; return true;
@ -105,7 +105,7 @@ export class TRigidVar extends TypeBase {
} }
export class TUniVar extends TypeBase { export class TRegularVar extends TypeBase {
public readonly kind = TypeKind.UniVar; public readonly kind = TypeKind.UniVar;
@ -122,8 +122,8 @@ export class TUniVar extends TypeBase {
yield this; yield this;
} }
public shallowClone(): TUniVar { public shallowClone(): TRegularVar {
return new TUniVar(this.id, this.node); return new TRegularVar(this.id, this.node);
} }
public substitute(sub: TVSub): Type { public substitute(sub: TVSub): Type {
@ -451,7 +451,7 @@ export type Type
= TCon = TCon
| TArrow | TArrow
| TRigidVar | TRigidVar
| TUniVar | TRegularVar
| TApp | TApp
| TField | TField
| TNil | TNil
@ -459,10 +459,9 @@ export type Type
| TAbsent | TAbsent
export type TVar export type TVar
= TUniVar = TRegularVar
| TRigidVar | TRigidVar
export function typesEqual(a: Type, b: Type): boolean { export function typesEqual(a: Type, b: Type): boolean {
if (a.kind !== b.kind) { if (a.kind !== b.kind) {
return false; return false;