!include #1003139 // Assembly Machine sclass HighLevel { Grid g; int pc, startAddress; new Map vars; *(Grid *g, int *pc) { startAddress = pc; } *(int *pc) { startAddress = pc; g = new Grid; } void emit(int... blubb) { g.set(pc, blubb); pc += l(blubb); } int newvar() { emit(op_jump_rel, 3); int addr = pc; emit(0); ret addr; } int getvar(S name) { Int bla = vars.get(name); if (bla == null) vars.put(name, bla = newvar()); ret bla; } // for (; i < to; i++) (to = im value) class ForIm { int var, loop, patch; *(int *var, int to) { loop = pc; emit(op_less_im, var, to); patch = patch(op_branchifnot); } void end() { emit(op_add_im, var, 1); emit(op_jump, loop); g.set(patch, pc); } } int patch(int opcode) { emit(opcode, 0); ret pc-1; } int[] getProgram() { ret g.getRange(startAddress, g.lastNonZero()+1); } void set(int x, S val) { set(str(x), val); } int getaddr() { } void set(S x, S val) { if (isInteger(val)) emit(op_set, parseInt(val), if (isInteger(x)) ?parseInt(x)); else emit(op_set, parseInt(val), getvar(x)); else if (isIdentifier(val)) if (isInteger(x)) emit(op_copy, getvar(val), x); else fail(val); } void swap(S a, S b) { assertTrue(isIdentifier(a) && isIdentifier(b)); set("temp", a); set(a, b); set(b, "temp"); } }