1
0
Fork 0

split up module stuff

This commit is contained in:
Sean Sube 2022-06-23 16:46:05 -05:00
parent ca1514f0c7
commit aef57b44cf
15 changed files with 40 additions and 40 deletions

View File

@ -1,10 +1,10 @@
import { readFile } from 'node:fs/promises';
import { argv } from 'node:process';
import { codec as abstractCodec } from './abstract.js';
import { codec as assemblyCodec } from './assembly.js';
import { codec as binaryCodec } from './binary.js';
import { Codec } from './module.js';
import { codec as abstractCodec } from './module/abstract.js';
import { codec as assemblyCodec } from './module/assembly.js';
import { codec as binaryCodec } from './module/binary.js';
import { Codec } from './module/index.js';
import { runProgram } from './runtime.js';
const CODECS: Record<string, Codec> = {

View File

@ -1,13 +1,13 @@
import pegjs from 'pegjs';
import { FunctionNode, RawRegisters } from './ast.js';
import { loadFile } from './files.js';
import { Codec, ProgramModule } from './module.js';
import { FunctionNode, RawRegisters } from '../ast.js';
import { loadFile } from '../files.js';
import { Codec, ProgramModule } from './index.js';
const { generate } = pegjs;
export function loadGrammar() {
const grammar = loadFile('src', 'abstract.peg');
const grammar = loadFile('src', 'module', 'abstract.peg');
return generate(grammar);
}

View File

@ -1,13 +1,13 @@
import pegjs from 'pegjs';
import { CommandNode, RawRegisters } from './ast.js';
import { loadFile } from './files.js';
import { Codec, ProgramModule } from './module.js';
import { CommandNode, RawRegisters } from '../ast.js';
import { loadFile } from '../files.js';
import { Codec, ProgramModule } from './index.js';
const { generate } = pegjs;
export function loadGrammar() {
const grammar = loadFile('src', 'assembly.peg');
const grammar = loadFile('src', 'module', 'assembly.peg');
return generate(grammar);
}

View File

@ -1,4 +1,4 @@
import { Codec, ProgramModule } from './module.js';
import { Codec, ProgramModule } from './index.js';
export function binaryToAST(source: string): ProgramModule {
throw new Error('not implemented');

View File

@ -1,4 +1,4 @@
import { FunctionNode, ProgramRegisters, RawRegisters } from './ast';
import { FunctionNode, ProgramRegisters, RawRegisters } from '../ast';
export interface Table<TEntry> {
names: Array<[string, number]>;

View File

@ -1,7 +1,7 @@
import { Address, BranchNode, ProgramRegisters, RawRegisters, Register } from './ast.js';
import { Program, ProgramModule } from './module.js';
import { Address, ProgramRegisters, RawRegisters, Register } from './ast.js';
import { Program, ProgramModule } from './module/index.js';
import { RUNTIME_MODULE } from './runtime/module/base.js';
import { NODE_STEP } from './runtime/node.js';
import { step } from './runtime/node.js';
export function address(offset = 0): Address {
return {
@ -45,21 +45,6 @@ export function prep(runtime: ProgramModule, program: ProgramModule): Program {
};
}
/**
* Step the program and execute one command.
*/
export function step(state: Program, fn: BranchNode, pc_offset = 0): boolean {
const instruction = fn.body[state.registers.PC.offset - pc_offset];
console.log('step', instruction, state.registers);
const nodeFn = NODE_STEP[instruction.type];
if (nodeFn) {
return nodeFn(instruction, state);
} else {
throw new Error('unknown node');
}
}
/**
* Step the program until the function has been exhausted.
*/

View File

@ -1,4 +1,4 @@
import { ProgramModule } from '../../module.js';
import { ProgramModule } from '../../module/index.js';
export const RUNTIME_MODULE: ProgramModule = {
functions: {

View File

@ -1,5 +1,6 @@
import {
AssignmentNode,
BranchNode,
CommandNode,
ConditionalNode,
ConstantNode,
@ -9,8 +10,7 @@ import {
OpCode,
RegisterNode,
} from '../ast.js';
import { Program } from '../module.js';
import { step } from '../runtime.js';
import { Program } from '../module/index.js';
import { StepResult } from './opcode/base.js';
import { COMPARE_CODES } from './opcode/compare.js';
import { FLOW_CODES } from './opcode/flow.js';
@ -108,3 +108,18 @@ export function stepConstant(instruction: ConstantNode, _state: Program) {
console.log('constant', instruction);
return false;
}
/**
* Step the program and execute one command.
*/
export function step(state: Program, fn: BranchNode, pc_offset = 0): boolean {
const instruction = fn.body[state.registers.PC.offset - pc_offset];
console.log('step', instruction, state.registers);
const nodeFn = NODE_STEP[instruction.type];
if (nodeFn) {
return nodeFn(instruction, state);
} else {
throw new Error('unknown node');
}
}

View File

@ -1,5 +1,5 @@
import { CommandNode, ProgramRegisters } from '../../ast.js';
import { ProgramStack } from '../../module.js';
import { ProgramStack } from '../../module/index.js';
export interface StepResult {
stop: boolean;

View File

@ -1,5 +1,5 @@
import { CommandNode, OpCode, ProgramRegisters } from '../../ast.js';
import { ProgramStack } from '../../module.js';
import { ProgramStack } from '../../module/index.js';
import { getValue } from '../register.js';
import { OpCodeFn, StepResult } from './base.js';

View File

@ -1,5 +1,5 @@
import { CommandNode, OpCode, ProgramRegisters } from '../../ast.js';
import { ProgramStack } from '../../module.js';
import { ProgramStack } from '../../module/index.js';
import { RUNTIME_FUNCTIONS } from '../module/base.js';
import { getValue, setValue } from '../register.js';
import { OpCodeFn, StepResult } from './base.js';

View File

@ -1,5 +1,5 @@
import { CommandNode, OpCode, ProgramRegisters } from '../../ast.js';
import { ProgramStack } from '../../module.js';
import { ProgramStack } from '../../module/index.js';
import { getRegister, setValue, getValue } from '../register.js';
import { OpCodeFn, StepResult } from './base.js';

View File

@ -1,5 +1,5 @@
import { CommandNode, OpCode, ProgramRegisters } from '../../ast.js';
import { ProgramStack } from '../../module.js';
import { ProgramStack } from '../../module/index.js';
import { getRegister, setValue, getValue } from '../register.js';
import { OpCodeFn, StepResult } from './base.js';