Skip to content

Commit 8482ffe

Browse files
authored
Updated Java8 code generator
1 parent 0960654 commit 8482ffe

File tree

6 files changed

+137
-51
lines changed

6 files changed

+137
-51
lines changed

Attribute.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,9 @@ public boolean typeCheck(Vector types, Vector entities)
317317
initialExpression.typeCheck(types,entities,cntx,env);
318318
if (type == null)
319319
{ type = initialExpression.type;
320-
elementType = initialExpression.elementType;
320+
elementType = initialExpression.elementType;
321321
}
322+
System.out.println(">> Type of " + initialExpression + " is " + type + "(" + elementType + ")");
322323
}
323324

324325
if (type == null)
@@ -329,10 +330,14 @@ public boolean typeCheck(Vector types, Vector entities)
329330
String tname = type + "";
330331
Type t = Type.getTypeFor(tname, types, entities);
331332
if (t == null)
332-
{ type = new Type("OclAny", null);
333+
{ System.err.println("!! Warning: null type for attribute " + name);
334+
type = new Type("OclAny", null);
333335
return true;
334336
}
335337
type = t;
338+
if (initialExpression != null &&
339+
initialExpression.type == null)
340+
{ initialExpression.type = type; }
336341
return true;
337342
}
338343

@@ -2021,17 +2026,36 @@ public String initialiserJava7()
20212026
{ return "this." + nme + " = " + nme + "x;"; }
20222027
if (isFinal()) { return ""; }
20232028

2029+
2030+
2031+
20242032
if (initialExpression != null)
2025-
{ java.util.Map env = new java.util.HashMap();
2033+
{ System.out.println(">> Initialiser of " + this + " is " +
2034+
initialExpression + " " + initialExpression.type);
2035+
if ("Set{}".equals(initialExpression + ""))
2036+
{ initialExpression.setElementType(elementType); }
2037+
else if ("Sequence{}".equals(initialExpression + ""))
2038+
{ initialExpression.setElementType(elementType); }
2039+
else if ("Map{}".equals(initialExpression + ""))
2040+
{ initialExpression.setType(type);
2041+
initialExpression.setElementType(type.elementType);
2042+
}
2043+
2044+
java.util.Map env = new java.util.HashMap();
20262045
if (entity != null)
20272046
{ env.put(entity.getName(), "this"); }
20282047
return "this." + nme + " = " + initialExpression.queryFormJava7(env,true) + ";";
20292048
}
20302049

20312050

20322051
// if (initialValue != null && !initialValue.equals(""))
2033-
// { return "this." + nme + " = " + initialValue + ";"; }
2052+
// { return "this." + nme + " = " + initialValue + ";"; }
2053+
20342054
String def = type.getDefaultJava7();
2055+
2056+
System.out.println(">> Initialiser of " + this + " is " +
2057+
def);
2058+
20352059
if (def == null) { return ""; }
20362060
return "this." + nme + " = " + def + ";";
20372061
}

Compiler2.java

