Implement state

master
Araozu 2024-05-19 22:11:19 -05:00
parent 0861b053d8
commit 0d4aad83da
2 changed files with 22 additions and 19 deletions

View File

@ -106,6 +106,7 @@ const codeHtml = highlightCode(trimAndDedent(code));
ip: 0, ip: 0,
inst: ${steps}, inst: ${steps},
done: false, done: false,
state: {},
}`} }`}
> >
<span class="inline-block bg-[var(--code-theme-bg-acolor)] px-2 rounded-tl rounded-tr font-mono text-sm">thp code</span> <span class="inline-block bg-[var(--code-theme-bg-acolor)] px-2 rounded-tl rounded-tr font-mono text-sm">thp code</span>
@ -120,7 +121,9 @@ const codeHtml = highlightCode(trimAndDedent(code));
<div> <div>
<div class="p-1 border-b border-white">state</div> <div class="p-1 border-b border-white">state</div>
<div class="h-24 p-1"> <div class="h-24 p-1">
x = 20 <template x-for="(value, key) in state">
<div x-text="key + ' = ' + value"></div>
</template>
</div> </div>
</div> </div>
</div> </div>
@ -135,13 +138,14 @@ const codeHtml = highlightCode(trimAndDedent(code));
</div> </div>
<script> <script>
type Instruction = "line" | "out" ; type Instruction = "line" | "out" | "set";
type AlpineState = { type AlpineState = {
line: number, line: number,
stdout: string, stdout: string,
ip: number, ip: number,
inst: Array<Array<[Instruction, number]>> inst: Array<Array<[Instruction, string, string | undefined]>>
done: boolean, done: boolean,
state: {[key: string]: string},
} }
/// Executes the instruction following the state of the machine. /// Executes the instruction following the state of the machine.
@ -162,11 +166,14 @@ const codeHtml = highlightCode(trimAndDedent(code));
data.stdout += String(instructionArr[1]) data.stdout += String(instructionArr[1])
break; break;
} }
case "set": {
data.state[String(instructionArr[1])] = String(instructionArr[2]);
break;
}
} }
} }
if (data.ip >= len) { if (data.ip >= len) {
console.log("done");
data.done = true; data.done = true;
return; return;
} }
@ -179,6 +186,7 @@ const codeHtml = highlightCode(trimAndDedent(code));
data.stdout = ""; data.stdout = "";
data.ip = 0; data.ip = 0;
data.done = false; data.done = false;
data.state = {};
} }
// @ts-ignore // @ts-ignore
window.alpineReset = alpineReset; window.alpineReset = alpineReset;

View File

@ -16,32 +16,27 @@ Then run `thp hello.thp`
## Try ## Try
```thp
val x = 322
val y = 322
fun f(Int x, Int y) {
print("gaaaa {x} {y}")
}
f(x, y)
```
<InteractiveCode <InteractiveCode
code={` code={`
val x = 322 val x = 322
var y = 322 var y = 322
fun f(Int x, Int y) { fun f(Int a, Int b) {
print("gaaaa {x} {y}") print("hello {a} {b}")
} }
f(x, y) f(x, y)
`} `}
steps={`[ steps={`[
[["line", 1]], [["line", 1]],
[["line", 2]], [
[["line", 8]], ["line", 2],
["set", "x", "322"]
],
[
["line", 8],
["set", "y", "322"]
],
[["line", 4]], [["line", 4]],
[["line", 5]], [["line", 5]],
[ [