Minor changes

- Refactor Constructor to IdentifierAlt
 - Make () -> a unify with a
This commit is contained in:
Sam Vervaeck 2022-09-01 20:18:47 +02:00
parent 666256ed15
commit 20af138fa5
4 changed files with 27 additions and 18 deletions

View file

@ -9,7 +9,7 @@ import {
} from "./cst"; } from "./cst";
import { ArityMismatchDiagnostic, BindingNotFoudDiagnostic, Diagnostics, UnificationFailedDiagnostic } from "./diagnostics"; import { ArityMismatchDiagnostic, BindingNotFoudDiagnostic, Diagnostics, UnificationFailedDiagnostic } from "./diagnostics";
import { assert } from "./util"; import { assert } from "./util";
import { LabeledDirectedHashGraph, LabeledGraph, strongconnect, toposort } from "yagl" import { LabeledDirectedHashGraph, LabeledGraph, strongconnect } from "yagl"
export enum TypeKind { export enum TypeKind {
Arrow, Arrow,
@ -1001,6 +1001,14 @@ export class Checker {
return success; return success;
} }
if (left.kind === TypeKind.Arrow && left.paramTypes.length === 0) {
return this.unify(left.returnType, right, solution);
}
if (right.kind === TypeKind.Arrow) {
return this.unify(right, left, solution);
}
if (left.kind === TypeKind.Con && right.kind === TypeKind.Con) { if (left.kind === TypeKind.Con && right.kind === TypeKind.Con) {
if (left.id !== right.id) { if (left.id !== right.id) {
return false; return false;

View file

@ -69,7 +69,7 @@ export const enum SyntaxKind {
// Tokens // Tokens
Identifier, Identifier,
Constructor, IdentifierAlt,
CustomOperator, CustomOperator,
Assignment, Assignment,
LParen, LParen,
@ -207,6 +207,7 @@ export class Scope {
if (isNodeWithScope(curr)) { if (isNodeWithScope(curr)) {
return curr.getScope(); return curr.getScope();
} }
curr = curr.parent;
} }
return null; return null;
} }
@ -502,9 +503,9 @@ export class StringLiteral extends TokenBase {
} }
export class Constructor extends TokenBase { export class IdentifierAlt extends TokenBase {
public readonly kind = SyntaxKind.Constructor; public readonly kind = SyntaxKind.IdentifierAlt;
public constructor( public constructor(
public text: string, public text: string,
@ -792,7 +793,7 @@ export type Token
| LBracket | LBracket
| RBracket | RBracket
| Identifier | Identifier
| Constructor | IdentifierAlt
| CustomOperator | CustomOperator
| Integer | Integer
| StringLiteral | StringLiteral
@ -828,7 +829,7 @@ export class ReferenceTypeExpression extends SyntaxBase {
public constructor( public constructor(
public modulePath: Array<[Identifier, Dot]>, public modulePath: Array<[Identifier, Dot]>,
public name: Identifier, public name: IdentifierAlt,
) { ) {
super(); super();
} }
@ -900,7 +901,7 @@ export class NamedTuplePattern extends SyntaxBase {
public readonly kind = SyntaxKind.NamedTuplePattern; public readonly kind = SyntaxKind.NamedTuplePattern;
public constructor( public constructor(
public name: Constructor, public name: IdentifierAlt,
public elements: Pattern[], public elements: Pattern[],
) { ) {
super(); super();
@ -995,7 +996,7 @@ export class StructPattern extends SyntaxBase {
public readonly kind = SyntaxKind.StructPattern; public readonly kind = SyntaxKind.StructPattern;
public constructor( public constructor(
public name: Constructor, public name: IdentifierAlt,
public lbrace: LBrace, public lbrace: LBrace,
public elements: StructPatternElement[], public elements: StructPatternElement[],
public rbrace: RBrace, public rbrace: RBrace,
@ -1205,7 +1206,7 @@ export class StructExpression extends SyntaxBase {
public readonly kind = SyntaxKind.StructExpression; public readonly kind = SyntaxKind.StructExpression;
public constructor( public constructor(
public name: Constructor, public name: IdentifierAlt,
public lbrace: LBrace, public lbrace: LBrace,
public elements: StructExpressionElement[], public elements: StructExpressionElement[],
public rbrace: RBrace, public rbrace: RBrace,
@ -1228,7 +1229,7 @@ export class NamedTupleExpression extends SyntaxBase {
public readonly kind = SyntaxKind.NamedTupleExpression; public readonly kind = SyntaxKind.NamedTupleExpression;
public constructor( public constructor(
public name: Constructor, public name: IdentifierAlt,
public elements: Expression[], public elements: Expression[],
) { ) {
super(); super();

View file

@ -156,17 +156,17 @@ export class Parser {
} }
public parseReferenceTypeExpression(): ReferenceTypeExpression { public parseReferenceTypeExpression(): ReferenceTypeExpression {
const name = this.expectToken(SyntaxKind.Identifier); const name = this.expectToken(SyntaxKind.IdentifierAlt);
return new ReferenceTypeExpression([], name); return new ReferenceTypeExpression([], name);
} }
public parseTypeExpression(): TypeExpression { public parseTypeExpression(): TypeExpression {
const t0 = this.peekToken(); const t0 = this.peekToken();
switch (t0.kind) { switch (t0.kind) {
case SyntaxKind.Identifier: case SyntaxKind.IdentifierAlt:
return this.parseReferenceTypeExpression(); return this.parseReferenceTypeExpression();
default: default:
this.raiseParseError(t0, [ SyntaxKind.Identifier ]); this.raiseParseError(t0, [ SyntaxKind.IdentifierAlt ]);
} }
} }
@ -218,7 +218,7 @@ export class Parser {
return this.parseExpressionWithParens(); return this.parseExpressionWithParens();
case SyntaxKind.Identifier: case SyntaxKind.Identifier:
return this.parseReferenceExpression(); return this.parseReferenceExpression();
case SyntaxKind.Constructor: case SyntaxKind.IdentifierAlt:
{ {
this.getToken(); this.getToken();
const t1 = this.peekToken(); const t1 = this.peekToken();
@ -384,7 +384,7 @@ export class Parser {
} }
private parsePatternStartingWithConstructor() { private parsePatternStartingWithConstructor() {
const name = this.expectToken(SyntaxKind.Constructor); const name = this.expectToken(SyntaxKind.IdentifierAlt);
const t2 = this.peekToken(); const t2 = this.peekToken();
if (t2.kind === SyntaxKind.LBrace) { if (t2.kind === SyntaxKind.LBrace) {
this.getToken(); this.getToken();
@ -467,7 +467,7 @@ export class Parser {
{ {
this.getToken(); this.getToken();
const t1 = this.peekToken(); const t1 = this.peekToken();
if (t1.kind === SyntaxKind.Constructor) { if (t1.kind === SyntaxKind.IdentifierAlt) {
return this.parsePatternStartingWithConstructor(); return this.parsePatternStartingWithConstructor();
} else { } else {
return this.parseTuplePattern(); return this.parseTuplePattern();

View file

@ -25,7 +25,7 @@ import {
RBracket, RBracket,
ReturnKeyword, ReturnKeyword,
CustomOperator, CustomOperator,
Constructor, IdentifierAlt,
Integer, Integer,
TextFile, TextFile,
Dot, Dot,
@ -353,7 +353,7 @@ export class Scanner extends BufferedStream<Token> {
case 'elif': return new ElifKeyword(startPos); case 'elif': return new ElifKeyword(startPos);
default: default:
if (isUpper(text[0])) { if (isUpper(text[0])) {
return new Constructor(text, startPos); return new IdentifierAlt(text, startPos);
} else { } else {
return new Identifier(text, startPos); return new Identifier(text, startPos);
} }