Skip to content

Commit

Permalink
feat: created linker for import others deps
Browse files Browse the repository at this point in the history
  • Loading branch information
guiferpa committed Dec 29, 2023
1 parent ca41bbe commit 61a0233
Show file tree
Hide file tree
Showing 16 changed files with 638 additions and 504 deletions.
7 changes: 4 additions & 3 deletions src/builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { Parser } from "@/parser";

import Generator from "./generator";
import SymTable from "@/symtable/symtable";
import { read } from "@/fsutil";

export default class Builder {
constructor(private readonly _buffer: Buffer = Buffer.from("")) {}

public run(debug?: boolean): string[] {
public async run(debug?: boolean): Promise<string[]> {
const lexer = new Lexer(this._buffer);
const parser = new Parser(lexer, new SymTable("root"));
const tree = parser.parse();
const parser = new Parser({ read }, new SymTable("root"));
const tree = await parser.parse(lexer);
debug && console.log(colorize(JSON.stringify(tree, null, 2)));
return Generator.run(tree);
}
Expand Down
38 changes: 38 additions & 0 deletions src/fsutil/fsutil.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { noext } from "./fsutil";

describe("fsutil test suite", () => {
test("Should return filename and empty extension", async () => {
const raw = "greeting";
const expected = ["greeting", ""];
const got = noext(raw);
expect(got).toStrictEqual(expected);
});

test("Should return filename and 'br' extension", async () => {
const raw = "greeting.br";
const expected = ["greeting", "br"];
const got = noext(raw);
expect(got).toStrictEqual(expected);
});

test("Should return filename and 'ar' extension", async () => {
const raw = "greeting.ar";
const expected = ["greeting", "ar"];
const got = noext(raw);
expect(got).toStrictEqual(expected);
});

test("Should return 'gree.ting' filename and 'ar' extension", async () => {
const raw = "gree.ting.ar";
const expected = ["gree.ting", "ar"];
const got = noext(raw);
expect(got).toStrictEqual(expected);
});

test("Should return empty filename and empty extension", async () => {
const raw = "";
const expected = ["", ""];
const got = noext(raw);
expect(got).toStrictEqual(expected);
});
});
11 changes: 9 additions & 2 deletions src/fsutil/fsutil.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import fs from "fs";

export async function read(arg: string): Promise<Buffer> {
export async function read(raw: string): Promise<Buffer> {
const [filename] = noext(raw);

return new Promise((resolve, reject) => {
let buffer = Buffer.from("");

const reader = fs.createReadStream(arg);
const reader = fs.createReadStream(`${filename}.ar`);

reader.on("data", (chunk: Buffer) => {
buffer = Buffer.concat([buffer, chunk]);
Expand All @@ -30,3 +32,8 @@ export async function write(filename: string, content: string[]) {

writer.close();
}

export function noext(raw: string): [string, string] {
const [ext, ...splitted] = raw.split(".").reverse();
return splitted.length > 0 ? [splitted.reverse().join("."), ext] : [ext, ""];
}
3 changes: 0 additions & 3 deletions src/importer/dependency.ts

This file was deleted.

57 changes: 0 additions & 57 deletions src/importer/discover.test.ts

This file was deleted.

54 changes: 0 additions & 54 deletions src/importer/discover.ts

This file was deleted.

36 changes: 28 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { Interpreter } from "@/interpreter";
import { read } from "@/fsutil";
import { repl } from "@/repl";
import { Builder } from "./builder";
import Environment from "./environ/environ";
import { Parser } from "./parser";
import SymTable from "./symtable/symtable";
import { Lexer } from "./lexer";

function run() {
const program = new Command();
Expand All @@ -15,17 +19,28 @@ function run() {
program
.option("-t, --tree", "tree flag to show AST", false)
.option("-a, --args <string>", "pass arguments for runtime", "")
.action(function () {
.action(async function () {
const options = program.opts();

const optArgs = options.args.split(",");

const r = repl();
const interpreter = new Interpreter();

r.on("line", function (chunk) {
interpreter.write(Buffer.from(chunk));
console.log(`= ${interpreter.run(options.tree as boolean, optArgs)}`);
const environ = new Environment("global");
const interpreter = new Interpreter(environ);

r.on("line", async function (chunk) {
const buffer = Buffer.from(chunk);
const lexer = new Lexer(buffer);
const symtable = new SymTable("global");
const parser = new Parser({ read }, symtable);
const tree = await parser.parse(lexer);
const result = await interpreter.run(
tree,
options.tree as boolean,
optArgs
);
console.log(`= ${result}`);
r.prompt(true);
});

Expand All @@ -47,8 +62,13 @@ function run() {
const optArgs = options.args.split(",");

const buffer = await read(arg);
const interpreter = new Interpreter(buffer);
interpreter.run(options.tree as boolean, optArgs);
const lexer = new Lexer(buffer);
const symtable = new SymTable("global");
const parser = new Parser({ read }, symtable);
const tree = await parser.parse(lexer);
const environ = new Environment("global");
const interpreter = new Interpreter(environ);
await interpreter.run(tree, options.tree as boolean, optArgs);
} catch (err) {
if (err instanceof SyntaxError) {
console.log(err);
Expand All @@ -69,7 +89,7 @@ function run() {

const buffer = await read(arg);
const builder = new Builder(buffer);
const ops = builder.run(options.tree as boolean);
const ops = await builder.run(options.tree as boolean);
ops.forEach((op, idx) => {
console.log(`${idx}: ${op}`);
});
Expand Down
Loading

0 comments on commit 61a0233

Please sign in to comment.