Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

82
LINES

< > BotCompany Repo | #1003146 // HighLevel (include)

JavaX fragment (include)

!include #1003139 // Assembly Machine

sclass HighLevel {
  Grid g;
  int pc, startAddress;
  new Map<S, Int> 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");
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003146
Snippet name: HighLevel (include)
Eternal ID of this version: #1003146/1
Text MD5: adca1fd0f44ff1087c5d9f3399f33eba
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-05-15 18:39:08
Source code size: 1605 bytes / 82 lines
Pitched / IR pitched: No / No
Views / Downloads: 556 / 939
Referenced in: [show references]