Commit b27412cb authored by baueko's avatar baueko

added return type and auto generated variables to java-ast, did some

cleanup
parent 3a740c2a
......@@ -775,7 +775,8 @@ production ::=
new Name(
(String) lhs_id,
Range.fromLocations(lhs_idxleft, lhs_idxright)
),
),
((symbol_part) this.symbols.get(lhs_id))._the_symbol._stack_type,
(List<ProductionRight>) rhs,
Range.fromLocations(lhs_idxleft, sxright)
)
......
......@@ -3,6 +3,7 @@ package de.in.tum.www2.cup.analysis;
import de.in.tum.www2.cup.ast.ActionCodeBlock;
import de.in.tum.www2.cup.ast.IProductionRightPart;
import de.in.tum.www2.cup.ast.LabeledProductionSymbolRef;
import de.in.tum.www2.cup.ast.Production;
import de.in.tum.www2.cup.ast.ProductionRight;
public class LabelToVariableVisitor extends Visitor<Object> {
......@@ -19,5 +20,7 @@ public class LabelToVariableVisitor extends Visitor<Object> {
block.addVariable((LabeledProductionSymbolRef) part);
}
}
Production p = (Production) node.getParent();
block.setReturnType(p.getReturnType());
}
}
......@@ -9,6 +9,7 @@ import de.in.tum.www2.cup.analysis.LabelToVariableVisitor;
public class ActionCodeBlock extends CodeBlock implements IProductionRightPart {
private List<LabeledProductionSymbolRef> variables = new LinkedList<>();
private String returnType;
public ActionCodeBlock(int debugId, BlockType bt, String blob, Range range) {
super(debugId, bt, blob, range);
......@@ -40,4 +41,12 @@ public class ActionCodeBlock extends CodeBlock implements IProductionRightPart {
public List<LabeledProductionSymbolRef> getVariables() {
return variables;
}
public void setReturnType(String returnType) {
this.returnType = returnType;
}
public String getReturnType() {
return returnType;
}
}
package de.in.tum.www2.cup.ast;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import de.in.tum.www2.cup.ast.CodeBlock.BlockType;
import de.in.tum.www2.cup.Position;
public class JavaCompiler {
//private static List<Pair<ASTNode, BlockType>> nodes = new LinkedList<>();
private static ASTNode javaTree;
/*public static void doTreeMagic() {
// TODO add root node (Parser class)
// TODO add nodes -> action code in class Cup$Parser$actions
AST a = AST.newAST(AST.JLS8);
// public class Parser
TypeDeclaration td = a.newTypeDeclaration();
Modifier m = a.newModifier(ModifierKeyword.PUBLIC_KEYWORD);
td.modifiers().add(m);
td.setName(a.newSimpleName("Parser"));
// public void user_init() throws java.lang.Exception
MethodDeclaration userInit = a.newMethodDeclaration();
userInit.modifiers().add(a.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
userInit.setName(a.newSimpleName("user_init"));
// class CUP$Parser$actions
TypeDeclaration parserActions = a.newTypeDeclaration();
parserActions.setName(a.newSimpleName("CUP$Parser$actions"));
// public java_cup.runtime.Symbol scan() throws java.lang.Exception
MethodDeclaration scanWith = a.newMethodDeclaration();
scanWith.setName(a.newSimpleName("scan"));
scanWith.modifiers().add(a.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
scanWith.setReturnType2(a.newSimpleType(a.newQualifiedName(
a.newQualifiedName(a.newName("java_cup"), a.newSimpleName("runtime")), a.newSimpleName("Symbol"))));
// add stuff to Parser class body
td.bodyDeclarations().add(userInit);
for (Pair<ASTNode, BlockType> p : nodes) {
if (p.right == BlockType.Parser) {
TypeDeclaration td2 = (TypeDeclaration) p.left;
td2.delete();
System.out.println(td2.bodyDeclarations().toString());
td.bodyDeclarations().addAll(td2.bodyDeclarations());
// TODO fix this
}
}
td.bodyDeclarations().add(parserActions);
td.bodyDeclarations().add(scanWith);
int i;
}*/
public static ASTNode parseJava(String blob, CodeBlock codeblock) {
ASTParser parser = ASTParser.newParser(AST.JLS8);
ASTNode node;
parser.setEnvironment(new String[] { "" }, new String[] { "" }, new String[] { "UTF-8" }, true);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setUnitName("TODO: Edit name");
parser.setResolveBindings(true);
String tokkenblob = "";
final String DUMMY_CLASS_DEF = "public class DummyClass\n{";
final String RWING = "}";
final String DUMMY_METHOD_DEF = "\n\tpublic void dummymethod(";
final String DUMMY_METHOD_ARGS_END = ")\n\t{";
final String DUMMY_CLASS_DEF_END = "\n" + RWING;
final String DUMMY_METHOD_END = "\n\t" + RWING;
switch (codeblock.getBt()) {
case Action:
case Parser:
blob="public class DummyClass{"+blob+"}";
parser.setEnvironment(new String[] {""}, new String[] {""}, new String[] { "UTF-8"}, true);
parser.setResolveBindings(true);
parser.setSource(blob.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
tokkenblob = DUMMY_CLASS_DEF + blob + RWING;
parser.setSource(tokkenblob.toCharArray());
node = parser.createAST(null);
node.accept(new LocalVariableDetector());
break;
case Init:
case Scan:
blob="public class DummyClass{\n\tpublic void dummymethod()\n\t{"+blob+"\n\t}\n}";
parser.setEnvironment(new String[] {""}, new String[] {""}, new String[] { "UTF-8"}, true);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(blob.toCharArray());
parser.setResolveBindings(true);
tokkenblob = DUMMY_CLASS_DEF + DUMMY_METHOD_DEF + DUMMY_METHOD_ARGS_END + blob + DUMMY_METHOD_END
+ DUMMY_CLASS_DEF_END;
parser.setSource(tokkenblob.toCharArray());
node = parser.createAST(null);
node.accept(new LocalVariableDetector());
break;
case ActionCode:
blob="public class DummyClass{\n\tpublic void dummymethod("+"Integer e"+")\n\t{"+blob+"\n\t}\n}";
parser.setEnvironment(new String[] {""}, new String[] {""}, new String[] { "UTF-8"}, true);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(blob.toCharArray());
parser.setResolveBindings(true);
ActionCodeBlock block = (ActionCodeBlock) codeblock;
tokkenblob = DUMMY_CLASS_DEF + DUMMY_METHOD_DEF;
tokkenblob += block.getReturnType() + " RESULT";
for (LabeledProductionSymbolRef l : block.getVariables()) {
tokkenblob += ", " + l.getDatatype() + " " + l.label;
tokkenblob += ", " + "Object" + " " + l.label + "xleft"; // TODO Object should really be a Location
tokkenblob += ", " + "Object" + " " + l.label + "xright"; // TODO Object should really be a Location
tokkenblob += ", " + "int" + " " + l.label + "left";
tokkenblob += ", " + "int" + " " + l.label + "right";
Position left = l.getBegin();
Position right = l.getEnd();
// xleft, xright
System.out.println(left + " to " + right);
}
tokkenblob += DUMMY_METHOD_ARGS_END + "\n\t\t";
// action code
tokkenblob += blob;
tokkenblob += DUMMY_METHOD_END + DUMMY_CLASS_DEF_END;
parser.setSource(tokkenblob.toCharArray());
node = parser.createAST(null);
node.accept(new LocalVariableDetector());
break;
......@@ -103,37 +75,4 @@ public class JavaCompiler {
return node;
}
private static String getVars() {
// TODO Auto-generated method stub
return null;
}
public static class Pair<L, R> {
private final L left;
private final R right;
public Pair(L left, R right) {
this.left = left;
this.right = right;
}
public L getLeft() {
return left;
}
public R getRight() {
return right;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair))
return false;
Pair pairo = (Pair) o;
return this.left.equals(pairo.getLeft()) && this.right.equals(pairo.getRight());
}
}
}
......@@ -2,16 +2,19 @@ package de.in.tum.www2.cup.ast;
import java.util.List;
import de.in.tum.www2.cup.analysis.AbstractVisitor;
import de.in.tum.www2.cup.Range;
import de.in.tum.www2.cup.analysis.AbstractVisitor;
public class Production extends AbstractNode
implements IWithName, IHasDeclarationReference
{
public class Production extends AbstractNode implements IWithName, IHasDeclarationReference {
private NonTerminal declarationRef;
private Name lhs; // TODO: This should really be a NonTerminal class
private List<ProductionRight> rhs;
private String returnType;
public String getReturnType() {
return returnType;
}
@Override
public Name getName() {
return lhs;
......@@ -21,7 +24,7 @@ public class Production extends AbstractNode
public AbstractNode getDeclaration() {
return declarationRef;
}
public List<ProductionRight> getRightHandSides() {
return rhs;
}
......@@ -33,10 +36,10 @@ public class Production extends AbstractNode
this.declarationRef = (NonTerminal) node;
}
public Production(Name lhs,
List<ProductionRight> rhs, Range range) {
public Production(Name lhs, String returnType, List<ProductionRight> rhs, Range range) {
super(range);
this.lhs = lhs;
this.returnType = returnType;
if (lhs != null)
this.lhs.setParent(this);
this.rhs = rhs;
......@@ -46,7 +49,10 @@ public class Production extends AbstractNode
pr.setParent(this);
}
@Override protected String getNodeName() { return "Production"; }
@Override
protected String getNodeName() {
return "Production";
}
@Override
public <T> void accept(AbstractVisitor<T> visitor, T data) {
......@@ -61,16 +67,16 @@ public class Production extends AbstractNode
}
visitor.postVisit(this, data);
}
@Override
protected void putDescription(StringBuilder builder) {
builder.append(lhs);
builder.append(" -> \n");
int lsize = rhs.size();
for (int i=0; i < lsize; i++) {
for (int i = 0; i < lsize; i++) {
builder.append(" ");
builder.append(rhs.get(i));
if (i < lsize-1)
if (i < lsize - 1)
builder.append(",");
builder.append("\n");
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment