Skip to content

Commit

Permalink
Print to Output window
Browse files Browse the repository at this point in the history
  • Loading branch information
mtalyat committed Oct 13, 2022
1 parent 7a52713 commit 60ce197
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 80 deletions.
5 changes: 3 additions & 2 deletions src/CoreInstruction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { format } from "path";
import { InstructionMnemonic } from "./InstructionMnemonic";
import { Line } from "./Line";
import { Output } from "./Output";
import { Parser } from "./Parser";

export class CoreInstruction {
Expand Down Expand Up @@ -159,7 +160,7 @@ export class CoreInstruction {

// if undefined, the argument is invalid
if (arg === undefined) {
console.log(`Invalid argument "${argStr}" from ${i} to ${i + lineIndex} on line: ${line.toString()}`);
Output.error(`Invalid argument "${argStr}" from ${i} to ${i + lineIndex} on line: ${line.toString()}`);
return undefined;
}

Expand All @@ -178,7 +179,7 @@ export class CoreInstruction {
splitIndex++;
} else {
// if not found, the syntax is not correct
console.log(`Invalid syntax, could not find "${split}" at ${i}, should follow "${this._argsFormat}" on line: ${line.toString()}`);
Output.error(`Invalid syntax, could not find "${split}" at ${i}, should follow "${this._argsFormat}" on line: ${line.toString()}`);
return undefined;
}
}
Expand Down
78 changes: 3 additions & 75 deletions src/Instruction.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { CoreInstruction } from "./CoreInstruction";
import { InstructionMnemonic } from "./InstructionMnemonic";
import { Line } from "./Line";
import { Output } from "./Output";
import { PackedNumber } from "./PackedNumber";
import { Simulation } from "./Simulation";
import { Parser } from './Parser';

export class Instruction {

Expand All @@ -26,88 +26,16 @@ export class Instruction {
}
}

// private getArgValues(index: number, core: CoreInstruction, line: Line): number[] {
// //split up
// let splitFormatting = core.getArgsFormat().split('X');
// if (splitFormatting.length >= 3) {
// splitFormatting = splitFormatting.splice(1, splitFormatting.length - 2); // remove beginning and end
// }

// // even indices are the numbers
// // odd indices are the splits

// let rawArgs = line.getArgs();

// // check if the raw arguments follows the given formatting
// let typeIndex = 0;
// let splitIndex = 1;
// let j: number;
// let inside: string;
// let value: number | null;

// let args: number[] = new Array();

// for (let i = 0; i < rawArgs.length; i++, typeIndex += 2, splitIndex += 2) {
// // find next split location
// if (splitIndex >= splitFormatting.length) {
// // if out of things to split by, go to end
// j = rawArgs.length;
// } else {
// //console.log(`Splitting by: ${splitFormatting[splitIndex]} for ${core.argTypes[parseInt(splitFormatting[typeIndex])]}`);
// // split by next thing to split by
// j = rawArgs.indexOf(splitFormatting[splitIndex], i);
// }

// // get the inside
// inside = rawArgs.substring(i, j).trim();

// // parse based on type
// switch (core.getArgTypes()[parseInt(splitFormatting[typeIndex])]) {
// case 'r': // register
// value = Parser.parseRegister(inside);
// break;
// case 'i': // immediate (number)
// value = Parser.parseImmediate(inside);
// break;
// case 'l': // label
// value = Parser.parseLabel(inside, index);
// break;
// default:
// value = -1;
// break;
// }

// // did we get an invalid result?
// if (value !== null) {
// args.push(value);
// } else {
// args.push(0);
// console.log(`Invalid parse: "${inside}" of type ${core.getArgTypes()[parseInt(splitFormatting[typeIndex])]}`);
// }

// // catch i up to j
// if (splitIndex >= splitFormatting.length) {
// // end of loop
// break;
// } else {
// i = j + splitFormatting[splitIndex].length - 1;
// }
// }

// return args;
// }

protected setCodes(core: CoreInstruction, values: number[]): void {
//console.log(`${core.getMnemonic()}: ${values.join(", ")};`);
this._code.setNumber(0);
}

public execute(sim: Simulation): void {
console.log(`Nothing executed for ${this.toString()}.`);
Output.error(`Nothing executed for ${this.toString()}.`);
}

protected fail(): void {
console.log(`Failed to execute ${this._mnemonic} in ${this.constructor.name}`);
Output.error(`Failed to execute ${this._mnemonic} in ${this.constructor.name}`);
}

public getCode(): number {
Expand Down
38 changes: 38 additions & 0 deletions src/Output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as vscode from 'vscode';

export class Output {
private static _outputChannel: vscode.OutputChannel | null = null;

public static getOutputChannel(): vscode.OutputChannel {
if (this._outputChannel === null) {
this._outputChannel = vscode.window.createOutputChannel("LEGv8 Day");
console.log("Created Output.");
}

return this._outputChannel;
}

public static clear(): void {
this.getOutputChannel().clear();
}

public static error(text: string): void {
this.writeLine("ERROR: " + text);
}

public static write(text: string): void {
this.getOutputChannel().append(text);
}

public static writeLine(text: string): void {
this.getOutputChannel().appendLine(text);
}

public static show(): void {
this.getOutputChannel().show();
}

public static hide(): void {
this.getOutputChannel().hide();
}
}
5 changes: 3 additions & 2 deletions src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { IMInstruction } from './IMInstruction';
import { RInstruction } from './RInstruction';
import { ZInstruction } from './ZInstruction';
import { DInstruction } from './DInstruction';
import { Output } from './Output';

export class Parser {
static readonly identifierComments: string = "//";
Expand Down Expand Up @@ -66,7 +67,7 @@ export class Parser {
if (args.length === 7) {
return new CoreInstruction(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
} else {
console.log(`ERR: Invalid arguments from line '${args.join("...")}' (${args.length})`);
Output.error(`Invalid arguments from line '${args.join("...")}' (${args.length})`);
return null;
}
}
Expand Down Expand Up @@ -107,7 +108,7 @@ export class Parser {
return new Instruction(core, line);
}
} else {
console.log(`Unrecognized: ${line.toString()}`);
Output.error(`Unrecognized: ${line.toString()}`);
}

return null;
Expand Down
4 changes: 3 additions & 1 deletion src/Simulation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { exec } from "child_process";
import { off } from "process";
import { Instruction } from "./Instruction";
import { Output } from "./Output";
import { PackedNumber } from "./PackedNumber";
import { Parser } from "./Parser";
import { Stopwatch } from "./Stopwatch";
Expand Down Expand Up @@ -279,7 +280,8 @@ export class Simulation {

// prints the text to the screen
public output(text: string): void {
console.log(text);
//console.log(text);
Output.writeLine(text);
}

private format(text: string): string {
Expand Down
5 changes: 5 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from 'vscode-languageclient/node';
import { CoreInstruction } from './CoreInstruction';
import { Simulation } from './Simulation';
import { Output } from './Output';

let client: LanguageClient;

Expand All @@ -25,6 +26,10 @@ export function activate(context: vscode.ExtensionContext) {
const editor = vscode.window.activeTextEditor;

if (editor) {
Output.show();

Output.write("Test");

const document = editor.document;
const start = editor.selection.start;
const end = editor.selection.end;
Expand Down

0 comments on commit 60ce197

Please sign in to comment.