mirror of
https://github.com/litruv/picoGraph.git
synced 2026-07-24 18:56:04 +10:00
Initial commit
moved from private version control
This commit is contained in:
46
scripts/nodes/library/table/add.js
Normal file
46
scripts/nodes/library/table/add.js
Normal file
@@ -0,0 +1,46 @@
|
||||
import { createNodeModule } from "../../nodeTypes.js";
|
||||
|
||||
/**
|
||||
* Adds a value to a table using PICO-8's add helper.
|
||||
*/
|
||||
export const tableAddNode = createNodeModule(
|
||||
{
|
||||
id: "table_add",
|
||||
title: "Add To Table",
|
||||
category: "Tables",
|
||||
description: "Append a value to a table, optionally inserting at an index.",
|
||||
searchTags: ["add", "table", "insert", "append", "push"],
|
||||
inputs: [
|
||||
{ id: "exec_in", name: "Exec", direction: "input", kind: "exec" },
|
||||
{ id: "table", name: "Table", direction: "input", kind: "table" },
|
||||
{ id: "value", name: "Value", direction: "input", kind: "any" },
|
||||
{
|
||||
id: "index",
|
||||
name: "Index",
|
||||
direction: "input",
|
||||
kind: "number",
|
||||
description: "Optional insertion index",
|
||||
},
|
||||
],
|
||||
outputs: [
|
||||
{ id: "exec_out", name: "Exec", direction: "output", kind: "exec" },
|
||||
],
|
||||
properties: [],
|
||||
},
|
||||
{
|
||||
emitExec: ({ indent, indentLevel, resolveValueInput, emitNextExec }) => {
|
||||
const OMIT = "__pg_omit__";
|
||||
const tableValue = resolveValueInput("table", "{}");
|
||||
const value = resolveValueInput("value", "nil");
|
||||
const index = resolveValueInput("index", OMIT);
|
||||
|
||||
const args = [tableValue, value];
|
||||
if (index !== OMIT) {
|
||||
args.push(index);
|
||||
}
|
||||
|
||||
const line = `${indent(indentLevel)}add(${args.join(", ")})`;
|
||||
return [line, ...emitNextExec("exec_out")];
|
||||
},
|
||||
}
|
||||
);
|
||||
25
scripts/nodes/library/table/all.js
Normal file
25
scripts/nodes/library/table/all.js
Normal file
@@ -0,0 +1,25 @@
|
||||
import { createNodeModule } from "../../nodeTypes.js";
|
||||
|
||||
/**
|
||||
* Produces a table iterator using PICO-8's all helper.
|
||||
*/
|
||||
export const tableAllNode = createNodeModule(
|
||||
{
|
||||
id: "table_all",
|
||||
title: "All Iterator",
|
||||
category: "Tables",
|
||||
description: "Create an iterator covering all indexed table values.",
|
||||
searchTags: ["all", "table", "iterator", "loop"],
|
||||
inputs: [{ id: "table", name: "Table", direction: "input", kind: "table" }],
|
||||
outputs: [
|
||||
{ id: "iterator", name: "Iterator", direction: "output", kind: "any" },
|
||||
],
|
||||
properties: [],
|
||||
},
|
||||
{
|
||||
evaluateValue: ({ resolveValueInput }) => {
|
||||
const tableValue = resolveValueInput("table", "{}");
|
||||
return `all(${tableValue})`;
|
||||
},
|
||||
}
|
||||
);
|
||||
40
scripts/nodes/library/table/count.js
Normal file
40
scripts/nodes/library/table/count.js
Normal file
@@ -0,0 +1,40 @@
|
||||
import { createNodeModule } from "../../nodeTypes.js";
|
||||
|
||||
/**
|
||||
* Counts entries in a table or matching values using PICO-8's count helper.
|
||||
*/
|
||||
export const tableCountNode = createNodeModule(
|
||||
{
|
||||
id: "table_count",
|
||||
title: "Count Table",
|
||||
category: "Tables",
|
||||
description: "Return the number of entries in a table, optionally matching a value.",
|
||||
searchTags: ["count", "table", "length", "size"],
|
||||
inputs: [
|
||||
{ id: "table", name: "Table", direction: "input", kind: "table" },
|
||||
{
|
||||
id: "value",
|
||||
name: "Value",
|
||||
direction: "input",
|
||||
kind: "any",
|
||||
description: "Optional value to match",
|
||||
},
|
||||
],
|
||||
outputs: [
|
||||
{ id: "result", name: "Count", direction: "output", kind: "number" },
|
||||
],
|
||||
properties: [],
|
||||
},
|
||||
{
|
||||
evaluateValue: ({ resolveValueInput }) => {
|
||||
const OMIT = "__pg_omit__";
|
||||
const tableValue = resolveValueInput("table", "{}");
|
||||
const value = resolveValueInput("value", OMIT);
|
||||
const args = [tableValue];
|
||||
if (value !== OMIT) {
|
||||
args.push(value);
|
||||
}
|
||||
return `count(${args.join(", ")})`;
|
||||
},
|
||||
}
|
||||
);
|
||||
31
scripts/nodes/library/table/del.js
Normal file
31
scripts/nodes/library/table/del.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import { createNodeModule } from "../../nodeTypes.js";
|
||||
|
||||
/**
|
||||
* Removes the first matching value from a table using PICO-8's del helper.
|
||||
*/
|
||||
export const tableDelNode = createNodeModule(
|
||||
{
|
||||
id: "table_del",
|
||||
title: "Delete From Table",
|
||||
category: "Tables",
|
||||
description: "Remove the first matching value from a table.",
|
||||
searchTags: ["del", "table", "remove", "delete"],
|
||||
inputs: [
|
||||
{ id: "exec_in", name: "Exec", direction: "input", kind: "exec" },
|
||||
{ id: "table", name: "Table", direction: "input", kind: "table" },
|
||||
{ id: "value", name: "Value", direction: "input", kind: "any" },
|
||||
],
|
||||
outputs: [
|
||||
{ id: "exec_out", name: "Exec", direction: "output", kind: "exec" },
|
||||
],
|
||||
properties: [],
|
||||
},
|
||||
{
|
||||
emitExec: ({ indent, indentLevel, resolveValueInput, emitNextExec }) => {
|
||||
const tableValue = resolveValueInput("table", "{}");
|
||||
const value = resolveValueInput("value", "nil");
|
||||
const line = `${indent(indentLevel)}del(${tableValue}, ${value})`;
|
||||
return [line, ...emitNextExec("exec_out")];
|
||||
},
|
||||
}
|
||||
);
|
||||
42
scripts/nodes/library/table/deli.js
Normal file
42
scripts/nodes/library/table/deli.js
Normal file
@@ -0,0 +1,42 @@
|
||||
import { createNodeModule } from "../../nodeTypes.js";
|
||||
|
||||
/**
|
||||
* Removes a table entry at a given index using PICO-8's deli helper.
|
||||
*/
|
||||
export const tableDeliNode = createNodeModule(
|
||||
{
|
||||
id: "table_deli",
|
||||
title: "Delete Table Index",
|
||||
category: "Tables",
|
||||
description: "Remove and return the value at an index, or the last entry when omitted.",
|
||||
searchTags: ["deli", "table", "remove", "pop"],
|
||||
inputs: [
|
||||
{ id: "exec_in", name: "Exec", direction: "input", kind: "exec" },
|
||||
{ id: "table", name: "Table", direction: "input", kind: "table" },
|
||||
{
|
||||
id: "index",
|
||||
name: "Index",
|
||||
direction: "input",
|
||||
kind: "number",
|
||||
description: "Optional index to remove",
|
||||
},
|
||||
],
|
||||
outputs: [
|
||||
{ id: "exec_out", name: "Exec", direction: "output", kind: "exec" },
|
||||
],
|
||||
properties: [],
|
||||
},
|
||||
{
|
||||
emitExec: ({ indent, indentLevel, resolveValueInput, emitNextExec }) => {
|
||||
const OMIT = "__pg_omit__";
|
||||
const tableValue = resolveValueInput("table", "{}");
|
||||
const index = resolveValueInput("index", OMIT);
|
||||
const args = [tableValue];
|
||||
if (index !== OMIT) {
|
||||
args.push(index);
|
||||
}
|
||||
const line = `${indent(indentLevel)}deli(${args.join(", ")})`;
|
||||
return [line, ...emitNextExec("exec_out")];
|
||||
},
|
||||
}
|
||||
);
|
||||
116
scripts/nodes/library/table/foreach.js
Normal file
116
scripts/nodes/library/table/foreach.js
Normal file
@@ -0,0 +1,116 @@
|
||||
import { createNodeModule } from "../../nodeTypes.js";
|
||||
|
||||
/**
|
||||
* Iterates over a table with a callback using PICO-8's foreach helper and exposes loop variables.
|
||||
*/
|
||||
export const tableForeachNode = createNodeModule(
|
||||
{
|
||||
id: "table_foreach",
|
||||
title: "Foreach Table",
|
||||
category: "Tables",
|
||||
description:
|
||||
"Execute a callback for each value produced by foreach().",
|
||||
searchTags: ["foreach", "table", "iterate", "loop", "index", "counter"],
|
||||
inputs: [
|
||||
{ id: "exec_in", name: "Exec", direction: "input", kind: "exec" },
|
||||
{ id: "table", name: "Table", direction: "input", kind: "table" },
|
||||
],
|
||||
outputs: [
|
||||
{ id: "loop", name: "Loop", direction: "output", kind: "exec" },
|
||||
{ id: "item", name: "Item", direction: "output", kind: "any" },
|
||||
{ id: "index", name: "Index", direction: "output", kind: "number" },
|
||||
{
|
||||
id: "completed",
|
||||
name: "Completed",
|
||||
direction: "output",
|
||||
kind: "exec",
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
key: "itemName",
|
||||
label: "Item Variable",
|
||||
type: "string",
|
||||
defaultValue: "item",
|
||||
},
|
||||
{
|
||||
key: "indexName",
|
||||
label: "Index Variable",
|
||||
type: "string",
|
||||
defaultValue: "i",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
emitExec: ({
|
||||
node,
|
||||
indent,
|
||||
indentLevel,
|
||||
resolveValueInput,
|
||||
sanitizeIdentifier,
|
||||
emitBranch,
|
||||
path,
|
||||
}) => {
|
||||
const tableValue = resolveValueInput("table", "{}");
|
||||
let itemName = sanitizeIdentifier(String(node.properties.itemName ?? "item"));
|
||||
if (!itemName) {
|
||||
itemName = "item";
|
||||
}
|
||||
let indexName = sanitizeIdentifier(String(node.properties.indexName ?? "i"));
|
||||
if (!indexName) {
|
||||
indexName = "i";
|
||||
}
|
||||
let counterName = sanitizeIdentifier(`__pg_${node.id}_index`);
|
||||
if (!counterName) {
|
||||
counterName = "__pg_index";
|
||||
}
|
||||
if (counterName === indexName) {
|
||||
counterName = `${counterName}_counter`;
|
||||
}
|
||||
const lines = [
|
||||
`${indent(indentLevel)}local ${counterName} = 0`,
|
||||
`${indent(indentLevel)}foreach(${tableValue}, function(${itemName})`,
|
||||
`${indent(indentLevel + 1)}${counterName} += 1`,
|
||||
`${indent(indentLevel + 1)}local ${indexName} = ${counterName}`,
|
||||
];
|
||||
|
||||
const branchLines = emitBranch("loop", {
|
||||
indentLevel: indentLevel + 1,
|
||||
path: new Set(path),
|
||||
});
|
||||
if (!branchLines.length) {
|
||||
lines.push(`${indent(indentLevel + 1)}-- foreach body`);
|
||||
} else {
|
||||
lines.push(...branchLines);
|
||||
}
|
||||
|
||||
lines.push(`${indent(indentLevel)}end)`);
|
||||
lines.push(
|
||||
...emitBranch("completed", {
|
||||
indentLevel,
|
||||
path,
|
||||
})
|
||||
);
|
||||
|
||||
return lines;
|
||||
},
|
||||
evaluateValue: ({ node, pinId, sanitizeIdentifier }) => {
|
||||
switch (pinId) {
|
||||
case "item": {
|
||||
const name = sanitizeIdentifier(
|
||||
String(node.properties.itemName ?? "item")
|
||||
);
|
||||
return name || "item";
|
||||
}
|
||||
case "index": {
|
||||
const name = sanitizeIdentifier(
|
||||
String(node.properties.indexName ?? "i")
|
||||
);
|
||||
return name || "i";
|
||||
}
|
||||
default:
|
||||
return "nil";
|
||||
}
|
||||
},
|
||||
}
|
||||
);
|
||||
34
scripts/nodes/library/table/index.js
Normal file
34
scripts/nodes/library/table/index.js
Normal file
@@ -0,0 +1,34 @@
|
||||
import { tableAddNode } from "./add.js";
|
||||
import { tableDelNode } from "./del.js";
|
||||
import { tableDeliNode } from "./deli.js";
|
||||
import { tableCountNode } from "./count.js";
|
||||
import { tableAllNode } from "./all.js";
|
||||
import { tableForeachNode } from "./foreach.js";
|
||||
import { tablePairsNode } from "./pairs.js";
|
||||
|
||||
/**
|
||||
* All table-related node modules backed by PICO-8 helpers.
|
||||
*/
|
||||
export const tableNodes = [
|
||||
tableAddNode,
|
||||
tableDelNode,
|
||||
tableDeliNode,
|
||||
tableCountNode,
|
||||
tableAllNode,
|
||||
tableForeachNode,
|
||||
tablePairsNode,
|
||||
];
|
||||
|
||||
/**
|
||||
* Checklist tracking table helper coverage from the PICO-8 manual.
|
||||
* @type {Array<{ function: string, nodeId: string, implemented: boolean }>}
|
||||
*/
|
||||
export const tableFunctionChecklist = [
|
||||
{ function: "ADD", nodeId: "table_add", implemented: true },
|
||||
{ function: "DEL", nodeId: "table_del", implemented: true },
|
||||
{ function: "DELI", nodeId: "table_deli", implemented: true },
|
||||
{ function: "COUNT", nodeId: "table_count", implemented: true },
|
||||
{ function: "ALL", nodeId: "table_all", implemented: true },
|
||||
{ function: "FOREACH", nodeId: "table_foreach", implemented: true },
|
||||
{ function: "PAIRS", nodeId: "table_pairs", implemented: true },
|
||||
];
|
||||
25
scripts/nodes/library/table/pairs.js
Normal file
25
scripts/nodes/library/table/pairs.js
Normal file
@@ -0,0 +1,25 @@
|
||||
import { createNodeModule } from "../../nodeTypes.js";
|
||||
|
||||
/**
|
||||
* Produces a key-value iterator using PICO-8's pairs helper.
|
||||
*/
|
||||
export const tablePairsNode = createNodeModule(
|
||||
{
|
||||
id: "table_pairs",
|
||||
title: "Pairs Iterator",
|
||||
category: "Tables",
|
||||
description: "Create an iterator yielding key-value pairs for a table.",
|
||||
searchTags: ["pairs", "table", "iterator", "loop"],
|
||||
inputs: [{ id: "table", name: "Table", direction: "input", kind: "table" }],
|
||||
outputs: [
|
||||
{ id: "iterator", name: "Iterator", direction: "output", kind: "any" },
|
||||
],
|
||||
properties: [],
|
||||
},
|
||||
{
|
||||
evaluateValue: ({ resolveValueInput }) => {
|
||||
const tableValue = resolveValueInput("table", "{}");
|
||||
return `pairs(${tableValue})`;
|
||||
},
|
||||
}
|
||||
);
|
||||
Reference in New Issue
Block a user