Skip to content

Commit

Permalink
~Cleaned up for deno
Browse files Browse the repository at this point in the history
  • Loading branch information
AjaniBilby committed Sep 11, 2023
1 parent eece97e commit 9d93c4e
Show file tree
Hide file tree
Showing 30 changed files with 89 additions and 115 deletions.
4 changes: 1 addition & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@
"Fuwawa",
"iovs"
],
"deno.enable": true,
"deno.lint": true,
"deno.unstable": false
"deno.enable": true
}
2 changes: 1 addition & 1 deletion source/bnf/syntax.d.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
23 changes: 16 additions & 7 deletions source/compiler/codegen/context.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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]);

Expand All @@ -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`
Expand Down
10 changes: 6 additions & 4 deletions source/compiler/codegen/expression.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions source/compiler/file.ts
Original file line number Diff line number Diff line change
@@ -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 ;

Expand Down
5 changes: 3 additions & 2 deletions source/compiler/function.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
14 changes: 13 additions & 1 deletion source/compiler/helper.ts → source/helper.ts
Original file line number Diff line number Diff line change
@@ -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)[];


Expand All @@ -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");
}
29 changes: 17 additions & 12 deletions source/parser.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}


Expand Down
2 changes: 1 addition & 1 deletion source/wasm/funcRef.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Byte } from "./helper.ts";
import type { Byte } from "../helper.ts";

import { EncodeU32, Intrinsic } from "./type.ts";

Expand Down
6 changes: 3 additions & 3 deletions source/wasm/function.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
10 changes: 0 additions & 10 deletions source/wasm/helper.ts

This file was deleted.

3 changes: 1 addition & 2 deletions source/wasm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
4 changes: 2 additions & 2 deletions source/wasm/instruction/call.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion source/wasm/instruction/constant.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion source/wasm/instruction/control-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand Down
2 changes: 1 addition & 1 deletion source/wasm/instruction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
4 changes: 2 additions & 2 deletions source/wasm/instruction/memory.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion source/wasm/instruction/variable.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion source/wasm/memoryRef.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Byte } from "./helper.ts";
import type { Byte } from "../helper.ts";
import { EncodeU32 } from "./type.ts";

export class MemoryRef {
Expand Down
8 changes: 4 additions & 4 deletions source/wasm/module.ts
Original file line number Diff line number Diff line change
@@ -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";



Expand Down
4 changes: 2 additions & 2 deletions source/wasm/section/code.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion source/wasm/section/data-count.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EncodeU32 } from "../type.ts";
import { Byte } from "../helper.ts";
import { Byte } from "../../helper.ts";
import { Data } from "./index.ts";


Expand Down
2 changes: 1 addition & 1 deletion source/wasm/section/data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Byte } from "../helper.ts";
import type { Byte } from "../../helper.ts";
import { EncodeI32, EncodeU32 } from "../type.ts";


Expand Down
2 changes: 1 addition & 1 deletion source/wasm/section/function.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion source/wasm/section/import.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Byte } from "../helper.ts";
import type { Byte } from "../../helper.ts";
import { EncodeName, EncodeU32 } from "../type.ts";

class Register {
Expand Down
6 changes: 2 additions & 4 deletions source/wasm/section/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,7 +27,5 @@ export {
Element,
Code,
Data,
DataCount,

Section
DataCount
}
4 changes: 2 additions & 2 deletions source/wasm/section/memory.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion source/wasm/section/type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Byte } from "../../helper.ts";
import { EncodeU32, Intrinsic } from "../type.ts";
import type { Byte } from "../helper.ts";



Expand Down
Loading

0 comments on commit 9d93c4e

Please sign in to comment.