Skip to content

Instantly share code, notes, and snippets.

@pramoth
Created March 3, 2019 11:42
Show Gist options
  • Select an option

  • Save pramoth/7379fd892b96970ead949877ed4b6b6a to your computer and use it in GitHub Desktop.

Select an option

Save pramoth/7379fd892b96970ead949877ed4b6b6a to your computer and use it in GitHub Desktop.
export class Stack<T> {
items: T[] = []
index = 0
push(value: T) {
this.items[this.index++] = value
}
pop(): T {
return this.items[--this.index]
}
empty(): boolean {
return this.index == 0
}
}
export class Opcode {
static ICONST_0 = (satckFrame: StackFrame): void => {
satckFrame.operandStack.push(0)
satckFrame.pc++
}
static ICONST_1 = (satckFrame: StackFrame): void => {
satckFrame.operandStack.push(1)
satckFrame.pc++
}
static ICONST_2 = (satckFrame: StackFrame): void => {
satckFrame.operandStack.push(2)
satckFrame.pc++
}
static IADD = (satckFrame: StackFrame): void => {
let result = satckFrame.operandStack.pop() + satckFrame.operandStack.pop()
satckFrame.operandStack.push(result)
satckFrame.pc++
}
static IMUL = (satckFrame: StackFrame): void => {
let result = satckFrame.operandStack.pop() * satckFrame.operandStack.pop()
satckFrame.operandStack.push(result)
satckFrame.pc++
}
static PRINT = (satckFrame: StackFrame): void => {
let operand = satckFrame.operandStack.pop()
console.log(operand)
satckFrame.pc++
}
static RETURN = (satckFrame: StackFrame): void => {
satckFrame.pc++
}
}
export class StackFrame {
pc: number = 0
operandStack: Stack<any> = new Stack()
constructor(private localVariables: any[], private instructions: any[]) {
}
run() {
while (this.instructions[this.pc] != Opcode.RETURN) {
this.instructions[this.pc].call(undefined, this)
}
}
}
export class Thread {
private stackFrame: Stack<StackFrame> = new Stack<StackFrame>()
constructor() {
let bytecode = [
Opcode.ICONST_1,
Opcode.ICONST_2,
Opcode.IMUL,
Opcode.ICONST_0,
Opcode.IADD,
Opcode.PRINT,
Opcode.RETURN]
this.stackFrame.push(new StackFrame([], bytecode))
}
run(): void {
while (!this.stackFrame.empty()) {
let stackFrame = this.stackFrame.pop()
stackFrame.run()
}
}
}
export class VerySimpleVM {
start(): void {
new Thread().run();
}
}
let vm = new VerySimpleVM();
vm.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment