Skip to content

Commit

Permalink
Roblox-TS compatibility update
Browse files Browse the repository at this point in the history
  • Loading branch information
phunanon committed Jul 24, 2023
1 parent dd54245 commit a5522cd
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 87 deletions.
20 changes: 11 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,17 @@ function exeOp(op: string, args: Val[], ctx: Ctx, errCtx: ErrCtx): Val {
case "criteria": {
const name = `(criteria ${args.map(val2str).join(" ")})`;
const ins: Ins[] = [
...args.flatMap((value, i) => {
const jmp = (len(args) - 1 - i) * 4 + 2;
return [
{ typ: "upa", value: 0, text: "x", errCtx },
{ typ: "val", value, errCtx },
{ typ: "exe", value: 1, errCtx },
{ typ: "if", value: jmp, errCtx },
] as Ins[];
}),
...flat(
args.map((value, i) => {
const jmp = (len(args) - 1 - i) * 4 + 2;
return [
{ typ: "upa", value: 0, text: "x", errCtx },
{ typ: "val", value, errCtx },
{ typ: "exe", value: 1, errCtx },
{ typ: "if", value: jmp, errCtx },
] as Ins[];
}),
),
{ typ: "val", value: _boo(true), errCtx },
{ typ: "jmp", value: 1, errCtx },
{ typ: "val", value: _boo(false), errCtx },
Expand Down
5 changes: 3 additions & 2 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ function parseForm(
{ typ: "exe", value: 1, errCtx },
{ typ: "if", value: 2, errCtx },
{ typ: "val", value: nullVal, errCtx },
{ typ: "jmp", value: parsed[0].length + 9 + body.length + 12, errCtx },
{ typ: "jmp", value: len(parsed[0]) + 9 + len(body) + 12, errCtx },
...parsed[0],
{ typ: "let", value: symNode.text + "-item", errCtx },
{ typ: "val", value: { t: "num", v: 0 }, errCtx },
Expand Down Expand Up @@ -575,7 +575,8 @@ function parseForm(
if (len(args) < 1) {
return [{ typ: "err", value: "provide a condition", errCtx }];
}
const [cond, ...params] = args;
const cond = args[0];
const params = slice(args, 1);
const flatParams = flat(params);
return [
...cond,
Expand Down
15 changes: 8 additions & 7 deletions src/repl.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
#!/usr/bin/env node
import readline = require("readline");
import { appendFileSync, readFileSync, rmSync, writeFileSync } from "fs";
import { unlinkSync, existsSync, mkdirSync } from "fs";
import { insituxVersion, symbols } from ".";
import { join as pathJoin, dirname } from "path";
import { Ctx, defaultCtx, ErrCtx, Val, ValOrErr } from "./types";
import { Operation, ExternalFunctions } from "./types";
import { InvokeOutput, invoker, parensRx, valueInvoker } from "./invoker";
import { tokenise } from "./parse";
import prompt = require("prompt-sync");
import { str, _nul, _str, _vec, num, _num, _key, val2str } from "./val";
import { ixToJs, jsToIx } from "./val-translate";

import { exit } from "process";
import { str, _nul, _str, _vec, num, _num, _key } from "./val";
import { jsToIx, val2str, ixToJs } from "./val";
import readline = require("readline");
import fetch from "cross-fetch";
import prompt = require("prompt-sync");
import clone = require("git-clone/promise");
const execSync = require("child_process").execSync;
import { appendFileSync, readFileSync, rmSync, writeFileSync } from "fs";
import { unlinkSync, existsSync, mkdirSync } from "fs";
import { join as pathJoin, dirname } from "path";

const githubRegex = /^(?!https*:)[^\/]+?\/[^\/]+$/;
let colourMode = true;
Expand Down
70 changes: 70 additions & 0 deletions src/val-translate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { isArray, isNum, isObj, isStr, objKeys, objVals } from "./poly-fills";
import { Val } from "./types";
import { val2str } from "./val";

/** Incomplete. */
export function jsToIx(
v: unknown,
ifUndetermined = (x: unknown) => <Val>{ t: "str", v: `${x}` },
): Val {
if (isStr(v)) {
return { t: "str", v };
}
if (isNum(v)) {
return { t: "num", v };
}
if (v === true || v === false) {
return { t: "bool", v };
}
if (v === null) {
return { t: "null", v: undefined };
}
const mapper = (v: unknown[]) => v.map(x => jsToIx(x, ifUndetermined));
if (isArray(v)) {
return { t: "vec", v: mapper(v) };
}
if (isObj(v)) {
return {
t: "dict",
v: { keys: mapper(objKeys(v)), vals: mapper(objVals(v)) },
};
}
return ifUndetermined(v);
}

/** Incomplete. */
export function ixToJs(
v: Val,
ifUndetermined = (x: Val) => x.v,
):
| string
| number
| boolean
| null
| Record<string, unknown>
| unknown[]
| unknown {
if (v.t === "str" || v.t === "num" || v.t === "bool" || v.t === "key") {
return v.v;
}
if (v.t === "vec") {
return v.v.map(x => ixToJs(x, ifUndetermined));
}
if (v.t === "null") {
return null;
}
if (v.t === "dict") {
const keys = v.v.keys.map(x => val2str(x));
const vals = v.v.vals.map(x => ixToJs(x, ifUndetermined));
const obj: Record<string, unknown> = {};
keys.forEach((k, i) => {
obj[k] = vals[i];
});
return obj;
}
if (v.t === "ext") {
return v.v;
}
return ifUndetermined(v);
}

69 changes: 1 addition & 68 deletions src/val.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { flat, isStr, len, max, slice, splice } from "./poly-fills";
import { isArray, isNum, isObj, objKeys, objVals } from "./poly-fills";
import { flat, len, max, slice, splice } from "./poly-fills";
import { assertUnreachable, Dict, Func, InvokeError, Val } from "./types";

export const num = ({ v }: Val) => v as number;
Expand Down Expand Up @@ -210,69 +209,3 @@ export function pathSet(
),
};
}

/** Incomplete. */
export function jsToIx(
v: unknown,
ifUndetermined = (x: unknown) => <Val>{ t: "str", v: `${x}` },
): Val {
if (isStr(v)) {
return { t: "str", v };
}
if (isNum(v)) {
return { t: "num", v };
}
if (v === true || v === false) {
return { t: "bool", v };
}
if (v === null) {
return { t: "null", v: undefined };
}
const mapper = (v: unknown[]) => v.map(x => jsToIx(x, ifUndetermined));
if (isArray(v)) {
return { t: "vec", v: mapper(v) };
}
if (isObj(v)) {
return {
t: "dict",
v: { keys: mapper(objKeys(v)), vals: mapper(objVals(v)) },
};
}
return ifUndetermined(v);
}

/** Incomplete. */
export function ixToJs(
v: Val,
ifUndetermined = (x: Val) => x.v,
):
| string
| number
| boolean
| null
| Record<string, unknown>
| unknown[]
| unknown {
if (v.t === "str" || v.t === "num" || v.t === "bool" || v.t === "key") {
return v.v;
}
if (v.t === "vec") {
return v.v.map(x => ixToJs(x, ifUndetermined));
}
if (v.t === "null") {
return null;
}
if (v.t === "dict") {
const keys = v.v.keys.map(x => val2str(x));
const vals = v.v.vals.map(x => ixToJs(x, ifUndetermined));
const obj: Record<string, unknown> = {};
keys.forEach((k, i) => {
obj[k] = vals[i];
});
return obj;
}
if (v.t === "ext") {
return v.v;
}
return ifUndetermined(v);
}
3 changes: 2 additions & 1 deletion src/web.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { functionInvoker, InvokeOutput, invoker } from "./invoker";
import { Ctx, defaultCtx, ExternalFunctions, Val, ValOrErr } from "./types";
import { num, str, val2str, _nul, _str, _num, _boo, jsToIx } from "./val";
import { num, str, val2str, _nul, _str, _num, _boo } from "./val";
import { jsToIx } from "./val-translate";

let state = new Map<string, Val>();

Expand Down

0 comments on commit a5522cd

Please sign in to comment.