From 190f85ff1ac23f952a7b4960638fee79495e249a Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Sun, 9 Nov 2025 21:52:06 -0800 Subject: filter --- package.json | 2 +- src/lang/js-lang/builtin/sbuiltin.ts | 21 ++++++++++ src/lang/ts-lang/builtin/sbuiltin.ts | 29 ++++++++++++++ src/lang/ts-lang/core/eval.ts | 3 ++ src/lang/ts-lang/core/lexer.ts | 2 +- tests/type-consistency/spec/index.ts | 4 +- tests/type-consistency/spec/mapReduce.ts | 34 ----------------- tests/type-consistency/spec/mapReduceFilter.ts | 53 ++++++++++++++++++++++++++ 8 files changed, 110 insertions(+), 38 deletions(-) delete mode 100644 tests/type-consistency/spec/mapReduce.ts create mode 100644 tests/type-consistency/spec/mapReduceFilter.ts diff --git a/package.json b/package.json index e5d98d5..288b247 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@aberrantflux/kai-script", "description": "A type safe framework for TypeScript", - "version": "0.3.0", + "version": "0.4.0", "repository": { "type": "git", "url": "https://git.aberrantflux.xyz/kai-script.git/" diff --git a/src/lang/js-lang/builtin/sbuiltin.ts b/src/lang/js-lang/builtin/sbuiltin.ts index 7d9b851..60cd117 100644 --- a/src/lang/js-lang/builtin/sbuiltin.ts +++ b/src/lang/js-lang/builtin/sbuiltin.ts @@ -64,6 +64,26 @@ export const V_SBUILTIN_Reduce: SBUILTIN = (node, frame) => { ); }; +export const V_SBUILTIN_Filter: SBUILTIN = (node, frame) => { + const children = getEvaluatedChildren(node, frame); + const fn = children[1] as FnPrim | undefined; + + if (!fn?.fn) { + throw new Error( + `Invalid params for filter: ${JSON.stringify(children, undefined, 2)}` + ); + } + + const values = children[0]; + + if (!Array.isArray(values)) { + // add to ts + throw new Error(`Can't filter non-array value: ${values}`); + } + + return values.filter((v, idx) => callFn(fn, [v, idx], frame) === true); +}; + export const V_SBUILTIN_IfElse: SBUILTIN = (node, frame) => { const children = node.children; @@ -84,5 +104,6 @@ export const nameToSBUILTIN: Record = { call: V_SBUILTIN_Call, map: V_SBUILTIN_Map, reduce: V_SBUILTIN_Reduce, + filter: V_SBUILTIN_Filter, "?": V_SBUILTIN_IfElse, }; diff --git a/src/lang/ts-lang/builtin/sbuiltin.ts b/src/lang/ts-lang/builtin/sbuiltin.ts index 38b4256..f089aea 100644 --- a/src/lang/ts-lang/builtin/sbuiltin.ts +++ b/src/lang/ts-lang/builtin/sbuiltin.ts @@ -70,6 +70,35 @@ export type SBUILTIN_Reduce< GetEvaluatedChildren >}`>; +type Filter< + Arr extends readonly any[], + Fn extends FnPrim, + Collected extends readonly any[] = [], + IdxLen extends readonly any[] = readonly [] +> = Arr extends [infer Head, ...infer Tail] + ? Filter< + Tail, + Fn, + CallFn extends true + ? [...Collected, Head] + : Collected, + [...IdxLen, any] + > + : Collected; + +export type SBUILTIN_Filter< + Node extends ASTNode, + Frame extends StackFrame, + Callstack extends readonly string[] +> = GetEvaluatedChildren extends [ + infer Arr extends readonly any[], + infer Fn extends FnPrim +] + ? Filter + : EvalError<`Invalid params for filter: ${ToString< + GetEvaluatedChildren + >}`>; + export type SBUILTIN_IfElse< Node extends ASTNode, Frame extends StackFrame, diff --git a/src/lang/ts-lang/core/eval.ts b/src/lang/ts-lang/core/eval.ts index bef0ef8..b937c2e 100644 --- a/src/lang/ts-lang/core/eval.ts +++ b/src/lang/ts-lang/core/eval.ts @@ -6,6 +6,7 @@ import { BUILTIN_Sub, BUILTIN_ToString, SBUILTIN_Call, + SBUILTIN_Filter, SBUILTIN_IfElse, SBUILTIN_Map, SBUILTIN_Reduce, @@ -37,6 +38,8 @@ export type MapBuiltins< ? SBUILTIN_Map : Node["name"] extends "reduce" ? SBUILTIN_Reduce + : Node["name"] extends "filter" + ? SBUILTIN_Filter : Node["name"] extends "?" ? SBUILTIN_IfElse : Node["name"] extends "tostring" diff --git a/src/lang/ts-lang/core/lexer.ts b/src/lang/ts-lang/core/lexer.ts index bcd5785..43dc3e5 100644 --- a/src/lang/ts-lang/core/lexer.ts +++ b/src/lang/ts-lang/core/lexer.ts @@ -36,7 +36,7 @@ export type IsClose = T extends `${TokenType.CLOSE_PAREN}` ? true : false; export type ChunkedLex< Ctx extends LexerCtx, Depth extends any[] = [] -> = Depth["length"] extends 50 +> = Depth["length"] extends 25 ? Ctx & { endChunk: true; } diff --git a/tests/type-consistency/spec/index.ts b/tests/type-consistency/spec/index.ts index 780c20c..c9b8c7a 100644 --- a/tests/type-consistency/spec/index.ts +++ b/tests/type-consistency/spec/index.ts @@ -2,7 +2,7 @@ import array from "./array"; import functions from "./function"; import types from "./types"; import tostring from "./tostring"; -import mapreduce from "./mapReduce"; +import mapreducefilter from "./mapReduceFilter"; import recursion from "./recursion"; -export default [array, functions, types, tostring, mapreduce, recursion]; +export default [array, functions, types, tostring, mapreducefilter, recursion]; diff --git a/tests/type-consistency/spec/mapReduce.ts b/tests/type-consistency/spec/mapReduce.ts deleted file mode 100644 index b5077ff..0000000 --- a/tests/type-consistency/spec/mapReduce.ts +++ /dev/null @@ -1,34 +0,0 @@ -import path from "path"; -import { createTestHarness } from "../harness"; - -export default createTestHarness({ - harnessName: "Map reduce", - generatedPath: path.join(__dirname, "..", "generated"), -}) - .createFunctionTest({ - name: "Map: numbers to string", - program: "fn(a, map(a, fn(n, tostring(n))))", - cases: [ - { input: [1, 2, 3], output: ["1", "2", "3"] }, - { input: [50], output: ["50"] }, - { input: [], output: [] }, - ], - }) - .createFunctionTest({ - name: "Reduce: array length", - program: "fn(a, reduce(a, fn(acc, add(acc, 1)), 0))", - cases: [ - { input: [1, 2, 3], output: 3 }, - { input: ["hello", ["hello", "world"]], output: 2 }, - { input: [], output: 0 }, - ], - }) - .createFunctionTest({ - name: "Reduce: sum of numbers times index", - program: "fn(a, reduce(a, fn(acc, cur, idx, add(acc, mul(cur, idx))), 0))", - cases: [ - { input: [1, 2, 3], output: 8 }, - { input: [], output: 0 }, - { input: [50, 10, 0], output: 10 }, - ], - }); diff --git a/tests/type-consistency/spec/mapReduceFilter.ts b/tests/type-consistency/spec/mapReduceFilter.ts new file mode 100644 index 0000000..4594e71 --- /dev/null +++ b/tests/type-consistency/spec/mapReduceFilter.ts @@ -0,0 +1,53 @@ +import path from "path"; +import { createTestHarness } from "../harness"; + +export default createTestHarness({ + harnessName: "Map reduce", + generatedPath: path.join(__dirname, "..", "generated"), +}) + .createFunctionTest({ + name: "Map: numbers to string", + program: "fn(a, map(a, fn(n, tostring(n))))", + cases: [ + { input: [1, 2, 3], output: ["1", "2", "3"] }, + { input: [50], output: ["50"] }, + { input: [], output: [] }, + ], + }) + .createFunctionTest({ + name: "Reduce: array length", + program: "fn(a, reduce(a, fn(acc, add(acc, 1)), 0))", + cases: [ + { input: [1, 2, 3], output: 3 }, + { input: ["hello", ["hello", "world"]], output: 2 }, + { input: [], output: 0 }, + ], + }) + .createFunctionTest({ + name: "Reduce: sum of numbers times index", + program: "fn(a, reduce(a, fn(acc, cur, idx, add(acc, mul(cur, idx))), 0))", + cases: [ + { input: [1, 2, 3], output: 8 }, + { input: [], output: 0 }, + { input: [50, 10, 0], output: 10 }, + ], + }) + .createFunctionTest({ + name: "Filter: remove 0s", + program: "fn(a, filter(a, fn(n, ?(eq(n, 0), false, true))))", + cases: [ + { input: [1, 2, 3], output: [1, 2, 3] }, + { input: [1, 0, 10], output: [1, 10] }, + { input: [0, 0, 0], output: [] }, + { input: [0, 0, 3, 0, 5, 0, 7, 0], output: [3, 5, 7] }, + ], + }) + .createFunctionTest({ + name: "Filter: remove first element", + program: "fn(a, filter(a, fn(n, idx, ?(eq(idx, 0), false, true))))", + cases: [ + { input: [true, true, false], output: [true, false] }, + { input: [1, 0, 10], output: [0, 10] }, + { input: ["hi", ["test", "test2"]], output: [["test", "test2"]] }, + ], + }); -- cgit v1.2.3-70-g09d2