Files
bf-server/client/src/BranFlakesExecutorVisitor.ts

147 lines
4.1 KiB
TypeScript
Raw Normal View History

2023-12-15 14:27:11 +05:30
import { AbstractParseTreeVisitor } from 'antlr4ts/tree/AbstractParseTreeVisitor';
import { LoopStmtContext } from './generated/bfParser';
import { bfVisitor } from './generated/bfVisitor';
import { DiagnosticSeverity } from 'vscode-languageclient';
import { getTree } from './BranFlakesParseRunner';
2024-01-02 15:45:31 +05:30
import { RuleNode } from 'antlr4ts/tree/RuleNode';
import InputStrategy from './input/InputStrategy';
2023-12-15 14:27:11 +05:30
export default class BranFlakesExecutorVisitor
2024-01-02 15:45:31 +05:30
extends AbstractParseTreeVisitor<Promise<void>>
implements bfVisitor<Promise<void>>
{
/**
*
* @param input Input string
* @param inputPtr Input pointer to start from
*/
constructor(
protected inputStrategy: InputStrategy,
protected logger: (val: string) => Thenable<string>,
protected inputPtr: number = 0
) {
super();
}
// /**
// * The memory cells (Can work with negative cells this way)
// */
// protected cells: Map<number, number> = new Map();
2023-12-15 14:27:11 +05:30
2024-01-02 15:45:31 +05:30
protected byteArraySize: number = 30000;
protected byteArray: Int8Array = new Int8Array(this.byteArraySize);
/**
* Pointer
*/
protected ptr: number = 0;
/** Output string */
protected outputStrArray: string[] = [];
2023-12-15 14:27:11 +05:30
2024-01-02 15:45:31 +05:30
/**
* Output string (Available only after visiting)
*/
public get outputStr() {
return this.outputStrArray.join('');
}
2023-12-15 14:27:11 +05:30
2024-01-02 15:45:31 +05:30
defaultResult() {
return Promise.resolve();
}
/**
* Run a file
* @param text
* @param fn
* @param inputStrategy
* @returns
*/
static async run(
text: string,
fn: string,
inputStrategy: InputStrategy,
logger: (str:string) => Thenable<string>
) {
//get tree and issues
const { tree, issues } = getTree(text, fn);
2023-12-15 14:27:11 +05:30
2024-01-02 15:45:31 +05:30
//get only errors
const x = issues.filter(e => e.type === DiagnosticSeverity.Error);
//if any error, drop
if (x.length > 0) {
throw Error('Errors exist');
}
// make visitor
const vis = new BranFlakesExecutorVisitor(inputStrategy, logger);
//visit the tree
await vis.visit(tree);
2023-12-15 14:27:11 +05:30
2024-01-02 15:45:31 +05:30
//get output
return vis.outputStr;
}
2023-12-15 14:27:11 +05:30
2024-01-02 15:45:31 +05:30
getCell(pointerIndex: number) {
return this.byteArray[pointerIndex];
}
setCell(pointerIndex: number, value: number): void {
this.byteArray[pointerIndex] = value;
}
2023-12-15 14:27:11 +05:30
2024-01-02 15:45:31 +05:30
async visitLoopStmt(ctx: LoopStmtContext) {
while ((this.getCell(this.ptr) ?? 0) !== 0) {
await this.visitChildren(ctx);
}
}
async visitPtrLeft() {
--this.ptr;
}
async visitPtrRight() {
++this.ptr;
}
async visitPtrIncr() {
const val = this.getCell(this.ptr);
this.setCell(this.ptr, (val + 1) % 256);
}
async visitPtrDecr() {
const val = this.getCell(this.ptr);
this.setCell(this.ptr, (val + 255) % 256);
}
async visitOutputStmt() {
const val = this.getCell(this.ptr) ?? 0;
const str = String.fromCharCode(val);
2023-12-15 14:27:11 +05:30
2024-01-02 15:45:31 +05:30
this.outputStrArray.push(str);
}
2023-12-15 14:27:11 +05:30
2024-01-02 15:45:31 +05:30
async visitInputStmt() {
//get char
const char = await this.inputStrategy.getInput() ?? 0;
//increment the input pointer after this
this.inputPtr++;
this.setCell(this.ptr, char);
}
// override for maintaining async
async visitChildren(node: RuleNode): Promise<void> {
// await this.logger("checking "+node.constructor.name)
let result = this.defaultResult();
await result;
let n = node.childCount;
for (let i = 0; i < n; i++) {
if (!this.shouldVisitNextChild(node, result)) {
break;
}
let c = node.getChild(i);
let childResult = c.accept(this);
result = this.aggregateResult(result, childResult);
await result;
}
return Promise.resolve();
}
// override for maintaining async
protected async aggregateResult(
aggregate: Promise<void>,
nextResult: Promise<void>
): Promise<void> {
await aggregate;
return nextResult;
}
2023-12-15 14:27:11 +05:30
}