Created
September 18, 2013 19:41
-
-
Save uehaj/6614447 to your computer and use it in GitHub Desktop.
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
@GrabResolver(name="maven-repo", root="https://raw.github.com/uehaj/maven-repo/gh-pages/snapshot") | |
@Grab("groovyx.ast.bytecode:groovy-bytecode-ast:0.2.0-separate-asm") | |
import groovyx.ast.bytecode.Bytecode | |
import java.lang.invoke.* | |
import java.lang.invoke.MethodHandles.Lookup | |
import static groovyjarjarasm.asm.Opcodes.H_INVOKESTATIC | |
class Brainfuck { | |
// Bootstrapメソッド | |
public static CallSite bootstrap(Lookup lookup, String methodName, MethodType type, String instructions) { | |
MethodHandle result = null | |
instructions.tr('<>.','lgo').each { insn -> | |
MethodHandle mh = lookup.findStatic(Brainfuck, insn, MethodType.methodType(void.class)) | |
result = (result == null) ? mh : MethodHandles.filterReturnValue(result, mh) | |
} | |
new ConstantCallSite(result) | |
} | |
// Brainfuck実行のためのデータ構造 | |
static int dp // データポインタ | |
static byte[] data = new byte[30000] // メモリ | |
// Brainfuck命令群 | |
static void '+'(){data[dp]++} | |
static void '-'(){data[dp]--} | |
static void l(){dp--} // < | |
static void g(){dp++} // > | |
static void o(){print((char)data[dp])} // . | |
static void ','(){System.in.read(data, dp, 1)} | |
@Bytecode | |
static void main(String[] args) throws Exception { | |
// Brainfuckからコンバートされたコード | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '>+++++++++' | |
_GOTO tmp1 | |
lab1: | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '<++++++++>-' | |
tmp1: | |
getstatic '.data','[B' | |
getstatic '.dp','I' | |
baload | |
ifne lab1 | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '<.>+++++++' | |
_GOTO tmp2 | |
lab2: | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '<++++>-' | |
tmp2: | |
getstatic '.data','[B' | |
getstatic '.dp','I' | |
baload | |
ifne lab2 | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '<+.+++++++..+++.' | |
_GOTO tmp3 | |
lab3: | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '-' | |
tmp3: | |
getstatic '.data','[B' | |
getstatic '.dp','I' | |
baload | |
ifne lab3 | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '>++++++++' | |
_GOTO tmp4 | |
lab4: | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '<++++>-' | |
tmp4: | |
getstatic '.data','[B' | |
getstatic '.dp','I' | |
baload | |
ifne lab4 | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '<.>+++++++++++' | |
_GOTO tmp5 | |
lab5: | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '<+++++>-' | |
tmp5: | |
getstatic '.data','[B' | |
getstatic '.dp','I' | |
baload | |
ifne lab5 | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '<.>++++++++' | |
_GOTO tmp6 | |
lab6: | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '<+++>-' | |
tmp6: | |
getstatic '.data','[B' | |
getstatic '.dp','I' | |
baload | |
ifne lab6 | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '<.+++.------.--------.' | |
_GOTO tmp7 | |
lab7: | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '-' | |
tmp7: | |
getstatic '.data','[B' | |
getstatic '.dp','I' | |
baload | |
ifne lab7 | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '>++++++++' | |
_GOTO tmp8 | |
lab8: | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '<++++>-' | |
tmp8: | |
getstatic '.data','[B' | |
getstatic '.dp','I' | |
baload | |
ifne lab8 | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '<+.' | |
_GOTO tmp9 | |
lab9: | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '-' | |
tmp9: | |
getstatic '.data','[B' | |
getstatic '.dp','I' | |
baload | |
ifne lab9 | |
invokedynamic 'dummy', '()V', [H_INVOKESTATIC, 'Brainfuck', 'bootstrap', [CallSite, Lookup, String, MethodType, String]], '++++++++++.' | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment