Newer
Older
AlgebraicDataflowArchitectureModel / AlgebraicDataflowArchitectureModel / src / tests / ASTTest.java
@c-okada c-okada 10 hours ago 2 KB バグ修正
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.setCondition(new PlainStatement("j < 5"));
        forStmt.setUpdaters(Arrays.asList(new PlainStatement("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 PlainStatement("list"));
        Block eForBody = new Block();
        eForBody.addStatement("System.out.println(s);");
        enhancedFor.setBody(eForBody);
        methodBody.addStatement(enhancedFor);

        WhileStatement whileStmt = new WhileStatement();
        whileStmt.setExpression(new PlainStatement("sum < 100"));

        Block whileBody = new Block();
        IfStatement ifStmt = new IfStatement();
        ifStmt.setExpression(new PlainStatement("sum % 2 == 0"));

        Block thenBlock = new Block();
        ExpressionStatement exprStmt = new ExpressionStatement();
        exprStmt.setExpression(new PlainStatement("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 PlainStatement("sum"));
        methodBody.addStatement(returnStmt);

        method.setBody(methodBody);

        System.out.println("--- Generated Code ---");
        System.out.println(method.toString());
    }
}