Skip to content

Commit b4fb061

Browse files
tools(vectors): #497 update codegen to generate sources in new pkg dirs
1 parent bfad851 commit b4fb061

File tree

1 file changed

+72
-33
lines changed

1 file changed

+72
-33
lines changed

packages/vectors/tools/codegen.ts

+72-33
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import type { IObjectOf } from "@thi.ng/api";
22
import { writeText } from "@thi.ng/file-io";
33
import { ConsoleLogger } from "@thi.ng/logger";
4-
import { bytes, interpolate, wordWrapLines } from "@thi.ng/strings";
4+
import { bytes, interpolate, lower, wordWrapLines } from "@thi.ng/strings";
55
import * as v from "@thi.ng/vectors";
66

7-
const OUT_DIR = "src2";
8-
const API_PREFIX = "../src";
7+
const OUT_DIR = "packages";
8+
const API_IMPORT = "@thi.ng/vec-api";
99
const LOGGER = new ConsoleLogger("codegen");
1010

1111
let TOTAL_SIZE = 0;
1212

1313
interface FnSpec {
1414
/** Function name */
1515
name: string;
16+
/** Derived function name for import/filename, default: `lower(name)` */
17+
importName?: string;
1618
/** Function type */
1719
type: string;
1820
/** Generic (nD) function type */
1921
typeGeneric?: string;
22+
/** Polymorphic (nD) function type */
23+
typePoly?: string;
2024
/** Higher order fn name (to replace `op` in original code) */
2125
op?: string;
2226
/** VOP dispatch argument index, default 1 */
@@ -75,8 +79,10 @@ const formatDocs = (
7579

7680
const emitFamily = ({
7781
name,
82+
importName = lower(name),
7883
type,
79-
typeGeneric = "Multi" + type,
84+
typeGeneric = type,
85+
typePoly = "Multi" + typeGeneric,
8086
dispatch = 1,
8187
doc = [],
8288
params,
@@ -88,19 +94,35 @@ const emitFamily = ({
8894
multi = true,
8995
op,
9096
}: FnSpec) => {
97+
LOGGER.debug("emitting family:", name);
9198
const nakedType = type.replace(/<[a-z0-9, ]+>$/i, "");
9299
const nakedTypeGeneric = typeGeneric.replace(/<[a-z0-9, ]+>$/i, "");
93-
const typeImport = `import type { ${nakedType} } from "${API_PREFIX}/api.js";`;
100+
const nakedTypePoly = typePoly.replace(/<[a-z0-9, ]+>$/i, "");
101+
const typeImport = `import type { ${nakedType} } from "${API_IMPORT}";`;
102+
const typeImportGeneric = `import type { ${nakedTypeGeneric} } from "${API_IMPORT}";`;
94103
const userImportsFixed = Object.entries(imports).map(
95104
([pkg, syms]) => `import { ${syms} } from "${pkg}";`
96105
);
97106
const userImportsGeneric = Object.entries(importsGeneric ?? imports).map(
98107
([pkg, syms]) => `import { ${syms} } from "${pkg}";`
99108
);
100-
const $imports = [...userImportsGeneric];
101-
if (multi) $imports.push(`import { vop } from "${API_PREFIX}/vop.js";`);
109+
const polyImports = [
110+
`import { ${name} as $${name} } from "@thi.ng/vec-nd/${importName}";`,
111+
];
102112
const $docs = doc.join("\n");
103-
const generic = multi
113+
const generic = [
114+
...formatDocs($docs, paramsGeneric, "n", ""),
115+
formatFunction(
116+
name,
117+
multi
118+
? (<any>v)[name].impl().toString()
119+
: (<any>v)[name].toString(),
120+
typeGeneric,
121+
undefined,
122+
op
123+
) + ";",
124+
];
125+
let polymorphic = multi
104126
? [
105127
...formatDocs(
106128
$docs +
@@ -109,25 +131,10 @@ const emitFamily = ({
109131
"n",
110132
""
111133
),
112-
`export const ${name}: ${typeGeneric} = vop(${dispatch});`,
113-
`${name}.default(${formatFunction(
114-
name,
115-
(<any>v)[name].impl().toString(),
116-
type,
117-
"",
118-
op
119-
)});`,
134+
`export const ${name}: ${typePoly} = vop(${dispatch});`,
135+
`${name}.default($${name})`,
120136
]
121-
: [
122-
...formatDocs($docs, paramsGeneric, "n", ""),
123-
`export const ${name}: ${typeGeneric} = ${formatFunction(
124-
name,
125-
(<any>v)[name].toString(),
126-
type,
127-
"",
128-
op
129-
)};`,
130-
];
137+
: [];
131138
let body: string;
132139
for (let d of [2, 3, 4]) {
133140
const id = name + sep + d;
@@ -141,22 +148,39 @@ const emitFamily = ({
141148
formatFunction(id, (<any>v)[id].toString(), type, undefined, op) +
142149
";",
143150
].join("\n");
144-
writeText(`${OUT_DIR}/${id}.ts`, body, LOGGER);
151+
writeText(`${OUT_DIR}/vec${d}/src/${importName}.ts`, body, LOGGER);
145152
TOTAL_SIZE += body.length;
146153
if (multi) {
147-
$imports.push(`import { ${id} } from "./${id}.js";`);
148-
generic.push(`${name}.add(${d}, ${id});`);
154+
polyImports.push(
155+
`import { ${id} } from "@thi.ng/vec${d}/${importName}";`
156+
);
157+
polymorphic!.push(`${name}.add(${d}, ${id});`);
149158
}
150159
}
160+
161+
// generic n-dimensional
151162
body = [
152-
`import type { ${nakedTypeGeneric} } from "${API_PREFIX}/api.js";`,
153-
...$imports,
163+
...userImportsGeneric,
164+
typeImportGeneric,
154165
"",
155166
...pre,
156167
...generic,
157168
].join("\n");
158-
writeText(`${OUT_DIR}/${name}.ts`, body, LOGGER);
169+
writeText(`${OUT_DIR}/vec-nd/src/${importName}.ts`, body, LOGGER);
159170
TOTAL_SIZE += body.length;
171+
172+
if (multi) {
173+
// polymorphic
174+
body = [
175+
`import type { ${nakedTypePoly} } from "./api.js";`,
176+
...polyImports,
177+
`import { vop } from "./vop.js";`,
178+
"",
179+
...polymorphic,
180+
].join("\n");
181+
writeText(`${OUT_DIR}/vec-poly/src/${importName}.ts`, body, LOGGER);
182+
TOTAL_SIZE += body.length;
183+
}
160184
};
161185

162186
const PARAMS_V = { o: "output vector", a: "input vector" };
@@ -474,18 +498,21 @@ const SPECS: FnSpec[] = [
474498
},
475499
{
476500
name: "asBVec",
501+
importName: "as-bvec",
477502
type: "ToBVecOpV",
478503
doc: ["Componentwise converts given {0}D vector to boolean."],
479504
params: PARAMS_V,
480505
},
481506
{
482507
name: "asIVec",
508+
importName: "as-ivec",
483509
type: "VecOpV",
484510
doc: ["Componentwise converts given {0}D vector to signed integer."],
485511
params: PARAMS_V,
486512
},
487513
{
488514
name: "asUVec",
515+
importName: "as-uvec",
489516
type: "VecOpV",
490517
doc: ["Componentwise converts given {0}D vector to unsigned integer."],
491518
params: PARAMS_V,
@@ -506,6 +533,7 @@ const SPECS: FnSpec[] = [
506533
// TODO fix original naming to: atan2_2 / atan2_3 ...
507534
{
508535
name: "atan_2",
536+
importName: "atan2",
509537
type: "VecOpVV",
510538
doc: [
511539
"Componentwise computes `Math.atan2` of the two given {0}D vectors.",
@@ -693,6 +721,7 @@ const SPECS: FnSpec[] = [
693721
},
694722
{
695723
name: "distChebyshev",
724+
importName: "dist-chebyshev",
696725
type: "VecOpRoVV<number>",
697726
dispatch: 0,
698727
doc: [
@@ -702,6 +731,7 @@ const SPECS: FnSpec[] = [
702731
},
703732
{
704733
name: "distManhattan",
734+
importName: "dist-manhattan",
705735
type: "VecOpRoVV<number>",
706736
dispatch: 0,
707737
doc: [
@@ -801,7 +831,7 @@ const SPECS: FnSpec[] = [
801831
importsGeneric: {
802832
"@thi.ng/checks/implements-function": "implementsFunction",
803833
"@thi.ng/math/api": "EPS",
804-
[`${API_PREFIX}/eqdelta`]: "eqDeltaS",
834+
[`${API_IMPORT}/eqdelta`]: "eqDeltaS",
805835
},
806836
dispatch: 0,
807837
},
@@ -818,6 +848,7 @@ const SPECS: FnSpec[] = [
818848
},
819849
{
820850
name: "exp_2",
851+
importName: "exp2",
821852
type: "VecOpV",
822853
doc: ["Componentwise computes `2^x` of given {0}D vector."],
823854
params: PARAMS_V,
@@ -890,6 +921,7 @@ const SPECS: FnSpec[] = [
890921
},
891922
{
892923
name: "fromBVec",
924+
importName: "from-bvec",
893925
type: "FromBVecOpV",
894926
doc: [
895927
"Componentwise converts given {0}D boolean vector to floating point (0 or 1).",
@@ -928,6 +960,7 @@ const SPECS: FnSpec[] = [
928960
},
929961
{
930962
name: "isInf",
963+
importName: "is-inf",
931964
type: "ToBVecOpV",
932965
doc: [
933966
"Componentwise checks if given {0}D vector is infinite and writes results to boolean output vector. If `out` is null, creates a new result vector.",
@@ -936,6 +969,7 @@ const SPECS: FnSpec[] = [
936969
},
937970
{
938971
name: "isNaN",
972+
importName: "is-nan",
939973
type: "ToBVecOpV",
940974
doc: [
941975
"Componentwise checks if given {0}D vector is NaN and writes results to boolean output vector. If `out` is null, creates a new result vector.",
@@ -952,6 +986,7 @@ const SPECS: FnSpec[] = [
952986
// TODO fix original naming to: log2_2, log2_3, log2_4
953987
{
954988
name: "log_2",
989+
importName: "log2",
955990
type: "VecOpV",
956991
doc: ["Componentwise `Math.log2` of given {0}D vector."],
957992
params: PARAMS_V,
@@ -1114,6 +1149,7 @@ const SPECS: FnSpec[] = [
11141149
},
11151150
{
11161151
name: "mixBilinear",
1152+
importName: "mix-bilinear",
11171153
type: "VecOpVVVVNN",
11181154
doc: ["Componentwise {0}D vector bilinear interpolation."],
11191155
params: {
@@ -1325,6 +1361,7 @@ const SPECS: FnSpec[] = [
13251361
},
13261362
{
13271363
name: "randDistrib",
1364+
importName: "rand-distrib",
13281365
type: "VecOpFNO",
13291366
doc: [
13301367
"Sets `v` to random vector, with each component drawn from given random distribution function (default: gaussian/normal distribution) and scaled to `n` (default: 1). Creates new vector if `v` is null.",
@@ -1348,6 +1385,7 @@ const SPECS: FnSpec[] = [
13481385
},
13491386
{
13501387
name: "randMinMax",
1388+
importName: "rand-minmax",
13511389
type: "VecOpVVO<IRandom>",
13521390
doc: [],
13531391
params: {
@@ -1363,6 +1401,7 @@ const SPECS: FnSpec[] = [
13631401
},
13641402
{
13651403
name: "randMinMaxS",
1404+
importName: "rand-minmaxs",
13661405
type: "VecOpSVVO<IRandom>",
13671406
typeGeneric: "VecOpSGVVO<IRandom>",
13681407
doc: ["Like {@link randMinMax{1}} but for {0}D strided vectors."],

0 commit comments

Comments
 (0)