Fixing constraint bug, pretty printing constraints

This commit is contained in:
Dustin Swan 2026-03-28 22:19:27 -06:00
parent 784e095345
commit 479abf7996
No known key found for this signature in database
GPG key ID: 30D46587E2100467
2 changed files with 6 additions and 4 deletions

View file

@ -344,7 +344,9 @@ export function prettyPrint(ast: AST, indent = 0): string {
case 'definition': case 'definition':
const ann = ast.annotation const ann = ast.annotation
? ` : ${prettyPrintType(ast.annotation.type)}` ? ` : ${ast.annotation.constraints.length > 0
? ast.annotation.constraints.map(c => `${c.className} ${c.typeVar}`).join(', ') + ' :: '
: ''}${prettyPrintType(ast.annotation.type)}`
: ''; : '';
if (!ast.body) return `${ast.name}${ann};`; if (!ast.body) return `${ast.name}${ann};`;
return `${ast.name}${ann} = ${prettyPrint(ast.body, indent)};`; return `${ast.name}${ann} = ${prettyPrint(ast.body, indent)};`;

View file

@ -185,7 +185,9 @@ export class Parser {
let annotation: Annotation | undefined; let annotation: Annotation | undefined;
if (this.current().kind === 'colon') { if (this.current().kind === 'colon') {
this.advance(); this.advance();
annotation = { constraints: this.parsedConstraints, type: this.parseType() }; const constraints = this.tryParseConstraints();
const type = this.parseType();
annotation = { constraints, type };
// Declaration only // Declaration only
if (this.current().kind === 'semicolon') { if (this.current().kind === 'semicolon') {
@ -633,8 +635,6 @@ export class Parser {
private parseType(): TypeAST { private parseType(): TypeAST {
// Check for constraints: Num a, Eq b :: <type> // Check for constraints: Num a, Eq b :: <type>
this.parsedConstraints = this.tryParseConstraints();
const left = this.parseTypeApply(); const left = this.parseTypeApply();
if (this.current().kind === 'backslash') { if (this.current().kind === 'backslash') {