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

82
LINES

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

JavaX fragment (include)

1  
!include #1003139 // Assembly Machine
2  
3  
sclass HighLevel {
4  
  Grid g;
5  
  int pc, startAddress;
6  
  new Map<S, Int> vars;
7  
  
8  
  *(Grid *g, int *pc) { startAddress = pc; }
9  
  *(int *pc) { startAddress = pc; g = new Grid; }
10  
  
11  
  void emit(int... blubb) {
12  
    g.set(pc, blubb);
13  
    pc += l(blubb);
14  
  }
15  
  
16  
  int newvar() {
17  
    emit(op_jump_rel, 3);
18  
    int addr = pc;
19  
    emit(0);
20  
    ret addr;
21  
  }
22  
  
23  
  int getvar(S name) {
24  
    Int bla = vars.get(name);
25  
    if (bla == null)
26  
      vars.put(name, bla = newvar());
27  
    ret bla;
28  
  }
29  
  
30  
  // for (; i < to; i++) (to = im value)
31  
  class ForIm {
32  
    int var, loop, patch;
33  
    
34  
    *(int *var, int to) {
35  
      loop = pc;
36  
      emit(op_less_im, var, to);
37  
      patch = patch(op_branchifnot);
38  
    }
39  
  
40  
    void end() {
41  
      emit(op_add_im, var, 1);
42  
      emit(op_jump, loop);
43  
      g.set(patch, pc);
44  
    }
45  
  }
46  
  
47  
  int patch(int opcode) {
48  
    emit(opcode, 0);
49  
    ret pc-1;
50  
  }
51  
  
52  
  int[] getProgram() {
53  
    ret g.getRange(startAddress, g.lastNonZero()+1);
54  
  }
55  
  
56  
  void set(int x, S val) {
57  
    set(str(x), val);
58  
  }
59  
  
60  
  int getaddr() {
61  
  }
62  
  
63  
  void set(S x, S val) {
64  
    if (isInteger(val))
65  
      
66  
        emit(op_set, parseInt(val), if (isInteger(x)) ?parseInt(x));
67  
      else
68  
        emit(op_set, parseInt(val), getvar(x));
69  
    else if (isIdentifier(val))
70  
      if (isInteger(x))
71  
        emit(op_copy, getvar(val), x);
72  
    else
73  
      fail(val);
74  
  }
75  
  
76  
  void swap(S a, S b) {
77  
    assertTrue(isIdentifier(a) && isIdentifier(b));
78  
    set("temp", a);
79  
    set(a, b);
80  
    set(b, "temp");
81  
  }
82  
}

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: 558 / 942
Referenced in: [show references]