Created
August 28, 2015 00:14
-
-
Save M4GNV5/273ce9aa10b9d810c800 to your computer and use it in GitHub Desktop.
TIS-100 like assembly language in vanilla Minecraft
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import("util.command"); | |
import("util.callAsync"); | |
import("util.setTimeout"); | |
import("chat.tellraw"); | |
import("scoreboard"); | |
static int base = 10; // try 2 or 16 | |
static bool debug = false; | |
object cursor = @e[type=ArmorStand,name=cursor]; | |
int arg0 = 0; | |
int arg1 = 0; | |
object stdInteger = scoreboard.objective("stdInteger", "dummy"); | |
object var0Selector = @e[type=ArmorStand,name=var0]; | |
object var1Selector = @e[type=ArmorStand,name=var1]; | |
object var0 = scoreboard.score(stdInteger, var0Selector); | |
object var1 = scoreboard.score(stdInteger, var1Selector); | |
function init() | |
{ | |
command("scoreboard objectives add selectedVar dummy"); | |
static if(debug) | |
command("scoreboard objectives setdisplay sidebar stdInteger"); | |
command("kill @e[type=ArmorStand,name=!validate]"); | |
command("summon ArmorStand -1 30 0 {CustomName:cursor,NoGravity:true}"); | |
command("scoreboard players set {0} stdInteger 0".format(cursor)); | |
command("stats entity {0} set SuccessCount {0} stdInteger".format(cursor)); | |
static for(static int i = 0; i < 16; i++) | |
{ | |
command("summon ArmorStand {0} 8 0 {CustomName:var,NoGravity:true}".format(i)); | |
} | |
} | |
function main1() | |
{ | |
isCmd("set", function() | |
{ | |
parseArgs(function() | |
{ | |
selectVar0(arg0, function() | |
{ | |
static if(debug) | |
tellraw("set ", arg0, " ", arg1); | |
var0 = arg1; | |
continueExecution(); | |
}); | |
}); | |
}); | |
isCmd("cp", function() | |
{ | |
parseArgs(function() | |
{ | |
selectVar0(arg0, function() | |
{ | |
selectVar1(arg1, function() | |
{ | |
static if(debug) | |
tellraw("cp ", arg0, " ", arg1); | |
var0 = var1; | |
continueExecution(); | |
}); | |
}); | |
}); | |
}); | |
isCmd("add", function() | |
{ | |
parseArgs(function() | |
{ | |
selectVar0(arg0, function() | |
{ | |
selectVar1(arg1, function() | |
{ | |
static if(debug) | |
tellraw("add ", arg0, " ", arg1); | |
var0 += var1; | |
continueExecution(); | |
}); | |
}); | |
}); | |
}); | |
isCmd("sub", function() | |
{ | |
parseArgs(function() | |
{ | |
selectVar0(arg0, function() | |
{ | |
selectVar1(arg1, function() | |
{ | |
static if(debug) | |
tellraw("sub ", arg0, " ", arg1); | |
var0 -= var1; | |
continueExecution(); | |
}); | |
}); | |
}); | |
}); | |
isCmd("mul", function() | |
{ | |
parseArgs(function() | |
{ | |
selectVar0(arg0, function() | |
{ | |
selectVar1(arg1, function() | |
{ | |
static if(debug) | |
tellraw("mul ", arg0, " ", arg1); | |
var0 *= var1; | |
continueExecution(); | |
}); | |
}); | |
}); | |
}); | |
isCmd("div", function() | |
{ | |
parseArgs(function() | |
{ | |
selectVar0(arg0, function() | |
{ | |
selectVar1(arg1, function() | |
{ | |
static if(debug) | |
tellraw("div ", arg0, " ", arg1); | |
var0 /= var1; | |
continueExecution(); | |
}); | |
}); | |
}); | |
}); | |
isCmd("mod", function() | |
{ | |
parseArgs(function() | |
{ | |
selectVar0(arg0, function() | |
{ | |
selectVar1(arg1, function() | |
{ | |
static if(debug) | |
tellraw("mod ", arg0, " ", arg1); | |
var0 %= var1; | |
continueExecution(); | |
}); | |
}); | |
}); | |
}); | |
isCmd("#", function() | |
{ | |
//comment | |
continueExecution(); | |
}); | |
} | |
function main2() | |
{ | |
isCmd("out", function() | |
{ | |
parseArg0(function() | |
{ | |
selectVar0(arg0, function() | |
{ | |
static if(debug) | |
tellraw("Variable ", arg0, " = ", var0); | |
else | |
tellraw(var0); | |
continueExecution(); | |
}); | |
}); | |
}); | |
isCmd("jmp", function() | |
{ | |
parseArg0(function() | |
{ | |
static if(debug) | |
tellraw("jmp ", arg0); | |
jumpToFunc(arg0); | |
}); | |
}); | |
isCmd("jez", function() | |
{ | |
parseArgs(function() | |
{ | |
selectVar0(arg0, function() | |
{ | |
static if(debug) | |
tellraw("jez ", arg0, " ", arg1); | |
if(var0 == 0) | |
jumpToFunc(arg1); | |
else | |
continueExecution(); | |
}); | |
}); | |
}); | |
isCmd("jnz", function() | |
{ | |
parseArgs(function() | |
{ | |
selectVar0(arg0, function() | |
{ | |
static if(debug) | |
tellraw("jnz ", arg0, " ", arg1); | |
if(var0 == 0) | |
continueExecution(); | |
else | |
jumpToFunc(arg1); | |
}); | |
}); | |
}); | |
isCmd("jlz", function() | |
{ | |
parseArgs(function() | |
{ | |
selectVar0(arg0, function() | |
{ | |
static if(debug) | |
tellraw("jlz ", arg0, " ", arg1); | |
if(var0 < 0) | |
jumpToFunc(arg1); | |
else | |
continueExecution(); | |
}); | |
}); | |
}); | |
isCmd("jgz", function() | |
{ | |
parseArgs(function() | |
{ | |
selectVar0(arg0, function() | |
{ | |
static if(debug) | |
tellraw("jgz ", arg0, " ", arg1); | |
if(var0 > 0) | |
jumpToFunc(arg1); | |
else | |
continueExecution(); | |
}); | |
}); | |
}); | |
isCmd("jro", function() | |
{ | |
parseArg0(function() | |
{ | |
selectVar0(arg0, function() | |
{ | |
static if(debug) | |
tellraw("jro ", arg0); | |
jumpToFunc(var0); | |
}); | |
}); | |
}); | |
} | |
int waitCount; | |
delegate waitCallback; | |
function waitElapsed() | |
{ | |
waitCount--; | |
if(waitCount <= 0) | |
waitCallback(); | |
} | |
function continueExecution() | |
{ | |
command("tp {0} ~ ~ ~1".format(cursor)); | |
command("entitydata {0} {CustomName:var}".format(var0Selector)); | |
command("entitydata {0} {CustomName:var}".format(var1Selector)); | |
callAsync(main1); | |
callAsync(main2); | |
} | |
int funcId; | |
function jumpToFunc(int _id) | |
{ | |
funcId = _id; | |
callAsync(_jumpToFunc); | |
} | |
function _jumpToFunc() | |
{ | |
command("tp {0} -1 30 -1".format(cursor)); | |
command("scoreboard players operation @e[name=cursor] stdInteger = funcId stdInteger".format(name)); | |
static for(static int i = 32; i >= 1; i /= 2) | |
{ | |
command("tp @e[name=cursor,score_stdInteger_min={0}] ~ ~-{0} ~".format(i)); | |
command("scoreboard players remove @e[name=cursor,score_stdInteger_min={0}] stdInteger {0}".format(i)); | |
} | |
continueExecution(); | |
} | |
int varId; | |
function selectVar0(int id, delegate callback) | |
{ | |
varId = id; | |
waitCallback = callback; | |
waitCount = 1; | |
callAsync(_selectVar0); | |
} | |
function _selectVar0() { _selectVariable("var0"); } | |
function selectVar1(int id, delegate callback) | |
{ | |
varId = id; | |
waitCallback = callback; | |
waitCount = 1; | |
callAsync(_selectVar1); | |
} | |
function _selectVar1() { _selectVariable("var1"); } | |
function _selectVariable(string varName) | |
{ | |
callAsync(function() | |
{ | |
static string name = "select"; | |
name += varName; | |
command("summon ArmorStand 0 8 0 {CustomName:{0},NoGravity:true}".format(name)); | |
command("scoreboard players operation @e[name={0}] stdInteger = varId stdInteger".format(name)); | |
static for(static int i = 32; i >= 1; i /= 2) | |
{ | |
command("tp @e[name={0},score_stdInteger_min={1}] ~{1} ~ ~".format(name, i)); | |
command("scoreboard players remove @e[name={0},score_stdInteger_min={1}] stdInteger {1}".format(name, i)); | |
} | |
command("execute @e[name={0}] ~ ~ ~ entitydata @e[r=0,name=var] {CustomName:{1}}".format(name, varName)); | |
command("kill @e[name={0}]".format(name)); | |
waitElapsed(); | |
}); | |
} | |
function isCmd(string label, delegate callback) | |
{ | |
async if("execute {0} ~ ~ ~ testforblock ~ ~ ~ minecraft:wall_sign -1 {Text1:\\"\\\\\"{1}\\\\\"\\"}".format(cursor, label)) //" | |
{ | |
callback(); | |
} | |
} | |
function parseArgs(delegate callback) | |
{ | |
command("scoreboard players set arg0 stdInteger -1"); | |
command("scoreboard players set arg1 stdInteger -1"); | |
waitCount = 6; | |
waitCallback = callback; | |
callAsync(_parseLine2Part1); | |
callAsync(_parseLine2Part2); | |
callAsync(_parseLine2Part3); | |
callAsync(_parseLine3Part1); | |
callAsync(_parseLine3Part2); | |
callAsync(_parseLine3Part3); | |
} | |
function parseArg0(delegate callback) | |
{ | |
command("scoreboard players set arg0 stdInteger -1"); | |
waitCount = 3; | |
waitCallback = callback; | |
callAsync(_parseLine2Part1); | |
callAsync(_parseLine2Part2); | |
callAsync(_parseLine2Part3); | |
} | |
function parseArg1(delegate callback) | |
{ | |
command("scoreboard players set arg1 stdInteger -1"); | |
waitCount = 3; | |
waitCallback = callback; | |
callAsync(_parseLine3Part1); | |
callAsync(_parseLine3Part2); | |
callAsync(_parseLine3Part3); | |
} | |
function _parseLine2Part1() { _parseLine(2, 0, 20); } | |
function _parseLine2Part2() { _parseLine(2, 21, 42); } | |
function _parseLine2Part3() { _parseLine(2, 43, 63); } | |
function _parseLine3Part1() { _parseLine(3, 0, 20); } | |
function _parseLine3Part2() { _parseLine(3, 21, 42); } | |
function _parseLine3Part3() { _parseLine(3, 43, 63); } | |
function _parseLine(int line, int start, int stop) | |
{ | |
static int arg = -2; | |
arg += line; | |
static for(static int i = start; i <= stop; i++) | |
{ | |
command("execute {0} ~ ~ ~ testforblock ~ ~ ~ minecraft:wall_sign -1 {Text{1}:\\"\\\\\"{2}\\\\\"\\"}".format(cursor, line, i.toString(base))); //" | |
command("execute @e[type=ArmorStand,name=cursor,score_stdInteger_min=1] ~ ~ ~ scoreboard players set arg{1} stdInteger {2}".format(cursor, arg, i)); | |
} | |
waitElapsed(); | |
} | |
init(); | |
// start main loop | |
setTimeout(main1, 200); | |
setTimeout(main2, 200); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment