Dinamically execute instructions on code samples

This commit is contained in:
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,
} }
@ -146,12 +146,15 @@ const codeHtml = highlightCode(trimAndDedent(code));
const len = data.inst.length; const len = data.inst.length;
const ip = data.ip; const ip = data.ip;
data.ip += 1; data.ip += 1;
const instructionArr = data.inst[ip]!; const instructions = data.inst[ip]!;
switch (instructionArr[0]) { for (const instructionSet of instructions) {
case "line": { const instructionArr = instructionSet;
data.line = Number(instructionArr[1]); switch (instructionArr[0]) {
break; case "line": {
data.line = Number(instructionArr[1]);
break;
}
} }
} }

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>