import { encodePosition } from "https://esm.town/v/beneskildsen/encodePosition";
import { getNextDir } from "https://esm.town/v/beneskildsen/getNextDir";
import { config } from "https://esm.town/v/beneskildsen/config";
export const genGrid = (state) => {
const locStack = [];
let loc = { ...state.initialPosition };
let gridMap = {};
let dir = "UP";
let i = 0;
for (let c of state.grammar) {
switch (c) {
case "[":
locStack.push({ ...loc });
break;
case "]":
loc = locStack.pop();
break;
case "^":
dir = "UP";
loc.y -= 1;
break;
case "V":
dir = "DOWN";
loc.y += 1;
break;
case "<":
dir = "LEFT";
loc.x -= 1;
break;
case ">":
dir = "RIGHT";
loc.x += 1;
break;
default:
const symbol = config.symbols[c];
let nextDir = getNextDir(state.grammar, i);
if (symbol) {
gridMap[encodePosition(loc)] = {
index: i,
dir,
nextDir,
symbol: c,
};
}
}
i++;
}
return gridMap;
};