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 {
@Test
public void test() {
JavaSpecific javaGen = new JavaSpecific();
MethodDeclaration methodDeclaration = javaGen.newMethodDeclaration("Test", JavaSpecific.typeVoid);
Block methodBody = new Block();
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(variableDeclarationStatement);
ForStatement forStatement = javaGen.getForStatementForList("j", "list");
Block forBody = new Block();
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 efStmt = javaGen.getForStatementForCollection(new VariableDeclaration(DataConstraintModel.typeString, "s"), "list");
Block efBody = new Block();
efBody.addStatement("System.out.println(s);");
efStmt.setBody(efBody);
methodBody.addStatement(efStmt);
Term moduloTerm = new Term(new Symbol("%", 2, Symbol.Type.INFIX));
moduloTerm.addChild(new models.algebra.Variable("sum"));
moduloTerm.addChild(new models.algebra.Constant("2"));
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();
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);
methodBody.addStatement(javaGen.getReturnStatement("sum"));
methodDeclaration.setBody(methodBody);
System.out.println("--- Generated Code");
System.out.println(methodDeclaration.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());
}
}