Last active
December 26, 2017 06:50
-
-
Save Octogonapus/077ce3195aad1a147f7c443b86edf1d6 to your computer and use it in GitHub Desktop.
ast-explorer
This file contains hidden or 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 org.codehaus.groovy.ast.*; | |
import org.codehaus.groovy.ast.expr.*; | |
import org.codehaus.groovy.ast.stmt.*; | |
import org.codehaus.groovy.transform.*; | |
import org.codehaus.groovy.control.*; | |
import org.codehaus.groovy.control.customizers.*; | |
import com.neuronrobotics.bowlerstudio.scripting.GroovyHelper; | |
import com.neuronrobotics.bowlerstudio.scripting.IDebugScriptRunner; | |
import com.neuronrobotics.bowlerstudio.scripting.IScriptingLanguage; | |
import com.neuronrobotics.bowlerstudio.scripting.ScriptingEngine; | |
import com.neuronrobotics.sdk.common.BowlerAbstractDevice; | |
import com.neuronrobotics.sdk.common.DeviceManager; | |
@GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION) | |
public class TestTransformation implements ASTTransformation { | |
@Override | |
public void visit(org.codehaus.groovy.ast.ASTNode[] astNodes, SourceUnit sourceUnit) { | |
sourceUnit.ast.classes.each { explore it } | |
} | |
def buildDetails(classMember) { | |
classMember.each { explore it } | |
} | |
def explore(ClassNode klass) { | |
println "Class Name: ${klass.toString()}" | |
buildDetails(klass.methods) | |
} | |
def explore(MethodNode node) { | |
println "\tMethod Name: ${node.name}" | |
buildDetails(node.code) | |
} | |
def explore(ExpressionStatement exp) { | |
explore exp.expression | |
} | |
def explore(DeclarationExpression decExp) { | |
println "Declaration Expression: ${decExp.text}" | |
explore decExp.leftExpression | |
explore decExp.rightExpression | |
} | |
def explore(MethodCallExpression methCallExp) { | |
println "Method Call: ${methCallExp.methodAsString}" | |
println "Object: " | |
explore methCallExp.objectExpression | |
explore MethodCallExpression.arguments | |
} | |
def explore(ArgumentListExpression argListExp) { | |
if (argListExp.expressions.size() > 0) { | |
println "Arguments: " | |
argListExp.expression.each { explore it } | |
} | |
} | |
def explore(ConstantExpression constExp) { | |
println "Constant Expression: ${constExp.text} : ${constExp.type}" | |
} | |
def explore(VariableExpression varExp) { | |
println "Variable Expression: ${varExp.text} : ${varExp.type}" | |
} | |
def explore(ClassExpression classExp) { | |
println "Class Expression: ${classExp.text}" | |
} | |
} | |
public class TestLang implements IScriptingLanguage { | |
@Override | |
public String getShellType() { | |
return "TestGroovy"; | |
} | |
@Override | |
public Object inlineScriptRun(File code, ArrayList<Object> args) throws Exception { | |
return this.inline(code, args); | |
} | |
@Override | |
public Object inlineScriptRun(String code, ArrayList<Object> args) throws Exception { | |
return this.inline(code, args); | |
} | |
@Override | |
public boolean getIsTextFile() { | |
return true; | |
} | |
@Override | |
public ArrayList<String> getFileExtenetion() { | |
return new ArrayList<>(Arrays.asList("java", "groovy")); | |
} | |
private Object inline(Object code, ArrayList<Object> args) throws Exception { | |
CompilerConfiguration cc = new CompilerConfiguration(); | |
cc.addCompilationCustomizers(new ImportCustomizer() | |
.addStarImports(ScriptingEngine.getImports()) | |
.addStaticStars( | |
"com.neuronrobotics.sdk.util.ThreadUtil", | |
"eu.mihosoft.vrl.v3d.Transform", | |
"com.neuronrobotics.bowlerstudio.vitamins.Vitamins")); | |
cc.addCompilationCustomizers(new ASTTransformationCustomizer(new TestTransformation())); | |
Binding binding = new Binding(); | |
for (String pm : DeviceManager.listConnectedDevice()) { | |
BowlerAbstractDevice device = DeviceManager.getSpecificDevice(null, pm); | |
binding.setVariable( | |
device.getScriptingName(), | |
Class.forName(device.getClass().getName()).cast(device)); | |
} | |
binding.setVariable("args", args); | |
GroovyShell shell = new GroovyShell(GroovyHelper.class.getClassLoader(), binding, cc); | |
Script script; | |
if (String.class.isInstance(code)) { | |
script = shell.parse((String) code); | |
} else { | |
if (!File.class.isInstance(code)) { | |
return null; | |
} | |
script = shell.parse((File) code); | |
} | |
return script.run(); | |
} | |
} | |
ScriptingEngine.addScriptingLanguage(new TestLang()); | |
ScriptingEngine.inlineScriptStringRun( | |
""" | |
""", | |
[], | |
"TestGroovy"); | |
return null; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment