Libraryless. Click here for Pure Java version (1044L/7K/25K).
1 | !7 |
2 | |
3 | sclass Operation {
|
4 | S function; |
5 | int[] registers; |
6 | int outRegister = -1; |
7 | |
8 | *(S *function, int... regs) {
|
9 | registers = subArray(regs, 0, l(regs)-1); |
10 | outRegister = last(regs); |
11 | } |
12 | } |
13 | |
14 | sclass Machine {
|
15 | O[] registers = new O[10]; |
16 | new TreeMap<S, O> functions; |
17 | |
18 | void function(S name, O implementation) {
|
19 | functions.put(name, implementation); |
20 | } |
21 | |
22 | void set(int register, O value) {
|
23 | if (register >= l(registers)) |
24 | registers = resizeArray(registers, register+1); |
25 | registers[register] = value; |
26 | } |
27 | |
28 | O get(int register) {
|
29 | ret _get(registers, register); |
30 | } |
31 | |
32 | void run(Operation op) {
|
33 | O f = functions.get(op.function); |
34 | if (f == null) fail("Unknown operation: " + op.function);
|
35 | int n = l(op.registers); |
36 | O[] arguments = new O[n]; |
37 | for i to n: arguments[i] = get(op.registers[i]); |
38 | O result = callF(f, arguments); |
39 | if (op.outRegister >= 0) |
40 | set(op.outRegister, result); |
41 | } |
42 | } |
43 | |
44 | p {
|
45 | new Machine machine; |
46 | machine.function("+", func(int a, int b) { a + b });
|
47 | machine.set(0, 5); |
48 | machine.set(1, 10); |
49 | machine.run(new Operation("+", 0, 1, 2));
|
50 | print(machine.get(2)); |
51 | } |
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: | 801 / 900 |
| Version history: | 4 change(s) |
| Referenced in: | [show references] |