diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/VariableDeclaration.java b/AlgebraicDataflowArchitectureModel/src/code/ast/VariableDeclaration.java index 6a7b25b..5d86c98 100644 --- a/AlgebraicDataflowArchitectureModel/src/code/ast/VariableDeclaration.java +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/VariableDeclaration.java @@ -9,6 +9,7 @@ public class VariableDeclaration extends ASTNode implements IAnnotatable { private Type type; private String variableName; + private Expression optionalInitializer; private Map annotations = new HashMap<>(); public VariableDeclaration(Type type, String variableName) { @@ -16,6 +17,11 @@ this.variableName = variableName; } + public VariableDeclaration(Type type, String variableName, Expression initializer) { + this(type, variableName); + this.optionalInitializer = initializer; + } + public Type getType() { return type; } @@ -32,6 +38,10 @@ this.variableName = variableName; } + public Expression getInitializer() { return optionalInitializer; } + + public void setInitializer(Expression initializer) { this.optionalInitializer = initializer; } + @Override public Annotation getAnnotation(String name) { return annotations.get(name); diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/VariableDeclarationStatement.java b/AlgebraicDataflowArchitectureModel/src/code/ast/VariableDeclarationStatement.java index 0348fce..c384ffa 100644 --- a/AlgebraicDataflowArchitectureModel/src/code/ast/VariableDeclarationStatement.java +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/VariableDeclarationStatement.java @@ -5,15 +5,15 @@ public class VariableDeclarationStatement extends Statement { - private List modifiers = new ArrayList<>(); + private List modifiers = new ArrayList<>(); private Type type; private List fragments = new ArrayList<>(); - public List getModifiers() { + public List getModifiers() { return modifiers; } - public void setModifiers(List modifiers) { + public void setModifiers(List modifiers) { this.modifiers = modifiers; } @@ -37,7 +37,7 @@ public String toString() { StringBuilder sb = new StringBuilder(); - for (Statement mod : modifiers) { + for (Integer mod : modifiers) { sb.append(mod.toString()).append(" "); } diff --git a/AlgebraicDataflowArchitectureModel/src/generators/JavaSpecific.java b/AlgebraicDataflowArchitectureModel/src/generators/JavaSpecific.java index 35bc2d5..d0c42c7 100644 --- a/AlgebraicDataflowArchitectureModel/src/generators/JavaSpecific.java +++ b/AlgebraicDataflowArchitectureModel/src/generators/JavaSpecific.java @@ -1,14 +1,13 @@ package generators; -import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import code.ast.*; +import code.ast.Variable; +import models.algebra.*; import models.algebra.Expression; -import models.algebra.Parameter; -import models.algebra.Term; import models.algebra.Type; -import models.algebra.Variable; import models.dataConstraintModel.DataConstraintModel; public class JavaSpecific implements ILanguageSpecific { @@ -173,7 +172,7 @@ @Override public ReturnStatement getReturnStatement(String returnValue) { ReturnStatement returnStatement = new ReturnStatement(); - returnStatement.setExpression(new Expression(returnValue) { + returnStatement.setExpression(new code.ast.Variable(returnValue) { }); return returnStatement; } @@ -181,38 +180,49 @@ @Override public IfStatement getIfStatement(Term condition, Statement block) { IfStatement ifStatement = new IfStatement(); - String condStr = condition.toImplementation(new String[] {""}); - ifStatement.setExpression(new Expression(condStr) { - }); + String conditionSource = condition.toImplementation(new String[]{""}); + ifStatement.setExpression(new code.ast.Variable(conditionSource)); ifStatement.setThenStatement(block); return ifStatement; } @Override - public ForStatement getForStatementForList(Variable varName, Expression list) { + public ForStatement getForStatementForList(models.algebra.Variable varName, Expression list) { ForStatement forStatement = new ForStatement(); forStatement.getInitializers().add(new PlainStatement("int " + varName.getName() + " = 0")); - String condition = varName.getName() + " < " + list.toString() + ".size()"; - forStatement.setExpression(new Expression(condition)); - forStatement.getUpdaters().add(new PlainStatement(varName.getName() + "++")); + InfixExpression condition = new InfixExpression( + new Symbol("<", 2, Symbol.Type.INFIX), + new code.ast.Variable(varName.getName()), + new code.ast.Variable(list.toString() + ".size()") + ); + forStatement.setExpression(condition); + PostfixExpression increment = new PostfixExpression( + new code.ast.Variable(varName.getName()), + PostfixExpression.Operator.INCREMENT + ); + forStatement.setUpdaters(Arrays.asList(new ExpressionStatement(increment))); return forStatement; } @Override public EnhancedForStatement getForStatementForCollection(String varName, String varType, String collection) { EnhancedForStatement enhancedForStatement = new EnhancedForStatement(); - VariableDeclaration varDeclaration = new VariableDeclaration(new Type(varType), varName); + + VariableDeclaration varDeclaration = new VariableDeclaration(new models.algebra.Type(varName,varType), varName); enhancedForStatement.setParameter(varDeclaration); - enhancedForStatement.setExpression(new Expression(collection)); + enhancedForStatement.setExpression(new code.ast.Variable(collection)); + return enhancedForStatement; } @Override public EnhancedForStatement getForStatementForMap(String varName, String varType, String map) { EnhancedForStatement enhancedForStatement = new EnhancedForStatement(); - VariableDeclaration varDeclaration = new VariableDeclaration(new Type(varDeclaration), varName); + + VariableDeclaration varDeclaration = new VariableDeclaration(new models.algebra.Type(varName,varType), varName); enhancedForStatement.setParameter(varDeclaration); - enhancedForStatement.setExpression(new Expression(map + ".keySet()")); + enhancedForStatement.setExpression(new code.ast.Variable(map + ".keySet()")); + return enhancedForStatement; } @@ -310,9 +320,9 @@ @Override public String getTupleGet(String tupleExp, int idx, int length) { - Expression t = new Variable(tupleExp, DataConstraintModel.typeTuple); + models.algebra.Expression t = new models.algebra.Variable(tupleExp); for (int i = 0; i < idx; i++) { - Term next = new Term(DataConstraintModel.snd); + Term next = new Term(DataConstraintModel.snd); next.addChild(t); t = next; } @@ -321,7 +331,7 @@ last.addChild(t); t = last; } - return t.toImplementation(new String[]{}); + return t.toImplementation(new String[]{""}); } @Override diff --git a/AlgebraicDataflowArchitectureModel/src/tests/ASTTest.java b/AlgebraicDataflowArchitectureModel/src/tests/ASTTest.java index d436321..57015ee 100644 --- a/AlgebraicDataflowArchitectureModel/src/tests/ASTTest.java +++ b/AlgebraicDataflowArchitectureModel/src/tests/ASTTest.java @@ -1,114 +1,172 @@ package tests; import code.ast.*; +import generators.JavaSpecific; import models.algebra.Symbol; +import models.algebra.Term; +import models.dataConstraintModel.DataConstraintModel; +import org.junit.Test; import java.util.Arrays; import java.util.List; public class ASTTest { - public static void main(String[] args) { + @Test + public void test() { - MethodDeclaration method = new MethodDeclaration("Test", false); + JavaSpecific javaGen = new JavaSpecific(); + MethodDeclaration methodDeclaration = javaGen.newMethodDeclaration("Test", JavaSpecific.typeVoid); Block methodBody = new Block(); - VariableDeclarationStatement varStmt = new VariableDeclarationStatement(); - varStmt.setModifiers(List.of(new PlainStatement("private"))); - - //VariableDeclarationのTypeとSimpleTypeの関係を確認(models.algebra)になってる? - varStmt.setFragments(List.of( - new VariableDeclaration(new SimpleType("int"), "x"), - new VariableDeclaration(new SimpleType("int"), "sum") + VariableDeclarationStatement variableDeclarationStatement = new VariableDeclarationStatement(); + variableDeclarationStatement.setModifiers(List.of(Modifier.PRIVATE)); + variableDeclarationStatement.setFragments(List.of( + new VariableDeclaration(DataConstraintModel.typeInt, "x"), + new VariableDeclaration(DataConstraintModel.typeInt, "sum") )); - methodBody.addStatement(varStmt); + methodBody.addStatement(variableDeclarationStatement); - ForStatement forStmt = new ForStatement(); - - //Typeがあれば可能 - forStmt.setInitializers(Arrays.asList(new PlainStatement("int j = 0"))); - - InfixExpression condition = new InfixExpression( - new Symbol("<", 2, Symbol.Type.INFIX), - new Variable("j"), - new Constant("5") - ); - forStmt.setExpression(condition); - - PostfixExpression increment = new PostfixExpression( - new Variable("j"), - PostfixExpression.Operator.INCREMENT - ); - forStmt.setUpdaters(Arrays.asList(new ExpressionStatement(increment))); - + models.algebra.Variable j = new models.algebra.Variable("j"); + models.algebra.Constant limit = new models.algebra.Constant("5"); + ForStatement forStatement = javaGen.getForStatementForList(j, limit); Block forBody = new Block(); - InfixExpression forAdd = new InfixExpression( - new Symbol("+=", 2, Symbol.Type.INFIX), - new Variable("sum"), - new Variable("j") - ); - forBody.addStatement(new ExpressionStatement(forAdd)); - forStmt.setBody(forBody); - methodBody.addStatement(forStmt); + forBody.addStatement(new ExpressionStatement( + new InfixExpression(new Symbol("+=", 2, Symbol.Type.INFIX), new Variable("sum"), new Variable("j")) + )); + forStatement.setBody(forBody); + methodBody.addStatement(forStatement); - EnhancedForStatement enhancedFor = new EnhancedForStatement(); - enhancedFor.setParameter(new SimpleType("String"), "s"); - enhancedFor.setExpression(new Variable("list")); + EnhancedForStatement efStmt = javaGen.getForStatementForCollection("s", "String", "list"); + Block efBody = new Block(); + efBody.addStatement("System.out.println(s);"); + efStmt.setBody(efBody); + methodBody.addStatement(efStmt); - Block eForBody = new Block(); - eForBody.addStatement("System.out.println(s);"); - enhancedFor.setBody(eForBody); - methodBody.addStatement(enhancedFor); + Term moduloTerm = new Term(new Symbol("%", 2, Symbol.Type.INFIX)); + moduloTerm.addChild(new models.algebra.Variable("sum")); + moduloTerm.addChild(new models.algebra.Constant("2")); - WhileStatement whileStmt = new WhileStatement(); - InfixExpression whileCond = new InfixExpression( - new Symbol("<", 2, Symbol.Type.INFIX), - new Variable("sum"), - new Constant("100") - ); - whileStmt.setExpression(whileCond); + Term ifCondTerm = new Term(new Symbol("==", 2, Symbol.Type.INFIX)); + ifCondTerm.addChild(moduloTerm); + ifCondTerm.addChild(new models.algebra.Constant("0"));Block thenBlock = new Block(); + thenBlock.addStatement(new ExpressionStatement(new InfixExpression(new Symbol("+=", 2, Symbol.Type.INFIX), new Variable("sum"), new Constant("10")))); + IfStatement ifStmt = javaGen.getIfStatement(ifCondTerm, thenBlock); + Block elseB = new Block(); + elseB.addStatement(new ExpressionStatement(new InfixExpression(new Symbol("+=", 2, Symbol.Type.INFIX), new Variable("sum"), new Constant("5")))); + ifStmt.setElseStatement(elseB); Block whileBody = new Block(); - IfStatement ifStmt = new IfStatement(); - - InfixExpression modExpr = new InfixExpression( - new Symbol("%", 2, Symbol.Type.INFIX), - new Variable("sum"), - new Constant("2") - ); - InfixExpression ifCond = new InfixExpression( - new Symbol("==", 2, Symbol.Type.INFIX), - modExpr, - new Constant("0") - ); - ifStmt.setExpression(ifCond); - - Block thenBlock = new Block(); - thenBlock.addStatement(new ExpressionStatement(new InfixExpression( - new Symbol("+=", 2, Symbol.Type.INFIX), - new Variable("sum"), - new Constant("10") - ))); - - Block elseBlock = new Block(); - elseBlock.addStatement(new ExpressionStatement(new InfixExpression( - new Symbol("+=", 2, Symbol.Type.INFIX), - new Variable("sum"), - new Constant("5") - ))); - - ifStmt.setThenStatement(thenBlock); - ifStmt.setElseStatement(elseBlock); whileBody.addStatement(ifStmt); + WhileStatement whileStmt = new WhileStatement(); + whileStmt.setExpression(new InfixExpression(new Symbol("<", 2, Symbol.Type.INFIX), new Variable("sum"), new Constant("100"))); whileStmt.setBody(whileBody); methodBody.addStatement(whileStmt); - ReturnStatement returnStmt = new ReturnStatement(); - returnStmt.setExpression(new Variable("sum")); - methodBody.addStatement(returnStmt); + methodBody.addStatement(javaGen.getReturnStatement("sum")); - method.setBody(methodBody); + methodDeclaration.setBody(methodBody); + System.out.println("--- Generated Code"); + System.out.println(methodDeclaration.toString()); - System.out.println("--- Generated Code ---"); - System.out.println(method.toString()); +// +// MethodDeclaration methodDeclaration = new MethodDeclaration("Test", false); +// Block methodBody = new Block(); +// +// VariableDeclarationStatement variableDeclarationStatement = new VariableDeclarationStatement(); +// variableDeclarationStatement.setModifiers(List.of(new PlainStatement("private"))); +// +// variableDeclarationStatement.setFragments(List.of( +// new VariableDeclaration(new SimpleType("int"), "x"), +// new VariableDeclaration(new SimpleType("int"), "sum") +// )); +// methodBody.addStatement(variableDeclarationStatement); +// +// ForStatement forStatement = new ForStatement(); +// +// forStatement.setInitializers(Arrays.asList(new PlainStatement("int j = 0"))); +// +// InfixExpression condition = new InfixExpression( +// new Symbol("<", 2, Symbol.Type.INFIX), +// new Variable("j"), +// new Constant("5") +// ); +// forStatement.setExpression(condition); +// +// PostfixExpression increment = new PostfixExpression( +// new Variable("j"), +// PostfixExpression.Operator.INCREMENT +// ); +// forStatement.setUpdaters(Arrays.asList(new ExpressionStatement(increment))); +// +// Block forBody = new Block(); +// InfixExpression forAddition = new InfixExpression( +// new Symbol("+=", 2, Symbol.Type.INFIX), +// new Variable("sum"), +// new Variable("j") +// ); +// forBody.addStatement(new ExpressionStatement(forAddition)); +// forStatement.setBody(forBody); +// methodBody.addStatement(forStatement); +// +// EnhancedForStatement enhancedForStatement = new EnhancedForStatement(); +// enhancedForStatement.setParameter(new SimpleType("String"), "s"); +// enhancedForStatement.setExpression(new Variable("list")); +// +// Block enhancedForBody = new Block(); +// enhancedForBody.addStatement("System.out.println(s);"); +// enhancedForStatement.setBody(enhancedForBody); +// methodBody.addStatement(enhancedForStatement); +// +// WhileStatement whileStatement = new WhileStatement(); +// InfixExpression whileCondition = new InfixExpression( +// new Symbol("<", 2, Symbol.Type.INFIX), +// new Variable("sum"), +// new Constant("100") +// ); +// whileStatement.setExpression(whileCondition); +// +// Block whileBody = new Block(); +// IfStatement ifStatement = new IfStatement(); +// +// InfixExpression moduloExpression = new InfixExpression( +// new Symbol("%", 2, Symbol.Type.INFIX), +// new Variable("sum"), +// new Constant("2") +// ); +// InfixExpression ifCondition = new InfixExpression( +// new Symbol("==", 2, Symbol.Type.INFIX), +// moduloExpression, +// new Constant("0") +// ); +// ifStatement.setExpression(ifCondition); +// +// Block thenBlock = new Block(); +// thenBlock.addStatement(new ExpressionStatement(new InfixExpression( +// new Symbol("+=", 2, Symbol.Type.INFIX), +// new Variable("sum"), +// new Constant("10") +// ))); +// +// Block elseBlock = new Block(); +// elseBlock.addStatement(new ExpressionStatement(new InfixExpression( +// new Symbol("+=", 2, Symbol.Type.INFIX), +// new Variable("sum"), +// new Constant("5") +// ))); +// +// ifStatement.setThenStatement(thenBlock); +// ifStatement.setElseStatement(elseBlock); +// whileBody.addStatement(ifStatement); +// whileStatement.setBody(whileBody); +// methodBody.addStatement(whileStatement); +// +// ReturnStatement returnStatement = new ReturnStatement(); +// returnStatement.setExpression(new Variable("sum")); +// methodBody.addStatement(returnStatement); +// +// methodDeclaration.setBody(methodBody); +// +// System.out.println("--- Generated Code ---"); +// System.out.println(methodDeclaration.toString()); } } \ No newline at end of file