From 5fd1e616e6f9a0c353825c1200d672c8f89b3594 Mon Sep 17 00:00:00 2001 From: Kevin Lano Date: Fri, 20 Sep 2024 15:14:57 +0100 Subject: [PATCH] extended assignments --- BSystemTypes.java | 105 ++++++++++++- BasicExpression.java | 338 +++++++++++++++++++++++++++++++++++++++++- BinaryExpression.java | 109 +++++++++++++- Compiler2.java | 59 +++++++- SetExpression.java | 3 +- Type.java | 91 +++++++++++- UCDArea.java | 9 +- UnaryExpression.java | 17 ++- 8 files changed, 702 insertions(+), 29 deletions(-) diff --git a/BSystemTypes.java b/BSystemTypes.java index 5abe3555..757addec 100644 --- a/BSystemTypes.java +++ b/BSystemTypes.java @@ -6689,7 +6689,31 @@ public static String generateConcatOp() " res.addAll(a); \n" + " res.addAll(b); \n" + " return res;\n" + - " }\n"; + " }\n\n"; + + res = res + + " public static Vector singleValueMatrix(Vector sh, Object x)\n" + + " { Vector result;\n" + + " if ((sh).size() == 0)\n" + + " { return (new Vector()); }\n" + + " int n = (int) sh.get(0);\n" + + " \n" + + " if ((sh).size() == 1)\n" + + " { Vector _results_0 = new Vector();\n" + + " for (int _i = 0; _i < n; _i++)\n" + + " { _results_0.add(x); }\n" + + " return _results_0;\n" + + " }\n" + + "\n" + + " Vector tl = (Vector) Set.tail(sh);\n" + + " Vector res = (new Vector());\n" + + " Vector _results_1 = new Vector();\n" + + " for (int _i = 0; _i < n; _i++)\n" + + " { _results_1.add(Set.singleValueMatrix(tl,x)); }\n" + + " res = (Vector) Set.concatenate(res, _results_1);\n" + + " return res;\n" + + " }\n\n"; + return res; } @@ -6699,18 +6723,66 @@ public static String generateConcatOpJava6() " res.addAll(a); \n" + " res.addAll(b); \n" + " return res;\n" + - " }\n"; + " }\n\n"; + + res = res + + " public static ArrayList singleValueMatrix(ArrayList sh, Object x)\n" + + " { ArrayList result;\n" + + " if (sh.size() == 0)\n" + + " { return new ArrayList(); }\n" + + " int n = (int) sh.get(0);\n" + + " \n" + + " if (sh.size() == 1)\n" + + " { ArrayList _results_0 = new ArrayList();\n" + + " for (int _i = 0; _i < n; _i++)\n" + + " { _results_0.add(x); }\n" + + " return _results_0;\n" + + " }\n" + + "\n" + + " ArrayList tl = (ArrayList) Set.tail(sh);\n" + + " ArrayList res = new ArrayList();\n" + + " ArrayList _results_1 = new ArrayList();\n" + + " for (int _i = 0; _i < n; _i++)\n" + + " { _results_1.add(Set.singleValueMatrix(tl,x)); }\n" + + " res = (ArrayList) Set.concatenate(res, _results_1);\n" + + " return res;\n" + + " }\n\n"; + return res; } public static String generateConcatOpJava7() - { String res = " public static ArrayList concatenate(Collection a, Collection b)\n" + + { String res = " public static ArrayList concatenate(Collection a, Collection b)\n" + " { ArrayList res = new ArrayList(); \n" + " res.addAll(a); \n" + " res.addAll(b); \n" + " return res;\n" + - " }\n"; - return res; + " }\n\n"; + + res = res + + " public static ArrayList singleValueMatrix(ArrayList sh,Object x)\n" + + " { ArrayList result;\n" + + " if ((sh).size() == 0)\n" + + " { return (new ArrayList()); }\n" + + " int n = (int) sh.get(0);\n" + + " \n" + + " if ((sh).size() == 1)\n" + + " { ArrayList _results_0 = new ArrayList();\n" + + " for (int _i = 0; _i < n; _i++)\n" + + " { _results_0.add(x); }\n" + + " return _results_0;\n" + + " }\n" + + "\n" + + " ArrayList tl = Ocl.tail(sh);\n" + + " ArrayList res = (new ArrayList());\n" + + " ArrayList> _results_1 = new ArrayList>();\n" + + " for (int _i = 0; _i < n; _i++)\n" + + " { _results_1.add(Ocl.singleValueMatrix(tl,x)); }\n" + + " res = Ocl.concatenate(res, _results_1);\n" + + " return res;\n" + + " }\n\n"; + + return res; } public static String generateConcatOpCSharp() @@ -6738,6 +6810,29 @@ public static String generateConcatOpCSharp() " return res;\n" + " }\n\n"; + res = res + + " public static ArrayList singleValueMatrix(ArrayList sh, object x)\n" + + " { ArrayList result;\n" + + " if (sh.Count == 0)\n" + + " { return new ArrayList(); }\n" + + " int n = (int) sh[0];\n" + + " \n" + + " if (sh.Count == 1)\n" + + " { ArrayList _results_0 = new ArrayList();\n" + + " for (int _i = 0; _i < n; _i++)\n" + + " { _results_0.Add(x); }\n" + + " return _results_0;\n" + + " }\n" + + "\n" + + " ArrayList tl = (ArrayList) SystemTypes.tail(sh);\n" + + " ArrayList res = new ArrayList();\n" + + " ArrayList _results_1 = new ArrayList();\n" + + " for (int _i = 0; _i < n; _i++)\n" + + " { _results_1.Add(SystemTypes.singleValueMatrix(tl,x)); }\n" + + " res = (ArrayList) SystemTypes.concatenate(res, _results_1);\n" + + " return res;\n" + + " }\n\n"; + return res; } diff --git a/BasicExpression.java b/BasicExpression.java index d334ee40..05701150 100644 --- a/BasicExpression.java +++ b/BasicExpression.java @@ -369,6 +369,9 @@ public void setVariableElementType(Type et) { variable.setElementType(et); } } + public void setArrayType(Type tt) + { arrayType = tt; } + public static BasicExpression newASTBasicExpression(ASTTerm t) { BasicExpression res = new BasicExpression(""); if (t instanceof ASTBasicTerm) @@ -11069,6 +11072,12 @@ public static String updateFormEqIndex(Expression obj, Expression ind, { // obj[ind] = val2 where obj is complex expression, // either a sequence, ref or map, or // itself an indexed expression + + Type objt = obj.getType(); + if (objt == null) + { return "/* No type for: " + obj + " */"; } + + Type objet = objt.getElementType(); if (ind != null) { String indopt = ind.queryForm(env,local); @@ -11076,10 +11085,50 @@ public static String updateFormEqIndex(Expression obj, Expression ind, String wind = ind.wrap(indopt); String wval = var.wrap(val2); - if (ind.type != null && "String".equals(ind.type.getName())) + if (ind.type != null && + "String".equals(ind.type.getName())) { return "((Map) " + lexp + ").put(" + wind + ", " + wval + ");"; } // map[ind] = val2 else if (obj.isRef()) { return lexp + "[" + indopt + " -1] = " + val2 + ";"; } + else if (Type.isSequenceType(objet) && + (var.isNumeric() || var.isString() || + var.isBoolean())) + { // obj[ind] := + // MatrixLib.singleValueMatrix( + // Sequence{obj[ind]->size()}, var) + + Expression col = + new BinaryExpression("->at", obj, ind); + + SetExpression setexpr = + new SetExpression(true); + Expression uex = new UnaryExpression("->size", col); + uex.setType(new Type("int", null)); + setexpr.addElement(uex); + setexpr.setElementType(new Type("int", null)); + + String setocl = + "(new SystemTypes.Set().add( " + + uex.queryForm(env,local) + " )).getElements()"; + + + Vector rangepars = new Vector(); + rangepars.add(setexpr); + rangepars.add(var); + + BasicExpression scope = + BasicExpression.newStaticCallBasicExpression( + "singleValueMatrix", "Set", rangepars); + scope.setType(new Type("Sequence", null)); + scope.setElementType(var.getType()); + scope.umlkind = QUERY; + + + return + BasicExpression.updateFormEqIndex(obj,ind, + "Set.singleValueMatrix(" + setocl + ", " + val2 + ")", + scope, env, local); + } else { return "((Vector) " + lexp + ").set((" + indopt + " -1), " + wval + ");"; } } @@ -11091,6 +11140,12 @@ public static String updateFormEqIndexJava6(Expression obj, Expression ind, { // obj[ind] = val2 where obj is complex expression, // either a sequence or map, or // itself an indexed expression + + Type objt = obj.getType(); + if (objt == null) + { return "/* No type for: " + obj + " */"; } + + Type objet = objt.getElementType(); if (ind != null) { String indopt = ind.queryFormJava6(env,local); @@ -11102,6 +11157,43 @@ public static String updateFormEqIndexJava6(Expression obj, Expression ind, { return "((HashMap) " + lexp + ").put(" + wind + ", " + wval + ");"; } // map[ind] = val2 else if (obj.isRef()) { return lexp + "[" + indopt + " -1] = " + val2 + ";"; } + else if (Type.isSequenceType(objet) && + (var.isNumeric() || var.isString() || + var.isBoolean())) + { // obj[ind] := + // MatrixLib.singleValueMatrix( + // Sequence{obj[ind]->size()}, var) + + Expression col = + new BinaryExpression("->at", obj, ind); + + SetExpression setexpr = + new SetExpression(true); + Expression uex = new UnaryExpression("->size", col); + uex.setType(new Type("int", null)); + setexpr.addElement(uex); + setexpr.setElementType(new Type("int", null)); + + String setocl = + "Set.addSequence(new ArrayList(), " + + uex.queryFormJava6(env,local) + " )"; + + Vector rangepars = new Vector(); + rangepars.add(setexpr); + rangepars.add(var); + + BasicExpression scope = + BasicExpression.newStaticCallBasicExpression( + "singleValueMatrix", "Set", rangepars); + scope.setType(new Type("Sequence", null)); + scope.setElementType(var.getType()); + scope.umlkind = QUERY; + + return + BasicExpression.updateFormEqIndexJava6(obj,ind, + "Set.singleValueMatrix(" + setocl + ", " + val2 + ")", + scope, env, local); + } else { return "((ArrayList) " + lexp + ").set((" + indopt + " -1), " + wval + ");"; } } @@ -11114,8 +11206,9 @@ public static String updateFormEqIndexJava7(Expression obj, Expression ind, // either a sequence or map, or // itself an indexed expression - System.out.println(">>> Update to " + obj + "[" + ind + - "] := " + val2); + JOptionPane.showInputDialog( + ">>> Update to " + obj + "[" + ind + + "] := " + val2); Type objt = obj.getType(); if (objt == null) @@ -11133,7 +11226,48 @@ public static String updateFormEqIndexJava7(Expression obj, Expression ind, if (objt.isMapType()) { return "((" + j7type + ") " + lexp + ").put(" + wind + ", " + wval + ");"; } // map[ind] = val2 else if (obj.isRef()) - { return lexp + "[" + indopt + " -1] = " + val2 + ";"; } + { return lexp + "[" + indopt + " -1] = " + val2 + ";"; } + else if (Type.isSequenceType(objet) && + (var.isNumeric() || var.isString() || + var.isBoolean())) + { // obj[ind] := + // MatrixLib.singleValueMatrix( + // Sequence{obj[ind]->size()}, var) + + Expression col = + new BinaryExpression("->at", obj, ind); + + SetExpression setexpr = + new SetExpression(true); + Expression uex = new UnaryExpression("->size", col); + uex.setType(new Type("int", null)); + setexpr.addElement(uex); + setexpr.setElementType(new Type("int", null)); + + String setocl = + "Ocl.addSequence(new ArrayList(), " + uex + ")"; + // String setocl = + // "(new SystemTypes.Set().add( " + + // uex.queryForm(env,local) + " )).getElements()"; + + + Vector rangepars = new Vector(); + rangepars.add(setexpr); + rangepars.add(var); + + BasicExpression scope = + BasicExpression.newStaticCallBasicExpression( + "singleValueMatrix", "Ocl", rangepars); + scope.setType(new Type("Sequence", null)); + scope.setElementType(var.getType()); + scope.umlkind = QUERY; + + + return + BasicExpression.updateFormEqIndexJava7(obj,ind, + "Ocl.singleValueMatrix(" + setocl + ", " + val2 + ")", + scope, env, local); + } else if (obj instanceof BasicExpression) { Vector pars = new Vector(); // setatpars.add(obj); @@ -11160,6 +11294,12 @@ public static String updateFormEqIndexCSharp(Expression obj, Expression ind, { // obj[ind] = val2 where obj is complex expression, // either a sequence, reference or map, or // itself an indexed expression + + Type objt = obj.getType(); + if (objt == null) + { return "/* No type for: " + obj + " */"; } + + Type objet = objt.getElementType(); if (ind != null) { String indopt = ind.queryFormCSharp(env,local); @@ -11172,6 +11312,43 @@ public static String updateFormEqIndexCSharp(Expression obj, Expression ind, { return "((Hashtable) " + lexp + ")[" + wind + "] = " + wval + ";"; } // map[ind] = val2 else if (Type.isReferenceType(obj.type)) { return lexp + "[" + indopt + "-1] = " + val2 + ";"; } + else if (Type.isSequenceType(objet) && + (var.isNumeric() || var.isString() || + var.isBoolean())) + { // obj[ind] := + // MatrixLib.singleValueMatrix( + // Sequence{obj[ind]->size()}, var) + + Expression col = + new BinaryExpression("->at", obj, ind); + + SetExpression setexpr = + new SetExpression(true); + Expression uex = new UnaryExpression("->size", col); + uex.setType(new Type("int", null)); + setexpr.addElement(uex); + setexpr.setElementType(new Type("int", null)); + + String setocl = + "SystemTypes.addSet(new ArrayList(), " + + uex.queryFormCSharp(env,local) + " )"; + + Vector rangepars = new Vector(); + rangepars.add(setexpr); + rangepars.add(var); + + BasicExpression scope = + BasicExpression.newStaticCallBasicExpression( + "singleValueMatrix", "SystemTypes", rangepars); + scope.setType(new Type("Sequence", null)); + scope.setElementType(var.getType()); + scope.umlkind = QUERY; + + return + BasicExpression.updateFormEqIndexCSharp(obj,ind, + "SystemTypes.singleValueMatrix(" + setocl + ", " + val2 + ")", + scope, env, local); + } else { return "((ArrayList) " + lexp + ")[" + indopt + " -1] = " + wval + ";"; } } @@ -11211,7 +11388,45 @@ public String updateFormEq(java.util.Map env, String datax = data; if (objectRef != null) - { datax = objectRef.queryFormCPP(env,local) + "." + data; } + { datax = objectRef.queryForm(env,local) + "." + data; } + // queryFormCPP ?? + + if (Type.isSequenceType(type) && + (var.isNumeric() || var.isString() || + var.isBoolean())) + { // this := + // MatrixLib.singleValueMatrix( + // Sequence{this->size()}, var) + + SetExpression setexpr = + new SetExpression(true); + + Expression uex = new UnaryExpression("->size", this); + uex.setType(new Type("int", null)); + setexpr.addElement(uex); + setexpr.setElementType(new Type("int", null)); + + String setocl = + "(new SystemTypes.Set().add( " + + uex.queryForm(env,local) + " )).getElements()"; + + Vector rangepars = new Vector(); + rangepars.add(setexpr); + rangepars.add(var); + + Expression scope = + BasicExpression.newStaticCallBasicExpression( + "singleValueMatrix", "Set", rangepars); + scope.setType(new Type("Sequence", null)); + scope.setElementType(var.getType()); + scope.umlkind = QUERY; + + return + this.updateFormEq(env, + "Set.singleValueMatrix(" + setocl + ", " + val2 + ")", + scope, local); + } + if ("subrange".equals(data) && parameters != null && objectRef != null && @@ -11461,6 +11676,43 @@ public String updateFormEqJava6(java.util.Map env, // System.out.println("#### " + this + " := " + val2); + if (Type.isSequenceType(type) && + (var.isNumeric() || var.isString() || + var.isBoolean())) + { // this := + // MatrixLib.singleValueMatrix( + // Sequence{this->size()}, var) + + SetExpression setexpr = + new SetExpression(true); + + Expression uex = new UnaryExpression("->size", this); + uex.setType(new Type("int", null)); + setexpr.addElement(uex); + setexpr.setElementType(new Type("int", null)); + + String setocl = + "Set.addSequence(new ArrayList(), " + + uex.queryFormJava6(env,local) + " )"; + + Vector rangepars = new Vector(); + rangepars.add(setexpr); + rangepars.add(var); + + Expression scope = + BasicExpression.newStaticCallBasicExpression( + "singleValueMatrix", "Set", rangepars); + scope.setType(new Type("Sequence", null)); + scope.setElementType(var.getType()); + scope.umlkind = QUERY; + + return + this.updateFormEqJava6(env, + "Set.singleValueMatrix(" + setocl + ", " + val2 + ")", + scope, local); + } + + if ("subrange".equals(data) && parameters != null && objectRef != null && objectRef instanceof BasicExpression) @@ -11704,10 +11956,48 @@ else if (arrayIndex != null) public String updateFormEqJava7(java.util.Map env, String val2, Expression var, boolean local) { String cont = "Controller.inst()"; + String datax = data; if (objectRef != null) { datax = objectRef.queryFormJava7(env,local) + "." + data; } + if (Type.isSequenceType(type) && + (var.isNumeric() || var.isString() || + var.isBoolean())) + { // this := + // MatrixLib.singleValueMatrix( + // Sequence{this->size()}, var) + + SetExpression setexpr = + new SetExpression(true); + + Expression uex = new UnaryExpression("->size", this); + uex.setType(new Type("int", null)); + setexpr.addElement(uex); + setexpr.setElementType(new Type("int", null)); + + String setocl = + "Ocl.addSequence(new ArrayList(), " + + uex.queryFormJava7(env,local) + ")"; + + Vector rangepars = new Vector(); + rangepars.add(setexpr); + rangepars.add(var); + + Expression scope = + BasicExpression.newStaticCallBasicExpression( + "singleValueMatrix", "Ocl", rangepars); + scope.setType(new Type("Sequence", null)); + scope.setElementType(var.getType()); + scope.umlkind = QUERY; + + return + this.updateFormEqJava7(env, + "Ocl.singleValueMatrix(" + setocl + ", " + val2 + ")", + scope, local); + } + + if ("subrange".equals(data) && parameters != null && objectRef != null && objectRef instanceof BasicExpression) @@ -12053,6 +12343,44 @@ public String updateFormEqCSharp(java.util.Map env, // System.out.println(">>> Assignment " + this + " = " + val2); + if (Type.isSequenceType(type) && + (var.isNumeric() || var.isString() || + var.isBoolean())) + { // this := + // MatrixLib.singleValueMatrix( + // Sequence{this->size()}, var) + + SetExpression setexpr = + new SetExpression(true); + + Expression uex = new UnaryExpression("->size", this); + uex.setType(new Type("int", null)); + setexpr.addElement(uex); + setexpr.setElementType(new Type("int", null)); + + String setocl = + "SystemTypes.addSet(new ArrayList(), " + + uex.queryFormCSharp(env,local) + " )"; + + Vector rangepars = new Vector(); + rangepars.add(setexpr); + rangepars.add(var); + + Expression scope = + BasicExpression.newStaticCallBasicExpression( + "singleValueMatrix", "SystemTypes", rangepars); + scope.setType(new Type("Sequence", null)); + scope.setElementType(var.getType()); + scope.umlkind = QUERY; + + return + this.updateFormEqCSharp(env, + "SystemTypes.singleValueMatrix(" + setocl + ", " + val2 + ")", + scope, local); + } + + + String datax = data; if (objectRef != null) diff --git a/BinaryExpression.java b/BinaryExpression.java index 9c383fdd..c1caf146 100644 --- a/BinaryExpression.java +++ b/BinaryExpression.java @@ -6911,12 +6911,22 @@ else if (left instanceof SetExpression) ((BasicExpression) right).arrayIndex == null && operator.equals("->oclIsKindOf")) { return "(" + lqf + " instanceof " + right + ")"; } + if (operator.equals("->oclIsKindOf")) + { // right is a type name: int, Sequence, etc + return "(" + Type.typeTestJava(lqf, right + "") + ")"; + } + // ->oclIsTypeOf (in the exact class) if (right.umlkind == CLASSID && ((BasicExpression) right).arrayIndex == null && operator.equals("->oclIsTypeOf")) { return "(" + lqf + ".getClass() == " + right + ".class)"; } + if (operator.equals("->oclIsTypeOf")) + { // right is a type name: int, Sequence, etc + return "(" + Type.typeTestJava(lqf, right + "") + ")"; + } + if (right.umlkind == CLASSID && ((BasicExpression) right).arrayIndex == null && operator.equals("<:")) { rqf = ((BasicExpression) right).classExtentQueryForm(env,local); @@ -7407,10 +7417,20 @@ else if (left.hasSequenceType() && right.hasSequenceType()) ((BasicExpression) right).arrayIndex == null && operator.equals("->oclIsKindOf")) { return "(" + lqf + " instanceof " + right + ")"; } // Java6 version of type named by right + if (operator.equals("->oclIsKindOf")) + { // right is a type name: int, Sequence, etc + return "(" + Type.typeTestJava6(lqf, right + "") + ")"; + } + if (right.umlkind == CLASSID && ((BasicExpression) right).arrayIndex == null && operator.equals("->oclIsTypeOf")) { return "(" + lqf + ".getClass() == " + right + ".class)"; } + if (operator.equals("->oclIsTypeOf")) + { // right is a type name: int, Sequence, etc + return "(" + Type.typeTestJava6(lqf, right + "") + ")"; + } + if (right.umlkind == CLASSID && ((BasicExpression) right).arrayIndex == null && operator.equals("/:")) { return "!(" + lqf + " instanceof " + right + ")"; } @@ -7875,10 +7895,20 @@ else if (left.hasSequenceType() && right.hasSequenceType()) ((BasicExpression) right).arrayIndex == null && operator.equals("->oclIsKindOf")) { return "(" + lqf + " instanceof " + right + ")"; } - if (right.umlkind == CLASSID && + if (operator.equals("->oclIsKindOf")) + { // right is a type name: int, Sequence, etc + return "(" + Type.typeTestJava7(lqf, right + "") + ")"; + } + + if (right.umlkind == CLASSID && ((BasicExpression) right).arrayIndex == null && operator.equals("->oclIsTypeOf")) { return "(" + lqf + ".getClass() == " + right + ".class)"; } + if (operator.equals("->oclIsTypeOf")) + { // right is a type name: int, Sequence, etc + return "(" + Type.typeTestJava7(lqf, right + "") + ")"; + } + if (right.umlkind == CLASSID && ((BasicExpression) right).arrayIndex == null && operator.equals("/:")) { return "!(" + lqf + " instanceof " + right + ")"; } @@ -8387,17 +8417,29 @@ else if (right.isString()) } if (right.umlkind == CLASSID && - ((BasicExpression) right).arrayIndex == null && operator.equals(":")) + ((BasicExpression) right).arrayIndex == null && + operator.equals(":")) { return "(" + lqf + " is " + right + ")"; } if (right.umlkind == CLASSID && - ((BasicExpression) right).arrayIndex == null && operator.equals("->oclIsKindOf")) + ((BasicExpression) right).arrayIndex == null && + operator.equals("->oclIsKindOf")) { return "(" + lqf + " is " + right + ")"; } - if (right.umlkind == CLASSID && - ((BasicExpression) right).arrayIndex == null && operator.equals("->oclIsTypeOf")) + if (operator.equals("->oclIsKindOf")) + { // right is a type name: int, Sequence, etc + return "(" + Type.typeTestCSharp(lqf, right + "") + ")"; + } + + if (right.umlkind == CLASSID && + ((BasicExpression) right).arrayIndex == null && + operator.equals("->oclIsTypeOf")) { return "(" + lqf + " is " + right + ")"; } + if (operator.equals("->oclIsTypeOf")) + { // right is a type name: int, Sequence, etc + return "(" + Type.typeTestCSharp(lqf, right + "") + ")"; + } if (right.umlkind == CLASSID && ((BasicExpression) right).arrayIndex == null && operator.equals("/:")) @@ -14701,6 +14743,63 @@ else if ("->reject".equals(operator)) BinaryExpression assign = new BinaryExpression("=", left, uniexp); return assign.updateForm(language,env,local); } + // else if ("->collect".equals(operator)) + // { // col->collect( e ) := var is + // for _i : Integer.subrange(1,col->size()) + // do (var _x : T := col->at(_i); _x.e := var->at(_i)) + // } + else if ("|C".equals(operator)) + { BinaryExpression lbe = (BinaryExpression) left; + BasicExpression vx = (BasicExpression) lbe.getLeft(); + Expression col = lbe.getRight(); + + if (col.isSequence()) { } + else + { System.err.println("!! Warning: can only update a sequence via a ->collect view, not other types of collections: " + col); } + + BasicExpression _ind = + BasicExpression.newVariableBasicExpression("_ind", + new Type("int", null)); + + Vector rangepars = new Vector(); + rangepars.add(new BasicExpression(1)); + rangepars.add(new UnaryExpression("->size", col)); + + Expression scope = + BasicExpression.newFunctionBasicExpression( + "subrange", "Integer", rangepars); + scope.setType(new Type("Sequence", null)); + scope.setElementType(new Type("int", null)); + + CreationStatement cs = new CreationStatement(vx, + col.getElementType()); + // Expression colclone = (Expression) col.clone(); + // colclone.setArrayIndex(_ind); + Expression iniExpr = new BinaryExpression("->at", col, _ind); + iniExpr.setType(col.getElementType()); + cs.setInitialisation(iniExpr); + + Expression rhs; + if (var.isNumeric() || var.isString() || + var.isBoolean()) + { rhs = var; } + else + { rhs = new BinaryExpression("->at", var, _ind); + rhs.setType(var.getElementType()); + } + + AssignStatement astat = + new AssignStatement(right, rhs); + + SequenceStatement ss = new SequenceStatement(); + ss.addStatement(cs); + ss.addStatement(astat); + ss.setBrackets(true); + + WhileStatement ws = + new WhileStatement(_ind, scope, ss); + return ws.updateForm(language, env, local); + } else if ("->including".equals(operator) && Type.isSetType(type)) { // left->intersection(var)->union(var - { right }) BinaryExpression subfg = new BinaryExpression("->intersection", left, var ); diff --git a/Compiler2.java b/Compiler2.java index f4344425..b3c48070 100644 --- a/Compiler2.java +++ b/Compiler2.java @@ -2964,7 +2964,9 @@ public Expression parse_factor_expression(int bc, int pstart, int pend, Vector e // div is only for ATL. public Expression parseAtExpression(int bc , int pstart, int pend, Vector entities, Vector types) - { for (int i = pend-1; pstart < i; i--) + { // e->at(i) + + for (int i = pend-1; pstart < i; i--) { String ss = lexicals.get(i).toString(); if (ss.equals("->") && i+2 < lexicals.size() && "(".equals(lexicals.get(i+2) + "") && ")".equals(lexicals.get(pend) + "")) @@ -2979,12 +2981,54 @@ public Expression parseAtExpression(int bc , int pstart, int pend, Vector entiti { BasicExpression be = (BasicExpression) ee2; be.setArrayIndex(ee1); - System.out.println(">>> Parsed basic expression: " + be); + System.out.println(">>> Parsed at expression: " + be); return be; } } } - } + } + return null; + } + + public Expression parseCollectExpression(int bc , int pstart, int pend, Vector entities, Vector types) + { // e->collect( v | f ) + + for (int i = pend-1; pstart < i; i--) + { String ss = lexicals.get(i).toString(); + + if (ss.equals("->") && i+4 < lexicals.size() && + "(".equals(lexicals.get(i+2) + "") && + ")".equals(lexicals.get(pend) + "")) + { String ss2 = lexicals.get(i+1).toString(); + String vdash = lexicals.get(i+4).toString(); + + if (i + 3 <= pend && "collect".equals(ss2) && + i + 5 <= pend && "|".equals(vdash)) + { // ee2->collect( ind | ee1) + + Expression ind = + parse_expression(bc+1, i+3, i+3, entities, types); + Expression ee1 = + parse_expression(bc+1,i+5,pend-1,entities, types); + + if (ind == null || ee1 == null) + { continue; } + + Expression ee2 = + parse_factor_expression( + bc,pstart,i-1,entities,types); + + if (ee2 == null) { continue; } + + BinaryExpression incol = + new BinaryExpression(":", ind, ee2); + BinaryExpression res = + new BinaryExpression("|C", incol, ee1); + return res; + } + } + } + return null; } @@ -7052,10 +7096,15 @@ else if ("var".equals(lexicals.get(s) + "")) ast.setOperator(":="); return ast; } + Expression e1 = parse_basic_expression(0,s,i-1,entities,types); - if (e1 == null) { e1 = parseAtExpression(0,s,i-1,entities,types); } if (e1 == null) - { return null; } + { e1 = parseAtExpression(0,s,i-1,entities,types); } + if (e1 == null) + { e1 = parseCollectExpression(0,s,i-1,entities,types); } + if (e1 == null) + { return null; } + Expression e2 = parse_expression(0,i+1,e,entities,types); if (e2 == null) { diff --git a/SetExpression.java b/SetExpression.java index 47c4d972..56df2f7e 100644 --- a/SetExpression.java +++ b/SetExpression.java @@ -703,7 +703,8 @@ public String queryFormCSharp(java.util.Map env, boolean local) for (int i = 0; i < elements.size(); i++) { Expression e = (Expression) elements.get(i); res = "SystemTypes.addSet(" + res + "," + - Expression.wrapCSharp(elementType, e.queryFormCSharp(env,local)) + ")"; + Expression.wrapCSharp(elementType, + e.queryFormCSharp(env,local)) + ")"; } return res; diff --git a/Type.java b/Type.java index bd783f29..3f4874e3 100644 --- a/Type.java +++ b/Type.java @@ -3856,7 +3856,96 @@ public String generateB(Type elemType) { return nme; } } - + public static String typeTestJava(String lqf, String typ) + { if ("int".equals(typ)) + { return "Set.isInteger(\"\" + " + lqf + ")"; } + if ("long".equals(typ)) + { return "Set.isLong(\"\" + " + lqf + ")"; } + if ("double".equals(typ)) + { return "Set.isReal(\"\" + " + lqf + ")"; } + + if ("boolean".equals(typ)) + { return "\"true\".equals(\"\" + " + lqf + ") || \"false\".equals(\"\" + " + lqf + ")"; } + + if ("Sequence".equals(typ) || "Set".equals(typ)) + { return lqf + " instanceof Vector"; } + + if ("Map".equals(typ)) + { return lqf + " instanceof Map"; } + + return lqf + " instanceof " + typ; + } + + public static String typeTestJava6(String lqf, String typ) + { if ("int".equals(typ)) + { return "Set.isInteger(\"\" + " + lqf + ")"; } + if ("long".equals(typ)) + { return "Set.isLong(\"\" + " + lqf + ")"; } + if ("double".equals(typ)) + { return "Set.isReal(\"\" + " + lqf + ")"; } + + if ("boolean".equals(typ)) + { return "\"true\".equals(\"\" + " + lqf + ") || \"false\".equals(\"\" + " + lqf + ")"; } + + if ("Sequence".equals(typ)) + { return lqf + " instanceof ArrayList"; } + + if ("Set".equals(typ)) + { return lqf + " instanceof Set"; } + + if ("Map".equals(typ)) + { return lqf + " instanceof Map"; } + + return lqf + " instanceof " + typ; + } + + public static String typeTestJava7(String lqf, String typ) + { if ("int".equals(typ)) + { return "Ocl.isInteger(\"\" + " + lqf + ")"; } + if ("long".equals(typ)) + { return "Ocl.isLong(\"\" + " + lqf + ")"; } + if ("double".equals(typ)) + { return "Ocl.isReal(\"\" + " + lqf + ")"; } + + if ("boolean".equals(typ)) + { return "\"true\".equals(\"\" + " + lqf + ") || \"false\".equals(\"\" + " + lqf + ")"; } + + if ("Sequence".equals(typ)) + { return lqf + " instanceof ArrayList"; } + + if ("Set".equals(typ)) + { return lqf + " instanceof Set"; } + + if ("Map".equals(typ)) + { return lqf + " instanceof Map"; } + + return lqf + " instanceof " + typ; + } + + public static String typeTestCSharp(String lqf, String typ) + { if ("int".equals(typ)) + { return "SystemTypes.isInteger(\"\" + " + lqf + ")"; } + if ("long".equals(typ)) + { return "SystemTypes.isLong(\"\" + " + lqf + ")"; } + if ("double".equals(typ)) + { return "SystemTypes.isReal(\"\" + " + lqf + ")"; } + + if ("boolean".equals(typ)) + { return "\"true\".equals(\"\" + " + lqf + ") || \"false\".equals(\"\" + " + lqf + ")"; } + + if ("Sequence".equals(typ)) + { return lqf + " is ArrayList"; } + + if ("Set".equals(typ)) + { return lqf + " is ArrayList"; } + + if ("Map".equals(typ)) + { return lqf + " is Hashtable"; } + + return lqf + " is " + typ; + } + + public String getJava() { String nme = getName(); if (nme.equals("Set") || nme.equals("Sequence")) diff --git a/UCDArea.java b/UCDArea.java index 7f5ac6e1..3c7469f1 100644 --- a/UCDArea.java +++ b/UCDArea.java @@ -11386,7 +11386,7 @@ private void generateSystemTypes(PrintWriter out) out.println(" public Set add(boolean x)"); out.println(" { elements.add(new Boolean(x));"); out.println(" return this; }\n"); - out.println(" public List getElements() { return elements; }\n"); + out.println(" public Vector getElements() { return elements; }\n"); String mop = BSystemTypes.generateCopyOps(); out.println("\n" + mop); @@ -11865,9 +11865,10 @@ private void generateSystemTypesJava7(PrintWriter out) out.println(" { s.add(new Boolean(x));"); out.println(" return s; }\n"); - out.println(" public static ArrayList addSequence(ArrayList s, T x)"); - out.println(" { if (x != null) { s.add(x); }"); - out.println(" return s; }\n"); + out.println(" public static ArrayList addSequence(ArrayList s, Object x)"); + out.println(" { if (x != null) { s.add((T) x); }"); + out.println(" return s;"); + out.println(" }\n"); out.println(" public static > SortedSequence addSequence(SortedSequence s, T x)"); out.println(" { if (x != null) { s.add(x); }"); diff --git a/UnaryExpression.java b/UnaryExpression.java index c32876f9..835606c8 100644 --- a/UnaryExpression.java +++ b/UnaryExpression.java @@ -931,19 +931,30 @@ else if ("->last".equals(operator)) // argument->last() := var return "if (" + eqempty.queryForm(language,env,local) + ") { " + setarg + " } else { " + setlast + " }"; } else if ("->first".equals(operator)) - { UnaryExpression selfsize = new UnaryExpression("->size", argument); + { /* UnaryExpression selfsize = new UnaryExpression("->size", argument); BinaryExpression eqempty = new BinaryExpression("=", selfsize, new BasicExpression(0) ); SetExpression se = new SetExpression(); se.setOrdered(true); se.addElement(var); BinaryExpression setarge = new BinaryExpression("=", argument, se); - String setarg = setarge.updateForm(language,env,local); + String setarg = setarge.updateForm(language,env,local); */ + BasicExpression argclone = (BasicExpression) argument.clone(); argclone.setArrayIndex(new BasicExpression(1)); + argclone.setType(type); + argclone.setElementType(elementType); + argclone.setArrayType(argument.getType()); + BinaryExpression setfirste = new BinaryExpression("=", argclone, var); String setfirst = setfirste.updateForm(language,env,local); - return "if (" + eqempty.queryForm(language,env,local) + ") { " + setarg + " } else { " + setfirst + " }"; + /* return "if (" + eqempty.queryForm(language,env,local) + ") { " + setarg + " } else { " + setfirst + " }"; + + return BasicExpression.updateFormEqIndex( + language,argument, + new BasicExpression(1),val,var,env,local); */ + + return setfirst; } else if ("->front".equals(operator)) { UnaryExpression selfsize = new UnaryExpression("->size", argument);