65 lines
1.4 KiB
TypeScript
65 lines
1.4 KiB
TypeScript
import type { Symbol } from "../semantics/symbol";
|
|
import type { Decl } from "./decl";
|
|
import { Node, SourceRange } from "./node";
|
|
import { Stmt } from "./stmt";
|
|
import type { Visitor } from "./visitor";
|
|
|
|
export class Module extends Node {
|
|
public symbols: Record<string, Symbol> = {};
|
|
|
|
constructor(
|
|
sourceRange: SourceRange,
|
|
public moduleDecl: ModuleDecl,
|
|
public imports: ImportStmt[],
|
|
public decls: Decl[],
|
|
) { super(sourceRange); }
|
|
|
|
accept<ResultT>(visitor: Visitor<ResultT>): ResultT {
|
|
return visitor.visitModule(this);
|
|
}
|
|
|
|
addSymbol(symbol: Symbol) {
|
|
this.symbols[symbol.name] = symbol;
|
|
}
|
|
|
|
hasSymbol(name: string): boolean {
|
|
return name in this.symbols;
|
|
}
|
|
|
|
getSymbols() {
|
|
return Object.values(this.symbols);
|
|
}
|
|
|
|
getPublicSymbols() {
|
|
return this.getSymbols().filter((sym) => {
|
|
return 'isPub' in sym && sym.isPub;
|
|
});
|
|
}
|
|
}
|
|
|
|
export class ModuleDecl extends Stmt {
|
|
constructor(sourceRange: SourceRange, public path: string[]) {
|
|
super(sourceRange);
|
|
}
|
|
|
|
accept<ResultT>(visitor: Visitor<ResultT>): ResultT {
|
|
return visitor.visitModuleDecl(this);
|
|
}
|
|
}
|
|
|
|
export class ImportStmt extends Stmt {
|
|
public module: Module | null = null;
|
|
|
|
constructor(
|
|
sourceRange: SourceRange,
|
|
public path: string[],
|
|
public alias: string | null = null,
|
|
) {
|
|
super(sourceRange);
|
|
}
|
|
|
|
accept<ResultT>(visitor: Visitor<ResultT>): ResultT {
|
|
return visitor.visitImportStmt(this);
|
|
}
|
|
}
|