Skip to content

Commit

Permalink
refactor(pointfree-lang): migrate CLI to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Oct 3, 2021
1 parent 2db3a8c commit 9381d83
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 94 deletions.
7 changes: 7 additions & 0 deletions packages/pointfree-lang/bin/pointfree
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
DIR="$( cd "$( dirname "$0" )" &> /dev/null && pwd )"
if [ "$(echo $DIR | grep '.nvm')" ]; then
DIR="$(dirname "$(readlink -f "$0")")"
fi

/usr/bin/env node --experimental-specifier-resolution=node --loader ts-node/esm node_modules/@thi.ng/pointfree-lang/cli.js "$@"
93 changes: 0 additions & 93 deletions packages/pointfree-lang/bin/pointfree.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/pointfree-lang/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"module": "./index.js",
"typings": "./index.d.ts",
"bin": {
"pointfree": "bin/pointfree.js"
"pointfree": "bin/pointfree"
},
"sideEffects": false,
"repository": {
Expand Down
102 changes: 102 additions & 0 deletions packages/pointfree-lang/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// thing:no-export
import type { FnU, IObjectOf } from "@thi.ng/api";
import { Args, flag, parse, string, usage } from "@thi.ng/args";
import { timedResult } from "@thi.ng/bench";
import { ConsoleLogger, NULL_LOGGER } from "@thi.ng/logger";
import { ensureStack, StackContext } from "@thi.ng/pointfree";
import { readdirSync, readFileSync, writeFileSync } from "fs";
import { ffi, run, runU } from "./runtime";

interface CLIOpts {
debug: boolean;
exec?: string;
}

const showUsage = () => {
process.stderr.write(
usage(argOpts, { prefix: "Usage: pointfree [opts] [file]" })
);
process.exit(1);
};

const argOpts: Args<CLIOpts> = {
debug: flag({ alias: "d", desc: "print debug info", group: "main" }),
exec: string({ alias: "e", desc: "execute given string" }),
};

const result = parse(argOpts, process.argv);
if (!result) showUsage();

const { result: opts, rest } = result!;
if (!(opts.exec || rest.length)) showUsage();

let src: string;

if (!opts.exec) {
try {
src = readFileSync(rest[0]).toString();
rest.shift();
} catch (e) {
process.stderr.write(`error reading source file ${(<Error>e).message}`);
process.exit(1);
}
} else {
src = opts.exec;
}

try {
const logger = opts.debug ? new ConsoleLogger("pointfree") : NULL_LOGGER;
const includeCache = new Set();
const rootEnv = { args: rest };
const builtins: IObjectOf<FnU<StackContext>> = {
include: (ctx) => {
const stack = ctx[0];
ensureStack(stack, 1);
const path = stack.pop();
if (!includeCache.has(path)) {
// cycle breaker
// TODO use dgraph
includeCache.add(path);
logger.debug(`including: ${path}`);
run(readFileSync(path).toString(), {
...ctx[2],
__vars: null,
});
} else {
logger.debug(`\t${path} already included, skipping...`);
}
return ctx;
},
"read-file": (ctx) => {
const stack = ctx[0];
ensureStack(stack, 1);
const path = stack.pop();
logger.debug(`reading file: ${path}`);
stack.push(readFileSync(path).toString());
return ctx;
},
"write-file": (ctx) => {
const stack = ctx[0];
ensureStack(stack, 2);
const path = stack.pop();
logger.debug(`writing file: ${path}`);
writeFileSync(path, stack.pop());
return ctx;
},
"read-dir": (ctx) => {
const stack = ctx[0];
ensureStack(stack, 1);
const path = stack.pop();
logger.debug(`reading directory: ${path}`);
stack.push(readdirSync(path));
return ctx;
},
};
const env = ffi(rootEnv, builtins);
const [res, time] = timedResult(() => runU(src, env));
logger.debug(`executed in ${time}ms`);
process.exit(typeof res === "number" ? res : 0);
} catch (e) {
console.log(e);
}
process.exit(1);

0 comments on commit 9381d83

Please sign in to comment.