diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/ExpressionStatement.java b/AlgebraicDataflowArchitectureModel/src/code/ast/ExpressionStatement.java index 5784610..f8fdf96 100644 --- a/AlgebraicDataflowArchitectureModel/src/code/ast/ExpressionStatement.java +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/ExpressionStatement.java @@ -3,6 +3,10 @@ public class ExpressionStatement extends Statement{ private Expression expression; + public ExpressionStatement(Expression expression) { + this.expression = expression; + } + public Expression getExpression() { return expression; } public void setExpression(Expression expression) { this.expression = expression; } diff --git a/AlgebraicDataflowArchitectureModel/src/tests/ASTTest.java b/AlgebraicDataflowArchitectureModel/src/tests/ASTTest.java index 70ab4c3..dfb709d 100644 --- a/AlgebraicDataflowArchitectureModel/src/tests/ASTTest.java +++ b/AlgebraicDataflowArchitectureModel/src/tests/ASTTest.java @@ -1,72 +1,115 @@ -//package tests; -// -//import code.ast.*; -//import models.dataConstraintModel.DataConstraintModel; -// -//import java.util.Arrays; -//import java.util.List; -// -//public class ASTTest { -// public static void main(String[] args) { -// -// MethodDeclaration method = new MethodDeclaration("Test", false); -// Block methodBody = new Block(); -// -// VariableDeclarationStatement varStmt = new VariableDeclarationStatement(); -// varStmt.setModifiers(List.of(new PlainStatement("private"))); -// //varStmt.setType(new PrimitiveType(new SimpleType("int"))); -// varStmt.setFragments(List.of( -// new VariableDeclaration(DataConstraintModel.typeInt, "x"), -// new VariableDeclaration(DataConstraintModel.typeInt, "sum") -// )); -// methodBody.addStatement(varStmt); -// -// ForStatement forStmt = new ForStatement(); -// forStmt.setInitializers(Arrays.asList(new PlainStatement("int j = 0"))); -// forStmt.setExpression(new PlainExpression("j < 5")); -// forStmt.setUpdaters(Arrays.asList(new Statement("j++"))); -// Block forBody = new Block(); -// forBody.addStatement("sum += j;"); -// forStmt.setBody(forBody); -// methodBody.addStatement(forStmt); -// -// EnhancedForStatement enhancedFor = new EnhancedForStatement(); -// enhancedFor.setParameter(new VariableDeclaration(DataConstraintModel.typeString, "s")); -// enhancedFor.setExpression(new PlainExpression("list")); -// Block eForBody = new Block(); -// eForBody.addStatement("System.out.println(s);"); -// enhancedFor.setBody(eForBody); -// methodBody.addStatement(enhancedFor); -// -// WhileStatement whileStmt = new WhileStatement(); -// whileStmt.setExpression(new PlainExpression("sum < 100")); -// -// Block whileBody = new Block(); -// IfStatement ifStmt = new IfStatement(); -// ifStmt.setExpression(new PlainExpression("sum % 2 == 0")); -// -// Block thenBlock = new Block(); -// ExpressionStatement exprStmt = new ExpressionStatement(); -// exprStmt.setExpression(new PlainExpression("sum += 10")); -// thenBlock.addStatement(exprStmt); -// -// Block elseBlock = new Block(); -// elseBlock.addStatement("sum += 5;"); -// -// ifStmt.setThenStatement(thenBlock); -// ifStmt.setElseStatement(elseBlock); -// -// whileBody.addStatement(ifStmt); -// whileStmt.setBody(whileBody); -// methodBody.addStatement(whileStmt); -// -// ReturnStatement returnStmt = new ReturnStatement(); -// returnStmt.setExpression(new PlainExpression("sum")); -// methodBody.addStatement(returnStmt); -// -// method.setBody(methodBody); -// -// System.out.println("--- Generated Code ---"); -// System.out.println(method.toString()); -// } -//} \ No newline at end of file +package tests; + +import code.ast.*; +import models.algebra.Symbol; +import models.dataConstraintModel.DataConstraintModel; + +import java.util.Arrays; +import java.util.List; + +public class ASTTest { + public static void main(String[] args) { + + MethodDeclaration method = new MethodDeclaration("Test", false); + Block methodBody = new Block(); + + VariableDeclarationStatement varStmt = new VariableDeclarationStatement(); + varStmt.setModifiers(List.of(new PlainStatement("private"))); + varStmt.setType(new SimpleType("int")); + varStmt.setFragments(List.of( + //Typeを使用したい + new VariableDeclaration(DataConstraintModel.typeInt, "x"), + new VariableDeclaration(DataConstraintModel.typeInt, "sum") + )); + methodBody.addStatement(varStmt); + + 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))); + + 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); + + EnhancedForStatement enhancedFor = new EnhancedForStatement(); + enhancedFor.setParameter(new VariableDeclaration(DataConstraintModel.typeString, "s")); + enhancedFor.setExpression(new Variable("list")); + + Block eForBody = new Block(); + eForBody.addStatement("System.out.println(s);"); + enhancedFor.setBody(eForBody); + methodBody.addStatement(enhancedFor); + + WhileStatement whileStmt = new WhileStatement(); + InfixExpression whileCond = new InfixExpression( + new Symbol("<", 2, Symbol.Type.INFIX), + new Variable("sum"), + new Constant("100") + ); + whileStmt.setExpression(whileCond); + + 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); + whileStmt.setBody(whileBody); + methodBody.addStatement(whileStmt); + + ReturnStatement returnStmt = new ReturnStatement(); + returnStmt.setExpression(new Variable("sum")); + methodBody.addStatement(returnStmt); + + method.setBody(methodBody); + + System.out.println("--- Generated Code ---"); + System.out.println(method.toString()); + } +} \ No newline at end of file