Lines changed: 84 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,7 @@ else if (t2 == null)
12231223
}
12241224
}
12251225
}
1226+
12261227
if (tt == null)
12271228
{ System.err.println("!!ERROR!!: Invalid map/function type, it must have 2 type arguments: " + showLexicals(st,en));
12281229
return null;
@@ -3084,14 +3085,15 @@ public Expression parse_basic_expression(int bc, int pstart, int pend, Vector en
30843085

30853086
if (pstart < pend && "]".equals(lexicals.get(pend) + "") &&
30863087
"[".equals(lexicals.get(pstart+1) + ""))
3087-
{ String ss = lexicals.elementAt(pstart).toString();
3088+
{ // iden[indexes]
3089+
3090+
String ss = lexicals.elementAt(pstart).toString();
30883091
BasicExpression ee = new BasicExpression(ss,0);
30893092
Expression op = ee.checkIfSetExpression();
30903093
// Expression op = parse_basic_expression(bc,pstart,pstart);
30913094
Expression arg = parse_additive_expression(bc,pstart+2,pend-1,entities,types);
30923095
if (arg == null)
3093-
{ // System.err.println("ERROR: Invalid array argument: " + showLexicals(pstart+2,pend-1));
3094-
Vector args = parse_array_index_sequence(bc,pstart+2,pend-1,entities,types);
3096+
{ Vector args = parse_array_index_sequence(bc,pstart+2,pend-1,entities,types);
30953097
if (args != null)
30963098
{ Expression opx = defineArrayIndex(op,args,entities,types);
30973099
System.out.println(">>> Array index sequence: " + args + " for " + opx);
@@ -3108,19 +3110,30 @@ else if (op != null && op instanceof BasicExpression) // must be
31083110
}
31093111

31103112
if (pstart < pend && "]".equals(lexicals.get(pend-1) + "") &&
3111-
"[".equals(lexicals.get(pstart+1) + "")) // be[arrind].be2
3112-
{ String tail = lexicals.get(pend) + "";
3113+
"[".equals(lexicals.get(pstart+1) + "") &&
3114+
(lexicals.get(pend) + "").charAt(0) == '.')
3115+
{ // be[inds].f
3116+
3117+
String tail = lexicals.get(pend) + "";
31133118
if (tail.charAt(0) == '.')
31143119
{ tail = tail.substring(1,tail.length()); }
3120+
31153121
Expression op = parse_basic_expression(bc,pstart,pstart,entities,types);
31163122
Expression arg = parse_additive_expression(bc,pstart+2,pend-2,entities,types);
31173123
if (arg == null)
31183124
{ // System.err.println("ERROR: Invalid array argument: " + showLexicals(pstart+2,pend-2));
3125+
/* Vector args = parse_array_index_sequence(bc,pstart+2,pend-2,entities,types);
3126+
if (args != null)
3127+
{ Expression opx = defineArrayIndex(op,args,entities,types);
3128+
System.out.println(">>> Array index sequence: " + args + " for " + opx);
3129+
return opx;
3130+
} */
31193131
}
31203132
else if (op != null && op instanceof BasicExpression) // must be
31213133
{ BasicExpression beop = (BasicExpression) op;
31223134
beop.setArrayIndex(arg);
3123-
BasicExpression te = // new BasicExpression(ss,0); // (ss) surely?
3135+
BasicExpression te =
3136+
// new BasicExpression(ss,0); // (ss) surely?
31243137
new BasicExpression(tail,0);
31253138
Expression ef = te.checkIfSetExpression();
31263139
if (ef != null && (ef instanceof BasicExpression))
@@ -3176,8 +3189,8 @@ else if (op != null && op instanceof BasicExpression) // must be
31763189
if (ve != null)
31773190
{ BasicExpression beop = (BasicExpression) op;
31783191
String tail = strs.substring(1,strs.length());
3179-
BasicExpression rees = new BasicExpression(tail, 0);
3180-
rees.setObjectRef(beop);
3192+
BasicExpression rees = new BasicExpression(tail, 0);
3193+
rees.setObjectRef(beop);
31813194
rees.setIsEvent();
31823195
rees.setParameters(ve);
31833196
System.out.println("*** Parsed extended call expression " + rees);
@@ -3188,37 +3201,52 @@ else if (op != null && op instanceof BasicExpression) // must be
31883201
}
31893202
}
31903203

3191-
/* if (pstart < pend &&
3192-
"[".equals(lexicals.get(pstart+1) + "")) // be[arrind].something
3193-
{ for (int k = pstart + 2; k < pend; k++)
3194-
{ String kstring = lexicals.get(k) + "";
3195-
if ("]".equals(kstring))
3196-
{ Expression op = parse_basic_expression(bc,pstart,pstart);
3197-
Expression arg = parse_factor_expression(bc,pstart+2,k-1);
3204+
if (pstart < pend &&
3205+
"[".equals(lexicals.get(pstart+1) + "") &&
3206+
"]".equals(lexicals.get(pend-1) + "") &&
3207+
(lexicals.get(pend) + "").charAt(0) == '.')
3208+
{ // iden[ ... ].f valid basic expression before .f
3209+
3210+
Expression op = parse_basic_expression(
3211+
bc,pstart,pend-1,entities,types);
3212+
String tail = lexicals.get(pend) + "";
3213+
if (tail.charAt(0) == '.')
3214+
{ tail = tail.substring(1,tail.length()); }
3215+
3216+
if (op instanceof BasicExpression) // must be
3217+
{ BasicExpression beop = (BasicExpression) op;
3218+
BasicExpression rees = new BasicExpression(tail, 0);
3219+
rees.setObjectRef(beop);
3220+
System.out.println(">>> Parsed array 2 expression: " + rees);
3221+
return rees;
3222+
}
3223+
// return null;
3224+
}
3225+
3226+
if (pstart + 1 < pend &&
3227+
"]".equals(lexicals.get(pend) + ""))
3228+
{ // be [ ind ] valid basic expression be
3229+
3230+
for (int k = pend-1; k >= pstart; k--)
3231+
{ String lex = lexicals.get(k) + "";
3232+
if ("[".equals(lex))
3233+
{ Expression op = parse_basic_expression(
3234+
bc,pstart,k-1,entities,types);
31983235

3199-
String tail = lexicals.get(k+1) + "";
3200-
if (tail.charAt(0) == '.')
3201-
{ tail = tail.substring(1,tail.length()); }
32023236
if (op instanceof BasicExpression) // must be
32033237
{ BasicExpression beop = (BasicExpression) op;
3204-
beop.setArrayIndex(arg);
3205-
Expression te = parse_basic_expression(bc,k+1,pend);
3206-
if (te != null)
3207-
{ if (te instanceof BasicExpression)
3208-
{ BasicExpression bte = (BasicExpression) te;
3209-
bte.setData(tail);
3210-
bte.setObjectRef(beop);
3211-
System.out.println("Parsed array 2 expression: " + bte);
3212-
return bte;
3213-
}
3214-
else
3215-
{ return null; }
3216-
}
3217-
}
3218-
}
3219-
}
3220-
return null;
3221-
} */
3238+
Expression arg = parse_additive_expression(
3239+
bc,k+1,pend-1,entities,types);
3240+
if (arg != null && beop.arrayIndex == null)
3241+
{ beop.setArrayIndex(arg);
3242+
System.out.println(">>> Parsed array 3 expression: " + beop);
3243+
return beop;
3244+
}
3245+
}
3246+
}
3247+
}
3248+
// return null;
3249+
}
32223250

32233251
if (pstart + 2 < pend)
32243252
{ for (int i = pstart + 1; i < pend; i++)
@@ -5043,10 +5071,6 @@ public static String isKeywordOrPart(String st, String[] mess)
50435071
return "then";
50445072
}
50455073

5046-
if ("else".startsWith(st))
5047-
{ mess[0] = "else part of if-expression or if-statement";
5048-
return "else";
5049-
}
50505074

50515075
if ("while".startsWith(st))
50525076
{ mess[0] = "Unbounded loop statement: while expr do statement\nCan only be used in an activity";
@@ -5136,6 +5160,13 @@ public static String isKeywordOrPart(String st, String[] mess)
51365160
return "assert";
51375161
}
51385162

5163+
if (st.length() > 2)
5164+
{ if ("else".startsWith(st))
5165+
{ mess[0] = "else part of if-expression or if-statement";
5166+
return "else";
5167+
}
5168+
}
5169+
51395170
if (st.length() > 3)
51405171
{ if ("reference".startsWith(st))
51415172
{ mess[0] = "reference declaration, eg: reference name : Type;\nType is a class type or collection (of class element type)";
@@ -5235,6 +5266,18 @@ public static String isKeywordOrPart(String st, String[] mess)
52355266
return "arg->reverse()";
52365267
}
52375268

5269+
if ("->restrict".startsWith(st))
5270+
{ mess[0] = "arg->restrict(keys) operator on maps.\n" +
5271+
"Returns submap of arg: elements with key in keys";
5272+
return "arg->restrict(keys)";
5273+
}
5274+
5275+
if ("->antirestrict".startsWith(st))
5276+
{ mess[0] = "arg->antirestrict(keys) operator on maps.\n" +
5277+
"Returns submap of arg: elements with key not in keys";
5278+
return "arg->antirestrict(keys)";
5279+
}
5280+
52385281
if ("->first".startsWith(st))
52395282
{ mess[0] = "arg->first() operator on non-empty strings, sequences\n" +
52405283
"Returns arg->at(1)";
@@ -9724,7 +9767,7 @@ public static void main(String[] args)
97249767
// Type xx = c.parseType(0,c.lexicals.size()-1,exs, new Vector());
97259768

97269769
// c.nospacelexicalanalysis("arr[x][y]");
9727-
c.nospacelexicalanalysis("(OclFile.newOclFile(\"(text)\")).setReadOnly()");
9770+
c.nospacelexicalanalysis("Worksheets[Y].Range[X].Value");
97289771
// c.nospacelexicalanalysis("(OclFile.newOclFile_Read(OclFile.newOclFile(s))).readObject()");
97299772

97309773
// c.nospacelexicalanalysis("(MyString).subrange((MyString)->indexOf((MyString)->trim()))");

Entity.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,27 +1455,37 @@ public void removeOperations(String nme)
14551455
}
14561456

14571457
public void typeCheckAttributes(Vector types, Vector entities)
1458-
{ for (int i = 0; i < attributes.size(); i++)
1458+
{ Vector localtypes = new Vector();
1459+
localtypes.addAll(types);
1460+
localtypes.addAll(typeParameters);
1461+
for (int i = 0; i < attributes.size(); i++)
14591462
{ Attribute att = (Attribute) attributes.get(i);
1460-
att.typeCheck(types,entities);
1463+
// System.out.println(">> Type-checking " + att + " with " + localtypes + " " + entities);
1464+
att.typeCheck(localtypes,entities);
14611465
}
14621466
}
14631467

14641468
public void typeCheckOps(Vector types, Vector entities)
1465-
{ for (int i = 0; i < operations.size(); i++)
1469+
{ Vector localtypes = new Vector();
1470+
localtypes.addAll(types);
1471+
localtypes.addAll(typeParameters);
1472+
for (int i = 0; i < operations.size(); i++)
14661473
{ BehaviouralFeature bf = (BehaviouralFeature) operations.get(i);
1467-
bf.typeCheck(types,entities);
1474+
bf.typeCheck(localtypes,entities);
14681475
}
14691476
}
14701477

14711478
public void typeCheckInvariants(Vector types, Vector entities)
14721479
{ Vector contexts = new Vector();
14731480
contexts.add(this);
14741481
Vector vars = new Vector();
1482+
Vector localtypes = new Vector();
1483+
localtypes.addAll(types);
1484+
localtypes.addAll(typeParameters);
14751485

14761486
for (int i = 0; i < invariants.size(); i++)
14771487
{ Constraint inv = (Constraint) invariants.get(i);
1478-
inv.typeCheck(types,entities,contexts,vars);
1488+
inv.typeCheck(localtypes,entities,contexts,vars);
14791489
}
14801490
}
14811491

Type.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2878,7 +2878,12 @@ public String getDefaultJava7()
28782878
{ return "new ArrayList()"; }
28792879
}
28802880
if (nme.equals("Map"))
2881-
{ if (elementType != null)
2881+
{ if (keyType != null && elementType != null)
2882+
{ String kt = keyType.typeWrapperJava7();
2883+
String et = elementType.typeWrapperJava7();
2884+
return "new HashMap<" + kt + "," + et + ">()";
2885+
}
2886+
else if (elementType != null)
28822887
{ return "new HashMap<String," + elementType.typeWrapperJava7() + ">()"; }
28832888
else
28842889
{ return "new HashMap()"; }
@@ -3636,6 +3641,8 @@ public String getJava7()
36363641
if (nme.equals("Map"))
36373642
{ if (keyType != null && elementType != null)
36383643
{ return "HashMap<" + keyType.typeWrapperJava7() + ", " + elementType.typeWrapperJava7() + ">"; }
3644+
else if (elementType != null)
3645+
{ return "HashMap<String, " + elementType.typeWrapperJava7() + ">"; }
36393646
else
36403647
{ return "HashMap"; }
36413648

@@ -3646,6 +3653,8 @@ public String getJava7()
36463653
if (nme.equals("Function"))
36473654
{ if (keyType != null && elementType != null)
36483655
{ return "Evaluation<" + keyType.typeWrapperJava7() + ", " + elementType.typeWrapperJava7() + ">"; }
3656+
else if (elementType != null)
3657+
{ return "Evaluation<String, " + elementType.typeWrapperJava7() + ">"; }
36493658
else
36503659
{ return "Evaluation<String,Object>"; }
36513660
}

cg (2).zip

30.4 KB
Binary file not shown.

libraries (2).zip

30.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)