From 49a798a7f0b7a4fd15c60bddf26439ce13ef84a8 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Tue, 16 Aug 2022 17:38:42 +0300 Subject: [PATCH] refactor --- src/ast.ts | 17 +++++++++------- src/common.ts | 2 +- src/compiler.ts | 2 +- src/diagnostics.ts | 50 ++++++++++++++++++++++++++++++++++++++++------ src/flow.ts | 4 ++-- src/index-wasm.ts | 28 +++++++++++++++++++++----- src/module.ts | 34 +++++++++++++++---------------- src/parser.ts | 6 +++--- src/program.ts | 6 +++--- src/resolver.ts | 7 ++----- src/tokenizer.ts | 48 +++----------------------------------------- src/tsconfig.json | 2 +- 12 files changed, 110 insertions(+), 96 deletions(-) diff --git a/src/ast.ts b/src/ast.ts index c5a2427d1e..bc070e7f0b 100644 --- a/src/ast.ts +++ b/src/ast.ts @@ -22,8 +22,11 @@ import { } from "./common"; import { - Token, Range +} from "./diagnostics"; + +import { + Token } from "./tokenizer"; import { @@ -41,7 +44,7 @@ import { } from "./types"; /** Indicates the kind of a node. */ -export enum NodeKind { +export const enum NodeKind { SOURCE, @@ -931,7 +934,7 @@ export class TypeParameterNode extends Node { } /** Represents the kind of a parameter. */ -export enum ParameterKind { +export const enum ParameterKind { /** No specific flags. */ DEFAULT, /** Is an optional parameter. */ @@ -1083,7 +1086,7 @@ export class DecoratorNode extends Node { } /** Comment kinds. */ -export enum CommentKind { +export const enum CommentKind { /** Line comment. */ LINE, /** Triple-slash line comment. */ @@ -1126,7 +1129,7 @@ export class IdentifierExpression extends Expression { } /** Indicates the kind of a literal. */ -export enum LiteralKind { +export const enum LiteralKind { FLOAT, INTEGER, STRING, @@ -1161,7 +1164,7 @@ export class ArrayLiteralExpression extends LiteralExpression { } /** Indicates the kind of an assertion. */ -export enum AssertionKind { +export const enum AssertionKind { /** A prefix assertion, i.e. `expr`. */ PREFIX, /** An as assertion, i.e. `expr as T`. */ @@ -1602,7 +1605,7 @@ export class CompiledExpression extends Expression { export abstract class Statement extends Node { } /** Indicates the specific kind of a source. */ -export enum SourceKind { +export const enum SourceKind { /** User-provided file. */ USER = 0, /** User-provided entry file. */ diff --git a/src/common.ts b/src/common.ts index acf2767c35..661dbd1143 100644 --- a/src/common.ts +++ b/src/common.ts @@ -4,7 +4,7 @@ */ /** Indicates traits of a {@link Node} or {@link Element}. */ -export enum CommonFlags { +export const enum CommonFlags { /** No flags set. */ NONE = 0, diff --git a/src/compiler.ts b/src/compiler.ts index 8e0c3dab7b..16454f998c 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -15,6 +15,7 @@ import { } from "./builtins"; import { + Range, DiagnosticCode, DiagnosticEmitter } from "./diagnostics"; @@ -106,7 +107,6 @@ import { import { Token, - Range, operatorTokenToString } from "./tokenizer"; diff --git a/src/diagnostics.ts b/src/diagnostics.ts index da386b458b..59b58166a9 100644 --- a/src/diagnostics.ts +++ b/src/diagnostics.ts @@ -3,10 +3,6 @@ * @license Apache-2.0 */ -import { - Range -} from "./tokenizer"; - import { Source } from "./ast"; @@ -35,7 +31,7 @@ export { } from "./diagnosticMessages.generated"; /** Indicates the category of a {@link DiagnosticMessage}. */ -export enum DiagnosticCategory { +export const enum DiagnosticCategory { /** Overly pedantic message. */ PEDANTIC, /** Informatory message. */ @@ -46,6 +42,48 @@ export enum DiagnosticCategory { ERROR } +export class Range { + + source!: Source; + debugInfoRef: usize = 0; + + constructor(public start: i32, public end: i32) {} + + static join(a: Range, b: Range): Range { + if (a.source != b.source) throw new Error("source mismatch"); + let range = new Range( + a.start < b.start ? a.start : b.start, + a.end > b.end ? a.end : b.end + ); + range.source = a.source; + return range; + } + + equals(other: Range): bool { + return ( + this.source == other.source && + this.start == other.start && + this.end == other.end + ); + } + + get atStart(): Range { + let range = new Range(this.start, this.start); + range.source = this.source; + return range; + } + + get atEnd(): Range { + let range = new Range(this.end, this.end); + range.source = this.source; + return range; + } + + toString(): string { + return this.source.text.substring(this.start, this.end); + } +} + /** Returns the string representation of the specified diagnostic category. */ export function diagnosticCategoryToString(category: DiagnosticCategory): string { switch (category) { @@ -239,7 +277,7 @@ function formatDiagnosticContext(range: Range): string { lineNumber, " │ ", text.substring(start, end).replaceAll("\t", " "), - "\n ", + "\n ", lineSpace, " │ " ]; diff --git a/src/flow.ts b/src/flow.ts index 511764a477..12f8e0ea16 100644 --- a/src/flow.ts +++ b/src/flow.ts @@ -162,7 +162,7 @@ export const enum FlowFlags { } /** Flags indicating the current state of a local. */ -export enum LocalFlags { +export const enum LocalFlags { /** No specific conditions. */ NONE = 0, @@ -177,7 +177,7 @@ export enum LocalFlags { } /** Flags indicating the current state of a field. */ -export enum FieldFlags { +export const enum FieldFlags { NONE = 0, INITIALIZED = 1 << 0 } diff --git a/src/index-wasm.ts b/src/index-wasm.ts index 3d76aca303..80cbcb24f8 100644 --- a/src/index-wasm.ts +++ b/src/index-wasm.ts @@ -14,13 +14,31 @@ * When compiling to WebAssembly `glue/wasm/index.ts` must be included. */ -import { Target, Runtime, Feature } from "./common"; -import { Compiler, Options } from "./compiler"; -import { TSDBuilder, JSBuilder } from "./bindings"; -import { DiagnosticMessage, DiagnosticCategory, formatDiagnosticMessage } from "./diagnostics"; +import { + Target, + Runtime, + Feature +} from "./common"; + +import { + Compiler, + Options +} from "./compiler"; + +import { + TSDBuilder, + JSBuilder +} from "./bindings"; + +import { + Range, + DiagnosticMessage, + DiagnosticCategory, + formatDiagnosticMessage +} from "./diagnostics"; + import { Module } from "./module"; import { Program } from "./program"; -import { Range } from "./tokenizer"; import { Source } from "./ast"; // Options diff --git a/src/module.ts b/src/module.ts index 7595dc60aa..fc2e5055a7 100644 --- a/src/module.ts +++ b/src/module.ts @@ -85,7 +85,7 @@ export namespace HeapTypeRef { } /** Binaryen feature constants. */ -export enum FeatureFlags { +export const enum FeatureFlags { MVP = 0 /* _BinaryenFeatureMVP */, Atomics = 1 /* _BinaryenFeatureAtomics */, MutableGlobals = 2 /* _BinaryenFeatureMutableGlobals */, @@ -107,7 +107,7 @@ export enum FeatureFlags { } /** Binaryen expression id constants. */ -export enum ExpressionId { +export const enum ExpressionId { Invalid = 0 /* _BinaryenInvalidId */, Block = 1 /* _BinaryenBlockId */, If = 2 /* _BinaryenIfId */, @@ -181,7 +181,7 @@ export enum ExpressionId { } /** Binaryen external kind constants. */ -export enum ExternalKind { +export const enum ExternalKind { Function = 0 /* _BinaryenExternalFunction */, Table = 1 /* _BinaryenExternalTable */, Memory = 2 /* _BinaryenExternalMemory */, @@ -190,7 +190,7 @@ export enum ExternalKind { } /** Binaryen unary operation constants. */ -export enum UnaryOp { +export const enum UnaryOp { /** i32.clz */ ClzI32 = 0 /* _BinaryenClzInt32 */, /** i64.clz */ @@ -466,7 +466,7 @@ export enum UnaryOp { } /** Binaryen binary operation constants. */ -export enum BinaryOp { +export const enum BinaryOp { /** i32.add */ AddI32 = 0 /* _BinaryenAddInt32 */, /** i32.sub */ @@ -920,7 +920,7 @@ export enum BinaryOp { } /** Binaryen atomic read-modify-write operation constants. */ -export enum AtomicRMWOp { +export const enum AtomicRMWOp { /** i32.atomic.rmw.add, i32.atomic.rmw8.add_u, i32.atomic.rmw16.add_u, i64.atomic.rmw.add, i64.atomic.rmw8.add_u, i64.atomic.rmw16.add_u, i64.atomic.rmw32.add_u */ Add = 0 /* _BinaryenAtomicRMWAdd */, /** i32.atomic.rmw.sub, i32.atomic.rmw8.sub_u, i32.atomic.rmw16.sub_u, i64.atomic.rmw.sub, i64.atomic.rmw8.sub_u, i64.atomic.rmw16.sub_u, i64.atomic.rmw32.sub_u */ @@ -936,7 +936,7 @@ export enum AtomicRMWOp { } /** Binaryen SIMD extract operation constants. */ -export enum SIMDExtractOp { +export const enum SIMDExtractOp { /** i8x16.extract_lane_s */ ExtractLaneI8x16 = 0 /* _BinaryenExtractLaneSVecI8x16 */, /** i8x16.extract_lane_u */ @@ -956,7 +956,7 @@ export enum SIMDExtractOp { } /** Binaryen SIMD replace operation constants. */ -export enum SIMDReplaceOp { +export const enum SIMDReplaceOp { /** i8x16.replace_lane */ ReplaceLaneI8x16 = 0 /* _BinaryenReplaceLaneVecI8x16 */, /** i16x8.replace_lane */ @@ -972,7 +972,7 @@ export enum SIMDReplaceOp { } /** Binaryen SIMD shift operation constants. */ -export enum SIMDShiftOp { +export const enum SIMDShiftOp { /** i8x16.shl */ ShlI8x16 = 0 /* _BinaryenShlVecI8x16 */, /** i8x16.shr_s */ @@ -1000,7 +1000,7 @@ export enum SIMDShiftOp { } /** Binaryen SIMD load operation constants. */ -export enum SIMDLoadOp { +export const enum SIMDLoadOp { /** v128.load8_splat */ Load8Splat = 0 /* _BinaryenLoad8SplatVec128 */, /** v128.load16_splat */ @@ -1028,7 +1028,7 @@ export enum SIMDLoadOp { } /** Binaryen SIMD load/store lane operation constants. */ -export enum SIMDLoadStoreLaneOp { +export const enum SIMDLoadStoreLaneOp { /** v128.load8_lane */ Load8Lane = 0 /* _BinaryenLoad8LaneVec128 */, /** v128.load16_lane */ @@ -1048,13 +1048,13 @@ export enum SIMDLoadStoreLaneOp { } /** Binaryen SIMD ternary operation constants. */ -export enum SIMDTernaryOp { +export const enum SIMDTernaryOp { /** v128.bitselect */ Bitselect = 0 /* _BinaryenBitselectVec128 */ } /** Binaryen RefIs operation constants. */ -export enum RefIsOp { +export const enum RefIsOp { /** ref.is_null */ RefIsNull = 0 /* _BinaryenRefIsNull */, /** ref.is_func */ @@ -1066,7 +1066,7 @@ export enum RefIsOp { } /** Binaryen RefAs operation constants. */ -export enum RefAsOp { +export const enum RefAsOp { /** ref.as_non_null */ RefAsNonNull = 0 /* _BinaryenRefAsNonNull */, /** ref.as_func */ @@ -1078,7 +1078,7 @@ export enum RefAsOp { } /** Binaryen BrOn operation constants. */ -export enum BrOnOp { +export const enum BrOnOp { /** br_on_null */ BrOnNull = 0 /* TODO_BinaryenBrOnNull */, /** br_on_cast */ @@ -1092,7 +1092,7 @@ export enum BrOnOp { } /** Binaryen expression runner flags. */ -export enum ExpressionRunnerFlags { +export const enum ExpressionRunnerFlags { Default = 0 /* _ExpressionRunnerFlagsDefault */, PreserveSideeffects = 1 /* _ExpressionRunnerFlagsPreserveSideeffects */, TraverseCalls = 2 /* _ExpressionRunnerFlagsTraverseCalls */ @@ -3127,7 +3127,7 @@ export class SwitchBuilder { } } -export enum SideEffects { +export const enum SideEffects { None = 0 /* _BinaryenSideEffectNone */, Branches = 1 /* _BinaryenSideEffectBranches */, Calls = 2 /* _BinaryenSideEffectCalls */, diff --git a/src/parser.ts b/src/parser.ts index d12e784dd8..314d3a2cdf 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -16,13 +16,13 @@ import { import { Tokenizer, Token, - Range, CommentHandler, IdentifierHandling, isIllegalVariableIdentifier } from "./tokenizer"; import { + Range, DiagnosticCode, DiagnosticEmitter, DiagnosticMessage @@ -122,10 +122,10 @@ export class Parser extends DiagnosticEmitter { /** Constructs a new parser. */ constructor( diagnostics: DiagnosticMessage[] | null = null, - sources: Source[] | null = null + sources: Source[] = [] ) { super(diagnostics); - this.sources = sources ? sources : new Array(); + this.sources = sources; } /** Parses a file and adds its definitions to the program. */ diff --git a/src/program.ts b/src/program.ts index e8d3917ac2..5d47d44b88 100644 --- a/src/program.ts +++ b/src/program.ts @@ -57,6 +57,7 @@ import { } from "./compiler"; import { + Range, DiagnosticCode, DiagnosticMessage, DiagnosticEmitter @@ -70,8 +71,7 @@ import { } from "./types"; import { - Token, - Range + Token } from "./tokenizer"; import { @@ -2677,7 +2677,7 @@ export class Program extends DiagnosticEmitter { } /** Indicates the specific kind of an {@link Element}. */ -export enum ElementKind { +export const enum ElementKind { /** A {@link Global}. */ GLOBAL, /** A {@link Local}. */ diff --git a/src/resolver.ts b/src/resolver.ts index b0680e3798..5a19f8e152 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -12,6 +12,7 @@ */ import { + Range, DiagnosticEmitter, DiagnosticCode } from "./diagnostics"; @@ -44,10 +45,6 @@ import { Flow } from "./flow"; -import { - Range -} from "./tokenizer"; - import { FunctionTypeNode, ParameterKind, @@ -109,7 +106,7 @@ import { } from "./builtins"; /** Indicates whether errors are reported or not. */ -export enum ReportMode { +export const enum ReportMode { /** Report errors. */ REPORT, /** Swallow errors. */ diff --git a/src/tokenizer.ts b/src/tokenizer.ts index 1053a5c51d..da75224ff4 100644 --- a/src/tokenizer.ts +++ b/src/tokenizer.ts @@ -12,6 +12,7 @@ */ import { + Range, DiagnosticCode, DiagnosticMessage, DiagnosticEmitter @@ -35,7 +36,7 @@ import { } from "./util"; /** Named token types. */ -export enum Token { +export const enum Token { // keywords // discarded: ANY, BOOLEAN, NEVER, NUMBER, STRING, SYMBOL, UNDEFINED, LESSTHAN_SLASH @@ -173,7 +174,7 @@ export enum Token { ENDOFFILE } -export enum IdentifierHandling { +export const enum IdentifierHandling { DEFAULT, PREFER, ALWAYS @@ -443,49 +444,6 @@ export function operatorTokenToString(token: Token): string { } } -export class Range { - - start: i32; - end: i32; - source!: Source; - debugInfoRef: usize = 0; - - constructor(start: i32, end: i32) { - this.start = start; - this.end = end; - } - - static join(a: Range, b: Range): Range { - if (a.source != b.source) throw new Error("source mismatch"); - let range = new Range( - a.start < b.start ? a.start : b.start, - a.end > b.end ? a.end : b.end - ); - range.source = a.source; - return range; - } - - equals(other: Range): bool { - return this.source == other.source && this.start == other.start && this.end == other.end; - } - - get atStart(): Range { - let range = new Range(this.start, this.start); - range.source = this.source; - return range; - } - - get atEnd(): Range { - let range = new Range(this.end, this.end); - range.source = this.source; - return range; - } - - toString(): string { - return this.source.text.substring(this.start, this.end); - } -} - /** Handler for intercepting comments while tokenizing. */ export type CommentHandler = (kind: CommentKind, text: string, range: Range) => void; diff --git a/src/tsconfig.json b/src/tsconfig.json index 379a91ad15..082bb02ec8 100644 --- a/src/tsconfig.json +++ b/src/tsconfig.json @@ -1,12 +1,12 @@ { "extends": "../std/portable.json", "compilerOptions": { + "target": "esnext", "outDir": "../build", "types" : ["node"], "allowJs": false, "sourceMap": true, "skipLibCheck": true, - "target": "esnext", "useDefineForClassFields": false, "strict": true, "noImplicitReturns": true,