add addition l-program

This commit is contained in:
Elizabeth Hunt 2023-11-17 10:04:55 -07:00
parent 56bf487823
commit 7d897cb2b5
Signed by: simponic
GPG Key ID: 52B3774857EB24B1
2 changed files with 88 additions and 50 deletions

View File

@ -1,46 +1,82 @@
<!doctype html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>Liz's L-Program Compiler</title> <title>Liz's L-Program Compiler</title>
<link rel="stylesheet" type="text/css" href="codemirror/codemirror.css" /> <link rel="stylesheet" type="text/css" href=
<link rel="stylesheet" type="text/css" href="css/styles.css" /> "codemirror/codemirror.css">
</head> <link rel="stylesheet" type="text/css" href="css/styles.css">
<body> </head>
<div class="container"> <body>
<h1>Liz's L-Program Compiler</h1> <div class="container">
<p><i>Developed for Kulyukin's CS5000</i></p> <h1>Liz's L-Program Compiler</h1>
<hr /> <p><i>Developed for Kulyukin's CS5000</i></p>
<div class="source-container"> <hr>
<div class="textarea-container"> <div class="source-container">
<h3>L Source</h3> <div class="textarea-container">
<textarea id="instructions"> <h3>L Source</h3>
// asdf <textarea id=
[ B1 ] X1 <- X1 + 1 "instructions">// 0. primitive instructions only
IF X1 != 0 GOTO B1 // 1. labels match the regex [A-E](:digit:)+.
X1 <- X1 // 2. variables are initialized to zero
</textarea // and match (Y | (X|Z)(:digit:)+).
> // 3. instructions must be delimited by a newline.
<div> // 4. anything following the comment token "//" up to a
<button id="compile">Compile</button // newline are ignored.
><span style="margin-left: 0.5rem" id="compile_status"></span> // 5. by default, a computation that takes more than 500_000
</div> // "procedures", it will be halted.
// 6. the implicit exit label "E" exists; thus to exit
// prematurely, "GOTO E".
// 7. input your initial snapshot in the "variables" map
// on the Program in the compiled JS output
// 8. for a more detailed view on the grammar, it's at
// /godel/grammar.peg
// Y &lt;- X1
[ A1 ] IF X1 != 0 GOTO A2
GOTO B1
[ A2 ] X1 &lt;- X1 - 1
Y &lt;- Y + 1
GOTO A1
// Z1 &lt;- X2
[ B1 ] IF X2 != 0 GOTO B2
GOTO C1
[ B2 ] X2 &lt;- X2 - 1
Z1 &lt;- Z1 + 1
GOTO B1
// Y &lt;- Y + Z1
[ C1 ] IF Z1 != 0 GOTO C2
GOTO E
[ C2 ] Z1 &lt;- Z1 - 1
Y &lt;- Y + 1
GOTO C1</textarea>
<div>
<button id="compile">Compile</button>
</div> </div>
<div class="textarea-container"> <div>
<h3>Compiled JS</h3> <span style="margin-left: 0.5rem" id=
<textarea id="compiled"></textarea> "compile_status"></span>
<div> </div>
<button id="eval">Eval</button </div>
><span style="margin-left: 0.5rem" id="eval_status"></span> <div class="textarea-container">
</div> <h3>Compiled JS</h3>
<textarea id="compiled"></textarea>
<div>
<button id="eval">Eval</button>
</div>
<div>
<span style="margin-left: 0.5rem" id=
"eval_status"></span>
</div> </div>
</div> </div>
</div> </div>
<script src="codemirror/codemirror.js"></script> </div>
<script src="js-beautify/js-beautify.js"></script> <script src="codemirror/codemirror.js"></script>
<script src="js-beautify/js-beautify.js"></script>
<script src="js/observable.js"></script> <script src="js/observable.js"></script>
<script src="js/parser.js"></script> <script src="js/parser.js"></script>
<script src="js/compiler.js"></script> <script src="js/compiler.js"></script>
<script src="js/main.js"></script> <script src="js/main.js"></script>
</body> </body>
</html> </html>

View File

@ -65,7 +65,8 @@ const compile = (ast) => {
this.finalInstruction = ${ this.finalInstruction = ${
ast.instructions.length + 1 ast.instructions.length + 1
}; // instruction of the implied "exit" label }; // instruction of the implied "exit" label
this.labelInstructions.set("E", this.finalInstruction); // "E" is the exit label // "E" is the exit label
this.labelInstructions.set("E", this.finalInstruction);
} }
get(variable) { get(variable) {
@ -106,13 +107,14 @@ const compile = (ast) => {
return this.variables.get("Y"); return this.variables.get("Y");
} }
run(maxIter=50000) { run(maxIter=500_000) {
let iter = 0; let iter = 0;
while (!this.isCompleted() && (++iter) < maxIter) this.step(); while (!this.isCompleted() && (++iter) < maxIter) this.step();
if (iter < maxIter) { if (iter < maxIter) {
return this.getResult(); return this.getResult();
} }
throw new Error("Iterations went over maxIter=(" + maxIter + "). To resolve, please ask Turing how we can tell if a program will halt during compilation."); throw new Error("Too many iterations. To resolve, please ask"
+ " Turing how we can find if a program will halt during compilation.");
} }
main() { main() {
@ -124,10 +126,10 @@ const compile = (ast) => {
const instructionIdx = i + 1; const instructionIdx = i + 1;
if (line.label) { if (line.label) {
stringBuilder.add( stringBuilder.add(
`this.instructions.set(${instructionIdx}, () => this.${line.label}());\n`, `this.instructions.set(${instructionIdx}, () => this.${line.label}());\n`
); );
stringBuilder.add( stringBuilder.add(
`this.labelInstructions.set("${line.label}", ${instructionIdx});\n`, `this.labelInstructions.set("${line.label}", ${instructionIdx});\n`
); );
} }
} }
@ -138,7 +140,7 @@ const compile = (ast) => {
const instructionIdx = i + 1; const instructionIdx = i + 1;
if (instruction.label) { if (instruction.label) {
stringBuilder.add( stringBuilder.add(
` this.followGoto("${instruction.label}");\n}\n\n${instruction.label}() {\n`, ` this.followGoto("${instruction.label}");\n}\n\n${instruction.label}() {\n`
); );
stringBuilder.add(`this.instructionPointer = ${instructionIdx};\n`); stringBuilder.add(`this.instructionPointer = ${instructionIdx};\n`);
instruction = instruction.instruction; instruction = instruction.instruction;
@ -152,12 +154,12 @@ const compile = (ast) => {
stringBuilder.add("const program = new Program();\n\n"); stringBuilder.add("const program = new Program();\n\n");
stringBuilder.add("// set the initial Snapshot here\n"); stringBuilder.add("// set the initial Snapshot here\n");
stringBuilder.add('// program.variables.set("X1", 2);\n\n'); stringBuilder.add('// program.variables.set("X1", 2);\n\n');
stringBuilder.add( stringBuilder.add("program.run();\n");
"program.run(50_000); // 50_000 is the max iterations before throwing an exception\n", stringBuilder.add("console.log(program.variables);\n");
); stringBuilder.add("program.getResult();\n");
return js_beautify(stringBuilder.build(), { return js_beautify(stringBuilder.build(), {
indent_size: 2, indent_size: 2,
wrap_line_length: 120, wrap_line_length: 100,
}); });
}; };