Fix symbol resolution and error handling

This commit is contained in:
Sam Vervaeck 2020-05-27 20:59:45 +02:00
parent 05b024c3f4
commit ce8c0aa7a1
9 changed files with 36 additions and 35 deletions

View file

@ -120,7 +120,7 @@ node BoltLiftedTypeExpression > BoltTypeExpression {
node BoltTypeParameter { node BoltTypeParameter {
index: usize, index: usize,
name: BoltIdentifier, name: BoltIdentifier,
typeNode: Option<BoltTypeExpression>, typeExpr: Option<BoltTypeExpression>,
defaultType: Option<BoltTypeExpression>, defaultType: Option<BoltTypeExpression>,
} }
@ -131,7 +131,7 @@ node BoltBindPattern > BoltPattern {
} }
node BoltTypePattern > BoltPattern { node BoltTypePattern > BoltPattern {
type: BoltTypeExpression, typeExpr: BoltTypeExpression,
nestedPattern: BoltPattern, nestedPattern: BoltPattern,
} }
@ -246,7 +246,7 @@ node BoltExpressionStatement > BoltStatement {
node BoltParameter { node BoltParameter {
index: usize, index: usize,
bindings: BoltPattern, bindings: BoltPattern,
type: Option<BoltTypeExpression>, typeExpr: Option<BoltTypeExpression>,
defaultValue: Option<BoltExpression>, defaultValue: Option<BoltExpression>,
} }
@ -280,7 +280,7 @@ node BoltFunctionDeclaration > BoltFunctionBodyElement, BoltDeclaration {
node BoltVariableDeclaration > BoltFunctionBodyElement, BoltDeclaration { node BoltVariableDeclaration > BoltFunctionBodyElement, BoltDeclaration {
modifiers: BoltModifiers, modifiers: BoltModifiers,
bindings: BoltPattern, bindings: BoltPattern,
type: Option<BoltTypeExpression>, typeExpr: Option<BoltTypeExpression>,
value: Option<BoltExpression>, value: Option<BoltExpression>,
} }
@ -335,7 +335,7 @@ node BoltRecordMember;
node BoltRecordField > BoltRecordMember { node BoltRecordField > BoltRecordMember {
name: BoltIdentifier, name: BoltIdentifier,
type: BoltTypeExpression, typeExpr: BoltTypeExpression,
} }
node BoltRecordDeclaration > BoltDeclaration, BoltTypeDeclaration { node BoltRecordDeclaration > BoltDeclaration, BoltTypeDeclaration {

22
src/ast.d.ts vendored
View file

@ -1,5 +1,5 @@
import { Type } from "./checker" import { Type } from "./types"
import { Package } from "./common" import { Package } from "./common"
import { TextSpan } from "./text" import { TextSpan } from "./text"
@ -2941,7 +2941,7 @@ export interface BoltTypeParameter extends SyntaxBase {
kind: SyntaxKind.BoltTypeParameter; kind: SyntaxKind.BoltTypeParameter;
index: number; index: number;
name: BoltIdentifier; name: BoltIdentifier;
typeNode: BoltTypeExpression | null; typeExpr: BoltTypeExpression | null;
defaultType: BoltTypeExpression | null; defaultType: BoltTypeExpression | null;
parentNode: BoltTypeParameterParent; parentNode: BoltTypeParameterParent;
getChildNodes(): IterableIterator<BoltTypeParameterChild> getChildNodes(): IterableIterator<BoltTypeParameterChild>
@ -3020,7 +3020,7 @@ export type BoltBindPatternChild
export interface BoltTypePattern extends SyntaxBase { export interface BoltTypePattern extends SyntaxBase {
kind: SyntaxKind.BoltTypePattern; kind: SyntaxKind.BoltTypePattern;
type: BoltTypeExpression; typeExpr: BoltTypeExpression;
nestedPattern: BoltPattern; nestedPattern: BoltPattern;
parentNode: BoltTypePatternParent; parentNode: BoltTypePatternParent;
getChildNodes(): IterableIterator<BoltTypePatternChild> getChildNodes(): IterableIterator<BoltTypePatternChild>
@ -4250,7 +4250,7 @@ export interface BoltParameter extends SyntaxBase {
kind: SyntaxKind.BoltParameter; kind: SyntaxKind.BoltParameter;
index: number; index: number;
bindings: BoltPattern; bindings: BoltPattern;
type: BoltTypeExpression | null; typeExpr: BoltTypeExpression | null;
defaultValue: BoltExpression | null; defaultValue: BoltExpression | null;
parentNode: BoltParameterParent; parentNode: BoltParameterParent;
getChildNodes(): IterableIterator<BoltParameterChild> getChildNodes(): IterableIterator<BoltParameterChild>
@ -4380,7 +4380,7 @@ export interface BoltVariableDeclaration extends SyntaxBase {
kind: SyntaxKind.BoltVariableDeclaration; kind: SyntaxKind.BoltVariableDeclaration;
modifiers: BoltModifiers; modifiers: BoltModifiers;
bindings: BoltPattern; bindings: BoltPattern;
type: BoltTypeExpression | null; typeExpr: BoltTypeExpression | null;
value: BoltExpression | null; value: BoltExpression | null;
parentNode: BoltVariableDeclarationParent; parentNode: BoltVariableDeclarationParent;
getChildNodes(): IterableIterator<BoltVariableDeclarationChild> getChildNodes(): IterableIterator<BoltVariableDeclarationChild>
@ -4622,7 +4622,7 @@ export type BoltRecordMember
export interface BoltRecordField extends SyntaxBase { export interface BoltRecordField extends SyntaxBase {
kind: SyntaxKind.BoltRecordField; kind: SyntaxKind.BoltRecordField;
name: BoltIdentifier; name: BoltIdentifier;
type: BoltTypeExpression; typeExpr: BoltTypeExpression;
parentNode: BoltRecordFieldParent; parentNode: BoltRecordFieldParent;
getChildNodes(): IterableIterator<BoltRecordFieldChild> getChildNodes(): IterableIterator<BoltRecordFieldChild>
} }
@ -7884,9 +7884,9 @@ export function createBoltModulePath(isAbsolute: boolean, elements: BoltIdentifi
export function createBoltReferenceTypeExpression(name: BoltQualName, arguments: BoltTypeExpression[] | null, span?: TextSpan | null): BoltReferenceTypeExpression; export function createBoltReferenceTypeExpression(name: BoltQualName, arguments: BoltTypeExpression[] | null, span?: TextSpan | null): BoltReferenceTypeExpression;
export function createBoltFunctionTypeExpression(params: BoltParameter[], returnType: BoltTypeExpression | null, span?: TextSpan | null): BoltFunctionTypeExpression; export function createBoltFunctionTypeExpression(params: BoltParameter[], returnType: BoltTypeExpression | null, span?: TextSpan | null): BoltFunctionTypeExpression;
export function createBoltLiftedTypeExpression(expression: BoltExpression, span?: TextSpan | null): BoltLiftedTypeExpression; export function createBoltLiftedTypeExpression(expression: BoltExpression, span?: TextSpan | null): BoltLiftedTypeExpression;
export function createBoltTypeParameter(index: number, name: BoltIdentifier, typeNode: BoltTypeExpression | null, defaultType: BoltTypeExpression | null, span?: TextSpan | null): BoltTypeParameter; export function createBoltTypeParameter(index: number, name: BoltIdentifier, typeExpr: BoltTypeExpression | null, defaultType: BoltTypeExpression | null, span?: TextSpan | null): BoltTypeParameter;
export function createBoltBindPattern(name: BoltIdentifier, span?: TextSpan | null): BoltBindPattern; export function createBoltBindPattern(name: BoltIdentifier, span?: TextSpan | null): BoltBindPattern;
export function createBoltTypePattern(type: BoltTypeExpression, nestedPattern: BoltPattern, span?: TextSpan | null): BoltTypePattern; export function createBoltTypePattern(typeExpr: BoltTypeExpression, nestedPattern: BoltPattern, span?: TextSpan | null): BoltTypePattern;
export function createBoltExpressionPattern(expression: BoltExpression, span?: TextSpan | null): BoltExpressionPattern; export function createBoltExpressionPattern(expression: BoltExpression, span?: TextSpan | null): BoltExpressionPattern;
export function createBoltTuplePatternElement(index: number, pattern: BoltPattern, span?: TextSpan | null): BoltTuplePatternElement; export function createBoltTuplePatternElement(index: number, pattern: BoltPattern, span?: TextSpan | null): BoltTuplePatternElement;
export function createBoltTuplePattern(elements: BoltTuplePatternElement[], span?: TextSpan | null): BoltTuplePattern; export function createBoltTuplePattern(elements: BoltTuplePatternElement[], span?: TextSpan | null): BoltTuplePattern;
@ -7910,10 +7910,10 @@ export function createBoltConditionalCase(test: BoltExpression | null, body: Bol
export function createBoltConditionalStatement(cases: BoltConditionalCase[], span?: TextSpan | null): BoltConditionalStatement; export function createBoltConditionalStatement(cases: BoltConditionalCase[], span?: TextSpan | null): BoltConditionalStatement;
export function createBoltResumeStatement(value: BoltExpression, span?: TextSpan | null): BoltResumeStatement; export function createBoltResumeStatement(value: BoltExpression, span?: TextSpan | null): BoltResumeStatement;
export function createBoltExpressionStatement(expression: BoltExpression, span?: TextSpan | null): BoltExpressionStatement; export function createBoltExpressionStatement(expression: BoltExpression, span?: TextSpan | null): BoltExpressionStatement;
export function createBoltParameter(index: number, bindings: BoltPattern, type: BoltTypeExpression | null, defaultValue: BoltExpression | null, span?: TextSpan | null): BoltParameter; export function createBoltParameter(index: number, bindings: BoltPattern, typeExpr: BoltTypeExpression | null, defaultValue: BoltExpression | null, span?: TextSpan | null): BoltParameter;
export function createBoltModule(modifiers: BoltModifiers, name: BoltIdentifier[], elements: BoltSourceElement[], span?: TextSpan | null): BoltModule; export function createBoltModule(modifiers: BoltModifiers, name: BoltIdentifier[], elements: BoltSourceElement[], span?: TextSpan | null): BoltModule;
export function createBoltFunctionDeclaration(modifiers: BoltModifiers, target: string, name: BoltSymbol, params: BoltParameter[], returnType: BoltTypeExpression | null, typeParams: BoltTypeParameter[] | null, body: BoltFunctionBodyElement[], span?: TextSpan | null): BoltFunctionDeclaration; export function createBoltFunctionDeclaration(modifiers: BoltModifiers, target: string, name: BoltSymbol, params: BoltParameter[], returnType: BoltTypeExpression | null, typeParams: BoltTypeParameter[] | null, body: BoltFunctionBodyElement[], span?: TextSpan | null): BoltFunctionDeclaration;
export function createBoltVariableDeclaration(modifiers: BoltModifiers, bindings: BoltPattern, type: BoltTypeExpression | null, value: BoltExpression | null, span?: TextSpan | null): BoltVariableDeclaration; export function createBoltVariableDeclaration(modifiers: BoltModifiers, bindings: BoltPattern, typeExpr: BoltTypeExpression | null, value: BoltExpression | null, span?: TextSpan | null): BoltVariableDeclaration;
export function createBoltPlainImportSymbol(remote: BoltQualName, local: BoltSymbol, span?: TextSpan | null): BoltPlainImportSymbol; export function createBoltPlainImportSymbol(remote: BoltQualName, local: BoltSymbol, span?: TextSpan | null): BoltPlainImportSymbol;
export function createBoltImportDirective(modifiers: BoltModifiers, file: BoltStringLiteral, symbols: BoltImportSymbol[], span?: TextSpan | null): BoltImportDirective; export function createBoltImportDirective(modifiers: BoltModifiers, file: BoltStringLiteral, symbols: BoltImportSymbol[], span?: TextSpan | null): BoltImportDirective;
export function createBoltExportSymbol(span?: TextSpan | null): BoltExportSymbol; export function createBoltExportSymbol(span?: TextSpan | null): BoltExportSymbol;
@ -7922,7 +7922,7 @@ export function createBoltExportDirective(file: string, symbols: BoltExportSymbo
export function createBoltTraitDeclaration(modifiers: BoltModifiers, name: BoltIdentifier, typeParams: BoltTypeParameter[] | null, elements: BoltDeclaration[], span?: TextSpan | null): BoltTraitDeclaration; export function createBoltTraitDeclaration(modifiers: BoltModifiers, name: BoltIdentifier, typeParams: BoltTypeParameter[] | null, elements: BoltDeclaration[], span?: TextSpan | null): BoltTraitDeclaration;
export function createBoltImplDeclaration(modifiers: BoltModifiers, name: BoltIdentifier, trait: BoltTypeExpression, typeParams: BoltTypeParameter[] | null, elements: BoltDeclaration[], span?: TextSpan | null): BoltImplDeclaration; export function createBoltImplDeclaration(modifiers: BoltModifiers, name: BoltIdentifier, trait: BoltTypeExpression, typeParams: BoltTypeParameter[] | null, elements: BoltDeclaration[], span?: TextSpan | null): BoltImplDeclaration;
export function createBoltTypeAliasDeclaration(modifiers: BoltModifiers, name: BoltIdentifier, typeParams: BoltTypeParameter[] | null, typeExpr: BoltTypeExpression, span?: TextSpan | null): BoltTypeAliasDeclaration; export function createBoltTypeAliasDeclaration(modifiers: BoltModifiers, name: BoltIdentifier, typeParams: BoltTypeParameter[] | null, typeExpr: BoltTypeExpression, span?: TextSpan | null): BoltTypeAliasDeclaration;
export function createBoltRecordField(name: BoltIdentifier, type: BoltTypeExpression, span?: TextSpan | null): BoltRecordField; export function createBoltRecordField(name: BoltIdentifier, typeExpr: BoltTypeExpression, span?: TextSpan | null): BoltRecordField;
export function createBoltRecordDeclaration(modifiers: BoltModifiers, name: BoltIdentifier, typeParms: BoltTypeParameter[] | null, members: BoltRecordMember[] | null, span?: TextSpan | null): BoltRecordDeclaration; export function createBoltRecordDeclaration(modifiers: BoltModifiers, name: BoltIdentifier, typeParms: BoltTypeParameter[] | null, members: BoltRecordMember[] | null, span?: TextSpan | null): BoltRecordDeclaration;
export function createBoltMacroCall(name: BoltIdentifier, text: string, span?: TextSpan | null): BoltMacroCall; export function createBoltMacroCall(name: BoltIdentifier, text: string, span?: TextSpan | null): BoltMacroCall;
export function createJSOperator(text: string, span?: TextSpan | null): JSOperator; export function createJSOperator(text: string, span?: TextSpan | null): JSOperator;

View file

@ -23,7 +23,7 @@ export class CheckInvalidFilePaths extends NodeVisitor {
const sourceFile = this.program.resolveToSourceFile(node.file.value, node); const sourceFile = this.program.resolveToSourceFile(node.file.value, node);
if (sourceFile === null) { if (sourceFile === null) {
this.diagnostics.add({ this.diagnostics.add({
severity: 'error', severity: 'fatal',
message: E_FILE_NOT_FOUND, message: E_FILE_NOT_FOUND,
args: { filename: node.file.value }, args: { filename: node.file.value },
node: node.file, node: node.file,
@ -62,7 +62,7 @@ export class CheckReferences extends NodeVisitor {
const sym = currScope!.getLocalSymbol(name.text);; const sym = currScope!.getLocalSymbol(name.text);;
if (sym === null) { if (sym === null) {
failedToFindScope = true; failedToFindScope = true;
partiallyMatchingModules.push((currScope!.source) as NodeScopeSource).node); partiallyMatchingModules.push(((currScope!.source) as NodeScopeSource).node);
break; break;
} }
assert(every(sym.declarations.values(), decl => decl.kind === SyntaxKind.BoltModule)); assert(every(sym.declarations.values(), decl => decl.kind === SyntaxKind.BoltModule));
@ -115,7 +115,7 @@ export class CheckReferences extends NodeVisitor {
} }
protected visitBoltReferenceTypeExpression(node: BoltReferenceTypeExpression) { protected visitBoltReferenceTypeExpression(node: BoltReferenceTypeExpression) {
const scope = this.resolver.getScopeForNode(node, ScopeType.Type); const scope = this.resolver.getScopeSurroundingNode(node, ScopeType.Type);
assert(scope !== null); assert(scope !== null);
const symbolPath = getSymbolPathFromNode(node.name); const symbolPath = getSymbolPathFromNode(node.name);
const resolvedSym = this.resolver.resolveSymbolPath(symbolPath, scope!); const resolvedSym = this.resolver.resolveSymbolPath(symbolPath, scope!);

View file

@ -40,7 +40,8 @@ function firstIndexOfNonEmpty(str: string) {
export class DiagnosticPrinter { export class DiagnosticPrinter {
public hasErrors = false; public hasErrors = false
public hasFatal = false;
public add(diagnostic: Diagnostic): void { public add(diagnostic: Diagnostic): void {
@ -61,6 +62,9 @@ export class DiagnosticPrinter {
this.hasErrors = true; this.hasErrors = true;
out += chalk.bold.red('error: '); out += chalk.bold.red('error: ');
break; break;
case 'fatal':
this.hasFatal = true;
out += chalk.bold.red('fatal:' );
case 'warning': case 'warning':
this.hasErrors = true; this.hasErrors = true;
out += chalk.bold.red('warning: '); out += chalk.bold.red('warning: ');

View file

@ -373,8 +373,8 @@ export class Parser {
const t1 = tokens.get(); const t1 = tokens.get();
assertToken(t1, SyntaxKind.BoltStringLiteral); assertToken(t1, SyntaxKind.BoltStringLiteral);
const symbols: BoltImportSymbol[] = []; const symbols = null;
const t2 = tokens.get(); const t2 = tokens.peek();
if (t2.kind === SyntaxKind.BoltParenthesized) { if (t2.kind === SyntaxKind.BoltParenthesized) {
// TODO implement grammar and parsing logic for symbols // TODO implement grammar and parsing logic for symbols
} }

View file

@ -518,13 +518,7 @@ export class SymbolResolver {
// Once we've handled any module path that might have been present, // Once we've handled any module path that might have been present,
// we resolve the actual symbol using a helper method. // we resolve the actual symbol using a helper method.
const sym = scope.getSymbol(path.name); return scope.getSymbol(path.name);
if (sym === null) {
return null;
}
return sym;
} }
} }

View file

@ -1,6 +1,6 @@
import { Program } from "../program" import { Program } from "../program"
import { Container, Newable } from "../di" import { Container, Newable } from "../ioc"
import {SourceFile} from "../ast"; import {SourceFile} from "../ast";
export interface Transformer { export interface Transformer {

View file

@ -1,5 +1,5 @@
import { Type } from "./checker" import { Type } from "./types"
import { Package } from "./common" import { Package } from "./common"
import { TextSpan } from "./text" import { TextSpan } from "./text"

View file

@ -292,20 +292,23 @@ export class TypeChecker {
case SyntaxKind.BoltFunctionExpression: case SyntaxKind.BoltFunctionExpression:
{ {
const paramTypes = node.params.map(param => { const paramTypes = node.params.map(param => {
if (param.type === null) { if (param.typeExpr === null) {
return this.anyType; return this.anyType;
} }
return this.createInitialTypeForTypeExpression(param.type); return this.createInitialTypeForTypeExpression(param.typeExpr);
}); });
let returnType = node.returnType === null let returnType = node.returnType === null
? this.anyType ? this.anyType
: this.createInitialTypeForTypeExpression(node.returnType); : this.createInitialTypeForTypeExpression(node.returnType);
const funcType = new FunctionType(paramTypes, returnType); resultType = new FunctionType(paramTypes, returnType);
break; break;
} }
case SyntaxKind.BoltQuoteExpression: case SyntaxKind.BoltQuoteExpression:
return this.syntaxType; {
resultType = this.syntaxType;
break
}
case SyntaxKind.BoltMemberExpression: case SyntaxKind.BoltMemberExpression:
case SyntaxKind.BoltReferenceExpression: case SyntaxKind.BoltReferenceExpression: