diff --git a/build.gradle b/build.gradle index 82a5dd2..7c71215 100644 --- a/build.gradle +++ b/build.gradle @@ -14,6 +14,11 @@ mavenCentral() } +tasks.withType(JavaExec).configureEach { + jvmArgs '-Dstdout.encoding=UTF-8' + jvmArgs '-Dstderr.encoding=UTF-8' +} + dependencies { implementation project(':RDLApplicationRuntime') implementation project(':RDLCodeGenerator') diff --git a/src/main/java/org/ntlab/rdlworkbench/Main.java b/src/main/java/org/ntlab/rdlworkbench/Main.java index 56afc33..1e0414e 100644 --- a/src/main/java/org/ntlab/rdlworkbench/Main.java +++ b/src/main/java/org/ntlab/rdlworkbench/Main.java @@ -1,7 +1,14 @@ package org.ntlab.rdlworkbench; +import org.ntlab.rdlworkbench.generator.ASTGenerator; +import org.ntlab.rdlworkbench.models.RDLSpec; +import org.ntlab.rdlworkbench.rdlparser.RDLParser; + public class Main { public static void main(String[] args) { - + RDLParser rdl = new RDLParser(); + RDLSpec rdlSpec = rdl.parseRDL(); + ASTGenerator astGenerator = new ASTGenerator(rdlSpec); + System.out.println(astGenerator.generate()); } } diff --git a/src/main/java/org/ntlab/rdlworkbench/generator/ASTGenerator.java b/src/main/java/org/ntlab/rdlworkbench/generator/ASTGenerator.java new file mode 100644 index 0000000..8f2e00f --- /dev/null +++ b/src/main/java/org/ntlab/rdlworkbench/generator/ASTGenerator.java @@ -0,0 +1,630 @@ +package org.ntlab.rdlworkbench.generator; + +import exceptions.SyntaxException; +import org.ntlab.rdlcodegenerator.ast.*; +import org.ntlab.rdlworkbench.models.*; + +import java.util.*; + +public class ASTGenerator { + private static Annotation override = new Annotation("Override"); + private List rdlSpecStatements = new ArrayList<>(); + private RDLSpec spec; + private static final List getValueArgumentList = List.of("tableId", "key", "columnNo"); + private static final List setWidgetTextArgumentList = List.of("scId", "wId", "wText"); + private static final List setWidgetColorArgumentList = List.of("scId", "wId", "wColor"); + private static final List setWidgetVisibilityArgumentList = List.of("scId", "wId", "wVisible"); + private static final List setWidgetEnabledArgumentList = List.of("scId", "wId", "wEnabled"); + private static final List changeScreenArgumentList = List.of("curScId"); + private static final List updateValueArgumentList = List.of("tableId", "key", "columnNo", "wText"); + private static final List getCurWidgetTextArgumentList = List.of("wId"); + private static final List getCurTableKeyArgumentList = List.of("tableId"); + private static final List getWidgetTextArgumentList = List.of("scId", "wId"); + private static final List getCurWidgetStateArgumentList = List.of("wId"); + private static final List setCurWidgetTextArgumentList = List.of("wId", "wText"); + private static final List setCurWidgetVisibilityArgumentList = List.of("wId", "wVisible"); + private static final List setCurWidgetEnabledArgumentList = List.of("wId", "wEnabled"); + + public ASTGenerator (RDLSpec spec) { + this.spec = spec; + rdlSpecStatements = spec.getRDLSpecStatements(); + } + public Codebase generate() { + Codebase codebase = new Codebase(); + TypeDeclaration type = createApplicationEventReceiver(codebase); + TypeDeclaration applicationEventReceiverImplDeclaration = createApplicationConstraintHandlerImpl(codebase); + return codebase; + } + + private TypeDeclaration createApplicationConstraintHandlerImpl(Codebase codebase) { + String name = "ApplicationConstraintHandlerImpl"; + if (codebase.getCompilationUnit(name) != null) { + return codebase.getCompilationUnit(name).types().get(0); + } + TypeDeclaration applicationConstraintHandlerImpl = createClass(codebase, name); + applicationConstraintHandlerImpl.addImplementsInterface("ApplicationConstraintHandler"); + applicationConstraintHandlerImpl.addField(new FieldDeclaration(new SimpleType("ApplicationRuntime"), "runtime")); + MethodDeclaration beforeEventMethod = createMethod(applicationConstraintHandlerImpl, "beforeEvent"); + MethodDeclaration afterEventMethod = createMethod(applicationConstraintHandlerImpl, "afterEvent"); + beforeEventMethod = createBeforeEventMethod(beforeEventMethod); + afterEventMethod = createAfterEventMethod(afterEventMethod); + MethodDeclaration setApplicationRuntime = createMethod(applicationConstraintHandlerImpl, "setApplicationRuntime"); + List parameters = new ArrayList<>(); + parameters.add(new VariableDeclaration(new SimpleType("ApplicationRuntime"), "runtime")); + setApplicationRuntime.setParameters(parameters); + ExpressionStatement expression = new ExpressionStatement(new Assignment(new FieldAccess(new ThisExpression(), "runtime"), new Variable("runtime"))); + setApplicationRuntime.addStatement(expression); + codebase.addCompilationUnit(name, new CompilationUnit(applicationConstraintHandlerImpl)); + return applicationConstraintHandlerImpl; + } + + private MethodDeclaration createBeforeEventMethod(MethodDeclaration beforeEventMethod) { + for (int i = 0; i < rdlSpecStatements.size(); i++) { + //ResourceDeclarationとInitializationを省く + if (rdlSpecStatements.get(i).isRDLSpecResourceDeclaration() || rdlSpecStatements.get(i).isRDLSpecInitialization()) { + continue; + } + Expression beforeEventMethodExpression = new Constant("runtime"); + if (rdlSpecStatements.get(i) instanceof RDLSpecEquationFormula) { + RDLSpecEquationFormula equationFormula = (RDLSpecEquationFormula) rdlSpecStatements.get(i); + RDLSpecEvaluatableTerm leftSideHand = (RDLSpecEvaluatableTerm) equationFormula.getLeftSideHand(); + RDLSpecEvaluatableTerm rightSideHand = (RDLSpecEvaluatableTerm) equationFormula.getRightSideHand(); + if (((Typable) leftSideHand).getStructuralType() == StructuralType.structure || ((Typable) rightSideHand).getStructuralType() == StructuralType.structure) {//構造型の時はskip + continue; + } + String mostLeftResourceInLeftSideHand = getMostLeftResource(leftSideHand); + String mostLeftResourceInRightSideHand = getMostLeftResource(rightSideHand); + if (!(containsDependencyTermOnDependedTerms(leftSideHand) || containsDependencyTermOnDependedTerms(rightSideHand))) {//eventMethod + if (containsStr(leftSideHand, "scId'") || containsStr(rightSideHand, "scId'")) {//afterEvent + continue; + } else if (containsStr(leftSideHand, "scId") || containsStr(rightSideHand, "scId")) {//beforeEvent + HashMap argumentsMap = new HashMap<>(); + if (mostLeftResourceInLeftSideHand.startsWith("w")) {//left input location, right input content + HashMap rightSideHandArgumentsMap = new HashMap<>(); + mostLeftResourceInRightSideHand = setArguments(rightSideHand, null, rightSideHandArgumentsMap, null); + mostLeftResourceInLeftSideHand = setArguments(leftSideHand, null, argumentsMap, null); + if (rightSideHand instanceof RDLSpecResourceConstant) { + if (((RDLSpecResourceConstant) rightSideHand).getValueType().getName().equals("string")) { + argumentsMap.put(mostLeftResourceInLeftSideHand, new Constant("\"" + ((RDLSpecResourceConstant) rightSideHand).getName() + "\"")); + } else { + argumentsMap.put(mostLeftResourceInLeftSideHand, new Constant(((RDLSpecResourceConstant) rightSideHand).getName())); + } + } else { + Expression expression = new Constant("runtime"); + List argumentList = new ArrayList<>(); + expression = createGetWidgetMethodExpression(mostLeftResourceInRightSideHand, expression, argumentList, rightSideHandArgumentsMap); + assert expression instanceof MethodInvocation; + ((MethodInvocation) expression).setArguments(argumentList); + argumentsMap.put(mostLeftResourceInLeftSideHand, expression); + } + List arguments = new ArrayList<>(); + beforeEventMethodExpression = createSetWidgetMethodExpression(mostLeftResourceInLeftSideHand, beforeEventMethodExpression, arguments, argumentsMap); + ((MethodInvocation) beforeEventMethodExpression).setArguments(arguments); + } else if (mostLeftResourceInRightSideHand.startsWith("w")) {//right input location, left input content + HashMap leftSideHandArgumentsMap = new HashMap<>(); + mostLeftResourceInLeftSideHand = setArguments(leftSideHand, null, leftSideHandArgumentsMap, null); + mostLeftResourceInRightSideHand = setArguments(rightSideHand, null, argumentsMap, null); + if (leftSideHand instanceof RDLSpecResourceConstant) { + if (((RDLSpecResourceConstant) leftSideHand).getValueType().getName().equals("string")) { + argumentsMap.put(mostLeftResourceInRightSideHand, new Constant("\"" + ((RDLSpecResourceConstant) leftSideHand).getName() + "\"")); + } else { + argumentsMap.put(mostLeftResourceInRightSideHand, new Constant(((RDLSpecResourceConstant) leftSideHand).getName())); + } + } else { + Expression expression = new Constant("runtime"); + List argumentList = new ArrayList<>(); + expression = createGetWidgetMethodExpression(mostLeftResourceInLeftSideHand, expression, argumentList, leftSideHandArgumentsMap); + assert expression instanceof MethodInvocation; + ((MethodInvocation) expression).setArguments(argumentList); + argumentsMap.put(mostLeftResourceInRightSideHand, expression); + } + List arguments = new ArrayList<>(); + beforeEventMethodExpression = createSetWidgetMethodExpression(mostLeftResourceInRightSideHand, beforeEventMethodExpression, arguments, argumentsMap); + ((MethodInvocation) beforeEventMethodExpression).setArguments(arguments); + } else { + throw new SyntaxException("mostLeftResourceInLeftSideHand or mostLeftResourceInRightSideHand need widget"); + } + ExpressionStatement beforeStatement = new ExpressionStatement(beforeEventMethodExpression); + beforeEventMethod.addStatement(beforeStatement); + } + } else { + continue; + } + } + } + if (beforeEventMethod.getBody() == null) { + beforeEventMethod.setBody(new Block()); + } + return beforeEventMethod; + } + + private MethodDeclaration createAfterEventMethod(MethodDeclaration afterEventMethod) { + for (int i = 0; i < rdlSpecStatements.size(); i++) { + if (rdlSpecStatements.get(i).isRDLSpecResourceDeclaration() || rdlSpecStatements.get(i).isRDLSpecInitialization()) { + continue; + } + Expression afterEventMethodExpression = new Constant("runtime"); + if (rdlSpecStatements.get(i) instanceof RDLSpecEquationFormula) { + RDLSpecEquationFormula equationFormula = (RDLSpecEquationFormula) rdlSpecStatements.get(i); + RDLSpecEvaluatableTerm leftSideHand = (RDLSpecEvaluatableTerm) equationFormula.getLeftSideHand(); + RDLSpecEvaluatableTerm rightSideHand = (RDLSpecEvaluatableTerm) equationFormula.getRightSideHand(); + if (((Typable) leftSideHand).getStructuralType() == StructuralType.structure || ((Typable) rightSideHand).getStructuralType() == StructuralType.structure) {//構造型の時はskip + continue; + } + String mostLeftResourceInLeftSideHand = getMostLeftResource(leftSideHand); + String mostLeftResourceInRightSideHand = getMostLeftResource(rightSideHand); + if (!(containsDependencyTermOnDependedTerms(leftSideHand) || containsDependencyTermOnDependedTerms(rightSideHand))) {//eventMethod + if (containsStr(leftSideHand, "scId'") || containsStr(rightSideHand, "scId'")) {//afterEvent + HashMap argumentsMap = new HashMap<>(); + if (mostLeftResourceInLeftSideHand.startsWith("w")) {//left input location, right input content + HashMap rightSideHandArgumentsMap = new HashMap<>(); + mostLeftResourceInRightSideHand = setArguments(rightSideHand, null, rightSideHandArgumentsMap, null); + mostLeftResourceInLeftSideHand = setArguments(leftSideHand, null, argumentsMap, null); + if (rightSideHand instanceof RDLSpecResourceConstant) { + if (((RDLSpecResourceConstant) rightSideHand).getValueType().getName().equals("string")) { + argumentsMap.put(mostLeftResourceInLeftSideHand, new Constant("\"" + ((RDLSpecResourceConstant) rightSideHand).getName() + "\"")); + } else { + argumentsMap.put(mostLeftResourceInLeftSideHand, new Constant(((RDLSpecResourceConstant) rightSideHand).getName())); + } + } else { + Expression expression = new Constant("runtime"); + List argumentList = new ArrayList<>(); + expression = createGetWidgetMethodExpression(mostLeftResourceInRightSideHand, expression, argumentList, rightSideHandArgumentsMap); + ((MethodInvocation) expression).setArguments(argumentList); + argumentsMap.put(mostLeftResourceInLeftSideHand, expression); + } + List arguments = new ArrayList<>(); + afterEventMethodExpression = createSetWidgetMethodExpression(mostLeftResourceInLeftSideHand, afterEventMethodExpression, arguments, argumentsMap); + ((MethodInvocation) afterEventMethodExpression).setArguments(arguments); + } else if (mostLeftResourceInRightSideHand.startsWith("w")) {//right input location, left input content + HashMap leftSideHandArgumentsMap = new HashMap<>(); + mostLeftResourceInLeftSideHand = setArguments(leftSideHand, null, leftSideHandArgumentsMap, null); + mostLeftResourceInRightSideHand = setArguments(rightSideHand, null, argumentsMap, null); + if (leftSideHand instanceof RDLSpecResourceConstant) { + if (((RDLSpecResourceConstant) leftSideHand).getValueType().getName().equals("string")) { + argumentsMap.put(mostLeftResourceInRightSideHand, new Constant("\"" + ((RDLSpecResourceConstant) leftSideHand).getName() + "\"")); + } else { + argumentsMap.put(mostLeftResourceInRightSideHand, new Constant(((RDLSpecResourceConstant) leftSideHand).getName())); + } + } else { + Expression expression = new Constant("runtime"); + List argumentList = new ArrayList<>(); + expression = createGetWidgetMethodExpression(mostLeftResourceInLeftSideHand, expression, argumentList, leftSideHandArgumentsMap); + assert expression instanceof MethodInvocation; + ((MethodInvocation) expression).setArguments(argumentList); + argumentsMap.put(mostLeftResourceInRightSideHand, expression); + } + List arguments = new ArrayList<>(); + afterEventMethodExpression = createSetWidgetMethodExpression(mostLeftResourceInRightSideHand, afterEventMethodExpression, arguments, argumentsMap); + ((MethodInvocation) afterEventMethodExpression).setArguments(arguments); + } else { + throw new SyntaxException("mostLeftResourceInLeftSideHand or mostLeftResourceInRightSideHand need widget"); + } + ExpressionStatement afterStatement = new ExpressionStatement(afterEventMethodExpression); + afterEventMethod.addStatement(afterStatement); + } else if (containsStr(leftSideHand, "scId") || containsStr(rightSideHand, "scId")) {//beforeEvent + continue; + } + } else { + continue; + } + } + } + if (afterEventMethod.getBody() == null) { + afterEventMethod.setBody(new Block()); + } + return afterEventMethod; + } + + private String setArguments(RDLSpecEvaluatableTerm rdlSpecEvaluatableTerm, String leftResourceName, HashMap arguments, IfStatement ifStatement) {// + List dependedTerms = ((RDLSpecDependencyTerm) rdlSpecEvaluatableTerm).getDependedTerms().stream().map(item -> (RDLSpecEvaluatableTerm) item).toList(); + List argumentTerms = ((RDLSpecDependencyTerm) rdlSpecEvaluatableTerm).getArgumentTerms().stream().map(item -> (RDLSpecEvaluatableTerm) item).toList(); + HashMap expressionMap = new HashMap<>(); + for (int i = 0; i < dependedTerms.size(); i++) { + String dependedTermName; + if (dependedTerms.get(i).isDependencyTerm()) {//ifStatement(今は外側に一つだけだと仮定する + HashMap dependedTermsArguments = new HashMap<>(); + dependedTermName = setArguments(dependedTerms.get(i), null, dependedTermsArguments, null); + if (dependedTermName.contains("'")) {//コンマを除ける + dependedTermName = dependedTermName.substring(0, dependedTermName.length() - 1); + } + if (argumentTerms.get(i) instanceof RDLSpecResourceConstant) { + ifStatement = createConditionalExpression(dependedTermsArguments, dependedTermName, new Constant("\"" + ((RDLSpecResourceConstant) argumentTerms.get(i)).getName() + "\""), ifStatement); + } else if (argumentTerms.get(i).isDependencyTerm()) { + HashMap argumentTermsArguments = new HashMap<>(); + String argumentTermDependingTermName = setArguments(argumentTerms.get(i), null, argumentTermsArguments, null); + if (argumentTermDependingTermName.contains("'")) {//コンマを除ける + argumentTermDependingTermName = argumentTermDependingTermName.substring(0, argumentTermDependingTermName.length() - 1); + } + Expression expression = new Constant("runtime"); + List argumentList = new ArrayList<>(); + expression = createGetWidgetMethodExpression(argumentTermDependingTermName, expression, argumentList, argumentTermsArguments); + assert expression instanceof MethodInvocation; + ((MethodInvocation) expression).setArguments(argumentList); + ifStatement = createConditionalExpression(dependedTermsArguments, dependedTermName, expression, ifStatement); + } + continue; + } else if (dependedTerms.get(i) instanceof RDLSpecResourceConstant) { + throw new SyntaxException("resource constant must not here"); + } else { + dependedTermName = ((RDLSpecResource) dependedTerms.get(i)).getName(); + } + if (dependedTermName.contains("'")) {//コンマを除ける + dependedTermName = dependedTermName.substring(0, dependedTermName.length() - 1); + } + if (argumentTerms.get(i).isDependencyTerm()) { + HashMap argumentTermsArguments = new HashMap<>(); + String argumentLeftResourceName = setArguments(argumentTerms.get(i), null, argumentTermsArguments, null); + Expression expression = new Constant("runtime"); + List argumentList = new ArrayList<>(); + expression = createGetWidgetMethodExpression(argumentLeftResourceName, expression, argumentList, argumentTermsArguments); + assert expression instanceof MethodInvocation; + ((MethodInvocation) expression).setArguments(argumentList); + expressionMap.put(argumentLeftResourceName, expression); + } else if (argumentTerms.get(i) instanceof RDLSpecResourceConstant) { + if (((RDLSpecResourceConstant) argumentTerms.get(i)).getValueType().getName().equals("string")) { + expressionMap.put(dependedTermName, new Constant("\"" + ((RDLSpecResourceConstant) argumentTerms.get(i)).getName() + "\"")); + } else { + expressionMap.put(dependedTermName, new Constant(((RDLSpecResourceConstant) argumentTerms.get(i)).getName())); + } + } else if (argumentTerms.get(i) instanceof RDLSpecResource) { + expressionMap.put(dependedTermName, null);//currentのものを取ってくるとき + } + } + for (String arg : expressionMap.keySet()) { + arguments.put(arg, expressionMap.get(arg)); + } + RDLSpecEvaluatableTerm dependingTerm = (RDLSpecEvaluatableTerm) ((RDLSpecDependencyTerm) rdlSpecEvaluatableTerm).getDependingTerm(); + if (dependingTerm.isDependencyTerm()) { + leftResourceName = setArguments(dependingTerm, leftResourceName, arguments, null); + } else if (dependingTerm instanceof RDLSpecResource) { + leftResourceName = ((RDLSpecResource) dependingTerm).getName(); + } else { + throw new SyntaxException("resource constant must not here"); + } + if (leftResourceName.contains("'")) { + leftResourceName = leftResourceName.substring(0, leftResourceName.length() - 1); + } + return leftResourceName; + } + + private TypeDeclaration createApplicationEventReceiver(Codebase codebase) { + String name = "ApplicationEventReceiverImpl"; + if (codebase.getCompilationUnit(name) != null) { + return codebase.getCompilationUnit(name).types().get(0); + } + TypeDeclaration applicationEventReceiverImplDeclaration = createClass(codebase, name); + applicationEventReceiverImplDeclaration.addImplementsInterface("ApplicationEventReceiver"); + applicationEventReceiverImplDeclaration.addField(new FieldDeclaration(new SimpleType("ApplicationRuntime"), "runtime")); + createOnWidgetChangedMethod(applicationEventReceiverImplDeclaration); + MethodDeclaration setApplicationRuntime = createMethod(applicationEventReceiverImplDeclaration, "setApplicationRuntime"); + List parameters = new ArrayList<>(); + parameters.add(new VariableDeclaration(new SimpleType("ApplicationRuntime"), "runtime")); + setApplicationRuntime.setParameters(parameters); + ExpressionStatement expression = new ExpressionStatement(new Assignment(new FieldAccess(new ThisExpression(), "runtime"), new Variable("runtime"))); + setApplicationRuntime.addStatement(expression); + codebase.addCompilationUnit(name, new CompilationUnit(applicationEventReceiverImplDeclaration)); + return applicationEventReceiverImplDeclaration; + } + + private void createOnWidgetChangedMethod(TypeDeclaration applicationEventReceiverImplDeclaration) { + record EvaluatableTermRecord(List dependedTerms, List argumentTerms) {} + HashMap methodDeclarationMap = new HashMap<>(); + HashMap> ifMethodEqualsMaps = new HashMap<>();//if文の管理 + for (int i = 0; i < rdlSpecStatements.size(); i++) { + if (rdlSpecStatements.get(i).isRDLSpecResourceDeclaration() || rdlSpecStatements.get(i).isRDLSpecInitialization()) { + continue; + } + if (rdlSpecStatements.get(i) instanceof RDLSpecEquationFormula) { + RDLSpecEquationFormula equationFormula = (RDLSpecEquationFormula) rdlSpecStatements.get(i); + RDLSpecEvaluatableTerm leftSideHand = (RDLSpecEvaluatableTerm) equationFormula.getLeftSideHand(); + RDLSpecEvaluatableTerm rightSideHand = (RDLSpecEvaluatableTerm) equationFormula.getRightSideHand(); + if (containsDependencyTermOnDependedTerms(leftSideHand)) { + IfStatement ifStatement = new IfStatement(); + HashMap argumentsMap = new HashMap<>(); + String mostLeftResourceInLeftSideHand = setArguments(leftSideHand, null, argumentsMap, ifStatement); + List dependedTerms = ((RDLSpecDependencyTerm) leftSideHand).getDependedTerms().stream().map(item -> (RDLSpecEvaluatableTerm) item).toList(); + List argumentTerms = ((RDLSpecDependencyTerm) leftSideHand).getArgumentTerms().stream().map(item -> (RDLSpecEvaluatableTerm) item).toList(); + String variable = getMostLeftResource(dependedTerms.get(0)).substring(1); + MethodDeclaration onWidgetChangedMethod; + if (methodDeclarationMap.containsKey(variable)) { + onWidgetChangedMethod = methodDeclarationMap.get(variable); + } else { + onWidgetChangedMethod = createMethod(applicationEventReceiverImplDeclaration, "onWidget" + variable + "Changed"); + methodDeclarationMap.put(variable, onWidgetChangedMethod); + onWidgetChangedMethod.addAnnotation(override); + VariableDeclaration screenId = new VariableDeclaration(new SimpleType("String"), "scId"); + VariableDeclaration widgetId = new VariableDeclaration(new SimpleType("String"), "wId"); + VariableDeclaration value = new VariableDeclaration(new SimpleType("String"), getMostLeftResource(dependedTerms.get(0))); + List variableDeclarations = new ArrayList<>(); + variableDeclarations.add(screenId); + variableDeclarations.add(widgetId); + variableDeclarations.add(value); + onWidgetChangedMethod.setParameters(variableDeclarations); + ifMethodEqualsMaps.put(variable, new HashMap<>()); + } + + if (ifMethodEqualsMaps.get(variable).containsKey(new EvaluatableTermRecord(dependedTerms, argumentTerms))) { + ifStatement = ifMethodEqualsMaps.get(variable).get(new EvaluatableTermRecord(dependedTerms, argumentTerms)); + } else { + ifMethodEqualsMaps.get(variable).put(new EvaluatableTermRecord(dependedTerms, argumentTerms), ifStatement); + onWidgetChangedMethod.addStatement(ifStatement); + } + + ifStatement = createThenStatement(ifStatement, mostLeftResourceInLeftSideHand, rightSideHand, leftSideHand, argumentsMap); + } else if (containsDependencyTermOnDependedTerms(rightSideHand)) { + //TODO + } + } + } + } + + private Expression createSetWidgetMethodExpression(String variable, Expression method, List arguments, HashMap argumentsMap) { + if (variable.equals("wVisible")) { + method = new MethodInvocation(method, "getUIState"); + if (argumentsMap.containsKey("scId") && argumentsMap.get("scId") == null) { + method = new MethodInvocation(method, "setCurWidgetVisibility"); + for (String arg : setCurWidgetVisibilityArgumentList) { + arguments.add(argumentsMap.get(arg)); + } + } else { + method = new MethodInvocation(method, "setWidgetVisibility"); + for (String arg : setWidgetVisibilityArgumentList) { + arguments.add(argumentsMap.get(arg)); + } + } + } else if (variable.equals("wText")) { + method = new MethodInvocation(method, "getUIState"); + if (argumentsMap.containsKey("scId") && argumentsMap.get("scId") == null) { + method = new MethodInvocation(method, "setCurWidgetText"); + for (String arg : setCurWidgetTextArgumentList) { + arguments.add(argumentsMap.get(arg)); + } + } else { + method = new MethodInvocation(method, "setWidgetText"); + for (String arg : setWidgetTextArgumentList) { + arguments.add(argumentsMap.get(arg)); + } + } + } else if (variable.equals("wColor")) { + method = new MethodInvocation(method, "getUIState"); + method = new MethodInvocation(method, "setWidgetColor"); + for (String arg : setWidgetColorArgumentList) { + arguments.add(argumentsMap.get(arg)); + } + } else if (variable.equals("wEnable")) { + method = new MethodInvocation(method, "getUIState"); + if (argumentsMap.containsKey("scId") && argumentsMap.get("scId") == null) { + method = new MethodInvocation(method, "setCurWidgetEnabled"); + for (String arg : setCurWidgetEnabledArgumentList) { + arguments.add(argumentsMap.get(arg)); + } + } else { + method = new MethodInvocation(method, "setWidgetEnabled"); + for (String arg : setWidgetEnabledArgumentList) { + arguments.add(argumentsMap.get(arg)); + } + } + } else if (variable.equals("curScId")) { + method = new MethodInvocation(method, "getUIState"); + method = new MethodInvocation(method, "changeScreen"); + for (String arg : changeScreenArgumentList) { + arguments.add(argumentsMap.get(arg)); + } + } else if (variable.equals("columnValue")) { + method = new MethodInvocation(method, "getTableState"); + method = new MethodInvocation(method, "updateValue"); + for (String arg : updateValueArgumentList) { + if (arg.equals("key") && argumentsMap.get(arg) == null) { + Expression keyExpression = new Constant("runtime"); + keyExpression = new MethodInvocation(keyExpression, "getCurTableKey", List.of(argumentsMap.get(getCurTableKeyArgumentList.get(0)))); + arguments.add(keyExpression); + } else { + arguments.add(argumentsMap.get(arg)); + } + } + } else { + + } + return method; + } + + private Expression createGetWidgetMethodExpression(String variable, Expression method, List arguments, HashMap argumentsMap) { + if (variable.equals("wText")) { + method = new MethodInvocation(method, "getUIState"); + if (argumentsMap.containsKey("scId") && argumentsMap.get("scId") == null) { + method = new MethodInvocation(method, "getCurWidgetText"); + for (String arg : getCurWidgetTextArgumentList) { + arguments.add(argumentsMap.get(arg)); + } + } else { + method = new MethodInvocation(method, "getWidgetText"); + for (String arg : getWidgetTextArgumentList) { + arguments.add(argumentsMap.get(arg)); + } + } + } else if (variable.equals("columnValue")) { + method = new MethodInvocation(method, "getTableState"); + method = new MethodInvocation(method, "getValue"); + for (String arg : getValueArgumentList) { + if (arg.equals("key") && argumentsMap.get(arg) == null) { + Expression keyExpression = new Constant("runtime"); + keyExpression = new MethodInvocation(keyExpression, "getCurTableKey", List.of(argumentsMap.get(getCurTableKeyArgumentList.get(0)))); + arguments.add(keyExpression); + } else { + arguments.add(argumentsMap.get(arg)); + } + } + } else if (variable.equals("wState")) { + method = new MethodInvocation(method, "getUIState"); + if (argumentsMap.containsKey("scId") && argumentsMap.get("scId") == null) { + method = new MethodInvocation(method, "getCurWidgetState"); + for (String arg : getCurWidgetStateArgumentList) { + arguments.add(argumentsMap.get(arg)); + } + } else { + throw new SyntaxException("state is getCurWidgetState only"); + } + } + return method; + } + + private String getMostLeftResource(RDLSpecEvaluatableTerm rdlSpecEvaluatableTerm) { + //only Resource + if (rdlSpecEvaluatableTerm instanceof RDLSpecResource) { + return ((RDLSpecResource) rdlSpecEvaluatableTerm).getName(); + } else if (rdlSpecEvaluatableTerm instanceof RDLSpecResourceConstant) { + return ((RDLSpecResourceConstant) rdlSpecEvaluatableTerm).getName(); + } + RDLSpecEvaluatableTerm currentEvaluatableTerm = rdlSpecEvaluatableTerm; + while (true) { + if (currentEvaluatableTerm.isDependencyTerm()) { + currentEvaluatableTerm = (RDLSpecEvaluatableTerm) ((RDLSpecDependencyTerm) currentEvaluatableTerm).getDependingTerm(); + } else { + break; + } + } + if (currentEvaluatableTerm instanceof RDLSpecResource) { + return ((RDLSpecResource) currentEvaluatableTerm).getName(); + } else if (currentEvaluatableTerm instanceof RDLSpecResourceConstant) { + return ((RDLSpecResourceConstant) currentEvaluatableTerm).getName(); + } else { + throw new SyntaxException("currentEvaluatableTerm is not Resource in getMostLeftResource"); + } + } + + private static boolean containsDependencyTermOnDependedTerms(RDLSpecEvaluatableTerm evaluatableTerm) { + if (evaluatableTerm.isDependencyTerm()) { + List dependedTerms = ((RDLSpecDependencyTerm) evaluatableTerm).getDependedTerms().stream().map(item -> (RDLSpecEvaluatableTerm) item).toList(); + for (RDLSpecEvaluatableTerm terms : dependedTerms) { + if (terms.isDependencyTerm()) { + return true; + } + } + return false; + } else { + return false; + } + } + + private boolean containsStr(RDLSpecEvaluatableTerm evaluatableTerm, String str) { + if (evaluatableTerm instanceof RDLSpecResource) { + String name = ((RDLSpecResource) evaluatableTerm).getName(); + if (name.contains(str)) { + return true; + } else { + return false; + } + } else if (evaluatableTerm instanceof RDLSpecResourceConstant) { + return false; + } else if (evaluatableTerm.isDependencyTerm()) { + RDLSpecEvaluatableTerm dependingTerm = (RDLSpecEvaluatableTerm) ((RDLSpecDependencyTerm) evaluatableTerm).getDependingTerm(); + List dependedTerms = ((RDLSpecDependencyTerm) evaluatableTerm).getDependedTerms().stream().map(item -> (RDLSpecEvaluatableTerm) item).toList(); + List argumentTerms = ((RDLSpecDependencyTerm) evaluatableTerm).getArgumentTerms().stream().map(item -> (RDLSpecEvaluatableTerm) item).toList(); + if (containsStr(dependingTerm, str)) { + return true; + } + for (int i = 0; i < dependedTerms.size(); i++) { + if (containsStr(dependedTerms.get(i), str)) { + return true; + } + } + for (int i = 0; i < argumentTerms.size(); i++) { + if (containsStr(argumentTerms.get(i), str)) { + return true; + } + } + return false; + } else { + return false; + } + } + + private TypeDeclaration createClass(Codebase codebase, String name) { + if (codebase.getCompilationUnit(name) != null) { + return codebase.getCompilationUnit(name).types().get(0); + } + TypeDeclaration type = new TypeDeclaration(name); + CompilationUnit compilationUnit = new CompilationUnit(type); + codebase.addCompilationUnit(name, compilationUnit); + return type; + } + + private MethodDeclaration createMethod(TypeDeclaration type, String name) { + for (MethodDeclaration method : type.getMethods()) { + if (method.getName().equals(name)) return method; + } + MethodDeclaration method = new MethodDeclaration(name); + type.addMethod(method); + return method; + } + + private IfStatement createConditionalExpression(HashMap leftToRightMap, String variable, Expression value, IfStatement ifStatement) { + ArrayList equalsMethods = new ArrayList<>(); + ArrayList arguments = new ArrayList<>(); + arguments.add(value); + equalsMethods.add(new MethodInvocation(new Variable(variable), "equals", arguments)); + for (String left : leftToRightMap.keySet()) { + Expression right = leftToRightMap.get(left); + arguments = new ArrayList<>(); + arguments.add(right); + equalsMethods.add(new MethodInvocation(new Variable(left), "equals", arguments)); + } + InfixExpression infixExpression = null; + if (equalsMethods.size() == 1) { + ParenthesizedExpression parenthesizedExpression = new ParenthesizedExpression(equalsMethods.get(0)); + ifStatement.setExpression(parenthesizedExpression); + ifStatement.setThenStatement(new Block()); + return ifStatement; + } else { + for (int i = 0; i < equalsMethods.size() - 1; i++) { + InfixExpression andInfixExpression = new InfixExpression("&&", equalsMethods.get(i), equalsMethods.get(i + 1)); + if (i == 0) { + infixExpression = andInfixExpression; + } else { + infixExpression = new InfixExpression("&&", infixExpression, equalsMethods.get(i + 1)); + } + } + ParenthesizedExpression parenthesizedExpression = new ParenthesizedExpression(infixExpression); + ifStatement.setExpression(parenthesizedExpression); + ifStatement.setThenStatement(new Block()); + return ifStatement; + } + } + + private IfStatement createThenStatement(IfStatement ifStatement, String mostLeftResourceInLeftSideHand, RDLSpecEvaluatableTerm rightSideHand, RDLSpecEvaluatableTerm leftSideHand, HashMap argumentsMap) { + if (rightSideHand instanceof RDLSpecResourceConstant) { + if (((RDLSpecResourceConstant) rightSideHand).getValueType().getName().equals("string")) { + argumentsMap.put(mostLeftResourceInLeftSideHand, new Constant("\"" + ((RDLSpecResourceConstant) rightSideHand).getName() + "\"")); + } else { + argumentsMap.put(mostLeftResourceInLeftSideHand, new Constant(((RDLSpecResourceConstant) rightSideHand).getName())); + } + } else { + HashMap rightSideHandArguments = new HashMap<>(); + String mostLeftResourceInRightSideHand = setArguments(rightSideHand, null, rightSideHandArguments, null); + Expression expression = new Constant("runtime"); + List argumentList = new ArrayList<>(); + expression = createGetWidgetMethodExpression(mostLeftResourceInRightSideHand, expression, argumentList, rightSideHandArguments); + assert expression instanceof MethodInvocation; + ((MethodInvocation) expression).setArguments(argumentList); + argumentsMap.put(mostLeftResourceInRightSideHand, expression); + } + List arguments = new ArrayList<>(); + Expression expression = new Constant("runtime"); + expression = createSetWidgetMethodExpression(mostLeftResourceInLeftSideHand, expression, arguments, argumentsMap); + assert expression instanceof MethodInvocation; + ((MethodInvocation) expression).setArguments(arguments); + ExpressionStatement expressionStatement = new ExpressionStatement(expression); + Block block = new Block(); + if (ifStatement.getThenStatement() != null) { + block = (Block) ifStatement.getThenStatement(); + } + if (expressionStatement.toString().contains("changeScreen")) { + block.addStatement(expressionStatement); + } else { + block.addFirstStatement(expressionStatement); + } + ifStatement.setThenStatement(block); + return ifStatement; + } +} diff --git a/src/main/java/org/ntlab/rdlworkbench/generator/Codebase.java b/src/main/java/org/ntlab/rdlworkbench/generator/Codebase.java new file mode 100644 index 0000000..9acb7ff --- /dev/null +++ b/src/main/java/org/ntlab/rdlworkbench/generator/Codebase.java @@ -0,0 +1,45 @@ +package org.ntlab.rdlworkbench.generator; + +import org.ntlab.rdlcodegenerator.ast.CompilationUnit; + +import java.util.*; + +public class Codebase { + private Map classes = new HashMap<>(); + private Map interfaces = new HashMap<>(); + + public Codebase() { + } + + public Collection getCompilationUnits() { + return classes.values(); + } + + public CompilationUnit getCompilationUnit(String name) { + return classes.get(name); + } + + public void addCompilationUnit(String name, CompilationUnit compilationUnit) { + classes.put(name, compilationUnit); + } + + public CompilationUnit getInterfaceCompilationUnit(String name) { + return interfaces.get(name); + } + + public void addInterfaceCompilationUnit(String name, CompilationUnit compilationUnit) { + interfaces.put(name, compilationUnit); + } + + @Override + public String toString() { + StringBuilder code = new StringBuilder(); + for (CompilationUnit cu : classes.values()) { + code.append(cu.toString()); + } + for (CompilationUnit cu : interfaces.values()) { + code.append(cu.toString()); + } + return code.toString(); + } +} diff --git a/src/main/java/org/ntlab/rdlworkbench/rdlparser/RDLParser.java b/src/main/java/org/ntlab/rdlworkbench/rdlparser/RDLParser.java index d2f0689..f5e3c74 100644 --- a/src/main/java/org/ntlab/rdlworkbench/rdlparser/RDLParser.java +++ b/src/main/java/org/ntlab/rdlworkbench/rdlparser/RDLParser.java @@ -420,7 +420,6 @@ private ArrayList splitByDoubleQuotation(String line) { ArrayList newTokens = new ArrayList<>(); String[] tokens = line.split(DOUBLE_QUOT); - System.out.println(line); boolean fFirstToken = true; for (int i = 0; i < tokens.length; i++) { String token = tokens[i];