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

51
LINES

< > BotCompany Repo | #1006735 // AI Assembler Intro

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (1044L/7K/25K).

!7

sclass Operation {
  S function;
  int[] registers;
  int outRegister = -1;
  
  *(S *function, int... regs) {
    registers = subArray(regs, 0, l(regs)-1);
    outRegister = last(regs);
  }
}

sclass Machine {
  O[] registers = new O[10];
  new TreeMap<S, O> functions;
  
  void function(S name, O implementation) {
    functions.put(name, implementation);
  }
  
  void set(int register, O value) {
    if (register >= l(registers))
      registers = resizeArray(registers, register+1);
    registers[register] = value;
  }
  
  O get(int register) {
    ret _get(registers, register);
  }
  
  void run(Operation op) {
    O f = functions.get(op.function);
    if (f == null) fail("Unknown operation: " + op.function);
    int n = l(op.registers);
    O[] arguments = new O[n];
    for i to n: arguments[i] = get(op.registers[i]);
    O result = callF(f, arguments);
    if (op.outRegister >= 0)
      set(op.outRegister, result);
  }
}

p {
  new Machine machine;
  machine.function("+", func(int a, int b) { a + b });
  machine.set(0, 5);
  machine.set(1, 10);
  machine.run(new Operation("+", 0, 1, 2));
  print(machine.get(2));
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1006735
Snippet name: AI Assembler Intro
Eternal ID of this version: #1006735/5
Text MD5: 3dacd858e4389510057665d9d1a4eb8f
Transpilation MD5: 25d92f3af91f747c0941448075cb7979
Author: stefan
Category: javax / a.i.
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-02-03 06:09:17
Source code size: 1193 bytes / 51 lines
Pitched / IR pitched: No / No
Views / Downloads: 562 / 625
Version history: 4 change(s)
Referenced in: [show references]