Skip to content

Commit

Permalink
~Migrated to deno execution
Browse files Browse the repository at this point in the history
  • Loading branch information
AjaniBilby committed Sep 11, 2023
1 parent fb02f5e commit eece97e
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 21 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"bitcode",
"Fuwawa",
"iovs"
]
],
"deno.enable": true,
"deno.lint": true,
"deno.unstable": false
}
33 changes: 33 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"compilerOptions": {
"allowJs": true,
"strict": true,
"strictNullChecks": true,
"lib": ["ESNext", "DOM"]
},
"lint": {
"include": ["source/"],
"exclude": ["source/bnf/"],
"rules": {
"tags": ["recommended"],
"include": ["ban-untagged-todo"],
"exclude": ["no-unused-vars"]
}
},
"ignore-fmt": {
"useTabs": true,
"semiColons": true,
"proseWrap": "preserve",
"include": ["source/"],
"exclude": ["source/bnf/"]
},
"lock": false,
"nodeModulesDir": true,
"test": {
"include": ["source/"],
"exclude": ["source/bnf/"]
},
"tasks": {
"start": "deno run --allow-read --allow-write cli.ts"
}
}
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
"bin/*"
],
"scripts": {
"cli": "node --no-warnings --loader ts-node/esm ./source/cli.ts",
"build": "run-s build:*",
"build:syntax": "npx bnf-compile ./source/bnf/",
"build:ts": "tsc",
"test": "run-s test:*",
"test:types": "tsc --noEmit",
"test:mocha": "npx mocha"
Expand Down
20 changes: 12 additions & 8 deletions source/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node
"use strict";
/// <reference lib="deno.ns" />

import { existsSync, writeFileSync } from "node:fs";
import { resolve, join, relative } from "node:path";
Expand All @@ -9,30 +8,35 @@ import chalk from "chalk";
import Project from "./compiler/project.ts";
import Function from "./compiler/function.ts";

if (process.argv.includes("--version")) {
if (Deno.args.includes("--version")) {
console.log("version: 0.0.0");
process.exit(0);
Deno.exit(0);
}

if (!Deno.args[0]) {
console.error(`${chalk.red("Error")}: Please provide an entry file`);
Deno.exit(1);
}

const cwd = resolve("./");
const root = join(cwd, process.argv[2]);
const root = join(cwd, Deno.args[0]);

if (!existsSync(root)) {
console.error(`${chalk.red("Error")}: Cannot find entry ${chalk.cyan(relative(cwd, root))}`);
process.exit(1);
Deno.exit(1);
}

const project = new Project(root);
if (project.failed) {
console.error(`Compilation ${chalk.red("Failed")}`);
process.exit(1);
Deno.exit(1);
}

const mainFile = project.entry;
const mainFunc = mainFile.namespace["fibonacci"];
if (!(mainFunc instanceof Function)) {
console.error(`Main namespace is not a function: ${mainFunc.constructor.name}`);
process.exit(1);
Deno.exit(1);
}


Expand Down
6 changes: 3 additions & 3 deletions source/compiler/codegen/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function CompileDeclare(ctx: Context, syntax: Syntax.Term_Declare) {
`${chalk.red("Error")}: Cannot find type\n`
+ SourceView(ctx.file.path, ctx.file.name, type.ref)
)
process.exit(1);
Deno.exit(1);
}
}

Expand All @@ -62,7 +62,7 @@ function CompileDeclare(ctx: Context, syntax: Syntax.Term_Declare) {
`${chalk.red("Error")}: type ${resolveType.name} != type ${typeRef.name}\n`
+ SourceView(ctx.file.path, ctx.file.name, type?.ref || syntax.ref)
)
process.exit(1);
Deno.exit(1);
}

let reg = ctx.scope.registerVariable(name, typeRef || resolveType, syntax.ref);
Expand All @@ -71,7 +71,7 @@ function CompileDeclare(ctx: Context, syntax: Syntax.Term_Declare) {
`${chalk.red("Error")}: Variable ${name} is already declared\n`
+ SourceView(ctx.file.path, ctx.file.name, syntax.ref)
)
process.exit(1);
Deno.exit(1);
}

ctx.block.push(Instruction.local.set(reg.register.ref));
Expand Down
10 changes: 5 additions & 5 deletions source/compiler/codegen/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function CompileConstInt(ctx: Context, syntax: Syntax.Term_Integer, prefix?: Syn
`${chalk.red("Error")}: Invalid number ${syntax.value[0].value}\n`
+ SourceView(ctx.file.path, ctx.file.name, syntax.ref)
)
process.exit(1);
Deno.exit(1);
}

const unsigned = expect === u32 || expect === u64;
Expand All @@ -54,14 +54,14 @@ function CompileConstInt(ctx: Context, syntax: Syntax.Term_Integer, prefix?: Syn
`${chalk.red("Error")}: Cannot negate an integer\n`
+ SourceView(ctx.file.path, ctx.file.name, syntax.ref)
)
process.exit(1);
Deno.exit(1);
case "-":
if (unsigned) {
console.error(
`${chalk.red("Error")}: Cannot have a negative unsigned integer\n`
+ SourceView(ctx.file.path, ctx.file.name, syntax.ref)
)
process.exit(1);
Deno.exit(1);
}

num *= -1;
Expand Down Expand Up @@ -89,7 +89,7 @@ function CompileConstFloat(ctx: Context, syntax: Syntax.Term_Float, prefix?: Syn
`${chalk.red("Error")}: Invalid number ${syntax.value[0].value}\n`
+ SourceView(ctx.file.path, ctx.file.name, syntax.ref)
)
process.exit(1);
Deno.exit(1);
}

if (prefix) {
Expand All @@ -100,7 +100,7 @@ function CompileConstFloat(ctx: Context, syntax: Syntax.Term_Float, prefix?: Syn
`${chalk.red("Error")}: Cannot negate an integer\n`
+ SourceView(ctx.file.path, ctx.file.name, syntax.ref)
)
process.exit(1);
Deno.exit(1);
case "-":
num *= -1;
break;
Expand Down
5 changes: 3 additions & 2 deletions source/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import chalk from "chalk";
import { ParseError, ReferenceRange, Reference } from "./bnf/shared.js";
import * as Syntax from "./bnf/syntax.js";

await Syntax.ready;

export function Parse(data: string, path: string, name: string): Syntax.Term_Program {
const res = Syntax.Parse_Program(data, true);

if (res instanceof ParseError) {
console.error(`${chalk.red("FATAL ERROR")}: Syntax Parser Completely crashed`);
process.exit(1);
Deno.exit(1);
};

if (res.isPartial) {
Expand All @@ -24,7 +25,7 @@ export function Parse(data: string, path: string, name: string): Syntax.Term_Pro
: ReferenceRange.blank()
)
);
process.exit(1);
Deno.exit(1);
}

return res.root;
Expand Down

0 comments on commit eece97e

Please sign in to comment.