1
0
Fork 0

read registers from program

This commit is contained in:
Sean Sube 2022-06-20 22:45:53 -05:00
parent eacb5a10f6
commit b320f0dbe6
5 changed files with 52 additions and 14 deletions

View File

@ -6,6 +6,8 @@
"author": "Sean Sube <seansube@gmail.com>",
"license": "MIT",
"dependencies": {
"@types/pegjs": "^0.10.3",
"pegjs": "^0.10.0",
"typescript": "^4.7.4"
},
"devDependencies": {

View File

@ -1,3 +1,8 @@
R1 0
R2 0
R3 0
R4 10
; program
add R1, R1, 5
push R1
cmp R1, R4

View File

@ -1,10 +1,10 @@
// @ts-ignore
import parser from './parser.cjs';
const { parse } = parser;
import { join } from 'node:path';
import { argv } from 'node:process';
import { readFile } from 'node:fs/promises';
import pegjs, { Parser } from 'pegjs';
const { generate } = pegjs;
type Address = number;
type Counter = number;
type Register = number; // int32, specifically
@ -91,6 +91,7 @@ interface GlobalEntry {
interface ProgramModule {
functions: Table<FunctionEntry>;
globals: Table<GlobalEntry>;
registers: Partial<ProgramRegisters>;
}
interface ProgramStack {
@ -106,24 +107,30 @@ interface Program {
stack: ProgramStack;
}
interface ParserOutput {
instructions: Array<Instruction>;
registers: Partial<ProgramRegisters>;
}
/**
* Load a module from compiled source.
*/
function load(data: string): ProgramModule {
const ast = parse(data) as Array<Instruction>;
function load(data: string, parser: Parser): ProgramModule {
const ast = parser.parse(data) as ParserOutput;
console.log(ast);
return {
functions: {
names: [['main', 0]],
entries: [{
instructions: ast,
instructions: ast.instructions,
}],
},
globals: {
names: [],
entries: [],
},
registers: ast.registers,
};
}
@ -138,9 +145,10 @@ function prep(runtime: ProgramModule, program: ProgramModule): Program {
EH: 0,
IH: 0,
R1: 0,
R2: 2, // increment by
R2: 0,
R3: 0,
R4: 10, // increment until
R4: 0,
...program.registers,
},
runtime,
program,
@ -241,8 +249,7 @@ function stepInstruction(instruction: Instruction, registers: ProgramRegisters,
/**
* Step the program until the function has been exhausted.
*/
function runProgram(data: string): ProgramStack {
const main = load(data);
function runProgram(main: ProgramModule): ProgramStack {
const state = prep(main, main);
console.log('start');
while (state.registers.PC < state.program.functions.entries[0].instructions.length) {
@ -253,14 +260,20 @@ function runProgram(data: string): ProgramStack {
}
async function main() {
const grammar = await readFile(join(process.cwd(), 'src', 'parser.peg'), {
encoding: 'utf-8',
});
const parser = generate(grammar);
const [_node, _script, sourcePath] = argv;
console.log('loading', sourcePath);
const source = await readFile(sourcePath, {
encoding: 'utf-8',
});
const main = load(source, parser);
const stack = runProgram(source);
const stack = runProgram(main);
console.log('stack', stack);
const other = `add R1, R1, 5

View File

@ -1,8 +1,16 @@
Program
= Instruction+
= registers:Registers instructions:Instruction+ {
return {
instructions: instructions.filter((it) => it !== null),
registers,
};
}
Registers = entries:(key:Register _ value:Constant Break { return [ key, value ]; })* { return Object.fromEntries(entries); }
Instruction
= FreeInstruction
= Comment Break { return null; }
/ FreeInstruction
/ SingleInstruction
/ DoubleInstruction
/ TripleInstruction

View File

@ -24,6 +24,11 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.0.0.tgz#67c7b724e1bcdd7a8821ce0d5ee184d3b4dd525a"
integrity sha512-cHlGmko4gWLVI27cGJntjs/Sj8th9aYwplmZFwmmgYQQvL5NUsgVJG7OddLvNfLqYS31KFN0s3qlaD9qCaxACA==
"@types/pegjs@^0.10.3":
version "0.10.3"
resolved "https://registry.yarnpkg.com/@types/pegjs/-/pegjs-0.10.3.tgz#9e254036c6bf2254cd98caec447a1d79b6607bff"
integrity sha512-C/ZkUNe7HONOaDHXfNTZOUzrOvOgrWdrJj1JZ3QTEPi5gOIygcjCpXyxpdJTKVvWFzbobajRbMbQY8d0WrZ6fg==
"@types/sinon-chai@^3.2.8":
version "3.2.8"
resolved "https://registry.yarnpkg.com/@types/sinon-chai/-/sinon-chai-3.2.8.tgz#5871d09ab50d671d8e6dd72e9073f8e738ac61dc"
@ -433,6 +438,11 @@ path-is-absolute@^1.0.0:
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
integrity sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==
pegjs@^0.10.0:
version "0.10.0"
resolved "https://registry.yarnpkg.com/pegjs/-/pegjs-0.10.0.tgz#cf8bafae6eddff4b5a7efb185269eaaf4610ddbd"
integrity sha512-qI5+oFNEGi3L5HAxDwN2LA4Gg7irF70Zs25edhjld9QemOgp0CbvMtbFcMvFtEo1OityPrcCzkQFB8JP/hxgow==
picomatch@^2.0.4, picomatch@^2.2.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42"