Skip to content

Commit b1c5e59

Browse files
authored
Updated code-generation by example
New CGBE facilities for synthesis of code generators from examples, as described in our Modelsward 2022 paper.
1 parent e14821e commit b1c5e59

22 files changed

+1464
-429
lines changed

ASTCompositeTerm.java

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ public String cgRules(CGSpec cgs, Vector rules)
201201
Vector vars = r.getVariables();
202202

203203
if (tokens.size() > terms.size())
204-
{ // System.out.println("> " + tag + " rule " + r + " does not match " + this);
205-
// System.out.println("!! Too many elements on rule LHS (" + tokens.size() + ") to match subterms: (" + terms.size() + ")");
204+
{ System.out.println("> " + tag + " rule " + r + " does not match " + this);
205+
System.out.println("!! Too many elements on rule LHS (" + tokens.size() + ") to match subterms: (" + terms.size() + ")");
206206
continue;
207207
}
208208
else if (vars.contains("_*") && terms.size() >= tokens.size())
@@ -215,13 +215,14 @@ else if (tokens.size() == terms.size())
215215
// System.out.println("> Trying to match tokens of rule " + r + " for " + this);
216216

217217
Vector args = new Vector();
218-
// Strings resulting from terms[k].cg(cgs)
218+
// Strings resulting from the terms[k].cg(cgs)
219219
Vector eargs = new Vector();
220220
// the actual terms[k]
221221

222222
int k = 0;
223223
boolean failed = false;
224-
for (int j = 0; j < tokens.size() && k < terms.size() && !failed; j++)
224+
for (int j = 0; j < tokens.size() &&
225+
k < terms.size() && !failed; j++)
225226
{ String tok = (String) tokens.get(j);
226227
ASTTerm tm = (ASTTerm) terms.get(k);
227228

@@ -234,31 +235,45 @@ else if (tokens.size() == terms.size())
234235
if (tokens.size() > j+1)
235236
{ nextTok = (String) tokens.get(j+1); }
236237

238+
System.out.println(">> End token for _* is: " + nextTok);
239+
int remainingTokens = tokens.size() - (j+1);
240+
237241
boolean finished = false;
238242

239243
Vector rem = new Vector();
240244
for (int p = j ; p < terms.size() && !finished; p++)
241-
{ ASTTerm pterm = (ASTTerm) terms.get(p);
245+
{ ASTTerm pterm = (ASTTerm) terms.get(p);
246+
int remainingTerms = terms.size() - (k+1);
247+
242248
if (nextTok != null &&
243249
pterm.literalForm().equals(nextTok))
244250
{ finished = true; }
251+
else if (remainingTokens > remainingTerms)
252+
{ finished = true; }
245253
else
246254
{ rem.add(pterm);
247255
k++;
248256
}
257+
System.out.println(">>> Terms for _* are: " + rem);
249258
}
250259
eargs.add(rem); // corresponds to _* variable
251260
}
252261
else if (vars.contains(tok))
253262
{ // allocate terms(j) to tok
263+
264+
System.out.println(">> Matched variable " + tok +
265+
" and term " + tm);
254266
eargs.add(tm);
255267
k++;
256268
}
257269
else if (tok.equals(tm.literalForm()))
258-
{ k++; }
270+
{ System.out.println(">> Matched token " + tok +
271+
" and term " + tm);
272+
k++;
273+
}
259274
else
260-
{ // System.out.println("> " + tag + " rule " + r + " does not match " + this);
261-
// System.out.println(tok + " /= " + tm.literalForm());
275+
{ System.out.println("> " + tag + " rule " + r + " does not match " + this);
276+
System.out.println(tok + " /= " + tm.literalForm());
262277
k++;
263278
failed = true; // try next rule
264279
}
@@ -5932,6 +5947,57 @@ else if ("fread".equals(fname) && args.size() == 4)
59325947
return new AssignStatement(data, readN);
59335948
}
59345949
}
5950+
else if ("fwrite".equals(fname) && args.size() == 4)
5951+
{ Expression fle = (Expression) args.get(3);
5952+
Expression data = (Expression) args.get(0);
5953+
Expression n = (Expression) args.get(2);
5954+
if (data.isStringSequence())
5955+
{ Vector pars = new Vector();
5956+
pars.add(unitExpression);
5957+
pars.add(n);
5958+
Expression subrng =
5959+
BasicExpression.newFunctionBasicExpression("subrange",
5960+
data, pars);
5961+
Expression sumexpr =
5962+
new UnaryExpression("->sum", subrng);
5963+
BasicExpression res =
5964+
BasicExpression.newCallBasicExpression(
5965+
"write", fle, sumexpr);
5966+
InvocationStatement ee =
5967+
InvocationStatement.newInvocationStatement(res,
5968+
sumexpr);
5969+
return ee;
5970+
}
5971+
else if (data.isIntSequence())
5972+
{ // fle.write(data.subrange(1,n)->collect(
5973+
// _z | _z->byte2char() )->sum()
5974+
Vector pars = new Vector();
5975+
pars.add(unitExpression);
5976+
pars.add(n);
5977+
Expression subrng =
5978+
BasicExpression.newFunctionBasicExpression("subrange",
5979+
data, pars);
5980+
BasicExpression chr =
5981+
BasicExpression.newVariableBasicExpression("_chr");
5982+
chr.setType(new Type("int", null));
5983+
Expression coldom =
5984+
new BinaryExpression(":", chr, subrng);
5985+
Expression par =
5986+
new UnaryExpression("->byte2char", chr);
5987+
Expression colexpr =
5988+
new BinaryExpression("|C", coldom, par);
5989+
5990+
Expression sumexpr =
5991+
new UnaryExpression("->sum", colexpr);
5992+
BasicExpression res =
5993+
BasicExpression.newCallBasicExpression(
5994+
"write", fle, sumexpr);
5995+
InvocationStatement ee =
5996+
InvocationStatement.newInvocationStatement(res,
5997+
sumexpr);
5998+
return ee;
5999+
}
6000+
}
59356001
else if (("putc".equals(fname) ||
59366002
"fputc".equals(fname)) && args.size() == 2)
59376003
{ Expression fle = (Expression) args.get(1);

0 commit comments

Comments
 (0)