From 9d93c4eb07a0f629b83d5958833028834600f025 Mon Sep 17 00:00:00 2001 From: Ajani Bilby <11359344+AjaniBilby@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:10:30 +1000 Subject: [PATCH] ~Cleaned up for deno --- .vscode/settings.json | 4 +-- source/bnf/syntax.d.ts | 2 +- source/compiler/codegen/context.ts | 23 +++++++++----- source/compiler/codegen/expression.ts | 10 ++++--- source/compiler/file.ts | 4 +-- source/compiler/function.ts | 5 ++-- source/{compiler => }/helper.ts | 14 ++++++++- source/parser.ts | 29 ++++++++++-------- source/wasm/funcRef.ts | 2 +- source/wasm/function.ts | 6 ++-- source/wasm/helper.ts | 10 ------- source/wasm/index.ts | 3 +- source/wasm/instruction/call.ts | 4 +-- source/wasm/instruction/constant.ts | 2 +- source/wasm/instruction/control-flow.ts | 2 +- source/wasm/instruction/index.ts | 2 +- source/wasm/instruction/memory.ts | 4 +-- source/wasm/instruction/variable.ts | 2 +- source/wasm/memoryRef.ts | 2 +- source/wasm/module.ts | 8 ++--- source/wasm/section/code.ts | 4 +-- source/wasm/section/data-count.ts | 2 +- source/wasm/section/data.ts | 2 +- source/wasm/section/function.ts | 2 +- source/wasm/section/import.ts | 2 +- source/wasm/section/index.ts | 6 ++-- source/wasm/section/memory.ts | 4 +-- source/wasm/section/type.ts | 2 +- source/wasm/type.ts | 2 +- tsconfig.json | 40 ------------------------- 30 files changed, 89 insertions(+), 115 deletions(-) rename source/{compiler => }/helper.ts (65%) delete mode 100644 source/wasm/helper.ts delete mode 100644 tsconfig.json diff --git a/.vscode/settings.json b/.vscode/settings.json index dcc010e..45fd4f7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,7 +7,5 @@ "Fuwawa", "iovs" ], - "deno.enable": true, - "deno.lint": true, - "deno.unstable": false + "deno.enable": true } \ No newline at end of file diff --git a/source/bnf/syntax.d.ts b/source/bnf/syntax.d.ts index b6d1872..2418b34 100644 --- a/source/bnf/syntax.d.ts +++ b/source/bnf/syntax.d.ts @@ -1,4 +1,4 @@ -import type _Shared from './shared.js'; +import type * as _Shared from './shared.js'; export type _Literal = { type: "literal", value: string, start: number, end: number, count: number, ref: _Shared.ReferenceRange }; export type Term_Program = { type: 'program', diff --git a/source/compiler/codegen/context.ts b/source/compiler/codegen/context.ts index d6b062f..56f5962 100644 --- a/source/compiler/codegen/context.ts +++ b/source/compiler/codegen/context.ts @@ -1,12 +1,13 @@ -import { SourceView, type Syntax } from "../../parser.ts"; -import type { Scope } from "./scope.ts"; -import type { File, Namespace } from "../file.ts"; +import chalk from "chalk"; +import type * as Syntax from "../../bnf/syntax.d.ts"; +import type { File, Namespace } from "../file.ts"; +import type { Scope } from "./scope.ts"; import { Instruction, AnyInstruction } from "../../wasm/index.ts"; import { AssertUnreachable } from "../../bnf/shared.js"; -import { Intrinsic } from "../intrinsic.ts"; -import chalk from "chalk"; import { CompileExpr } from "./expression.ts"; +import { SourceView } from "../../parser.ts"; +import { Intrinsic } from "../intrinsic.ts"; export class Context { file: File; @@ -43,7 +44,7 @@ function CompileDeclare(ctx: Context, syntax: Syntax.Term_Declare) { const type = syntax.value[1].value[0]; const value = syntax.value[2]; - let typeRef: null | Namespace = null; + let typeRef: Namespace | null = null; if (type) { typeRef = ctx.file.get(type.value[0]); @@ -56,7 +57,15 @@ function CompileDeclare(ctx: Context, syntax: Syntax.Term_Declare) { } } - const resolveType = CompileExpr(ctx, value, typeRef || undefined); + const resolveType: Intrinsic = CompileExpr(ctx, value, typeRef || undefined); + if (!typeRef && !resolveType) { + console.error( + `${chalk.red("Error")}: Unable to determine type\n` + + SourceView(ctx.file.path, ctx.file.name, syntax.ref) + ) + Deno.exit(1); + } + if (typeRef && resolveType !== typeRef) { console.error( `${chalk.red("Error")}: type ${resolveType.name} != type ${typeRef.name}\n` diff --git a/source/compiler/codegen/expression.ts b/source/compiler/codegen/expression.ts index 3bd7edd..c44f02d 100644 --- a/source/compiler/codegen/expression.ts +++ b/source/compiler/codegen/expression.ts @@ -1,10 +1,11 @@ -import { SourceView, type Syntax } from "../../parser.ts"; +import chalk from "chalk"; -import { Instruction, AnyInstruction } from "../../wasm/index.ts"; -import { AssertUnreachable } from "../../bnf/shared.js"; +import type * as Syntax from "../../bnf/syntax.d.ts"; import { Intrinsic, f32, f64, i32, i64, u32, u64 } from "../intrinsic.ts"; +import { AssertUnreachable } from "../../helper.ts"; +import { Instruction } from "../../wasm/index.ts"; +import { SourceView } from "../../parser.ts"; import { Context } from "./context.ts"; -import chalk from "chalk"; export function CompileExpr(ctx: Context, syntax: Syntax.Term_Expr, expect?: Intrinsic) { return CompileArg(ctx, syntax.value[0], expect); @@ -55,6 +56,7 @@ function CompileConstInt(ctx: Context, syntax: Syntax.Term_Integer, prefix?: Syn + SourceView(ctx.file.path, ctx.file.name, syntax.ref) ) Deno.exit(1); + break; case "-": if (unsigned) { console.error( diff --git a/source/compiler/file.ts b/source/compiler/file.ts index 6bff92a..3c9afc3 100644 --- a/source/compiler/file.ts +++ b/source/compiler/file.ts @@ -1,16 +1,16 @@ import { AssertUnreachable } from "../bnf/shared.js"; import fs from "node:fs"; -import type { Term_Access, Term_Function, Term_Program } from "../bnf/syntax.js"; +import type { Term_Access, Term_Function, Term_Program } from "../bnf/syntax.d.ts"; import type Project from "./project.ts"; import { Intrinsic, i32, i64, u32, u64, f32, f64 } from "./intrinsic.ts"; +import { FlatAccess, FlattenAccess } from "../helper.ts"; import { Parse } from "../parser.ts"; import Structure from "./structure.ts"; import Function from "./function.ts"; import Global from "./global.ts"; import Import from "./import.ts"; -import { FlatAccess, FlattenAccess } from "./helper.ts"; export type Namespace = Function | Import | Global | Structure | Intrinsic ; diff --git a/source/compiler/function.ts b/source/compiler/function.ts index 21e5bb8..2048b63 100644 --- a/source/compiler/function.ts +++ b/source/compiler/function.ts @@ -1,7 +1,8 @@ -import type { File, Namespace } from "./file.ts"; import chalk from "chalk"; -import { Term_Access, Term_Function } from "../bnf/syntax.js"; +import type { Term_Access, Term_Function } from "../bnf/syntax.d.ts"; +import type { File, Namespace } from "./file.ts"; + import { ReferenceRange, SourceView } from "../parser.ts"; import { Intrinsic } from "./intrinsic.ts"; import { Context } from "./codegen/context.ts"; diff --git a/source/compiler/helper.ts b/source/helper.ts similarity index 65% rename from source/compiler/helper.ts rename to source/helper.ts index 722d06e..d1435ce 100644 --- a/source/compiler/helper.ts +++ b/source/helper.ts @@ -1,4 +1,4 @@ -import type { Term_Access, Term_Access_comp, Term_Access_dynamic, Term_Access_static, Term_Name } from "../bnf/syntax.js"; +import type { Term_Access, Term_Access_comp, Term_Access_dynamic, Term_Access_static, Term_Name } from "./bnf/syntax.d.ts"; export type FlatAccess = (Term_Name | Term_Access_static | Term_Access_dynamic | Term_Access_comp)[]; @@ -18,4 +18,16 @@ export function FlatAccessToStr(access: FlatAccess): string { : x.type === "access_comp" ? "#[]" : "UNK" ).join("") +} + + +export type Byte = number; + +export function isByte(value: number): value is Byte { + return Number.isInteger(value) && value >= 0 && value <= 255; +} + + +export function AssertUnreachable(x: never): never { + throw new Error("Unreachable code path reachable"); } \ No newline at end of file diff --git a/source/parser.ts b/source/parser.ts index cbdbcb1..196e541 100644 --- a/source/parser.ts +++ b/source/parser.ts @@ -1,18 +1,18 @@ -import * as fs from "node:fs"; import chalk from "chalk"; import { ParseError, ReferenceRange, Reference } from "./bnf/shared.js"; -import * as Syntax from "./bnf/syntax.js"; +import * as Instance from "./bnf/syntax.js"; +import * as Syntax from "./bnf/syntax.d.ts"; -await Syntax.ready; +await Instance.ready; export function Parse(data: string, path: string, name: string): Syntax.Term_Program { - const res = Syntax.Parse_Program(data, true); + const res = Instance.Parse_Program(data, true); if (res instanceof ParseError) { console.error(`${chalk.red("FATAL ERROR")}: Syntax Parser Completely crashed`); Deno.exit(1); - }; + } if (res.isPartial) { console.error( @@ -28,7 +28,7 @@ export function Parse(data: string, path: string, name: string): Syntax.Term_Pro Deno.exit(1); } - return res.root; + return res.root as Syntax.Term_Program; } export function SourceView(path: string, name: string, range: ReferenceRange) { @@ -85,25 +85,30 @@ function FindNewLine(source: string, index: number, step: number) { } -function ReadByteRange(path: string, start: number, end: number) { +function ReadByteRange(path: string, start: number, end: number): string { // Ensure end byte is not before the start byte if (end < start) throw new Error('End byte should be greater than start byte'); start = Math.max(0, start); // Open the file for reading - const fd = fs.openSync(path, 'r'); + const file = Deno.openSync(path, { read: true }); const bufferSize = end - start + 1; - const buffer = Buffer.alloc(bufferSize); + const buffer = new Uint8Array(bufferSize); + + // Position the file cursor to the start byte + file.seekSync(start, Deno.SeekMode.Start); // Read the specified byte range into the buffer - fs.readSync(fd, buffer, 0, bufferSize, start); + file.readSync(buffer); // Close the file - fs.closeSync(fd); + file.close(); - return buffer.toString(); + // Convert Uint8Array to string + const decoder = new TextDecoder(); + return decoder.decode(buffer); } diff --git a/source/wasm/funcRef.ts b/source/wasm/funcRef.ts index b7edae2..ebf93da 100644 --- a/source/wasm/funcRef.ts +++ b/source/wasm/funcRef.ts @@ -1,4 +1,4 @@ -import type { Byte } from "./helper.ts"; +import type { Byte } from "../helper.ts"; import { EncodeU32, Intrinsic } from "./type.ts"; diff --git a/source/wasm/function.ts b/source/wasm/function.ts index 3ba8f79..4e969b8 100644 --- a/source/wasm/function.ts +++ b/source/wasm/function.ts @@ -1,7 +1,7 @@ -import { FuncRef, LocalRef } from "./funcRef.ts"; -import { Byte } from "./helper.ts"; -import { EncodeU32, Intrinsic } from "./type.ts"; import * as Instruction from "./instruction/index.ts"; +import { EncodeU32, Intrinsic } from "./type.ts"; +import { FuncRef, LocalRef } from "./funcRef.ts"; +import { Byte } from "../helper.ts"; export class Function { diff --git a/source/wasm/helper.ts b/source/wasm/helper.ts deleted file mode 100644 index 1851d75..0000000 --- a/source/wasm/helper.ts +++ /dev/null @@ -1,10 +0,0 @@ -export type Byte = number; - -export function isByte(value: number): value is Byte { - return Number.isInteger(value) && value >= 0 && value <= 255; -} - - -export function AssertUnreachable(x: never): never { - throw new Error("Unreachable code path reachable"); -} \ No newline at end of file diff --git a/source/wasm/index.ts b/source/wasm/index.ts index 4a43f79..a370a57 100644 --- a/source/wasm/index.ts +++ b/source/wasm/index.ts @@ -2,11 +2,10 @@ import Instruction, { Any } from "./instruction/index.ts"; import * as Type from "./type.ts"; import Module from "./module.ts"; -type AnyInstruction = Any; +export type AnyInstruction = Any; export { Instruction, - AnyInstruction, Module, Type }; diff --git a/source/wasm/instruction/call.ts b/source/wasm/instruction/call.ts index c39eda1..c7ab9f9 100644 --- a/source/wasm/instruction/call.ts +++ b/source/wasm/instruction/call.ts @@ -1,6 +1,6 @@ -import { FuncRef } from "../funcRef.ts"; import { EncodeU32 } from "../type.ts"; -import { Byte } from "../helper.ts"; +import { FuncRef } from "../funcRef.ts"; +import { Byte } from "../../helper.ts"; export default class Call { x: FuncRef | number; diff --git a/source/wasm/instruction/constant.ts b/source/wasm/instruction/constant.ts index f02d1f0..5056162 100644 --- a/source/wasm/instruction/constant.ts +++ b/source/wasm/instruction/constant.ts @@ -1,6 +1,6 @@ // https://webassembly.github.io/spec/core/binary/instructions.html#numeric-instructions import { EncodeF32, EncodeF64, EncodeI32, EncodeI64 } from "../type.ts"; -import { Byte } from "../helper.ts"; +import { Byte } from "../../helper.ts"; export enum Type { diff --git a/source/wasm/instruction/control-flow.ts b/source/wasm/instruction/control-flow.ts index a673b9d..fd94895 100644 --- a/source/wasm/instruction/control-flow.ts +++ b/source/wasm/instruction/control-flow.ts @@ -2,7 +2,7 @@ import type { Any } from "./index.ts"; import { EncodeU32 } from "../type.ts"; -import { Byte } from "../helper.ts"; +import { Byte } from "../../helper.ts"; export class Unreachable { toBinary(): Byte[] { diff --git a/source/wasm/instruction/index.ts b/source/wasm/instruction/index.ts index 5dc2587..8682d8b 100644 --- a/source/wasm/instruction/index.ts +++ b/source/wasm/instruction/index.ts @@ -2,7 +2,7 @@ import { Unreachable, IfBlock, Block, Loop, NoOp, Br, Br_If, Return } from "./co import { FuncRef } from "../funcRef.ts"; import { EncodeU32 } from "../type.ts"; -import { Byte } from "../helper.ts"; +import { Byte } from "../../helper.ts"; import varFuncs, { Variable } from "./variable.ts"; import constFuncs, { Constant } from "./constant.ts"; diff --git a/source/wasm/instruction/memory.ts b/source/wasm/instruction/memory.ts index 893e979..ec0e3ab 100644 --- a/source/wasm/instruction/memory.ts +++ b/source/wasm/instruction/memory.ts @@ -1,6 +1,6 @@ // https://webassembly.github.io/spec/core/binary/instructions.html#numeric-instructions -import { EncodeF32, EncodeF64, EncodeI32, EncodeI64, EncodeU32 } from "../type.ts"; -import { Byte } from "../helper.ts"; +import { EncodeU32 } from "../type.ts"; +import { Byte } from "../../helper.ts"; export enum Type { diff --git a/source/wasm/instruction/variable.ts b/source/wasm/instruction/variable.ts index 3774702..9ea11a7 100644 --- a/source/wasm/instruction/variable.ts +++ b/source/wasm/instruction/variable.ts @@ -1,7 +1,7 @@ // https://webassembly.github.io/spec/core/binary/instructions.html#variable-instructions import { EncodeU32 } from "../type.ts"; -import { Byte } from "../helper.ts"; import { LocalRef } from "../funcRef.ts"; +import { Byte } from "../../helper.ts"; export enum Type { diff --git a/source/wasm/memoryRef.ts b/source/wasm/memoryRef.ts index a145dd9..839596b 100644 --- a/source/wasm/memoryRef.ts +++ b/source/wasm/memoryRef.ts @@ -1,4 +1,4 @@ -import type { Byte } from "./helper.ts"; +import type { Byte } from "../helper.ts"; import { EncodeU32 } from "./type.ts"; export class MemoryRef { diff --git a/source/wasm/module.ts b/source/wasm/module.ts index 6013b8d..90734df 100644 --- a/source/wasm/module.ts +++ b/source/wasm/module.ts @@ -1,11 +1,11 @@ // https://webassembly.github.io/spec/core/binary/modules.html -import { Function } from "./function.ts"; -import { FuncRef } from "./funcRef.ts"; import * as Section from "./section/index.ts"; -import { Intrinsic } from "./type.ts"; -import { Byte } from "./helper.ts"; import { MemoryRef } from "./memoryRef.ts"; +import { Intrinsic } from "./type.ts"; +import { Function } from "./function.ts"; +import { FuncRef } from "./funcRef.ts"; +import { Byte } from "../helper.ts"; diff --git a/source/wasm/section/code.ts b/source/wasm/section/code.ts index 1f1af20..f52a15c 100644 --- a/source/wasm/section/code.ts +++ b/source/wasm/section/code.ts @@ -1,6 +1,6 @@ -import { Function } from "../function.ts"; -import { Byte } from "../helper.ts"; import { EncodeU32 } from "../type.ts"; +import { Function } from "../function.ts"; +import { Byte } from "../../helper.ts"; export default class CodeSection { diff --git a/source/wasm/section/data-count.ts b/source/wasm/section/data-count.ts index b394f98..01aaa3c 100644 --- a/source/wasm/section/data-count.ts +++ b/source/wasm/section/data-count.ts @@ -1,5 +1,5 @@ import { EncodeU32 } from "../type.ts"; -import { Byte } from "../helper.ts"; +import { Byte } from "../../helper.ts"; import { Data } from "./index.ts"; diff --git a/source/wasm/section/data.ts b/source/wasm/section/data.ts index f014ac9..ac14d02 100644 --- a/source/wasm/section/data.ts +++ b/source/wasm/section/data.ts @@ -1,4 +1,4 @@ -import { Byte } from "../helper.ts"; +import type { Byte } from "../../helper.ts"; import { EncodeI32, EncodeU32 } from "../type.ts"; diff --git a/source/wasm/section/function.ts b/source/wasm/section/function.ts index 5e4aae5..33953a0 100644 --- a/source/wasm/section/function.ts +++ b/source/wasm/section/function.ts @@ -1,6 +1,6 @@ import { EncodeU32 } from "../type.ts"; import { Function } from "../function.ts"; -import { Byte } from "../helper.ts"; +import { Byte } from "../../helper.ts"; export default class FunctionSection { diff --git a/source/wasm/section/import.ts b/source/wasm/section/import.ts index 3827f66..53f7e11 100644 --- a/source/wasm/section/import.ts +++ b/source/wasm/section/import.ts @@ -1,4 +1,4 @@ -import type { Byte } from "../helper.ts"; +import type { Byte } from "../../helper.ts"; import { EncodeName, EncodeU32 } from "../type.ts"; class Register { diff --git a/source/wasm/section/index.ts b/source/wasm/section/index.ts index c820692..8c8eaaf 100644 --- a/source/wasm/section/index.ts +++ b/source/wasm/section/index.ts @@ -12,7 +12,7 @@ import Code from "./code.ts"; import Data from "./data.ts"; import DataCount from "./data-count.ts"; -type Section = Custom | Type | Import | Function | Table | Memory | Global | Export | Start | Element | Code | Data | DataCount ; +export type Section = Custom | Type | Import | Function | Table | Memory | Global | Export | Start | Element | Code | Data | DataCount ; export { Custom, @@ -27,7 +27,5 @@ export { Element, Code, Data, - DataCount, - - Section + DataCount } \ No newline at end of file diff --git a/source/wasm/section/memory.ts b/source/wasm/section/memory.ts index b9f4fe6..d837ee2 100644 --- a/source/wasm/section/memory.ts +++ b/source/wasm/section/memory.ts @@ -1,6 +1,6 @@ -import { Byte } from "../helper.ts"; -import { MemoryRef } from "../memoryRef.ts"; import { EncodeLimitType, EncodeU32 } from "../type.ts"; +import { MemoryRef } from "../memoryRef.ts"; +import { Byte } from "../../helper.ts"; type Range = { diff --git a/source/wasm/section/type.ts b/source/wasm/section/type.ts index cbfa2b3..4dc8de4 100644 --- a/source/wasm/section/type.ts +++ b/source/wasm/section/type.ts @@ -1,5 +1,5 @@ +import type { Byte } from "../../helper.ts"; import { EncodeU32, Intrinsic } from "../type.ts"; -import type { Byte } from "../helper.ts"; diff --git a/source/wasm/type.ts b/source/wasm/type.ts index 444e09c..41caf5a 100644 --- a/source/wasm/type.ts +++ b/source/wasm/type.ts @@ -1,4 +1,4 @@ -import type { Byte } from "./helper.ts"; +import type { Byte } from "../helper.ts"; // https://webassembly.github.io/spec/core/binary/types.html diff --git a/tsconfig.json b/tsconfig.json deleted file mode 100644 index 96805ef..0000000 --- a/tsconfig.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "compilerOptions": { - /* Visit https://aka.ms/tsconfig to read more about this file */ - - /* Language and Environment */ - "target": "ESNext", - "allowImportingTsExtensions": true, - "allowJs": true, - - /* Modules */ - "module": "NodeNext", - "moduleResolution": "NodeNext", - "resolveJsonModule": true, - "forceConsistentCasingInFileNames": true, - "esModuleInterop": true, - - /* Type Checking */ - "strict": true, - "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ - "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ - "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ - "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ - "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ - "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ - "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ - - /* Completeness */ - // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ - "skipLibCheck": true, /* Skip type checking all .d.ts files. */ - - "outDir": "./bin" - }, - "include": ["source"], - "exclude": ["node_modules"], - - "ts-node": { - "esm": true, - "experimentalSpecifierResolution": "node" - } -}