Skip to content

Commit 769a159

Browse files
authored
Updated code generation tools
1 parent cce341e commit 769a159

20 files changed

+750
-198
lines changed

ASTBasicTerm.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public String cgRules(CGSpec cgs, Vector rules)
107107
{ if (rules == null)
108108
{ return value; }
109109

110+
ASTTerm term0 = getTerm(0);
111+
110112
for (int i = 0; i < rules.size(); i++)
111113
{ CGRule r = (CGRule) rules.get(i);
112114
Vector tokens = r.lhsTokens;
@@ -133,8 +135,8 @@ public String cgRules(CGSpec cgs, Vector rules)
133135
for (int j = 0; j < tokens.size() && !failed; j++)
134136
{ String tok = (String) tokens.get(j);
135137
if (vars.contains(tok))
136-
{ // allocate terms(j) to tok
137-
eargs.add(value);
138+
{ // allocate terms(0) to tok
139+
eargs.add(term0);
138140
k++;
139141
}
140142
else if (tok.equals(value))
@@ -147,13 +149,18 @@ else if (tok.equals(value))
147149
}
148150

149151
if (!failed)
150-
{ System.out.println("> Matched " + tag + " rule " + r + " for " + this);
151-
152+
{
152153
for (int p = 0; p < eargs.size(); p++)
153-
{ String textp = (String) eargs.get(p);
154+
{ String textp = ((ASTTerm) eargs.get(p)).literalForm();
154155
args.add(textp);
155-
}
156-
return r.applyRule(args,eargs,cgs);
156+
}
157+
158+
Vector ents = new Vector();
159+
160+
if (r.satisfiesConditions(eargs,ents))
161+
{ System.out.println(">>>> Applying basic term " + tag + " rule " + r + " for " + this);
162+
return r.applyRule(args,eargs,cgs);
163+
}
157164
}
158165
}
159166

@@ -1102,6 +1109,14 @@ else if (tag.equals("literal") &&
11021109
return value;
11031110
}
11041111

1112+
public boolean isCharacter()
1113+
{ if (value.length() > 2 &&
1114+
value.charAt(0) == '\'' &&
1115+
value.charAt(value.length()-1) == '\'')
1116+
{ return true; }
1117+
return false;
1118+
}
1119+
11051120
public boolean isInteger()
11061121
{ if (tag.equals("integerLiteral"))
11071122
{ return true; }
@@ -1123,7 +1138,11 @@ public boolean isBoolean()
11231138
{ if (value.equals("true") || value.equals("false"))
11241139
{ return true; }
11251140
return false;
1126-
} // Ok for Java and OCL.
1141+
} // Ok for Java and OCL.
1142+
1143+
public boolean isString()
1144+
{ return Expression.isString(value); }
1145+
11271146

11281147
public boolean isIdentifier()
11291148
{ return "primary".equals(tag) &&
@@ -1171,4 +1190,10 @@ public String preSideEffect()
11711190
public String postSideEffect()
11721191
{ return null; }
11731192

1193+
public static void main(String[] args)
1194+
{ ASTBasicTerm tt = new ASTBasicTerm("primaryExpression", "'a'");
1195+
System.out.println(tt.isCharacter());
1196+
}
1197+
1198+
11741199
}

ASTCompositeTerm.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ else if (tok.equals(tm.literalForm()))
265265
}
266266

267267
if (failed == false)
268-
{ System.out.println("> Matched ruleset rule " + r + " for " + this);
268+
{ System.out.println(">> Matched " + tag + " rule " + r + " for " + this);
269269

270270
for (int p = 0; p < eargs.size(); p++)
271271
{ Object obj = eargs.get(p);
@@ -288,7 +288,7 @@ else if (obj instanceof Vector)
288288
Vector ents = new Vector();
289289

290290
if (r.satisfiesConditions(eargs,ents))
291-
{ System.out.println(">>>> Applying ruleset rule " + r);
291+
{ System.out.println(">>>> Applying " + tag + " rule " + r);
292292
return r.applyRule(args,eargs,cgs);
293293
}
294294
}
@@ -6695,6 +6695,7 @@ else if ("memcmp".equals(fname) && args.size() == 3)
66956695
else if ("strchr".equals(fname) && args.size() == 2)
66966696
{ // if arg1->indexOf(arg2) = 0 then null
66976697
// else arg1.subrange(arg1->indexOf(arg2)) endif
6698+
66986699
Expression arg1 = (Expression) args.get(0);
66996700
arg1.setType(new Type("String", null));
67006701
Expression arg2 = (Expression) args.get(1);
@@ -6787,7 +6788,9 @@ else if ("strrchr".equals(fname) && args.size() == 2)
67876788
return conde;
67886789
}
67896790
else if ("strlen".equals(fname) && args.size() == 1)
6790-
{ Expression arg1 = (Expression) args.get(0);
6791+
{ // _1->size()
6792+
6793+
Expression arg1 = (Expression) args.get(0);
67916794
arg1.setType(new Type("String", null));
67926795
arg1.setBrackets(true);
67936796
UnaryExpression res =
@@ -6796,7 +6799,10 @@ else if ("strlen".equals(fname) && args.size() == 1)
67966799
return res;
67976800
}
67986801
else if ("strstr".equals(fname) && args.size() == 2)
6799-
{ Expression arg1 = (Expression) args.get(0);
6802+
{ // if _1->indexOf(_2) > 0
6803+
// then _1.subrange(_1->indexOf(_2)) else "" endif
6804+
6805+
Expression arg1 = (Expression) args.get(0);
68006806
arg1.setType(new Type("String", null));
68016807
Expression arg2 = (Expression) args.get(1);
68026808
arg1.setBrackets(true);

ASTSymbolTerm.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ public String cgRules(CGSpec cgs, Vector rules)
4949

5050
if (symbol.equals(tok))
5151
{ return r.rhs; }
52-
}
53-
54-
return symbol;
52+
}
53+
return symbol;
5554
}
5655

5756
public String toString()
@@ -306,6 +305,37 @@ public String toKM3()
306305
public boolean isIdentifier()
307306
{ return false; }
308307

308+
public boolean isCharacter()
309+
{ if (symbol.length() > 2 &&
310+
symbol.charAt(0) == '\'' &&
311+
symbol.charAt(symbol.length()-1) == '\'')
312+
{ return true; }
313+
return false;
314+
}
315+
316+
public boolean isInteger()
317+
{ if (Expression.isInteger(symbol) ||
318+
Expression.isLong(symbol))
319+
{ return true; }
320+
return false;
321+
}
322+
323+
public boolean isDouble()
324+
{ if (Expression.isDouble(symbol))
325+
{ return true; }
326+
return false;
327+
}
328+
329+
public boolean isBoolean()
330+
{ if (symbol.equals("true") || symbol.equals("false"))
331+
{ return true; }
332+
return false;
333+
} // Ok for Java and OCL.
334+
335+
public boolean isString()
336+
{ return Expression.isString(symbol); }
337+
338+
309339
public boolean updatesObject(ASTTerm t)
310340
{ return false; }
311341

ASTTerm.java

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,16 @@ public static String getElementType(ASTTerm t)
183183

184184

185185
public boolean hasType(String str)
186-
{ if ("integer".equalsIgnoreCase(str))
186+
{ if ("character".equalsIgnoreCase(str))
187+
{ return isCharacter(); }
188+
if ("integer".equalsIgnoreCase(str))
187189
{ return isInteger(); }
188190
if ("real".equalsIgnoreCase(str))
189191
{ return isReal(); }
190192
if ("boolean".equalsIgnoreCase(str))
191-
{ return isBoolean(); }
193+
{ return isBoolean(); }
194+
if ("String".equalsIgnoreCase(str))
195+
{ return isString(); }
192196

193197
if ("Sequence".equals(str))
194198
{ return isSequence(); }
@@ -377,38 +381,61 @@ public static boolean isBoolean(String typ)
377381
}
378382

379383
public boolean isString()
380-
{ String typ = ASTTerm.getType(literalForm());
384+
{ String litf = literalForm();
385+
String typ = ASTTerm.getType(litf);
381386
if (typ == null)
382-
{ return false; }
387+
{ return Expression.isString(litf); }
383388
return
384389
"String".equals(typ) || "Character".equals(typ) ||
385390
"StringBuffer".equals(typ) || "char".equals(typ) ||
386391
"StringBuilder".equals(typ);
387392
}
388393

394+
public boolean isCharacter()
395+
{ String s = literalForm();
396+
if (s.length() > 2 && s.startsWith("'") &&
397+
s.endsWith("'"))
398+
{ return true; }
399+
return false;
400+
}
401+
389402
public boolean isInteger()
390-
{ String typ = ASTTerm.getType(literalForm());
391-
if (typ == null)
392-
{ return false; }
403+
{ String litf = literalForm();
404+
String typ = ASTTerm.getType(litf);
405+
if (typ == null)
406+
{ return Expression.isInteger(litf) ||
407+
Expression.isLong(litf);
408+
}
393409
return ASTTerm.isInteger(typ);
394410
}
395411

396412
public boolean isReal()
397-
{ String typ = ASTTerm.getType(literalForm());
413+
{ String litf = literalForm();
414+
String typ = ASTTerm.getType(litf);
398415
if (typ == null)
399-
{ return false; }
416+
{ return Expression.isDouble(litf); }
400417
return ASTTerm.isReal(typ);
401418
}
402419

403420
public boolean isNumber()
404-
{ String typ = ASTTerm.getType(literalForm());
421+
{ String litf = literalForm();
422+
String typ = ASTTerm.getType(litf);
423+
if (typ == null)
424+
{ return Expression.isInteger(litf) ||
425+
Expression.isLong(litf) ||
426+
Expression.isDouble(litf);
427+
}
428+
405429
return ASTTerm.isReal(typ) || ASTTerm.isInteger(typ);
406430
}
407431

408432
public boolean isBoolean()
409-
{ String typ = ASTTerm.getType(literalForm());
433+
{ String litf = literalForm();
434+
String typ = ASTTerm.getType(litf);
410435
if (typ == null)
411-
{ return false; }
436+
{ return "true".equalsIgnoreCase(litf) ||
437+
"false".equalsIgnoreCase(litf);
438+
}
412439
return
413440
"boolean".equals(typ) || "Boolean".equals(typ);
414441
}

BSystemTypes.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4461,13 +4461,56 @@ public static String generateCopyOps() // Java4
44614461
" result.addAll(s);\n" +
44624462
" return result;\n" +
44634463
" }\n\n";
4464+
44644465
res = res +
44654466
" public static HashMap copyMap(Map s)\n" +
44664467
" { HashMap result = new HashMap();\n" +
44674468
" result.putAll(s);\n" +
44684469
" return result;\n" +
44694470
" }\n\n";
44704471

4472+
res = res +
4473+
" public static int[] newRefint(int x)\n" +
4474+
" { int[] res = new int[1]; \n" +
4475+
" res[0] = x; \n" +
4476+
" return res; \n" +
4477+
" } \n\n";
4478+
4479+
res = res +
4480+
" public static long[] newReflong(long x)\n" +
4481+
" { long[] res = new long[1]; \n" +
4482+
" res[0] = x; \n" +
4483+
" return res; \n" +
4484+
" } \n\n";
4485+
4486+
res = res +
4487+
" public static double[] newRefdouble(double x)\n" +
4488+
" { double[] res = new double[1]; \n" +
4489+
" res[0] = x; \n" +
4490+
" return res; \n" +
4491+
" } \n\n";
4492+
4493+
res = res +
4494+
" public static boolean[] newRefboolean(boolean x)\n" +
4495+
" { boolean[] res = new boolean[1]; \n" +
4496+
" res[0] = x; \n" +
4497+
" return res; \n" +
4498+
" } \n\n";
4499+
4500+
res = res +
4501+
" public static String[] newRefString(String x)\n" +
4502+
" { String[] res = new String[1]; \n" +
4503+
" res[0] = x; \n" +
4504+
" return res; \n" +
4505+
" } \n\n";
4506+
4507+
res = res +
4508+
" public static Object[] newRefObject(Object x)\n" +
4509+
" { Object[] res = new Object[1]; \n" +
4510+
" res[0] = x; \n" +
4511+
" return res; \n" +
4512+
" } \n\n";
4513+
44714514
res = res +
44724515
" public static Vector sequenceRange(Object[] arr, int n)\n" +
44734516
" { Vector res = new Vector();\n" +

BinaryExpression.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9928,7 +9928,10 @@ private String composeNeqQueryForms(String lqf,
99289928
String rqf, boolean lprim,
99299929
boolean rprim)
99309930
{ String res;
9931-
if (lprim && rprim)
9931+
9932+
if (left.isRef() && right.isRef())
9933+
{ res = lqf + " != " + rqf; }
9934+
else if (lprim && rprim)
99329935
{ res = lqf + " != " + rqf; }
99339936
else if (lprim) // also for booleans, doubles
99349937
{ String rr = right.unwrap(rqf);
@@ -9971,7 +9974,9 @@ private String composeEqQueryForms(String lqf,
99719974
boolean rprim)
99729975
{ String res;
99739976

9974-
if (left.isString())
9977+
if (left.isRef() && right.isRef())
9978+
{ res = lqf + " == " + rqf; }
9979+
else if (left.isString())
99759980
{ res = "((String) " + lqf + ").equals(" + rqf + ")"; }
99769981
else if (right.isString())
99779982
{ res = "((String) " + rqf + ").equals(" + lqf + ")"; }

0 commit comments

Comments
 (0)