Dinamically execute instructions on code samples

master
Araozu 2024-05-19 21:53:34 -05:00
parent 525e4f5197
commit 3fd4b719b9
2 changed files with 13 additions and 31 deletions

View File

@ -1,6 +1,6 @@
--- ---
import { lex } from "../lexer/lexer"; import { lex } from "../lexer/lexer";
const {code} = Astro.props; const {code, steps} = Astro.props;
/** /**
* Performs the following: * Performs the following:
@ -104,7 +104,7 @@ const codeHtml = highlightCode(trimAndDedent(code));
line: 0, line: 0,
stdout: "", stdout: "",
ip: 0, ip: 0,
inst: [["line", 5]], inst: ${steps},
done: false, done: false,
}`} }`}
> >
@ -137,7 +137,7 @@ const codeHtml = highlightCode(trimAndDedent(code));
line: number, line: number,
stdout: string, stdout: string,
ip: number, ip: number,
inst: Array<[Instruction, number]> inst: Array<Array<[Instruction, number]>>
done: boolean, done: boolean,
} }
@ -147,13 +147,16 @@ const codeHtml = highlightCode(trimAndDedent(code));
const ip = data.ip; const ip = data.ip;
data.ip += 1; data.ip += 1;
const instructionArr = data.inst[ip]!; const instructions = data.inst[ip]!;
for (const instructionSet of instructions) {
const instructionArr = instructionSet;
switch (instructionArr[0]) { switch (instructionArr[0]) {
case "line": { case "line": {
data.line = Number(instructionArr[1]); data.line = Number(instructionArr[1]);
break; break;
} }
} }
}
if (data.ip >= len) { if (data.ip >= len) {
console.log("done"); console.log("done");

View File

@ -38,26 +38,5 @@ f(x, y)
f(x, y) f(x, y)
`} `}
steps={` steps={`[ [["line", 1]], [["line", 2]] ]`}
step {
line 1
}
step {
line 1
set x 322
}
step {
line 2
set y 322
}
step {
line 8
}
step {
line 4
push f
set x 322
set y 322
}
`}
></InteractiveCode> ></InteractiveCode>