Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
taisii committed Feb 26, 2024
1 parent 0a9e8c6 commit 460140e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 51 deletions.
67 changes: 35 additions & 32 deletions src/app/lib/solveProblem.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { CharacterVariable, History, SolveProblemResult, Variable } from '../../types';

import { Board as BoardClass } from './Board';
import type { Character } from './Character';
import { Character as CharacterClass } from './Character';

export function parseProgram(program: string): string[] {
Expand Down Expand Up @@ -28,6 +29,7 @@ export function instrumentCode(code: string): string {
const assignmentMatch = line.match(assignmentRegex);
if (assignmentMatch) {
const variableName = assignmentMatch[1].trim();

modifiedCodeLine = modifiedCodeLine.replace(sidRegex, `log(${sid}, '${variableName}', ${variableName});`);
}
}
Expand All @@ -39,10 +41,10 @@ export function instrumentCode(code: string): string {
type Trace = {
sid: number;
variableName: string;
variableValue: string;
variableValue: Character | string;
};

export async function traceCode(code: string): Promise<void> {
export function traceCode(code: string): Trace[] {
const Character = CharacterClass; // eslint-disable-line
const Board = BoardClass; // eslint-disable-line
const codeForTracing = `
Expand All @@ -64,28 +66,31 @@ function log(sid, variableName, variableValue) {
JSON.stringify(traceList);
`.replaceAll(/\s+/g, ' ');
const ret: Trace[] = JSON.parse(eval(codeForTracing));
console.log('variableList', traceToVariablesList(ret));
return ret;
}

function traceToVariablesList(traceList: Trace[]): Variable[][] {
console.log(traceList);
function traceToVariablesList(traceList: Trace[]): (CharacterVariable | Variable)[][] {
const lineCount = traceList.at(-1)?.sid ?? 0;
const variablesList: Variable[][] = [];
console.log('variableList', variablesList);
const variablesList: (CharacterVariable | Variable)[][] = [];
let index = 0;
for (let i = 0; i < lineCount; i++) {
const variables = variablesList.at(-1) ?? ([] as Variable[]);
const variables = variablesList.at(-1) ?? ([] as (CharacterVariable | Variable)[]);
while (traceList[index].sid <= i) {
const { variableName, variableValue } = traceList[index];
const variablesIndex = variables.findIndex((variable) => variable.name === variableName);
if (variablesIndex === -1) {
variables.push({ name: variableName, value: variableValue });
if (variableValue instanceof CharacterClass) {
console.log('character');
variables.push({ name: variableName, value: variableValue } as CharacterVariable);
} else {
variables.push({ name: variableName, value: variableValue } as Variable);
}
} else {
variables[variablesIndex].value = variableValue;
}
index++;
}
variablesList.push(variables);
variablesList.push([...variables]);
}
return variablesList;
}
Expand Down Expand Up @@ -132,38 +137,36 @@ export function extractVariableNames(command: string): string[] {
}

export function solveProblem(program: string): SolveProblemResult {
const commands = parseProgram(program);
const board = new BoardClass();
const histories: History[] = [{ step: 0, characterVariables: [], board, otherVariables: [] }];

for (let i = 0; i < commands.length; i++) {
if (i < commands.length) {
let mergedCommand = '';
const traceList = traceCode(instrumentCode(program));
const variablesList = traceToVariablesList(traceList);

for (let j = 0; j <= i; j++) {
mergedCommand += commands[j];
}
for (const variables of variablesList) {
console.log('variables', variables);
const characterVariables = selectCharacterVariables(variables);
const otherVariables = selectOtherVariables(variables);
console.log('characterVariables', characterVariables);
console.log('otherVariables', otherVariables);

const variables = executeEval(mergedCommand);
const characterVariables = selectCharacterVariables(variables);
const otherVariables = selectOtherVariables(variables);
const board = new BoardClass();
for (const history of histories) {
if (!history.characterVariables) continue;

const board = new BoardClass();
for (const history of histories) {
if (!history.characterVariables) continue;

for (const character of history.characterVariables) {
board.updateGrid(character.value);
}
for (const character of history.characterVariables) {
board.updateGrid(character.value);
}
for (const character of characterVariables) {
board.updateGrid(character.value as CharacterClass);
}

histories.push({ step: histories.length + 1, characterVariables, board, otherVariables });
}
for (const character of characterVariables) {
board.updateGrid(character.value as CharacterClass);
}

histories.push({ step: histories.length + 1, characterVariables, board, otherVariables });
}

console.log(histories);

const result: SolveProblemResult = {
characterVariables: histories?.at(-1)?.characterVariables,
otherVariables: histories?.at(-1)?.otherVariables,
Expand Down
22 changes: 3 additions & 19 deletions src/problems/problemData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { instrumentCode, traceCode } from '../app/lib/solveProblem';

export const courseIds = ['tuBeginner1', 'tuBeginner2'];
export type CourseId = (typeof courseIds)[number];

Expand Down Expand Up @@ -38,7 +36,8 @@ export const courseIdToProgramIdLists: Record<CourseId, ProgramId[][]> = {

export function generateProgram(programId: ProgramId, languageId: LanguageId): string {
// TODO(exKAZUu): 問題IDに紐づくプログラム(テンプレート)を取得して、乱数を使って具体的なプログラムを生成する。
const code = `const bear = new Character(); /* 1 */
return (
`const bear = new Character(); /* 1 */
bear.moveForward(); /* 2 */
bear.turnLeft(); /* 3 */
bear.upPen(); /* 4 */
Expand All @@ -50,22 +49,7 @@ const foo = 'あいうえお'; /* 9 */
var bar = 123; /* 10 */
i = i + 1; /* 11 */
turtle.moveForward(); /* 12 */
turtle.moveForward(); /* 13 */`;
traceCode(instrumentCode(code));
return (
`const bear = new Character();
bear.moveForward();
bear.turnLeft();
bear.upPen();
let i = 0;
bear.moveForward();
const turtle = new Character({x: 3, y: 1, color: 'green'});
turtle.moveForward();
const foo = 'あいうえお';
var bar = 123;
i = i + 1;
turtle.moveForward();
turtle.moveForward();` || programIdToLanguageIdToProgram[programId][languageId]
turtle.moveForward(); /* 13 */` || programIdToLanguageIdToProgram[programId][languageId]
);
}

Expand Down

0 comments on commit 460140e

Please sign in to comment.